xref: /freebsd/contrib/llvm-project/clang/lib/Format/UnwrappedLineParser.h (revision bdb86d1a853a919764f65fdedcea76d76e4d619b)
10b57cec5SDimitry Andric //===--- UnwrappedLineParser.h - Format C++ code ----------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric ///
90b57cec5SDimitry Andric /// \file
100b57cec5SDimitry Andric /// This file contains the declaration of the UnwrappedLineParser,
110b57cec5SDimitry Andric /// which turns a stream of tokens into UnwrappedLines.
120b57cec5SDimitry Andric ///
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #ifndef LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEPARSER_H
160b57cec5SDimitry Andric #define LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEPARSER_H
170b57cec5SDimitry Andric 
1806c3fb27SDimitry Andric #include "Encoding.h"
190b57cec5SDimitry Andric #include "FormatToken.h"
2006c3fb27SDimitry Andric #include "Macros.h"
210b57cec5SDimitry Andric #include "clang/Basic/IdentifierTable.h"
220b57cec5SDimitry Andric #include "clang/Format/Format.h"
2306c3fb27SDimitry Andric #include "llvm/ADT/ArrayRef.h"
2404eeddc0SDimitry Andric #include "llvm/ADT/BitVector.h"
2506c3fb27SDimitry Andric #include "llvm/ADT/DenseSet.h"
260b57cec5SDimitry Andric #include "llvm/Support/Regex.h"
27753f127fSDimitry Andric #include <list>
280b57cec5SDimitry Andric #include <stack>
29349cc55cSDimitry Andric #include <vector>
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric namespace clang {
320b57cec5SDimitry Andric namespace format {
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric struct UnwrappedLineNode;
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric /// An unwrapped line is a sequence of \c Token, that we would like to
370b57cec5SDimitry Andric /// put on a single line if there was no column limit.
380b57cec5SDimitry Andric ///
390b57cec5SDimitry Andric /// This is used as a main interface between the \c UnwrappedLineParser and the
400b57cec5SDimitry Andric /// \c UnwrappedLineFormatter. The key property is that changing the formatting
410b57cec5SDimitry Andric /// within an unwrapped line does not affect any other unwrapped lines.
420b57cec5SDimitry Andric struct UnwrappedLine {
430b57cec5SDimitry Andric   UnwrappedLine();
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric   /// The \c Tokens comprising this \c UnwrappedLine.
46753f127fSDimitry Andric   std::list<UnwrappedLineNode> Tokens;
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric   /// The indent level of the \c UnwrappedLine.
490b57cec5SDimitry Andric   unsigned Level;
500b57cec5SDimitry Andric 
51bdd1243dSDimitry Andric   /// The \c PPBranchLevel (adjusted for header guards) if this line is a
52bdd1243dSDimitry Andric   /// \c InMacroBody line, and 0 otherwise.
53bdd1243dSDimitry Andric   unsigned PPLevel;
54bdd1243dSDimitry Andric 
550b57cec5SDimitry Andric   /// Whether this \c UnwrappedLine is part of a preprocessor directive.
560b57cec5SDimitry Andric   bool InPPDirective;
57bdd1243dSDimitry Andric   /// Whether this \c UnwrappedLine is part of a pramga directive.
58bdd1243dSDimitry Andric   bool InPragmaDirective;
59bdd1243dSDimitry Andric   /// Whether it is part of a macro body.
60bdd1243dSDimitry Andric   bool InMacroBody;
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric   bool MustBeDeclaration;
630b57cec5SDimitry Andric 
64*bdb86d1aSDimitry Andric   /// Whether the parser has seen \c decltype(auto) in this line.
65*bdb86d1aSDimitry Andric   bool SeenDecltypeAuto = false;
66*bdb86d1aSDimitry Andric 
67bdd1243dSDimitry Andric   /// \c True if this line should be indented by ContinuationIndent in
68bdd1243dSDimitry Andric   /// addition to the normal indention level.
69bdd1243dSDimitry Andric   bool IsContinuation = false;
70bdd1243dSDimitry Andric 
710b57cec5SDimitry Andric   /// If this \c UnwrappedLine closes a block in a sequence of lines,
720b57cec5SDimitry Andric   /// \c MatchingOpeningBlockLineIndex stores the index of the corresponding
730b57cec5SDimitry Andric   /// opening line. Otherwise, \c MatchingOpeningBlockLineIndex must be
740b57cec5SDimitry Andric   /// \c kInvalidIndex.
750b57cec5SDimitry Andric   size_t MatchingOpeningBlockLineIndex = kInvalidIndex;
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric   /// If this \c UnwrappedLine opens a block, stores the index of the
780b57cec5SDimitry Andric   /// line with the corresponding closing brace.
790b57cec5SDimitry Andric   size_t MatchingClosingBlockLineIndex = kInvalidIndex;
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric   static const size_t kInvalidIndex = -1;
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric   unsigned FirstStartColumn = 0;
840b57cec5SDimitry Andric };
850b57cec5SDimitry Andric 
8606c3fb27SDimitry Andric /// Interface for users of the UnwrappedLineParser to receive the parsed lines.
8706c3fb27SDimitry Andric /// Parsing a single snippet of code can lead to multiple runs, where each
8806c3fb27SDimitry Andric /// run is a coherent view of the file.
8906c3fb27SDimitry Andric ///
9006c3fb27SDimitry Andric /// For example, different runs are generated:
9106c3fb27SDimitry Andric /// - for different combinations of #if blocks
9206c3fb27SDimitry Andric /// - when macros are involved, for the expanded code and the as-written code
9306c3fb27SDimitry Andric ///
9406c3fb27SDimitry Andric /// Some tokens will only be visible in a subset of the runs.
9506c3fb27SDimitry Andric /// For each run, \c UnwrappedLineParser will call \c consumeUnwrappedLine
9606c3fb27SDimitry Andric /// for each parsed unwrapped line, and then \c finishRun to indicate
9706c3fb27SDimitry Andric /// that the set of unwrapped lines before is one coherent view of the
9806c3fb27SDimitry Andric /// code snippet to be formatted.
990b57cec5SDimitry Andric class UnwrappedLineConsumer {
1000b57cec5SDimitry Andric public:
1010b57cec5SDimitry Andric   virtual ~UnwrappedLineConsumer() {}
1020b57cec5SDimitry Andric   virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0;
1030b57cec5SDimitry Andric   virtual void finishRun() = 0;
1040b57cec5SDimitry Andric };
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric class FormatTokenSource;
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric class UnwrappedLineParser {
1090b57cec5SDimitry Andric public:
11006c3fb27SDimitry Andric   UnwrappedLineParser(SourceManager &SourceMgr, const FormatStyle &Style,
1110b57cec5SDimitry Andric                       const AdditionalKeywords &Keywords,
1120b57cec5SDimitry Andric                       unsigned FirstStartColumn, ArrayRef<FormatToken *> Tokens,
11306c3fb27SDimitry Andric                       UnwrappedLineConsumer &Callback,
11406c3fb27SDimitry Andric                       llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator,
11506c3fb27SDimitry Andric                       IdentifierTable &IdentTable);
1160b57cec5SDimitry Andric 
1170b57cec5SDimitry Andric   void parse();
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric private:
12004eeddc0SDimitry Andric   enum class IfStmtKind {
12104eeddc0SDimitry Andric     NotIf,   // Not an if statement.
12204eeddc0SDimitry Andric     IfOnly,  // An if statement without the else clause.
12304eeddc0SDimitry Andric     IfElse,  // An if statement followed by else but not else if.
12404eeddc0SDimitry Andric     IfElseIf // An if statement followed by else if.
12504eeddc0SDimitry Andric   };
12604eeddc0SDimitry Andric 
1270b57cec5SDimitry Andric   void reset();
1280b57cec5SDimitry Andric   void parseFile();
12904eeddc0SDimitry Andric   bool precededByCommentOrPPDirective() const;
13081ad6265SDimitry Andric   bool parseLevel(const FormatToken *OpeningBrace = nullptr,
13181ad6265SDimitry Andric                   bool CanContainBracedList = true,
13281ad6265SDimitry Andric                   TokenType NextLBracesType = TT_Unknown,
13381ad6265SDimitry Andric                   IfStmtKind *IfKind = nullptr,
13481ad6265SDimitry Andric                   FormatToken **IfLeftBrace = nullptr);
13581ad6265SDimitry Andric   bool mightFitOnOneLine(UnwrappedLine &Line,
13681ad6265SDimitry Andric                          const FormatToken *OpeningBrace = nullptr) const;
13781ad6265SDimitry Andric   FormatToken *parseBlock(bool MustBeDeclaration = false,
13881ad6265SDimitry Andric                           unsigned AddLevels = 1u, bool MunchSemi = true,
13981ad6265SDimitry Andric                           bool KeepBraces = true, IfStmtKind *IfKind = nullptr,
14081ad6265SDimitry Andric                           bool UnindentWhitesmithsBraces = false,
14181ad6265SDimitry Andric                           bool CanContainBracedList = true,
14281ad6265SDimitry Andric                           TokenType NextLBracesType = TT_Unknown);
14381ad6265SDimitry Andric   void parseChildBlock(bool CanContainBracedList = true,
14481ad6265SDimitry Andric                        TokenType NextLBracesType = TT_Unknown);
1450b57cec5SDimitry Andric   void parsePPDirective();
1460b57cec5SDimitry Andric   void parsePPDefine();
1470b57cec5SDimitry Andric   void parsePPIf(bool IfDef);
1480b57cec5SDimitry Andric   void parsePPElse();
1490b57cec5SDimitry Andric   void parsePPEndIf();
150bdd1243dSDimitry Andric   void parsePPPragma();
1510b57cec5SDimitry Andric   void parsePPUnknown();
1520b57cec5SDimitry Andric   void readTokenWithJavaScriptASI();
15381ad6265SDimitry Andric   void parseStructuralElement(bool IsTopLevel = false,
15481ad6265SDimitry Andric                               TokenType NextLBracesType = TT_Unknown,
15581ad6265SDimitry Andric                               IfStmtKind *IfKind = nullptr,
15681ad6265SDimitry Andric                               FormatToken **IfLeftBrace = nullptr,
15781ad6265SDimitry Andric                               bool *HasDoWhile = nullptr,
15881ad6265SDimitry Andric                               bool *HasLabel = nullptr);
1590b57cec5SDimitry Andric   bool tryToParseBracedList();
1605ffd83dbSDimitry Andric   bool parseBracedList(bool ContinueOnSemicolons = false, bool IsEnum = false,
1610b57cec5SDimitry Andric                        tok::TokenKind ClosingBraceKind = tok::r_brace);
16206c3fb27SDimitry Andric   bool parseParens(TokenType AmpAmpTokenType = TT_Unknown);
1630b57cec5SDimitry Andric   void parseSquare(bool LambdaIntroducer = false);
16404eeddc0SDimitry Andric   void keepAncestorBraces();
16581ad6265SDimitry Andric   void parseUnbracedBody(bool CheckEOF = false);
16681ad6265SDimitry Andric   void handleAttributes();
16781ad6265SDimitry Andric   bool handleCppAttributes();
168bdd1243dSDimitry Andric   bool isBlockBegin(const FormatToken &Tok) const;
16906c3fb27SDimitry Andric   FormatToken *parseIfThenElse(IfStmtKind *IfKind, bool KeepBraces = false,
17006c3fb27SDimitry Andric                                bool IsVerilogAssert = false);
1710b57cec5SDimitry Andric   void parseTryCatch();
17281ad6265SDimitry Andric   void parseLoopBody(bool KeepBraces, bool WrapRightBrace);
17306c3fb27SDimitry Andric   void parseForOrWhileLoop(bool HasParens = true);
1740b57cec5SDimitry Andric   void parseDoWhile();
175a7dea167SDimitry Andric   void parseLabel(bool LeftAlignLabel = false);
1760b57cec5SDimitry Andric   void parseCaseLabel();
1770b57cec5SDimitry Andric   void parseSwitch();
1780b57cec5SDimitry Andric   void parseNamespace();
179bdd1243dSDimitry Andric   bool parseModuleImport();
1800b57cec5SDimitry Andric   void parseNew();
1810b57cec5SDimitry Andric   void parseAccessSpecifier();
1820b57cec5SDimitry Andric   bool parseEnum();
183fe6060f1SDimitry Andric   bool parseStructLike();
18481ad6265SDimitry Andric   bool parseRequires();
18581ad6265SDimitry Andric   void parseRequiresClause(FormatToken *RequiresToken);
18681ad6265SDimitry Andric   void parseRequiresExpression(FormatToken *RequiresToken);
18781ad6265SDimitry Andric   void parseConstraintExpression();
1880b57cec5SDimitry Andric   void parseJavaEnumBody();
1890b57cec5SDimitry Andric   // Parses a record (aka class) as a top level element. If ParseAsExpr is true,
1900b57cec5SDimitry Andric   // parses the record as a child block, i.e. if the class declaration is an
1910b57cec5SDimitry Andric   // expression.
1920b57cec5SDimitry Andric   void parseRecord(bool ParseAsExpr = false);
193e8d8bef9SDimitry Andric   void parseObjCLightweightGenerics();
1940b57cec5SDimitry Andric   void parseObjCMethod();
1950b57cec5SDimitry Andric   void parseObjCProtocolList();
1960b57cec5SDimitry Andric   void parseObjCUntilAtEnd();
1970b57cec5SDimitry Andric   void parseObjCInterfaceOrImplementation();
1980b57cec5SDimitry Andric   bool parseObjCProtocol();
1990b57cec5SDimitry Andric   void parseJavaScriptEs6ImportExport();
2000b57cec5SDimitry Andric   void parseStatementMacro();
2015ffd83dbSDimitry Andric   void parseCSharpAttribute();
2025ffd83dbSDimitry Andric   // Parse a C# generic type constraint: `where T : IComparable<T>`.
2035ffd83dbSDimitry Andric   // See:
2045ffd83dbSDimitry Andric   // https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/where-generic-type-constraint
2055ffd83dbSDimitry Andric   void parseCSharpGenericTypeConstraint();
2060b57cec5SDimitry Andric   bool tryToParseLambda();
2070eae32dcSDimitry Andric   bool tryToParseChildBlock();
2080b57cec5SDimitry Andric   bool tryToParseLambdaIntroducer();
2095ffd83dbSDimitry Andric   bool tryToParsePropertyAccessor();
2100b57cec5SDimitry Andric   void tryToParseJSFunction();
2115ffd83dbSDimitry Andric   bool tryToParseSimpleAttribute();
212bdd1243dSDimitry Andric   void parseVerilogHierarchyIdentifier();
213bdd1243dSDimitry Andric   void parseVerilogSensitivityList();
214bdd1243dSDimitry Andric   // Returns the number of levels of indentation in addition to the normal 1
215bdd1243dSDimitry Andric   // level for a block, used for indenting case labels.
216bdd1243dSDimitry Andric   unsigned parseVerilogHierarchyHeader();
217bdd1243dSDimitry Andric   void parseVerilogTable();
218bdd1243dSDimitry Andric   void parseVerilogCaseLabel();
21906c3fb27SDimitry Andric   std::optional<llvm::SmallVector<llvm::SmallVector<FormatToken *, 8>, 1>>
22006c3fb27SDimitry Andric   parseMacroCall();
22123408297SDimitry Andric 
22223408297SDimitry Andric   // Used by addUnwrappedLine to denote whether to keep or remove a level
22323408297SDimitry Andric   // when resetting the line state.
22423408297SDimitry Andric   enum class LineLevel { Remove, Keep };
22523408297SDimitry Andric 
22623408297SDimitry Andric   void addUnwrappedLine(LineLevel AdjustLevel = LineLevel::Remove);
2270b57cec5SDimitry Andric   bool eof() const;
2280b57cec5SDimitry Andric   // LevelDifference is the difference of levels after and before the current
2290b57cec5SDimitry Andric   // token. For example:
2300b57cec5SDimitry Andric   // - if the token is '{' and opens a block, LevelDifference is 1.
2310b57cec5SDimitry Andric   // - if the token is '}' and closes a block, LevelDifference is -1.
2320b57cec5SDimitry Andric   void nextToken(int LevelDifference = 0);
2330b57cec5SDimitry Andric   void readToken(int LevelDifference = 0);
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric   // Decides which comment tokens should be added to the current line and which
2360b57cec5SDimitry Andric   // should be added as comments before the next token.
2370b57cec5SDimitry Andric   //
2380b57cec5SDimitry Andric   // Comments specifies the sequence of comment tokens to analyze. They get
2390b57cec5SDimitry Andric   // either pushed to the current line or added to the comments before the next
2400b57cec5SDimitry Andric   // token.
2410b57cec5SDimitry Andric   //
2420b57cec5SDimitry Andric   // NextTok specifies the next token. A null pointer NextTok is supported, and
2430b57cec5SDimitry Andric   // signifies either the absence of a next token, or that the next token
244bdd1243dSDimitry Andric   // shouldn't be taken into account for the analysis.
2450b57cec5SDimitry Andric   void distributeComments(const SmallVectorImpl<FormatToken *> &Comments,
2460b57cec5SDimitry Andric                           const FormatToken *NextTok);
2470b57cec5SDimitry Andric 
2480b57cec5SDimitry Andric   // Adds the comment preceding the next token to unwrapped lines.
2490b57cec5SDimitry Andric   void flushComments(bool NewlineBeforeNext);
2500b57cec5SDimitry Andric   void pushToken(FormatToken *Tok);
2510b57cec5SDimitry Andric   void calculateBraceTypes(bool ExpectClassBody = false);
2520b57cec5SDimitry Andric 
2530b57cec5SDimitry Andric   // Marks a conditional compilation edge (for example, an '#if', '#ifdef',
2540b57cec5SDimitry Andric   // '#else' or merge conflict marker). If 'Unreachable' is true, assumes
2550b57cec5SDimitry Andric   // this branch either cannot be taken (for example '#if false'), or should
2560b57cec5SDimitry Andric   // not be taken in this round.
2570b57cec5SDimitry Andric   void conditionalCompilationCondition(bool Unreachable);
2580b57cec5SDimitry Andric   void conditionalCompilationStart(bool Unreachable);
2590b57cec5SDimitry Andric   void conditionalCompilationAlternative();
2600b57cec5SDimitry Andric   void conditionalCompilationEnd();
2610b57cec5SDimitry Andric 
2620b57cec5SDimitry Andric   bool isOnNewLine(const FormatToken &FormatTok);
2630b57cec5SDimitry Andric 
26406c3fb27SDimitry Andric   // Returns whether there is a macro expansion in the line, i.e. a token that
26506c3fb27SDimitry Andric   // was expanded from a macro call.
26606c3fb27SDimitry Andric   bool containsExpansion(const UnwrappedLine &Line) const;
26706c3fb27SDimitry Andric 
2680b57cec5SDimitry Andric   // Compute hash of the current preprocessor branch.
2690b57cec5SDimitry Andric   // This is used to identify the different branches, and thus track if block
2700b57cec5SDimitry Andric   // open and close in the same branch.
2710b57cec5SDimitry Andric   size_t computePPHash() const;
2720b57cec5SDimitry Andric 
27306c3fb27SDimitry Andric   bool parsingPPDirective() const { return CurrentLines != &Lines; }
27406c3fb27SDimitry Andric 
2750b57cec5SDimitry Andric   // FIXME: We are constantly running into bugs where Line.Level is incorrectly
2760b57cec5SDimitry Andric   // subtracted from beyond 0. Introduce a method to subtract from Line.Level
2770b57cec5SDimitry Andric   // and use that everywhere in the Parser.
2780b57cec5SDimitry Andric   std::unique_ptr<UnwrappedLine> Line;
2790b57cec5SDimitry Andric 
28006c3fb27SDimitry Andric   // Lines that are created by macro expansion.
28106c3fb27SDimitry Andric   // When formatting code containing macro calls, we first format the expanded
28206c3fb27SDimitry Andric   // lines to set the token types correctly. Afterwards, we format the
28306c3fb27SDimitry Andric   // reconstructed macro calls, re-using the token types determined in the first
28406c3fb27SDimitry Andric   // step.
28506c3fb27SDimitry Andric   // ExpandedLines will be reset every time we create a new LineAndExpansion
28606c3fb27SDimitry Andric   // instance once a line containing macro calls has been parsed.
28706c3fb27SDimitry Andric   SmallVector<UnwrappedLine, 8> CurrentExpandedLines;
28806c3fb27SDimitry Andric 
28906c3fb27SDimitry Andric   // Maps from the first token of a top-level UnwrappedLine that contains
29006c3fb27SDimitry Andric   // a macro call to the replacement UnwrappedLines expanded from the macro
29106c3fb27SDimitry Andric   // call.
29206c3fb27SDimitry Andric   llvm::DenseMap<FormatToken *, SmallVector<UnwrappedLine, 8>> ExpandedLines;
29306c3fb27SDimitry Andric 
29406c3fb27SDimitry Andric   // Map from the macro identifier to a line containing the full unexpanded
29506c3fb27SDimitry Andric   // macro call.
29606c3fb27SDimitry Andric   llvm::DenseMap<FormatToken *, std::unique_ptr<UnwrappedLine>> Unexpanded;
29706c3fb27SDimitry Andric 
29806c3fb27SDimitry Andric   // For recursive macro expansions, trigger reconstruction only on the
29906c3fb27SDimitry Andric   // outermost expansion.
30006c3fb27SDimitry Andric   bool InExpansion = false;
30106c3fb27SDimitry Andric 
30206c3fb27SDimitry Andric   // Set while we reconstruct a macro call.
30306c3fb27SDimitry Andric   // For reconstruction, we feed the expanded lines into the reconstructor
30406c3fb27SDimitry Andric   // until it is finished.
30506c3fb27SDimitry Andric   std::optional<MacroCallReconstructor> Reconstruct;
30606c3fb27SDimitry Andric 
3070b57cec5SDimitry Andric   // Comments are sorted into unwrapped lines by whether they are in the same
3080b57cec5SDimitry Andric   // line as the previous token, or not. If not, they belong to the next token.
3090b57cec5SDimitry Andric   // Since the next token might already be in a new unwrapped line, we need to
3100b57cec5SDimitry Andric   // store the comments belonging to that token.
3110b57cec5SDimitry Andric   SmallVector<FormatToken *, 1> CommentsBeforeNextToken;
31206c3fb27SDimitry Andric   FormatToken *FormatTok = nullptr;
3130b57cec5SDimitry Andric   bool MustBreakBeforeNextToken;
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric   // The parsed lines. Only added to through \c CurrentLines.
3160b57cec5SDimitry Andric   SmallVector<UnwrappedLine, 8> Lines;
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric   // Preprocessor directives are parsed out-of-order from other unwrapped lines.
3190b57cec5SDimitry Andric   // Thus, we need to keep a list of preprocessor directives to be reported
3200b57cec5SDimitry Andric   // after an unwrapped line that has been started was finished.
3210b57cec5SDimitry Andric   SmallVector<UnwrappedLine, 4> PreprocessorDirectives;
3220b57cec5SDimitry Andric 
3230b57cec5SDimitry Andric   // New unwrapped lines are added via CurrentLines.
3240b57cec5SDimitry Andric   // Usually points to \c &Lines. While parsing a preprocessor directive when
3250b57cec5SDimitry Andric   // there is an unfinished previous unwrapped line, will point to
3260b57cec5SDimitry Andric   // \c &PreprocessorDirectives.
3270b57cec5SDimitry Andric   SmallVectorImpl<UnwrappedLine> *CurrentLines;
3280b57cec5SDimitry Andric 
3290b57cec5SDimitry Andric   // We store for each line whether it must be a declaration depending on
3300b57cec5SDimitry Andric   // whether we are in a compound statement or not.
33104eeddc0SDimitry Andric   llvm::BitVector DeclarationScopeStack;
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric   const FormatStyle &Style;
3340b57cec5SDimitry Andric   const AdditionalKeywords &Keywords;
3350b57cec5SDimitry Andric 
3360b57cec5SDimitry Andric   llvm::Regex CommentPragmasRegex;
3370b57cec5SDimitry Andric 
3380b57cec5SDimitry Andric   FormatTokenSource *Tokens;
3390b57cec5SDimitry Andric   UnwrappedLineConsumer &Callback;
3400b57cec5SDimitry Andric 
3410b57cec5SDimitry Andric   ArrayRef<FormatToken *> AllTokens;
3420b57cec5SDimitry Andric 
34304eeddc0SDimitry Andric   // Keeps a stack of the states of nested control statements (true if the
34404eeddc0SDimitry Andric   // statement contains more than some predefined number of nested statements).
34504eeddc0SDimitry Andric   SmallVector<bool, 8> NestedTooDeep;
34604eeddc0SDimitry Andric 
347*bdb86d1aSDimitry Andric   // Keeps a stack of the states of nested lambdas (true if the return type of
348*bdb86d1aSDimitry Andric   // the lambda is `decltype(auto)`).
349*bdb86d1aSDimitry Andric   SmallVector<bool, 4> NestedLambdas;
350*bdb86d1aSDimitry Andric 
351*bdb86d1aSDimitry Andric   // Whether the parser is parsing the body of a function whose return type is
352*bdb86d1aSDimitry Andric   // `decltype(auto)`.
353*bdb86d1aSDimitry Andric   bool IsDecltypeAutoFunction = false;
354*bdb86d1aSDimitry Andric 
3550b57cec5SDimitry Andric   // Represents preprocessor branch type, so we can find matching
3560b57cec5SDimitry Andric   // #if/#else/#endif directives.
3570b57cec5SDimitry Andric   enum PPBranchKind {
3580b57cec5SDimitry Andric     PP_Conditional, // Any #if, #ifdef, #ifndef, #elif, block outside #if 0
3590b57cec5SDimitry Andric     PP_Unreachable  // #if 0 or a conditional preprocessor block inside #if 0
3600b57cec5SDimitry Andric   };
3610b57cec5SDimitry Andric 
3620b57cec5SDimitry Andric   struct PPBranch {
3630b57cec5SDimitry Andric     PPBranch(PPBranchKind Kind, size_t Line) : Kind(Kind), Line(Line) {}
3640b57cec5SDimitry Andric     PPBranchKind Kind;
3650b57cec5SDimitry Andric     size_t Line;
3660b57cec5SDimitry Andric   };
3670b57cec5SDimitry Andric 
3680b57cec5SDimitry Andric   // Keeps a stack of currently active preprocessor branching directives.
3690b57cec5SDimitry Andric   SmallVector<PPBranch, 16> PPStack;
3700b57cec5SDimitry Andric 
3710b57cec5SDimitry Andric   // The \c UnwrappedLineParser re-parses the code for each combination
3720b57cec5SDimitry Andric   // of preprocessor branches that can be taken.
3730b57cec5SDimitry Andric   // To that end, we take the same branch (#if, #else, or one of the #elif
3740b57cec5SDimitry Andric   // branches) for each nesting level of preprocessor branches.
3750b57cec5SDimitry Andric   // \c PPBranchLevel stores the current nesting level of preprocessor
3760b57cec5SDimitry Andric   // branches during one pass over the code.
3770b57cec5SDimitry Andric   int PPBranchLevel;
3780b57cec5SDimitry Andric 
3790b57cec5SDimitry Andric   // Contains the current branch (#if, #else or one of the #elif branches)
3800b57cec5SDimitry Andric   // for each nesting level.
3810b57cec5SDimitry Andric   SmallVector<int, 8> PPLevelBranchIndex;
3820b57cec5SDimitry Andric 
3830b57cec5SDimitry Andric   // Contains the maximum number of branches at each nesting level.
3840b57cec5SDimitry Andric   SmallVector<int, 8> PPLevelBranchCount;
3850b57cec5SDimitry Andric 
3860b57cec5SDimitry Andric   // Contains the number of branches per nesting level we are currently
3870b57cec5SDimitry Andric   // in while parsing a preprocessor branch sequence.
3880b57cec5SDimitry Andric   // This is used to update PPLevelBranchCount at the end of a branch
3890b57cec5SDimitry Andric   // sequence.
3900b57cec5SDimitry Andric   std::stack<int> PPChainBranchIndex;
3910b57cec5SDimitry Andric 
3920b57cec5SDimitry Andric   // Include guard search state. Used to fixup preprocessor indent levels
3930b57cec5SDimitry Andric   // so that include guards do not participate in indentation.
3940b57cec5SDimitry Andric   enum IncludeGuardState {
3950b57cec5SDimitry Andric     IG_Inited,   // Search started, looking for #ifndef.
3960b57cec5SDimitry Andric     IG_IfNdefed, // #ifndef found, IncludeGuardToken points to condition.
3970b57cec5SDimitry Andric     IG_Defined,  // Matching #define found, checking other requirements.
3980b57cec5SDimitry Andric     IG_Found,    // All requirements met, need to fix indents.
3990b57cec5SDimitry Andric     IG_Rejected, // Search failed or never started.
4000b57cec5SDimitry Andric   };
4010b57cec5SDimitry Andric 
4020b57cec5SDimitry Andric   // Current state of include guard search.
4030b57cec5SDimitry Andric   IncludeGuardState IncludeGuard;
4040b57cec5SDimitry Andric 
4050b57cec5SDimitry Andric   // Points to the #ifndef condition for a potential include guard. Null unless
4060b57cec5SDimitry Andric   // IncludeGuardState == IG_IfNdefed.
4070b57cec5SDimitry Andric   FormatToken *IncludeGuardToken;
4080b57cec5SDimitry Andric 
4090b57cec5SDimitry Andric   // Contains the first start column where the source begins. This is zero for
4100b57cec5SDimitry Andric   // normal source code and may be nonzero when formatting a code fragment that
4110b57cec5SDimitry Andric   // does not start at the beginning of the file.
4120b57cec5SDimitry Andric   unsigned FirstStartColumn;
4130b57cec5SDimitry Andric 
41406c3fb27SDimitry Andric   MacroExpander Macros;
41506c3fb27SDimitry Andric 
4160b57cec5SDimitry Andric   friend class ScopedLineState;
4170b57cec5SDimitry Andric   friend class CompoundStatementIndenter;
4180b57cec5SDimitry Andric };
4190b57cec5SDimitry Andric 
4200b57cec5SDimitry Andric struct UnwrappedLineNode {
4210b57cec5SDimitry Andric   UnwrappedLineNode() : Tok(nullptr) {}
42206c3fb27SDimitry Andric   UnwrappedLineNode(FormatToken *Tok,
42306c3fb27SDimitry Andric                     llvm::ArrayRef<UnwrappedLine> Children = {})
42406c3fb27SDimitry Andric       : Tok(Tok), Children(Children.begin(), Children.end()) {}
4250b57cec5SDimitry Andric 
4260b57cec5SDimitry Andric   FormatToken *Tok;
4270b57cec5SDimitry Andric   SmallVector<UnwrappedLine, 0> Children;
4280b57cec5SDimitry Andric };
4290b57cec5SDimitry Andric 
4300b57cec5SDimitry Andric inline UnwrappedLine::UnwrappedLine()
431bdd1243dSDimitry Andric     : Level(0), PPLevel(0), InPPDirective(false), InPragmaDirective(false),
432bdd1243dSDimitry Andric       InMacroBody(false), MustBeDeclaration(false),
4330b57cec5SDimitry Andric       MatchingOpeningBlockLineIndex(kInvalidIndex) {}
4340b57cec5SDimitry Andric 
4350b57cec5SDimitry Andric } // end namespace format
4360b57cec5SDimitry Andric } // end namespace clang
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric #endif
439