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