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" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 21 | #include "clang/Lex/Lexer.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 | |
| 27 | /// \brief A wrapper around a \c Token storing information about the |
| 28 | /// whitespace characters preceeding it. |
| 29 | struct FormatToken { |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 30 | FormatToken() |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame^] | 31 | : NewlinesBefore(0), HasUnescapedNewline(false), LastNewlineOffset(0), |
| 32 | TokenLength(0), IsFirst(false), MustBreakBefore(false) {} |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 33 | |
| 34 | /// \brief The \c Token. |
| 35 | Token Tok; |
| 36 | |
| 37 | /// \brief The number of newlines immediately before the \c Token. |
| 38 | /// |
| 39 | /// This can be used to determine what the user wrote in the original code |
| 40 | /// and thereby e.g. leave an empty line between two function definitions. |
| 41 | unsigned NewlinesBefore; |
| 42 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 43 | /// \brief Whether there is at least one unescaped newline before the \c |
| 44 | /// Token. |
| 45 | bool HasUnescapedNewline; |
| 46 | |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 47 | /// \brief The range of the whitespace immediately preceeding the \c Token. |
| 48 | SourceRange WhitespaceRange; |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 49 | |
Daniel Jasper | 1eee6c4 | 2013-03-04 13:43:19 +0000 | [diff] [blame] | 50 | /// \brief The offset just past the last '\n' in this token's leading |
| 51 | /// whitespace (relative to \c WhiteSpaceStart). 0 if there is no '\n'. |
| 52 | unsigned LastNewlineOffset; |
| 53 | |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 54 | /// \brief The length of the non-whitespace parts of the token. This is |
| 55 | /// necessary because we need to handle escaped newlines that are stored |
| 56 | /// with the token. |
| 57 | unsigned TokenLength; |
| 58 | |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 59 | /// \brief Indicates that this is the first token. |
| 60 | bool IsFirst; |
Daniel Jasper | 26f7e78 | 2013-01-08 14:56:18 +0000 | [diff] [blame] | 61 | |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 62 | /// \brief Whether there must be a line break before this token. |
| 63 | /// |
| 64 | /// This happens for example when a preprocessor directive ended directly |
| 65 | /// before the token. |
| 66 | bool MustBreakBefore; |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 67 | |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 68 | /// \brief Returns actual token start location without leading escaped |
| 69 | /// newlines and whitespace. |
| 70 | /// |
| 71 | /// This can be different to Tok.getLocation(), which includes leading escaped |
| 72 | /// newlines. |
| 73 | SourceLocation getStartOfNonWhitespace() const { |
Manuel Klimek | ad3094b | 2013-05-23 10:56:37 +0000 | [diff] [blame] | 74 | return WhitespaceRange.getEnd(); |
Alexander Kornienko | 919398b | 2013-04-17 17:34:05 +0000 | [diff] [blame] | 75 | } |
Manuel Klimek | de008c0 | 2013-05-27 15:23:34 +0000 | [diff] [blame^] | 76 | |
| 77 | /// \brief The raw text of the token. |
| 78 | /// |
| 79 | /// Contains the raw token text without leading whitespace and without leading |
| 80 | /// escaped newlines. |
| 81 | StringRef TokenText; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | /// \brief An unwrapped line is a sequence of \c Token, that we would like to |
| 85 | /// put on a single line if there was no column limit. |
| 86 | /// |
| 87 | /// This is used as a main interface between the \c UnwrappedLineParser and the |
| 88 | /// \c UnwrappedLineFormatter. The key property is that changing the formatting |
| 89 | /// within an unwrapped line does not affect any other unwrapped lines. |
| 90 | struct UnwrappedLine { |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 91 | UnwrappedLine() : Level(0), InPPDirective(false), MustBeDeclaration(false) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 94 | // FIXME: Don't use std::list here. |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 95 | /// \brief The \c Tokens comprising this \c UnwrappedLine. |
| 96 | std::list<FormatToken> Tokens; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 97 | |
| 98 | /// \brief The indent level of the \c UnwrappedLine. |
| 99 | unsigned Level; |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 100 | |
| 101 | /// \brief Whether this \c UnwrappedLine is part of a preprocessor directive. |
| 102 | bool InPPDirective; |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 103 | |
| 104 | bool MustBeDeclaration; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | class UnwrappedLineConsumer { |
| 108 | public: |
Daniel Jasper | accb0b0 | 2012-12-04 21:05:31 +0000 | [diff] [blame] | 109 | virtual ~UnwrappedLineConsumer() { |
| 110 | } |
Alexander Kornienko | 720ffb6 | 2012-12-05 13:56:52 +0000 | [diff] [blame] | 111 | virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 114 | class FormatTokenSource { |
| 115 | public: |
Matt Beaumont-Gay | 422daa1 | 2012-12-07 22:49:27 +0000 | [diff] [blame] | 116 | virtual ~FormatTokenSource() { |
| 117 | } |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 118 | virtual FormatToken getNextToken() = 0; |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 119 | |
| 120 | // FIXME: This interface will become an implementation detail of |
| 121 | // the UnwrappedLineParser once we switch to generate all tokens |
| 122 | // up-front. |
| 123 | virtual unsigned getPosition() { return 0; } |
Manuel Klimek | a3e2179 | 2013-05-23 10:02:51 +0000 | [diff] [blame] | 124 | virtual FormatToken setPosition(unsigned Position) { |
Manuel Klimek | b3778a6 | 2013-05-27 12:36:28 +0000 | [diff] [blame] | 125 | llvm_unreachable("Interface in transition; do not call!"); |
Manuel Klimek | a3e2179 | 2013-05-23 10:02:51 +0000 | [diff] [blame] | 126 | return FormatToken(); |
| 127 | } |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 130 | class UnwrappedLineParser { |
| 131 | public: |
Daniel Jasper | caf42a3 | 2013-05-15 08:14:19 +0000 | [diff] [blame] | 132 | UnwrappedLineParser(const FormatStyle &Style, FormatTokenSource &Tokens, |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 133 | UnwrappedLineConsumer &Callback); |
| 134 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 135 | /// Returns true in case of a structural error. |
| 136 | bool parse(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 137 | |
| 138 | private: |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 139 | void parseFile(); |
| 140 | void parseLevel(bool HasOpeningBrace); |
| 141 | void parseBlock(bool MustBeDeclaration, unsigned AddLevels = 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 142 | void parsePPDirective(); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 143 | void parsePPDefine(); |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 144 | void parsePPIf(); |
| 145 | void parsePPIfdef(); |
| 146 | void parsePPElIf(); |
| 147 | void parsePPElse(); |
| 148 | void parsePPEndIf(); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 149 | void parsePPUnknown(); |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 150 | void parseStructuralElement(); |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 151 | bool tryToParseBracedList(); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 152 | void parseBracedList(); |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 153 | void parseReturn(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 154 | void parseParens(); |
| 155 | void parseIfThenElse(); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 156 | void parseForOrWhileLoop(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 157 | void parseDoWhile(); |
| 158 | void parseLabel(); |
| 159 | void parseCaseLabel(); |
| 160 | void parseSwitch(); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 161 | void parseNamespace(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 162 | void parseAccessSpecifier(); |
| 163 | void parseEnum(); |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 164 | void parseRecord(); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 165 | void parseObjCProtocolList(); |
| 166 | void parseObjCUntilAtEnd(); |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 167 | void parseObjCInterfaceOrImplementation(); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 168 | void parseObjCProtocol(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 169 | void addUnwrappedLine(); |
| 170 | bool eof() const; |
| 171 | void nextToken(); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 172 | void readToken(); |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 173 | void flushComments(bool NewlineBeforeNext); |
| 174 | void pushToken(const FormatToken &Tok); |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 175 | void calculateBraceTypes(); |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 176 | void pushPPConditional(); |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 177 | |
| 178 | // Represents what type of block a left brace opens. |
| 179 | enum LBraceState { |
| 180 | BS_Unknown, |
| 181 | BS_Block, |
| 182 | BS_BracedInit |
| 183 | }; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 184 | |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 185 | // FIXME: We are constantly running into bugs where Line.Level is incorrectly |
| 186 | // subtracted from beyond 0. Introduce a method to subtract from Line.Level |
| 187 | // and use that everywhere in the Parser. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 188 | OwningPtr<UnwrappedLine> Line; |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 189 | |
| 190 | // Comments are sorted into unwrapped lines by whether they are in the same |
| 191 | // line as the previous token, or not. If not, they belong to the next token. |
| 192 | // Since the next token might already be in a new unwrapped line, we need to |
| 193 | // store the comments belonging to that token. |
| 194 | SmallVector<FormatToken, 1> CommentsBeforeNextToken; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 195 | FormatToken FormatTok; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 196 | bool MustBreakBeforeNextToken; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 197 | |
Manuel Klimek | ba287dc | 2013-01-18 18:24:28 +0000 | [diff] [blame] | 198 | // The parsed lines. Only added to through \c CurrentLines. |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 199 | std::vector<UnwrappedLine> Lines; |
| 200 | |
| 201 | // Preprocessor directives are parsed out-of-order from other unwrapped lines. |
| 202 | // Thus, we need to keep a list of preprocessor directives to be reported |
| 203 | // after an unwarpped line that has been started was finished. |
| 204 | std::vector<UnwrappedLine> PreprocessorDirectives; |
| 205 | |
| 206 | // New unwrapped lines are added via CurrentLines. |
| 207 | // Usually points to \c &Lines. While parsing a preprocessor directive when |
| 208 | // there is an unfinished previous unwrapped line, will point to |
| 209 | // \c &PreprocessorDirectives. |
| 210 | std::vector<UnwrappedLine> *CurrentLines; |
| 211 | |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 212 | // We store for each line whether it must be a declaration depending on |
| 213 | // whether we are in a compound statement or not. |
| 214 | std::vector<bool> DeclarationScopeStack; |
| 215 | |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 216 | // Will be true if we encounter an error that leads to possibily incorrect |
| 217 | // indentation levels. |
| 218 | bool StructuralError; |
| 219 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 220 | const FormatStyle &Style; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 221 | FormatTokenSource *Tokens; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 222 | UnwrappedLineConsumer &Callback; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 223 | |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 224 | // FIXME: This is a temporary measure until we have reworked the ownership |
| 225 | // of the format tokens. The goal is to have the actual tokens created and |
| 226 | // owned outside of and handed into the UnwrappedLineParser. |
| 227 | SmallVector<FormatToken, 16> AllTokens; |
| 228 | |
| 229 | // FIXME: Currently we cannot store attributes with tokens, as we treat |
| 230 | // them as read-only; thus, we now store the brace state indexed by the |
| 231 | // position of the token in the stream (see \c AllTokens). |
| 232 | SmallVector<LBraceState, 16> LBraces; |
| 233 | |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 234 | // Represents preprocessor branch type, so we can find matching |
| 235 | // #if/#else/#endif directives. |
| 236 | enum PPBranchKind { |
| 237 | PP_Conditional, // Any #if, #ifdef, #ifndef, #elif, block outside #if 0 |
| 238 | PP_Unreachable // #if 0 or a conditional preprocessor block inside #if 0 |
| 239 | }; |
| 240 | |
| 241 | // Keeps a stack of currently active preprocessor branching directives. |
| 242 | SmallVector<PPBranchKind, 16> PPStack; |
| 243 | |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 244 | friend class ScopedLineState; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 247 | } // end namespace format |
| 248 | } // end namespace clang |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 249 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 250 | #endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H |