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