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