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 | |
| 16 | #ifndef LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H |
| 17 | #define LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H |
| 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" |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 22 | #include <list> |
Daniel Jasper | 7c85fde | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 23 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 24 | namespace clang { |
| 25 | namespace format { |
| 26 | |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 27 | struct UnwrappedLineNode; |
| 28 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 29 | /// \brief An unwrapped line is a sequence of \c Token, that we would like to |
| 30 | /// put on a single line if there was no column limit. |
| 31 | /// |
| 32 | /// This is used as a main interface between the \c UnwrappedLineParser and the |
| 33 | /// \c UnwrappedLineFormatter. The key property is that changing the formatting |
| 34 | /// within an unwrapped line does not affect any other unwrapped lines. |
| 35 | struct UnwrappedLine { |
Douglas Gregor | 7cfed21 | 2013-09-05 18:28:53 +0000 | [diff] [blame] | 36 | UnwrappedLine(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 37 | |
Daniel Jasper | a67a8f0 | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 38 | // FIXME: Don't use std::list here. |
Daniel Jasper | daffc0d | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 39 | /// \brief The \c Tokens comprising this \c UnwrappedLine. |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 40 | std::list<UnwrappedLineNode> Tokens; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 41 | |
| 42 | /// \brief The indent level of the \c UnwrappedLine. |
| 43 | unsigned Level; |
Manuel Klimek | a71e5d8 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 44 | |
| 45 | /// \brief Whether this \c UnwrappedLine is part of a preprocessor directive. |
| 46 | bool InPPDirective; |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 47 | |
| 48 | bool MustBeDeclaration; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | class UnwrappedLineConsumer { |
| 52 | public: |
Daniel Jasper | 3ac9b9e | 2013-07-08 14:34:09 +0000 | [diff] [blame] | 53 | virtual ~UnwrappedLineConsumer() {} |
Alexander Kornienko | bc09a7e | 2012-12-05 13:56:52 +0000 | [diff] [blame] | 54 | virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0; |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 55 | virtual void finishRun() = 0; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 56 | }; |
| 57 | |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 58 | class FormatTokenSource; |
Alexander Kornienko | e327684 | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 59 | |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 60 | class UnwrappedLineParser { |
| 61 | public: |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 62 | UnwrappedLineParser(const FormatStyle &Style, ArrayRef<FormatToken *> Tokens, |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 63 | UnwrappedLineConsumer &Callback); |
| 64 | |
Alexander Kornienko | 870f9eb | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 65 | /// Returns true in case of a structural error. |
| 66 | bool parse(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 67 | |
| 68 | private: |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 69 | void reset(); |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 70 | void parseFile(); |
| 71 | void parseLevel(bool HasOpeningBrace); |
Manuel Klimek | b212f3b | 2013-10-12 22:46:56 +0000 | [diff] [blame] | 72 | void parseBlock(bool MustBeDeclaration, bool AddLevel = true, |
| 73 | bool MunchSemi = true); |
Manuel Klimek | 516e054 | 2013-09-04 13:25:30 +0000 | [diff] [blame] | 74 | void parseChildBlock(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 75 | void parsePPDirective(); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 76 | void parsePPDefine(); |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 77 | void parsePPIf(bool IfDef); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 78 | void parsePPElIf(); |
| 79 | void parsePPElse(); |
| 80 | void parsePPEndIf(); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 81 | void parsePPUnknown(); |
Manuel Klimek | 6b9eeba | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 82 | void parseStructuralElement(); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 83 | bool tryToParseBracedList(); |
Daniel Jasper | 015ed02 | 2013-09-13 09:20:45 +0000 | [diff] [blame] | 84 | bool parseBracedList(bool ContinueOnSemicolons = false); |
Manuel Klimek | 762dd18 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 85 | void parseReturn(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 86 | void parseParens(); |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 87 | void parseSquare(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 88 | void parseIfThenElse(); |
Alexander Kornienko | 37d6c94 | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 89 | void parseForOrWhileLoop(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 90 | void parseDoWhile(); |
| 91 | void parseLabel(); |
| 92 | void parseCaseLabel(); |
| 93 | void parseSwitch(); |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 94 | void parseNamespace(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 95 | void parseAccessSpecifier(); |
| 96 | void parseEnum(); |
Manuel Klimek | e01bab5 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 97 | void parseRecord(); |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 98 | void parseObjCProtocolList(); |
| 99 | void parseObjCUntilAtEnd(); |
Nico Weber | 2ce0ac5 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 100 | void parseObjCInterfaceOrImplementation(); |
Nico Weber | 8696a8d | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 101 | void parseObjCProtocol(); |
Daniel Jasper | b88b25f | 2013-12-23 07:29:06 +0000 | [diff] [blame] | 102 | bool tryToParseLambda(); |
Manuel Klimek | ffdeb59 | 2013-09-03 15:10:01 +0000 | [diff] [blame] | 103 | bool tryToParseLambdaIntroducer(); |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 104 | void addUnwrappedLine(); |
| 105 | bool eof() const; |
| 106 | void nextToken(); |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 107 | void readToken(); |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 108 | void flushComments(bool NewlineBeforeNext); |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 109 | void pushToken(FormatToken *Tok); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 110 | void calculateBraceTypes(); |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 111 | void pushPPConditional(); |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 112 | |
Manuel Klimek | ef2cfb1 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 113 | // FIXME: We are constantly running into bugs where Line.Level is incorrectly |
| 114 | // subtracted from beyond 0. Introduce a method to subtract from Line.Level |
| 115 | // and use that everywhere in the Parser. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 116 | OwningPtr<UnwrappedLine> Line; |
Manuel Klimek | f92f7bc | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 117 | |
| 118 | // Comments are sorted into unwrapped lines by whether they are in the same |
| 119 | // line as the previous token, or not. If not, they belong to the next token. |
| 120 | // Since the next token might already be in a new unwrapped line, we need to |
| 121 | // store the comments belonging to that token. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 122 | SmallVector<FormatToken *, 1> CommentsBeforeNextToken; |
| 123 | FormatToken *FormatTok; |
Manuel Klimek | 52b1515 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 124 | bool MustBreakBeforeNextToken; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 125 | |
Manuel Klimek | 05d82b7 | 2013-01-18 18:24:28 +0000 | [diff] [blame] | 126 | // The parsed lines. Only added to through \c CurrentLines. |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 127 | SmallVector<UnwrappedLine, 8> Lines; |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 128 | |
| 129 | // Preprocessor directives are parsed out-of-order from other unwrapped lines. |
| 130 | // Thus, we need to keep a list of preprocessor directives to be reported |
| 131 | // after an unwarpped line that has been started was finished. |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 132 | SmallVector<UnwrappedLine, 4> PreprocessorDirectives; |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 133 | |
| 134 | // New unwrapped lines are added via CurrentLines. |
| 135 | // Usually points to \c &Lines. While parsing a preprocessor directive when |
| 136 | // there is an unfinished previous unwrapped line, will point to |
| 137 | // \c &PreprocessorDirectives. |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 138 | SmallVectorImpl<UnwrappedLine> *CurrentLines; |
Manuel Klimek | d3b92fa | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 139 | |
Manuel Klimek | 0a3a3c9 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 140 | // We store for each line whether it must be a declaration depending on |
| 141 | // whether we are in a compound statement or not. |
| 142 | std::vector<bool> DeclarationScopeStack; |
| 143 | |
Manuel Klimek | 1a18c40 | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 144 | // Will be true if we encounter an error that leads to possibily incorrect |
| 145 | // indentation levels. |
| 146 | bool StructuralError; |
| 147 | |
Alexander Kornienko | 578fdd8 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 148 | const FormatStyle &Style; |
Manuel Klimek | 1abf789 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 149 | FormatTokenSource *Tokens; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 150 | UnwrappedLineConsumer &Callback; |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 151 | |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 152 | // FIXME: This is a temporary measure until we have reworked the ownership |
| 153 | // of the format tokens. The goal is to have the actual tokens created and |
| 154 | // owned outside of and handed into the UnwrappedLineParser. |
Manuel Klimek | 15dfe7a | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 155 | ArrayRef<FormatToken *> AllTokens; |
Manuel Klimek | ab41991 | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 156 | |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 157 | // Represents preprocessor branch type, so we can find matching |
| 158 | // #if/#else/#endif directives. |
| 159 | enum PPBranchKind { |
Daniel Jasper | 3ac9b9e | 2013-07-08 14:34:09 +0000 | [diff] [blame] | 160 | PP_Conditional, // Any #if, #ifdef, #ifndef, #elif, block outside #if 0 |
| 161 | PP_Unreachable // #if 0 or a conditional preprocessor block inside #if 0 |
Alexander Kornienko | f2e0212 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 162 | }; |
| 163 | |
| 164 | // Keeps a stack of currently active preprocessor branching directives. |
| 165 | SmallVector<PPBranchKind, 16> PPStack; |
| 166 | |
Manuel Klimek | 71814b4 | 2013-10-11 21:25:45 +0000 | [diff] [blame] | 167 | // The \c UnwrappedLineParser re-parses the code for each combination |
| 168 | // of preprocessor branches that can be taken. |
| 169 | // To that end, we take the same branch (#if, #else, or one of the #elif |
| 170 | // branches) for each nesting level of preprocessor branches. |
| 171 | // \c PPBranchLevel stores the current nesting level of preprocessor |
| 172 | // branches during one pass over the code. |
| 173 | int PPBranchLevel; |
| 174 | |
| 175 | // Contains the current branch (#if, #else or one of the #elif branches) |
| 176 | // for each nesting level. |
| 177 | SmallVector<int, 8> PPLevelBranchIndex; |
| 178 | |
| 179 | // Contains the maximum number of branches at each nesting level. |
| 180 | SmallVector<int, 8> PPLevelBranchCount; |
| 181 | |
| 182 | // Contains the number of branches per nesting level we are currently |
| 183 | // in while parsing a preprocessor branch sequence. |
| 184 | // This is used to update PPLevelBranchCount at the end of a branch |
| 185 | // sequence. |
| 186 | std::stack<int> PPChainBranchIndex; |
| 187 | |
Manuel Klimek | 8e07a1b | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 188 | friend class ScopedLineState; |
Alexander Kornienko | 3a33f02 | 2013-12-12 09:49:52 +0000 | [diff] [blame] | 189 | friend class CompoundStatementIndenter; |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 190 | }; |
| 191 | |
Daniel Jasper | 9fe0e8d | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 192 | struct UnwrappedLineNode { |
| 193 | UnwrappedLineNode() : Tok(NULL) {} |
| 194 | UnwrappedLineNode(FormatToken *Tok) : Tok(Tok) {} |
| 195 | |
| 196 | FormatToken *Tok; |
| 197 | SmallVector<UnwrappedLine, 0> Children; |
| 198 | }; |
| 199 | |
Douglas Gregor | 7cfed21 | 2013-09-05 18:28:53 +0000 | [diff] [blame] | 200 | inline UnwrappedLine::UnwrappedLine() |
| 201 | : Level(0), InPPDirective(false), MustBeDeclaration(false) {} |
| 202 | |
Daniel Jasper | 8d1832e | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 203 | } // end namespace format |
| 204 | } // end namespace clang |
Daniel Jasper | f793511 | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 205 | |
Daniel Jasper | 8d1832e | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 206 | #endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H |