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 | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 27 | /// \brief An unwrapped line is a sequence of \c Token, that we would like to |
| 28 | /// put on a single line if there was no column limit. |
| 29 | /// |
| 30 | /// This is used as a main interface between the \c UnwrappedLineParser and the |
| 31 | /// \c UnwrappedLineFormatter. The key property is that changing the formatting |
| 32 | /// within an unwrapped line does not affect any other unwrapped lines. |
| 33 | struct UnwrappedLine { |
Daniel Jasper | 2a409b6 | 2013-07-08 14:34:09 +0000 | [diff] [blame^] | 34 | UnwrappedLine() : Level(0), InPPDirective(false), MustBeDeclaration(false) {} |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 35 | |
Daniel Jasper | 3f8cdbf | 2013-01-16 10:41:46 +0000 | [diff] [blame] | 36 | // FIXME: Don't use std::list here. |
Daniel Jasper | cbb6c41 | 2013-01-16 09:10:19 +0000 | [diff] [blame] | 37 | /// \brief The \c Tokens comprising this \c UnwrappedLine. |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 38 | std::list<FormatToken *> Tokens; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 39 | |
| 40 | /// \brief The indent level of the \c UnwrappedLine. |
| 41 | unsigned Level; |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 42 | |
| 43 | /// \brief Whether this \c UnwrappedLine is part of a preprocessor directive. |
| 44 | bool InPPDirective; |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 45 | |
| 46 | bool MustBeDeclaration; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | class UnwrappedLineConsumer { |
| 50 | public: |
Daniel Jasper | 2a409b6 | 2013-07-08 14:34:09 +0000 | [diff] [blame^] | 51 | virtual ~UnwrappedLineConsumer() {} |
Alexander Kornienko | 720ffb6 | 2012-12-05 13:56:52 +0000 | [diff] [blame] | 52 | virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 55 | class FormatTokenSource; |
Alexander Kornienko | 469a21b | 2012-12-07 16:15:44 +0000 | [diff] [blame] | 56 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 57 | class UnwrappedLineParser { |
| 58 | public: |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 59 | UnwrappedLineParser(const FormatStyle &Style, ArrayRef<FormatToken *> Tokens, |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 60 | UnwrappedLineConsumer &Callback); |
| 61 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 62 | /// Returns true in case of a structural error. |
| 63 | bool parse(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 64 | |
| 65 | private: |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 66 | void parseFile(); |
| 67 | void parseLevel(bool HasOpeningBrace); |
| 68 | void parseBlock(bool MustBeDeclaration, unsigned AddLevels = 1); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 69 | void parsePPDirective(); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 70 | void parsePPDefine(); |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 71 | void parsePPIf(); |
| 72 | void parsePPIfdef(); |
| 73 | void parsePPElIf(); |
| 74 | void parsePPElse(); |
| 75 | void parsePPEndIf(); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 76 | void parsePPUnknown(); |
Manuel Klimek | f0ab0a3 | 2013-01-07 14:56:16 +0000 | [diff] [blame] | 77 | void parseStructuralElement(); |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 78 | bool tryToParseBracedList(); |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 79 | void parseBracedList(); |
Manuel Klimek | c44ee89 | 2013-01-21 10:07:49 +0000 | [diff] [blame] | 80 | void parseReturn(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 81 | void parseParens(); |
| 82 | void parseIfThenElse(); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 83 | void parseForOrWhileLoop(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 84 | void parseDoWhile(); |
| 85 | void parseLabel(); |
| 86 | void parseCaseLabel(); |
| 87 | void parseSwitch(); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 88 | void parseNamespace(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 89 | void parseAccessSpecifier(); |
| 90 | void parseEnum(); |
Manuel Klimek | 47ea7f6 | 2013-01-15 13:38:33 +0000 | [diff] [blame] | 91 | void parseRecord(); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 92 | void parseObjCProtocolList(); |
| 93 | void parseObjCUntilAtEnd(); |
Nico Weber | 50767d8 | 2013-01-09 23:25:37 +0000 | [diff] [blame] | 94 | void parseObjCInterfaceOrImplementation(); |
Nico Weber | 1abe6ea | 2013-01-09 21:15:03 +0000 | [diff] [blame] | 95 | void parseObjCProtocol(); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 96 | void addUnwrappedLine(); |
| 97 | bool eof() const; |
| 98 | void nextToken(); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 99 | void readToken(); |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 100 | void flushComments(bool NewlineBeforeNext); |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 101 | void pushToken(FormatToken *Tok); |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 102 | void calculateBraceTypes(); |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 103 | void pushPPConditional(); |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 104 | |
| 105 | // Represents what type of block a left brace opens. |
| 106 | enum LBraceState { |
| 107 | BS_Unknown, |
| 108 | BS_Block, |
| 109 | BS_BracedInit |
| 110 | }; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 111 | |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 112 | // FIXME: We are constantly running into bugs where Line.Level is incorrectly |
| 113 | // subtracted from beyond 0. Introduce a method to subtract from Line.Level |
| 114 | // and use that everywhere in the Parser. |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 115 | OwningPtr<UnwrappedLine> Line; |
Manuel Klimek | 86721d2 | 2013-01-22 16:31:55 +0000 | [diff] [blame] | 116 | |
| 117 | // Comments are sorted into unwrapped lines by whether they are in the same |
| 118 | // line as the previous token, or not. If not, they belong to the next token. |
| 119 | // Since the next token might already be in a new unwrapped line, we need to |
| 120 | // store the comments belonging to that token. |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 121 | SmallVector<FormatToken *, 1> CommentsBeforeNextToken; |
| 122 | FormatToken *FormatTok; |
Manuel Klimek | 526ed11 | 2013-01-09 15:25:02 +0000 | [diff] [blame] | 123 | bool MustBreakBeforeNextToken; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 124 | |
Manuel Klimek | ba287dc | 2013-01-18 18:24:28 +0000 | [diff] [blame] | 125 | // The parsed lines. Only added to through \c CurrentLines. |
Manuel Klimek | 525fe16 | 2013-01-18 14:04:34 +0000 | [diff] [blame] | 126 | std::vector<UnwrappedLine> Lines; |
| 127 | |
| 128 | // Preprocessor directives are parsed out-of-order from other unwrapped lines. |
| 129 | // Thus, we need to keep a list of preprocessor directives to be reported |
| 130 | // after an unwarpped line that has been started was finished. |
| 131 | std::vector<UnwrappedLine> PreprocessorDirectives; |
| 132 | |
| 133 | // New unwrapped lines are added via CurrentLines. |
| 134 | // Usually points to \c &Lines. While parsing a preprocessor directive when |
| 135 | // there is an unfinished previous unwrapped line, will point to |
| 136 | // \c &PreprocessorDirectives. |
| 137 | std::vector<UnwrappedLine> *CurrentLines; |
| 138 | |
Manuel Klimek | 70b03f4 | 2013-01-23 09:32:48 +0000 | [diff] [blame] | 139 | // We store for each line whether it must be a declaration depending on |
| 140 | // whether we are in a compound statement or not. |
| 141 | std::vector<bool> DeclarationScopeStack; |
| 142 | |
Manuel Klimek | 67d080d | 2013-04-12 14:13:36 +0000 | [diff] [blame] | 143 | // Will be true if we encounter an error that leads to possibily incorrect |
| 144 | // indentation levels. |
| 145 | bool StructuralError; |
| 146 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 147 | const FormatStyle &Style; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 148 | FormatTokenSource *Tokens; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 149 | UnwrappedLineConsumer &Callback; |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 150 | |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 151 | // FIXME: This is a temporary measure until we have reworked the ownership |
| 152 | // of the format tokens. The goal is to have the actual tokens created and |
| 153 | // owned outside of and handed into the UnwrappedLineParser. |
Manuel Klimek | 96e888b | 2013-05-28 11:55:06 +0000 | [diff] [blame] | 154 | ArrayRef<FormatToken *> AllTokens; |
Manuel Klimek | 80829bd | 2013-05-23 09:41:43 +0000 | [diff] [blame] | 155 | |
| 156 | // FIXME: Currently we cannot store attributes with tokens, as we treat |
| 157 | // them as read-only; thus, we now store the brace state indexed by the |
| 158 | // position of the token in the stream (see \c AllTokens). |
| 159 | SmallVector<LBraceState, 16> LBraces; |
| 160 | |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 161 | // Represents preprocessor branch type, so we can find matching |
| 162 | // #if/#else/#endif directives. |
| 163 | enum PPBranchKind { |
Daniel Jasper | 2a409b6 | 2013-07-08 14:34:09 +0000 | [diff] [blame^] | 164 | PP_Conditional, // Any #if, #ifdef, #ifndef, #elif, block outside #if 0 |
| 165 | PP_Unreachable // #if 0 or a conditional preprocessor block inside #if 0 |
Alexander Kornienko | 6fb46b0 | 2013-05-24 18:24:24 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | // Keeps a stack of currently active preprocessor branching directives. |
| 169 | SmallVector<PPBranchKind, 16> PPStack; |
| 170 | |
Manuel Klimek | bb42bf1 | 2013-01-10 11:52:21 +0000 | [diff] [blame] | 171 | friend class ScopedLineState; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 174 | } // end namespace format |
| 175 | } // end namespace clang |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 176 | |
Daniel Jasper | cd16238 | 2013-01-07 13:26:07 +0000 | [diff] [blame] | 177 | #endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H |