1*67e74705SXin Li //===--- UnwrappedLineParser.h - Format C++ code ----------------*- C++ -*-===//
2*67e74705SXin Li //
3*67e74705SXin Li // The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li ///
10*67e74705SXin Li /// \file
11*67e74705SXin Li /// \brief This file contains the declaration of the UnwrappedLineParser,
12*67e74705SXin Li /// which turns a stream of tokens into UnwrappedLines.
13*67e74705SXin Li ///
14*67e74705SXin Li //===----------------------------------------------------------------------===//
15*67e74705SXin Li
16*67e74705SXin Li #ifndef LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEPARSER_H
17*67e74705SXin Li #define LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEPARSER_H
18*67e74705SXin Li
19*67e74705SXin Li #include "FormatToken.h"
20*67e74705SXin Li #include "clang/Basic/IdentifierTable.h"
21*67e74705SXin Li #include "clang/Format/Format.h"
22*67e74705SXin Li #include <list>
23*67e74705SXin Li #include <stack>
24*67e74705SXin Li
25*67e74705SXin Li namespace clang {
26*67e74705SXin Li namespace format {
27*67e74705SXin Li
28*67e74705SXin Li struct UnwrappedLineNode;
29*67e74705SXin Li
30*67e74705SXin Li /// \brief An unwrapped line is a sequence of \c Token, that we would like to
31*67e74705SXin Li /// put on a single line if there was no column limit.
32*67e74705SXin Li ///
33*67e74705SXin Li /// This is used as a main interface between the \c UnwrappedLineParser and the
34*67e74705SXin Li /// \c UnwrappedLineFormatter. The key property is that changing the formatting
35*67e74705SXin Li /// within an unwrapped line does not affect any other unwrapped lines.
36*67e74705SXin Li struct UnwrappedLine {
37*67e74705SXin Li UnwrappedLine();
38*67e74705SXin Li
39*67e74705SXin Li // FIXME: Don't use std::list here.
40*67e74705SXin Li /// \brief The \c Tokens comprising this \c UnwrappedLine.
41*67e74705SXin Li std::list<UnwrappedLineNode> Tokens;
42*67e74705SXin Li
43*67e74705SXin Li /// \brief The indent level of the \c UnwrappedLine.
44*67e74705SXin Li unsigned Level;
45*67e74705SXin Li
46*67e74705SXin Li /// \brief Whether this \c UnwrappedLine is part of a preprocessor directive.
47*67e74705SXin Li bool InPPDirective;
48*67e74705SXin Li
49*67e74705SXin Li bool MustBeDeclaration;
50*67e74705SXin Li };
51*67e74705SXin Li
52*67e74705SXin Li class UnwrappedLineConsumer {
53*67e74705SXin Li public:
~UnwrappedLineConsumer()54*67e74705SXin Li virtual ~UnwrappedLineConsumer() {}
55*67e74705SXin Li virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0;
56*67e74705SXin Li virtual void finishRun() = 0;
57*67e74705SXin Li };
58*67e74705SXin Li
59*67e74705SXin Li class FormatTokenSource;
60*67e74705SXin Li
61*67e74705SXin Li class UnwrappedLineParser {
62*67e74705SXin Li public:
63*67e74705SXin Li UnwrappedLineParser(const FormatStyle &Style,
64*67e74705SXin Li const AdditionalKeywords &Keywords,
65*67e74705SXin Li ArrayRef<FormatToken *> Tokens,
66*67e74705SXin Li UnwrappedLineConsumer &Callback);
67*67e74705SXin Li
68*67e74705SXin Li void parse();
69*67e74705SXin Li
70*67e74705SXin Li private:
71*67e74705SXin Li void reset();
72*67e74705SXin Li void parseFile();
73*67e74705SXin Li void parseLevel(bool HasOpeningBrace);
74*67e74705SXin Li void parseBlock(bool MustBeDeclaration, bool AddLevel = true,
75*67e74705SXin Li bool MunchSemi = true);
76*67e74705SXin Li void parseChildBlock();
77*67e74705SXin Li void parsePPDirective();
78*67e74705SXin Li void parsePPDefine();
79*67e74705SXin Li void parsePPIf(bool IfDef);
80*67e74705SXin Li void parsePPElIf();
81*67e74705SXin Li void parsePPElse();
82*67e74705SXin Li void parsePPEndIf();
83*67e74705SXin Li void parsePPUnknown();
84*67e74705SXin Li void readTokenWithJavaScriptASI();
85*67e74705SXin Li void parseStructuralElement();
86*67e74705SXin Li bool tryToParseBracedList();
87*67e74705SXin Li bool parseBracedList(bool ContinueOnSemicolons = false);
88*67e74705SXin Li void parseParens();
89*67e74705SXin Li void parseSquare();
90*67e74705SXin Li void parseIfThenElse();
91*67e74705SXin Li void parseTryCatch();
92*67e74705SXin Li void parseForOrWhileLoop();
93*67e74705SXin Li void parseDoWhile();
94*67e74705SXin Li void parseLabel();
95*67e74705SXin Li void parseCaseLabel();
96*67e74705SXin Li void parseSwitch();
97*67e74705SXin Li void parseNamespace();
98*67e74705SXin Li void parseNew();
99*67e74705SXin Li void parseAccessSpecifier();
100*67e74705SXin Li bool parseEnum();
101*67e74705SXin Li void parseJavaEnumBody();
102*67e74705SXin Li void parseRecord();
103*67e74705SXin Li void parseObjCProtocolList();
104*67e74705SXin Li void parseObjCUntilAtEnd();
105*67e74705SXin Li void parseObjCInterfaceOrImplementation();
106*67e74705SXin Li void parseObjCProtocol();
107*67e74705SXin Li void parseJavaScriptEs6ImportExport();
108*67e74705SXin Li bool tryToParseLambda();
109*67e74705SXin Li bool tryToParseLambdaIntroducer();
110*67e74705SXin Li void tryToParseJSFunction();
111*67e74705SXin Li void addUnwrappedLine();
112*67e74705SXin Li bool eof() const;
113*67e74705SXin Li void nextToken();
114*67e74705SXin Li const FormatToken *getPreviousToken();
115*67e74705SXin Li void readToken();
116*67e74705SXin Li void flushComments(bool NewlineBeforeNext);
117*67e74705SXin Li void pushToken(FormatToken *Tok);
118*67e74705SXin Li void calculateBraceTypes(bool ExpectClassBody = false);
119*67e74705SXin Li
120*67e74705SXin Li // Marks a conditional compilation edge (for example, an '#if', '#ifdef',
121*67e74705SXin Li // '#else' or merge conflict marker). If 'Unreachable' is true, assumes
122*67e74705SXin Li // this branch either cannot be taken (for example '#if false'), or should
123*67e74705SXin Li // not be taken in this round.
124*67e74705SXin Li void conditionalCompilationCondition(bool Unreachable);
125*67e74705SXin Li void conditionalCompilationStart(bool Unreachable);
126*67e74705SXin Li void conditionalCompilationAlternative();
127*67e74705SXin Li void conditionalCompilationEnd();
128*67e74705SXin Li
129*67e74705SXin Li bool isOnNewLine(const FormatToken &FormatTok);
130*67e74705SXin Li
131*67e74705SXin Li // FIXME: We are constantly running into bugs where Line.Level is incorrectly
132*67e74705SXin Li // subtracted from beyond 0. Introduce a method to subtract from Line.Level
133*67e74705SXin Li // and use that everywhere in the Parser.
134*67e74705SXin Li std::unique_ptr<UnwrappedLine> Line;
135*67e74705SXin Li
136*67e74705SXin Li // Comments are sorted into unwrapped lines by whether they are in the same
137*67e74705SXin Li // line as the previous token, or not. If not, they belong to the next token.
138*67e74705SXin Li // Since the next token might already be in a new unwrapped line, we need to
139*67e74705SXin Li // store the comments belonging to that token.
140*67e74705SXin Li SmallVector<FormatToken *, 1> CommentsBeforeNextToken;
141*67e74705SXin Li FormatToken *FormatTok;
142*67e74705SXin Li bool MustBreakBeforeNextToken;
143*67e74705SXin Li
144*67e74705SXin Li // The parsed lines. Only added to through \c CurrentLines.
145*67e74705SXin Li SmallVector<UnwrappedLine, 8> Lines;
146*67e74705SXin Li
147*67e74705SXin Li // Preprocessor directives are parsed out-of-order from other unwrapped lines.
148*67e74705SXin Li // Thus, we need to keep a list of preprocessor directives to be reported
149*67e74705SXin Li // after an unwarpped line that has been started was finished.
150*67e74705SXin Li SmallVector<UnwrappedLine, 4> PreprocessorDirectives;
151*67e74705SXin Li
152*67e74705SXin Li // New unwrapped lines are added via CurrentLines.
153*67e74705SXin Li // Usually points to \c &Lines. While parsing a preprocessor directive when
154*67e74705SXin Li // there is an unfinished previous unwrapped line, will point to
155*67e74705SXin Li // \c &PreprocessorDirectives.
156*67e74705SXin Li SmallVectorImpl<UnwrappedLine> *CurrentLines;
157*67e74705SXin Li
158*67e74705SXin Li // We store for each line whether it must be a declaration depending on
159*67e74705SXin Li // whether we are in a compound statement or not.
160*67e74705SXin Li std::vector<bool> DeclarationScopeStack;
161*67e74705SXin Li
162*67e74705SXin Li const FormatStyle &Style;
163*67e74705SXin Li const AdditionalKeywords &Keywords;
164*67e74705SXin Li
165*67e74705SXin Li FormatTokenSource *Tokens;
166*67e74705SXin Li UnwrappedLineConsumer &Callback;
167*67e74705SXin Li
168*67e74705SXin Li // FIXME: This is a temporary measure until we have reworked the ownership
169*67e74705SXin Li // of the format tokens. The goal is to have the actual tokens created and
170*67e74705SXin Li // owned outside of and handed into the UnwrappedLineParser.
171*67e74705SXin Li ArrayRef<FormatToken *> AllTokens;
172*67e74705SXin Li
173*67e74705SXin Li // Represents preprocessor branch type, so we can find matching
174*67e74705SXin Li // #if/#else/#endif directives.
175*67e74705SXin Li enum PPBranchKind {
176*67e74705SXin Li PP_Conditional, // Any #if, #ifdef, #ifndef, #elif, block outside #if 0
177*67e74705SXin Li PP_Unreachable // #if 0 or a conditional preprocessor block inside #if 0
178*67e74705SXin Li };
179*67e74705SXin Li
180*67e74705SXin Li // Keeps a stack of currently active preprocessor branching directives.
181*67e74705SXin Li SmallVector<PPBranchKind, 16> PPStack;
182*67e74705SXin Li
183*67e74705SXin Li // The \c UnwrappedLineParser re-parses the code for each combination
184*67e74705SXin Li // of preprocessor branches that can be taken.
185*67e74705SXin Li // To that end, we take the same branch (#if, #else, or one of the #elif
186*67e74705SXin Li // branches) for each nesting level of preprocessor branches.
187*67e74705SXin Li // \c PPBranchLevel stores the current nesting level of preprocessor
188*67e74705SXin Li // branches during one pass over the code.
189*67e74705SXin Li int PPBranchLevel;
190*67e74705SXin Li
191*67e74705SXin Li // Contains the current branch (#if, #else or one of the #elif branches)
192*67e74705SXin Li // for each nesting level.
193*67e74705SXin Li SmallVector<int, 8> PPLevelBranchIndex;
194*67e74705SXin Li
195*67e74705SXin Li // Contains the maximum number of branches at each nesting level.
196*67e74705SXin Li SmallVector<int, 8> PPLevelBranchCount;
197*67e74705SXin Li
198*67e74705SXin Li // Contains the number of branches per nesting level we are currently
199*67e74705SXin Li // in while parsing a preprocessor branch sequence.
200*67e74705SXin Li // This is used to update PPLevelBranchCount at the end of a branch
201*67e74705SXin Li // sequence.
202*67e74705SXin Li std::stack<int> PPChainBranchIndex;
203*67e74705SXin Li
204*67e74705SXin Li friend class ScopedLineState;
205*67e74705SXin Li friend class CompoundStatementIndenter;
206*67e74705SXin Li };
207*67e74705SXin Li
208*67e74705SXin Li struct UnwrappedLineNode {
UnwrappedLineNodeUnwrappedLineNode209*67e74705SXin Li UnwrappedLineNode() : Tok(nullptr) {}
UnwrappedLineNodeUnwrappedLineNode210*67e74705SXin Li UnwrappedLineNode(FormatToken *Tok) : Tok(Tok) {}
211*67e74705SXin Li
212*67e74705SXin Li FormatToken *Tok;
213*67e74705SXin Li SmallVector<UnwrappedLine, 0> Children;
214*67e74705SXin Li };
215*67e74705SXin Li
UnwrappedLine()216*67e74705SXin Li inline UnwrappedLine::UnwrappedLine()
217*67e74705SXin Li : Level(0), InPPDirective(false), MustBeDeclaration(false) {}
218*67e74705SXin Li
219*67e74705SXin Li } // end namespace format
220*67e74705SXin Li } // end namespace clang
221*67e74705SXin Li
222*67e74705SXin Li #endif
223