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