1 //===--- UnwrappedLineParser.h - Format C++ code ----------------*- C++ -*-===// 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 //===----------------------------------------------------------------------===// 15 16 #ifndef LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H 17 #define LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H 18 19 #include "clang/Basic/IdentifierTable.h" 20 #include "clang/Format/Format.h" 21 #include "FormatToken.h" 22 #include <list> 23 24 namespace clang { 25 namespace format { 26 27 struct UnwrappedLineNode; 28 29 /// \brief An unwrapped line is a sequence of \c Token, that we would like to 30 /// put on a single line if there was no column limit. 31 /// 32 /// This is used as a main interface between the \c UnwrappedLineParser and the 33 /// \c UnwrappedLineFormatter. The key property is that changing the formatting 34 /// within an unwrapped line does not affect any other unwrapped lines. 35 struct UnwrappedLine { 36 UnwrappedLine(); 37 38 // FIXME: Don't use std::list here. 39 /// \brief The \c Tokens comprising this \c UnwrappedLine. 40 std::list<UnwrappedLineNode> Tokens; 41 42 /// \brief The indent level of the \c UnwrappedLine. 43 unsigned Level; 44 45 /// \brief Whether this \c UnwrappedLine is part of a preprocessor directive. 46 bool InPPDirective; 47 48 bool MustBeDeclaration; 49 }; 50 51 class UnwrappedLineConsumer { 52 public: 53 virtual ~UnwrappedLineConsumer() {} 54 virtual void consumeUnwrappedLine(const UnwrappedLine &Line) = 0; 55 }; 56 57 class FormatTokenSource; 58 59 class UnwrappedLineParser { 60 public: 61 UnwrappedLineParser(const FormatStyle &Style, ArrayRef<FormatToken *> Tokens, 62 UnwrappedLineConsumer &Callback); 63 64 /// Returns true in case of a structural error. 65 bool parse(); 66 67 private: 68 void parseFile(); 69 void parseLevel(bool HasOpeningBrace); 70 void parseBlock(bool MustBeDeclaration, bool AddLevel = true); 71 void parseChildBlock(); 72 void parsePPDirective(); 73 void parsePPDefine(); 74 void parsePPIf(); 75 void parsePPIfdef(); 76 void parsePPElIf(); 77 void parsePPElse(); 78 void parsePPEndIf(); 79 void parsePPUnknown(); 80 void parseStructuralElement(); 81 bool tryToParseBracedList(); 82 bool parseBracedList(bool ContinueOnSemicolons = false); 83 void parseReturn(); 84 void parseParens(); 85 void parseIfThenElse(); 86 void parseForOrWhileLoop(); 87 void parseDoWhile(); 88 void parseLabel(); 89 void parseCaseLabel(); 90 void parseSwitch(); 91 void parseNamespace(); 92 void parseAccessSpecifier(); 93 void parseEnum(); 94 void parseRecord(); 95 void parseObjCProtocolList(); 96 void parseObjCUntilAtEnd(); 97 void parseObjCInterfaceOrImplementation(); 98 void parseObjCProtocol(); 99 void tryToParseLambda(); 100 bool tryToParseLambdaIntroducer(); 101 void addUnwrappedLine(); 102 bool eof() const; 103 void nextToken(); 104 void readToken(); 105 void flushComments(bool NewlineBeforeNext); 106 void pushToken(FormatToken *Tok); 107 void calculateBraceTypes(); 108 void pushPPConditional(); 109 110 // FIXME: We are constantly running into bugs where Line.Level is incorrectly 111 // subtracted from beyond 0. Introduce a method to subtract from Line.Level 112 // and use that everywhere in the Parser. 113 OwningPtr<UnwrappedLine> Line; 114 115 // Comments are sorted into unwrapped lines by whether they are in the same 116 // line as the previous token, or not. If not, they belong to the next token. 117 // Since the next token might already be in a new unwrapped line, we need to 118 // store the comments belonging to that token. 119 SmallVector<FormatToken *, 1> CommentsBeforeNextToken; 120 FormatToken *FormatTok; 121 bool MustBreakBeforeNextToken; 122 123 // The parsed lines. Only added to through \c CurrentLines. 124 SmallVector<UnwrappedLine, 8> Lines; 125 126 // Preprocessor directives are parsed out-of-order from other unwrapped lines. 127 // Thus, we need to keep a list of preprocessor directives to be reported 128 // after an unwarpped line that has been started was finished. 129 SmallVector<UnwrappedLine, 4> PreprocessorDirectives; 130 131 // New unwrapped lines are added via CurrentLines. 132 // Usually points to \c &Lines. While parsing a preprocessor directive when 133 // there is an unfinished previous unwrapped line, will point to 134 // \c &PreprocessorDirectives. 135 SmallVectorImpl<UnwrappedLine> *CurrentLines; 136 137 // We store for each line whether it must be a declaration depending on 138 // whether we are in a compound statement or not. 139 std::vector<bool> DeclarationScopeStack; 140 141 // Will be true if we encounter an error that leads to possibily incorrect 142 // indentation levels. 143 bool StructuralError; 144 145 const FormatStyle &Style; 146 FormatTokenSource *Tokens; 147 UnwrappedLineConsumer &Callback; 148 149 // FIXME: This is a temporary measure until we have reworked the ownership 150 // of the format tokens. The goal is to have the actual tokens created and 151 // owned outside of and handed into the UnwrappedLineParser. 152 ArrayRef<FormatToken *> AllTokens; 153 154 // Represents preprocessor branch type, so we can find matching 155 // #if/#else/#endif directives. 156 enum PPBranchKind { 157 PP_Conditional, // Any #if, #ifdef, #ifndef, #elif, block outside #if 0 158 PP_Unreachable // #if 0 or a conditional preprocessor block inside #if 0 159 }; 160 161 // Keeps a stack of currently active preprocessor branching directives. 162 SmallVector<PPBranchKind, 16> PPStack; 163 164 friend class ScopedLineState; 165 }; 166 167 struct UnwrappedLineNode { 168 UnwrappedLineNode() : Tok(NULL) {} 169 UnwrappedLineNode(FormatToken *Tok) : Tok(Tok) {} 170 171 FormatToken *Tok; 172 SmallVector<UnwrappedLine, 0> Children; 173 }; 174 175 inline UnwrappedLine::UnwrappedLine() 176 : Level(0), InPPDirective(false), MustBeDeclaration(false) {} 177 178 } // end namespace format 179 } // end namespace clang 180 181 #endif // LLVM_CLANG_FORMAT_UNWRAPPED_LINE_PARSER_H 182