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_FORMAT_TOKEN_ANNOTATOR_H 17 #define LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H 18 19 #include "UnwrappedLineParser.h" 20 #include "clang/Format/Format.h" 21 #include <string> 22 23 namespace clang { 24 class SourceManager; 25 26 namespace format { 27 28 enum LineType { 29 LT_Invalid, 30 LT_Other, 31 LT_PreprocessorDirective, 32 LT_VirtualFunctionDecl, 33 LT_ObjCDecl, // An @interface, @implementation, or @protocol line. 34 LT_ObjCMethodDecl, 35 LT_ObjCProperty // An @property line. 36 }; 37 38 class AnnotatedLine { 39 public: AnnotatedLine(const UnwrappedLine & Line)40 AnnotatedLine(const UnwrappedLine &Line) 41 : First(Line.Tokens.front().Tok), Level(Line.Level), 42 InPPDirective(Line.InPPDirective), 43 MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false), 44 Affected(false), LeadingEmptyLinesAffected(false), 45 ChildrenAffected(false) { 46 assert(!Line.Tokens.empty()); 47 48 // Calculate Next and Previous for all tokens. Note that we must overwrite 49 // Next and Previous for every token, as previous formatting runs might have 50 // left them in a different state. 51 First->Previous = nullptr; 52 FormatToken *Current = First; 53 for (std::list<UnwrappedLineNode>::const_iterator I = ++Line.Tokens.begin(), 54 E = Line.Tokens.end(); 55 I != E; ++I) { 56 const UnwrappedLineNode &Node = *I; 57 Current->Next = I->Tok; 58 I->Tok->Previous = Current; 59 Current = Current->Next; 60 Current->Children.clear(); 61 for (SmallVectorImpl<UnwrappedLine>::const_iterator 62 I = Node.Children.begin(), 63 E = Node.Children.end(); 64 I != E; ++I) { 65 Children.push_back(new AnnotatedLine(*I)); 66 Current->Children.push_back(Children.back()); 67 } 68 } 69 Last = Current; 70 Last->Next = nullptr; 71 } 72 ~AnnotatedLine()73 ~AnnotatedLine() { 74 for (unsigned i = 0, e = Children.size(); i != e; ++i) { 75 delete Children[i]; 76 } 77 } 78 79 FormatToken *First; 80 FormatToken *Last; 81 82 SmallVector<AnnotatedLine *, 0> Children; 83 84 LineType Type; 85 unsigned Level; 86 bool InPPDirective; 87 bool MustBeDeclaration; 88 bool MightBeFunctionDecl; 89 90 /// \c True if this line should be formatted, i.e. intersects directly or 91 /// indirectly with one of the input ranges. 92 bool Affected; 93 94 /// \c True if the leading empty lines of this line intersect with one of the 95 /// input ranges. 96 bool LeadingEmptyLinesAffected; 97 98 /// \c True if a one of this line's children intersects with an input range. 99 bool ChildrenAffected; 100 101 private: 102 // Disallow copying. 103 AnnotatedLine(const AnnotatedLine &) LLVM_DELETED_FUNCTION; 104 void operator=(const AnnotatedLine &) LLVM_DELETED_FUNCTION; 105 }; 106 107 /// \brief Determines extra information about the tokens comprising an 108 /// \c UnwrappedLine. 109 class TokenAnnotator { 110 public: TokenAnnotator(const FormatStyle & Style,IdentifierInfo & Ident_in)111 TokenAnnotator(const FormatStyle &Style, IdentifierInfo &Ident_in) 112 : Style(Style), Ident_in(Ident_in) {} 113 114 /// \brief Adapts the indent levels of comment lines to the indent of the 115 /// subsequent line. 116 // FIXME: Can/should this be done in the UnwrappedLineParser? 117 void setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines); 118 119 void annotate(AnnotatedLine &Line); 120 void calculateFormattingInformation(AnnotatedLine &Line); 121 122 private: 123 /// \brief Calculate the penalty for splitting before \c Tok. 124 unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok, 125 bool InFunctionDecl); 126 127 bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left, 128 const FormatToken &Right); 129 130 bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok); 131 132 bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right); 133 134 bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right); 135 136 void printDebugInfo(const AnnotatedLine &Line); 137 138 void calculateUnbreakableTailLengths(AnnotatedLine &Line); 139 140 const FormatStyle &Style; 141 142 // Contextual keywords: 143 IdentifierInfo &Ident_in; 144 }; 145 146 } // end namespace format 147 } // end namespace clang 148 149 #endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H 150