1 //===--- UnwrappedLineParser.h - Format C++ code ----------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file contains the declaration of the UnwrappedLineParser,
12 /// which turns a stream of tokens into UnwrappedLines.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEPARSER_H
17 #define LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEPARSER_H
19 #include "FormatToken.h"
20 #include "clang/Basic/IdentifierTable.h"
21 #include "clang/Format/Format.h"
28 struct UnwrappedLineNode
;
30 /// \brief An unwrapped line is a sequence of \c Token, that we would like to
31 /// put on a single line if there was no column limit.
33 /// This is used as a main interface between the \c UnwrappedLineParser and the
34 /// \c UnwrappedLineFormatter. The key property is that changing the formatting
35 /// within an unwrapped line does not affect any other unwrapped lines.
36 struct UnwrappedLine
{
39 // FIXME: Don't use std::list here.
40 /// \brief The \c Tokens comprising this \c UnwrappedLine.
41 std::list
<UnwrappedLineNode
> Tokens
;
43 /// \brief The indent level of the \c UnwrappedLine.
46 /// \brief Whether this \c UnwrappedLine is part of a preprocessor directive.
49 bool MustBeDeclaration
;
52 class UnwrappedLineConsumer
{
54 virtual ~UnwrappedLineConsumer() {}
55 virtual void consumeUnwrappedLine(const UnwrappedLine
&Line
) = 0;
56 virtual void finishRun() = 0;
59 class FormatTokenSource
;
61 class UnwrappedLineParser
{
63 UnwrappedLineParser(const FormatStyle
&Style
,
64 const AdditionalKeywords
&Keywords
,
65 ArrayRef
<FormatToken
*> Tokens
,
66 UnwrappedLineConsumer
&Callback
);
68 /// Returns true in case of a structural error.
74 void parseLevel(bool HasOpeningBrace
);
75 void parseBlock(bool MustBeDeclaration
, bool AddLevel
= true,
76 bool MunchSemi
= true);
77 void parseChildBlock();
78 void parsePPDirective();
80 void parsePPIf(bool IfDef
);
84 void parsePPUnknown();
85 void parseStructuralElement();
86 bool tryToParseBracedList();
87 bool parseBracedList(bool ContinueOnSemicolons
= false);
90 void parseIfThenElse();
92 void parseForOrWhileLoop();
95 void parseCaseLabel();
97 void parseNamespace();
98 void parseAccessSpecifier();
100 void parseJavaEnumBody();
102 void parseObjCProtocolList();
103 void parseObjCUntilAtEnd();
104 void parseObjCInterfaceOrImplementation();
105 void parseObjCProtocol();
106 bool tryToParseLambda();
107 bool tryToParseLambdaIntroducer();
108 void tryToParseJSFunction();
109 void addUnwrappedLine();
113 void flushComments(bool NewlineBeforeNext
);
114 void pushToken(FormatToken
*Tok
);
115 void calculateBraceTypes();
117 // Marks a conditional compilation edge (for example, an '#if', '#ifdef',
118 // '#else' or merge conflict marker). If 'Unreachable' is true, assumes
119 // this branch either cannot be taken (for example '#if false'), or should
120 // not be taken in this round.
121 void conditionalCompilationCondition(bool Unreachable
);
122 void conditionalCompilationStart(bool Unreachable
);
123 void conditionalCompilationAlternative();
124 void conditionalCompilationEnd();
126 bool isOnNewLine(const FormatToken
&FormatTok
);
128 // FIXME: We are constantly running into bugs where Line.Level is incorrectly
129 // subtracted from beyond 0. Introduce a method to subtract from Line.Level
130 // and use that everywhere in the Parser.
131 std::unique_ptr
<UnwrappedLine
> Line
;
133 // Comments are sorted into unwrapped lines by whether they are in the same
134 // line as the previous token, or not. If not, they belong to the next token.
135 // Since the next token might already be in a new unwrapped line, we need to
136 // store the comments belonging to that token.
137 SmallVector
<FormatToken
*, 1> CommentsBeforeNextToken
;
138 FormatToken
*FormatTok
;
139 bool MustBreakBeforeNextToken
;
141 // The parsed lines. Only added to through \c CurrentLines.
142 SmallVector
<UnwrappedLine
, 8> Lines
;
144 // Preprocessor directives are parsed out-of-order from other unwrapped lines.
145 // Thus, we need to keep a list of preprocessor directives to be reported
146 // after an unwarpped line that has been started was finished.
147 SmallVector
<UnwrappedLine
, 4> PreprocessorDirectives
;
149 // New unwrapped lines are added via CurrentLines.
150 // Usually points to \c &Lines. While parsing a preprocessor directive when
151 // there is an unfinished previous unwrapped line, will point to
152 // \c &PreprocessorDirectives.
153 SmallVectorImpl
<UnwrappedLine
> *CurrentLines
;
155 // We store for each line whether it must be a declaration depending on
156 // whether we are in a compound statement or not.
157 std::vector
<bool> DeclarationScopeStack
;
159 // Will be true if we encounter an error that leads to possibily incorrect
160 // indentation levels.
161 bool StructuralError
;
163 const FormatStyle
&Style
;
164 const AdditionalKeywords
&Keywords
;
166 FormatTokenSource
*Tokens
;
167 UnwrappedLineConsumer
&Callback
;
169 // FIXME: This is a temporary measure until we have reworked the ownership
170 // of the format tokens. The goal is to have the actual tokens created and
171 // owned outside of and handed into the UnwrappedLineParser.
172 ArrayRef
<FormatToken
*> AllTokens
;
174 // Represents preprocessor branch type, so we can find matching
175 // #if/#else/#endif directives.
177 PP_Conditional
, // Any #if, #ifdef, #ifndef, #elif, block outside #if 0
178 PP_Unreachable
// #if 0 or a conditional preprocessor block inside #if 0
181 // Keeps a stack of currently active preprocessor branching directives.
182 SmallVector
<PPBranchKind
, 16> PPStack
;
184 // The \c UnwrappedLineParser re-parses the code for each combination
185 // of preprocessor branches that can be taken.
186 // To that end, we take the same branch (#if, #else, or one of the #elif
187 // branches) for each nesting level of preprocessor branches.
188 // \c PPBranchLevel stores the current nesting level of preprocessor
189 // branches during one pass over the code.
192 // Contains the current branch (#if, #else or one of the #elif branches)
193 // for each nesting level.
194 SmallVector
<int, 8> PPLevelBranchIndex
;
196 // Contains the maximum number of branches at each nesting level.
197 SmallVector
<int, 8> PPLevelBranchCount
;
199 // Contains the number of branches per nesting level we are currently
200 // in while parsing a preprocessor branch sequence.
201 // This is used to update PPLevelBranchCount at the end of a branch
203 std::stack
<int> PPChainBranchIndex
;
205 friend class ScopedLineState
;
206 friend class CompoundStatementIndenter
;
209 struct UnwrappedLineNode
{
210 UnwrappedLineNode() : Tok(nullptr) {}
211 UnwrappedLineNode(FormatToken
*Tok
) : Tok(Tok
) {}
214 SmallVector
<UnwrappedLine
, 0> Children
;
217 inline UnwrappedLine::UnwrappedLine()
218 : Level(0), InPPDirective(false), MustBeDeclaration(false) {}
220 } // end namespace format
221 } // end namespace clang