Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 1 | //===--- UnwrappedLineParser.h - Format C++ code ----------------*- C++ -*-===// |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// \file |
| 11 | /// \brief This file contains the declaration of the UnwrappedLineParser, |
| 12 | /// which turns a stream of tokens into UnwrappedLines. |
| 13 | /// |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 16 | #ifndef LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEPARSER_H |
| 17 | #define LLVM_CLANG_LIB_FORMAT_UNWRAPPEDLINEPARSER_H |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 18 | |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 19 | #include "FormatToken.h" |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 20 | #include "clang/Basic/IdentifierTable.h" |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 21 | #include "clang/Format/Format.h" |
Krasimir Georgiev | 00c5c72 | 2017-02-02 15:32:19 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Regex.h" |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 23 | #include <list> |
Hans Wennborg | 2f65f7f | 2014-10-29 22:49:58 +0000 | [diff] [blame] | 24 | #include <stack> |
Daniel Jasper | 7c85fde | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 25 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 26 | namespace clang { |
| 27 | namespace format { |
| 28 | |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 29 | struct UnwrappedLineNode; |
| 30 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 31 | /// \brief An unwrapped line is a sequence of \c Token, that we would like to |
| 32 | /// put on a single line if there was no column limit. |
| 33 | /// |
| 34 | /// This is used as a main interface between the \c UnwrappedLineParser and the |
| 35 | /// \c UnwrappedLineFormatter. The key property is that changing the formatting |
| 36 | /// within an unwrapped line does not affect any other unwrapped lines. |
| 37 | struct UnwrappedLine { |
Douglas Gregor | 7cfed21 | 2013-09-05 18:28:53 +0000 | [diff] [blame] | 38 | UnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 39 | |
Daniel Jasper | a67a8f0 | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 40 | // FIXME: Don't use std::list here. |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 41 | /// \brief The \c Tokens comprising this \c UnwrappedLine. |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 42 | std::list<UnwrappedLineNode> Tokens; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 43 | |
| 44 | /// \brief The indent level of the \c UnwrappedLine. |
| 45 | unsigned Level; |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 46 | |
| 47 | /// \brief Whether this \c UnwrappedLine is part of a preprocessor directive. |
| 48 | bool InPPDirective; |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 49 | |
| 50 | bool MustBeDeclaration; |
Krasimir Georgiev | 7cb267a | 2017-02-27 13:28:36 +0000 | [diff] [blame] | 51 | |
| 52 | /// \brief If this \c UnwrappedLine closes a block in a sequence of lines, |
| 53 | /// \c MatchingOpeningBlockLineIndex stores the index of the corresponding |
| 54 | /// opening line. Otherwise, \c MatchingOpeningBlockLineIndex must be |
| 55 | /// \c kInvalidIndex. |
Manuel Klimek | 0dddcf7 | 2018-04-23 09:34:26 +0000 | [diff] [blame^] | 56 | size_t MatchingOpeningBlockLineIndex = kInvalidIndex; |
| 57 | |
| 58 | /// \brief If this \c UnwrappedLine opens a block, stores the index of the |
| 59 | /// line with the corresponding closing brace. |
| 60 | size_t MatchingClosingBlockLineIndex = kInvalidIndex; |
Krasimir Georgiev | 7cb267a | 2017-02-27 13:28:36 +0000 | [diff] [blame] | 61 | |
| 62 | static const size_t kInvalidIndex = -1; |
Krasimir Georgiev | 9ad83fe | 2017-10-30 14:01:50 +0000 | [diff] [blame] | 63 | |
| 64 | unsigned FirstStartColumn = 0; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | class UnwrappedLineConsumer { |
| 68 | public: |
Angel Garcia Gomez | 637d1e6 | 2015-10-20 13:23:58 +0000 | [diff] [blame] | 69 | virtual ~UnwrappedLineConsumer() {} |
Alexander Kornienko | bc09a7e | 2012-12-05 13:56:52 +0000 | [diff] [blame] | 70 | virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0; |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 71 | virtual void finishRun() = 0; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 74 | class FormatTokenSource; |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 75 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 76 | class UnwrappedLineParser { |
| 77 | public: |
Daniel Jasper | d0ec0d6 | 2014-11-04 12:41:02 +0000 | [diff] [blame] | 78 | UnwrappedLineParser(const FormatStyle &Style, |
| 79 | const AdditionalKeywords &Keywords, |
Krasimir Georgiev | 9ad83fe | 2017-10-30 14:01:50 +0000 | [diff] [blame] | 80 | unsigned FirstStartColumn, |
Daniel Jasper | d0ec0d6 | 2014-11-04 12:41:02 +0000 | [diff] [blame] | 81 | ArrayRef<FormatToken *> Tokens, |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 82 | UnwrappedLineConsumer &Callback); |
| 83 | |
Manuel Klimek | 20e0af6 | 2015-05-06 11:56:29 +0000 | [diff] [blame] | 84 | void parse(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 85 | |
| 86 | private: |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 87 | void reset(); |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 88 | void parseFile(); |
| 89 | void parseLevel(bool HasOpeningBrace); |
Manuel Klimek | b212f3b | 2013-10-12 22:46:56 +0000 | [diff] [blame] | 90 | void parseBlock(bool MustBeDeclaration, bool AddLevel = true, |
| 91 | bool MunchSemi = true); |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 92 | void parseChildBlock(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 93 | void parsePPDirective(); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 94 | void parsePPDefine(); |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 95 | void parsePPIf(bool IfDef); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 96 | void parsePPElIf(); |
| 97 | void parsePPElse(); |
| 98 | void parsePPEndIf(); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 99 | void parsePPUnknown(); |
Daniel Jasper | 1dcbbcfc | 2016-03-14 19:21:36 +0000 | [diff] [blame] | 100 | void readTokenWithJavaScriptASI(); |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 101 | void parseStructuralElement(); |
Daniel Jasper | 3c883d1 | 2015-05-18 14:49:19 +0000 | [diff] [blame] | 102 | bool tryToParseBracedList(); |
Krasimir Georgiev | ff747be | 2017-06-27 13:43:07 +0000 | [diff] [blame] | 103 | bool parseBracedList(bool ContinueOnSemicolons = false, |
| 104 | tok::TokenKind ClosingBraceKind = tok::r_brace); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 105 | void parseParens(); |
Manuel Klimek | 9f0a4e5 | 2017-09-19 09:59:30 +0000 | [diff] [blame] | 106 | void parseSquare(bool LambdaIntroducer = false); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 107 | void parseIfThenElse(); |
Daniel Jasper | 04a71a4 | 2014-05-08 11:58:24 +0000 | [diff] [blame] | 108 | void parseTryCatch(); |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 109 | void parseForOrWhileLoop(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 110 | void parseDoWhile(); |
| 111 | void parseLabel(); |
| 112 | void parseCaseLabel(); |
| 113 | void parseSwitch(); |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 114 | void parseNamespace(); |
Daniel Jasper | 6acf513 | 2015-03-12 14:44:29 +0000 | [diff] [blame] | 115 | void parseNew(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 116 | void parseAccessSpecifier(); |
Daniel Jasper | 6f5a193 | 2015-12-29 08:54:23 +0000 | [diff] [blame] | 117 | bool parseEnum(); |
Daniel Jasper | 6be0f55 | 2014-11-13 15:56:28 +0000 | [diff] [blame] | 118 | void parseJavaEnumBody(); |
Martin Probst | 1027fb8 | 2017-02-07 14:05:30 +0000 | [diff] [blame] | 119 | // Parses a record (aka class) as a top level element. If ParseAsExpr is true, |
| 120 | // parses the record as a child block, i.e. if the class declaration is an |
| 121 | // expression. |
| 122 | void parseRecord(bool ParseAsExpr = false); |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 123 | void parseObjCProtocolList(); |
| 124 | void parseObjCUntilAtEnd(); |
Nico Weber | 2ce0ac5 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 125 | void parseObjCInterfaceOrImplementation(); |
Nico Weber | c068ff7 | 2018-01-23 17:10:25 +0000 | [diff] [blame] | 126 | bool parseObjCProtocol(); |
Daniel Jasper | fca735c | 2015-02-19 16:14:18 +0000 | [diff] [blame] | 127 | void parseJavaScriptEs6ImportExport(); |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 128 | bool tryToParseLambda(); |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 129 | bool tryToParseLambdaIntroducer(); |
Daniel Jasper | c03e16a | 2014-05-08 09:25:39 +0000 | [diff] [blame] | 130 | void tryToParseJSFunction(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 131 | void addUnwrappedLine(); |
| 132 | bool eof() const; |
Krasimir Georgiev | 3e05105 | 2017-07-24 14:51:59 +0000 | [diff] [blame] | 133 | // LevelDifference is the difference of levels after and before the current |
| 134 | // token. For example: |
| 135 | // - if the token is '{' and opens a block, LevelDifference is 1. |
| 136 | // - if the token is '}' and closes a block, LevelDifference is -1. |
| 137 | void nextToken(int LevelDifference = 0); |
| 138 | void readToken(int LevelDifference = 0); |
Krasimir Georgiev | f62f958 | 2017-02-08 10:30:44 +0000 | [diff] [blame] | 139 | |
| 140 | // Decides which comment tokens should be added to the current line and which |
| 141 | // should be added as comments before the next token. |
| 142 | // |
| 143 | // Comments specifies the sequence of comment tokens to analyze. They get |
| 144 | // either pushed to the current line or added to the comments before the next |
| 145 | // token. |
| 146 | // |
| 147 | // NextTok specifies the next token. A null pointer NextTok is supported, and |
Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 148 | // signifies either the absence of a next token, or that the next token |
Krasimir Georgiev | f62f958 | 2017-02-08 10:30:44 +0000 | [diff] [blame] | 149 | // shouldn't be taken into accunt for the analysis. |
| 150 | void distributeComments(const SmallVectorImpl<FormatToken *> &Comments, |
| 151 | const FormatToken *NextTok); |
| 152 | |
| 153 | // Adds the comment preceding the next token to unwrapped lines. |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 154 | void flushComments(bool NewlineBeforeNext); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 155 | void pushToken(FormatToken *Tok); |
Daniel Jasper | 3c883d1 | 2015-05-18 14:49:19 +0000 | [diff] [blame] | 156 | void calculateBraceTypes(bool ExpectClassBody = false); |
Manuel Klimek | 68b0304 | 2014-04-14 09:14:11 +0000 | [diff] [blame] | 157 | |
| 158 | // Marks a conditional compilation edge (for example, an '#if', '#ifdef', |
| 159 | // '#else' or merge conflict marker). If 'Unreachable' is true, assumes |
| 160 | // this branch either cannot be taken (for example '#if false'), or should |
| 161 | // not be taken in this round. |
| 162 | void conditionalCompilationCondition(bool Unreachable); |
| 163 | void conditionalCompilationStart(bool Unreachable); |
| 164 | void conditionalCompilationAlternative(); |
| 165 | void conditionalCompilationEnd(); |
| 166 | |
Daniel Jasper | b05a81d | 2014-05-09 13:11:16 +0000 | [diff] [blame] | 167 | bool isOnNewLine(const FormatToken &FormatTok); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 168 | |
Francois Ferrand | a98a95c | 2017-07-28 07:56:14 +0000 | [diff] [blame] | 169 | // Compute hash of the current preprocessor branch. |
| 170 | // This is used to identify the different branches, and thus track if block |
| 171 | // open and close in the same branch. |
| 172 | size_t computePPHash() const; |
| 173 | |
Manuel Klimek | ef2cfb1 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 174 | // FIXME: We are constantly running into bugs where Line.Level is incorrectly |
| 175 | // subtracted from beyond 0. Introduce a method to subtract from Line.Level |
| 176 | // and use that everywhere in the Parser. |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 177 | std::unique_ptr<UnwrappedLine> Line; |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 178 | |
| 179 | // Comments are sorted into unwrapped lines by whether they are in the same |
| 180 | // line as the previous token, or not. If not, they belong to the next token. |
| 181 | // Since the next token might already be in a new unwrapped line, we need to |
| 182 | // store the comments belonging to that token. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 183 | SmallVector<FormatToken *, 1> CommentsBeforeNextToken; |
| 184 | FormatToken *FormatTok; |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 185 | bool MustBreakBeforeNextToken; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 186 | |
Manuel Klimek | 05d82b7 | 2013-01-18 18:24:28 +0000 | [diff] [blame] | 187 | // The parsed lines. Only added to through \c CurrentLines. |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 188 | SmallVector<UnwrappedLine, 8> Lines; |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 189 | |
| 190 | // Preprocessor directives are parsed out-of-order from other unwrapped lines. |
| 191 | // Thus, we need to keep a list of preprocessor directives to be reported |
Francois Ferrand | a98a95c | 2017-07-28 07:56:14 +0000 | [diff] [blame] | 192 | // after an unwrapped line that has been started was finished. |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 193 | SmallVector<UnwrappedLine, 4> PreprocessorDirectives; |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 194 | |
| 195 | // New unwrapped lines are added via CurrentLines. |
| 196 | // Usually points to \c &Lines. While parsing a preprocessor directive when |
| 197 | // there is an unfinished previous unwrapped line, will point to |
| 198 | // \c &PreprocessorDirectives. |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 199 | SmallVectorImpl<UnwrappedLine> *CurrentLines; |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 200 | |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 201 | // We store for each line whether it must be a declaration depending on |
| 202 | // whether we are in a compound statement or not. |
| 203 | std::vector<bool> DeclarationScopeStack; |
| 204 | |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 205 | const FormatStyle &Style; |
Daniel Jasper | d0ec0d6 | 2014-11-04 12:41:02 +0000 | [diff] [blame] | 206 | const AdditionalKeywords &Keywords; |
Martin Probst | 1027fb8 | 2017-02-07 14:05:30 +0000 | [diff] [blame] | 207 | |
Krasimir Georgiev | 00c5c72 | 2017-02-02 15:32:19 +0000 | [diff] [blame] | 208 | llvm::Regex CommentPragmasRegex; |
Daniel Jasper | d0ec0d6 | 2014-11-04 12:41:02 +0000 | [diff] [blame] | 209 | |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 210 | FormatTokenSource *Tokens; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 211 | UnwrappedLineConsumer &Callback; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 212 | |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 213 | // FIXME: This is a temporary measure until we have reworked the ownership |
| 214 | // of the format tokens. The goal is to have the actual tokens created and |
| 215 | // owned outside of and handed into the UnwrappedLineParser. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 216 | ArrayRef<FormatToken *> AllTokens; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 217 | |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 218 | // Represents preprocessor branch type, so we can find matching |
| 219 | // #if/#else/#endif directives. |
| 220 | enum PPBranchKind { |
Daniel Jasper | 3ac9b9e | 2013-07-08 14:34:09 +0000 | [diff] [blame] | 221 | PP_Conditional, // Any #if, #ifdef, #ifndef, #elif, block outside #if 0 |
| 222 | PP_Unreachable // #if 0 or a conditional preprocessor block inside #if 0 |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 223 | }; |
| 224 | |
Francois Ferrand | a98a95c | 2017-07-28 07:56:14 +0000 | [diff] [blame] | 225 | struct PPBranch { |
| 226 | PPBranch(PPBranchKind Kind, size_t Line) : Kind(Kind), Line(Line) {} |
| 227 | PPBranchKind Kind; |
| 228 | size_t Line; |
| 229 | }; |
| 230 | |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 231 | // Keeps a stack of currently active preprocessor branching directives. |
Francois Ferrand | a98a95c | 2017-07-28 07:56:14 +0000 | [diff] [blame] | 232 | SmallVector<PPBranch, 16> PPStack; |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 233 | |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 234 | // The \c UnwrappedLineParser re-parses the code for each combination |
| 235 | // of preprocessor branches that can be taken. |
| 236 | // To that end, we take the same branch (#if, #else, or one of the #elif |
| 237 | // branches) for each nesting level of preprocessor branches. |
| 238 | // \c PPBranchLevel stores the current nesting level of preprocessor |
| 239 | // branches during one pass over the code. |
| 240 | int PPBranchLevel; |
| 241 | |
| 242 | // Contains the current branch (#if, #else or one of the #elif branches) |
| 243 | // for each nesting level. |
| 244 | SmallVector<int, 8> PPLevelBranchIndex; |
| 245 | |
| 246 | // Contains the maximum number of branches at each nesting level. |
| 247 | SmallVector<int, 8> PPLevelBranchCount; |
| 248 | |
| 249 | // Contains the number of branches per nesting level we are currently |
| 250 | // in while parsing a preprocessor branch sequence. |
| 251 | // This is used to update PPLevelBranchCount at the end of a branch |
| 252 | // sequence. |
| 253 | std::stack<int> PPChainBranchIndex; |
| 254 | |
Mark Zeren | 1c3afaf | 2018-02-05 15:59:00 +0000 | [diff] [blame] | 255 | // Include guard search state. Used to fixup preprocessor indent levels |
| 256 | // so that include guards do not participate in indentation. |
| 257 | enum IncludeGuardState { |
| 258 | IG_Inited, // Search started, looking for #ifndef. |
| 259 | IG_IfNdefed, // #ifndef found, IncludeGuardToken points to condition. |
| 260 | IG_Defined, // Matching #define found, checking other requirements. |
| 261 | IG_Found, // All requirements met, need to fix indents. |
| 262 | IG_Rejected, // Search failed or never started. |
| 263 | }; |
| 264 | |
| 265 | // Current state of include guard search. |
| 266 | IncludeGuardState IncludeGuard; |
| 267 | |
| 268 | // Points to the #ifndef condition for a potential include guard. Null unless |
| 269 | // IncludeGuardState == IG_IfNdefed. |
| 270 | FormatToken *IncludeGuardToken; |
| 271 | |
Krasimir Georgiev | 9ad83fe | 2017-10-30 14:01:50 +0000 | [diff] [blame] | 272 | // Contains the first start column where the source begins. This is zero for |
| 273 | // normal source code and may be nonzero when formatting a code fragment that |
| 274 | // does not start at the beginning of the file. |
| 275 | unsigned FirstStartColumn; |
Krasimir Georgiev | ad47c90 | 2017-08-30 14:34:57 +0000 | [diff] [blame] | 276 | |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 277 | friend class ScopedLineState; |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 278 | friend class CompoundStatementIndenter; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 279 | }; |
| 280 | |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 281 | struct UnwrappedLineNode { |
Craig Topper | 2145bc0 | 2014-05-09 08:15:10 +0000 | [diff] [blame] | 282 | UnwrappedLineNode() : Tok(nullptr) {} |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 283 | UnwrappedLineNode(FormatToken *Tok) : Tok(Tok) {} |
| 284 | |
| 285 | FormatToken *Tok; |
| 286 | SmallVector<UnwrappedLine, 0> Children; |
| 287 | }; |
| 288 | |
Manuel Klimek | 89628f6 | 2017-09-20 09:51:03 +0000 | [diff] [blame] | 289 | inline UnwrappedLine::UnwrappedLine() |
| 290 | : Level(0), InPPDirective(false), MustBeDeclaration(false), |
| 291 | MatchingOpeningBlockLineIndex(kInvalidIndex) {} |
Douglas Gregor | 7cfed21 | 2013-09-05 18:28:53 +0000 | [diff] [blame] | 292 | |
Daniel Jasper | 8d1832e | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 293 | } // end namespace format |
| 294 | } // end namespace clang |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 295 | |
Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 296 | #endif |