1 //===--- TokenAnnotator.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 implements a token annotator, i.e. creates 12 /// \c AnnotatedTokens out of \c FormatTokens with required extra information. 13 /// 14 //===----------------------------------------------------------------------===// 15 16 #ifndef LLVM_CLANG_LIB_FORMAT_TOKENANNOTATOR_H 17 #define LLVM_CLANG_LIB_FORMAT_TOKENANNOTATOR_H 18 19 #include "UnwrappedLineParser.h" 20 #include "clang/Format/Format.h" 21 22 namespace clang { 23 class SourceManager; 24 25 namespace format { 26 27 enum LineType { 28 LT_Invalid, 29 LT_ImportStatement, 30 LT_ObjCDecl, // An @interface, @implementation, or @protocol line. 31 LT_ObjCMethodDecl, 32 LT_ObjCProperty, // An @property line. 33 LT_Other, 34 LT_PreprocessorDirective, 35 LT_VirtualFunctionDecl 36 }; 37 38 class AnnotatedLine { 39 public: 40 AnnotatedLine(const UnwrappedLine &Line) 41 : First(Line.Tokens.front().Tok), Level(Line.Level), 42 MatchingOpeningBlockLineIndex(Line.MatchingOpeningBlockLineIndex), 43 InPPDirective(Line.InPPDirective), 44 MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false), 45 IsMultiVariableDeclStmt(false), Affected(false), 46 LeadingEmptyLinesAffected(false), ChildrenAffected(false), 47 FirstStartColumn(Line.FirstStartColumn) { 48 assert(!Line.Tokens.empty()); 49 50 // Calculate Next and Previous for all tokens. Note that we must overwrite 51 // Next and Previous for every token, as previous formatting runs might have 52 // left them in a different state. 53 First->Previous = nullptr; 54 FormatToken *Current = First; 55 for (std::list<UnwrappedLineNode>::const_iterator I = ++Line.Tokens.begin(), 56 E = Line.Tokens.end(); 57 I != E; ++I) { 58 const UnwrappedLineNode &Node = *I; 59 Current->Next = I->Tok; 60 I->Tok->Previous = Current; 61 Current = Current->Next; 62 Current->Children.clear(); 63 for (const auto &Child : Node.Children) { 64 Children.push_back(new AnnotatedLine(Child)); 65 Current->Children.push_back(Children.back()); 66 } 67 } 68 Last = Current; 69 Last->Next = nullptr; 70 } 71 72 ~AnnotatedLine() { 73 for (unsigned i = 0, e = Children.size(); i != e; ++i) { 74 delete Children[i]; 75 } 76 FormatToken *Current = First; 77 while (Current) { 78 Current->Children.clear(); 79 Current->Role.reset(); 80 Current = Current->Next; 81 } 82 } 83 84 /// \c true if this line starts with the given tokens in order, ignoring 85 /// comments. 86 template <typename... Ts> bool startsWith(Ts... Tokens) const { 87 return First && First->startsSequence(Tokens...); 88 } 89 90 /// \c true if this line ends with the given tokens in reversed order, 91 /// ignoring comments. 92 /// For example, given tokens [T1, T2, T3, ...], the function returns true if 93 /// this line is like "... T3 T2 T1". 94 template <typename... Ts> bool endsWith(Ts... Tokens) const { 95 return Last && Last->endsSequence(Tokens...); 96 } 97 98 /// \c true if this line looks like a function definition instead of a 99 /// function declaration. Asserts MightBeFunctionDecl. 100 bool mightBeFunctionDefinition() const { 101 assert(MightBeFunctionDecl); 102 // FIXME: Line.Last points to other characters than tok::semi 103 // and tok::lbrace. 104 return !Last->isOneOf(tok::semi, tok::comment); 105 } 106 107 FormatToken *First; 108 FormatToken *Last; 109 110 SmallVector<AnnotatedLine *, 0> Children; 111 112 LineType Type; 113 unsigned Level; 114 size_t MatchingOpeningBlockLineIndex; 115 bool InPPDirective; 116 bool MustBeDeclaration; 117 bool MightBeFunctionDecl; 118 bool IsMultiVariableDeclStmt; 119 120 /// \c True if this line should be formatted, i.e. intersects directly or 121 /// indirectly with one of the input ranges. 122 bool Affected; 123 124 /// \c True if the leading empty lines of this line intersect with one of the 125 /// input ranges. 126 bool LeadingEmptyLinesAffected; 127 128 /// \c True if one of this line's children intersects with an input range. 129 bool ChildrenAffected; 130 131 unsigned FirstStartColumn; 132 133 private: 134 // Disallow copying. 135 AnnotatedLine(const AnnotatedLine &) = delete; 136 void operator=(const AnnotatedLine &) = delete; 137 }; 138 139 /// \brief Determines extra information about the tokens comprising an 140 /// \c UnwrappedLine. 141 class TokenAnnotator { 142 public: 143 TokenAnnotator(const FormatStyle &Style, const AdditionalKeywords &Keywords) 144 : Style(Style), Keywords(Keywords) {} 145 146 /// \brief Adapts the indent levels of comment lines to the indent of the 147 /// subsequent line. 148 // FIXME: Can/should this be done in the UnwrappedLineParser? 149 void setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines); 150 151 void annotate(AnnotatedLine &Line); 152 void calculateFormattingInformation(AnnotatedLine &Line); 153 154 private: 155 /// \brief Calculate the penalty for splitting before \c Tok. 156 unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok, 157 bool InFunctionDecl); 158 159 bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left, 160 const FormatToken &Right); 161 162 bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok); 163 164 bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right); 165 166 bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right); 167 168 bool mustBreakForReturnType(const AnnotatedLine &Line) const; 169 170 void printDebugInfo(const AnnotatedLine &Line); 171 172 void calculateUnbreakableTailLengths(AnnotatedLine &Line); 173 174 const FormatStyle &Style; 175 176 const AdditionalKeywords &Keywords; 177 }; 178 179 } // end namespace format 180 } // end namespace clang 181 182 #endif 183