1 //===--- TokenAnnotator.h - Format C++ code ---------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file implements a token annotator, i.e. creates
12 /// \c AnnotatedTokens out of \c FormatTokens with required extra information.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CLANG_LIB_FORMAT_TOKENANNOTATOR_H
17 #define LLVM_CLANG_LIB_FORMAT_TOKENANNOTATOR_H
19 #include "UnwrappedLineParser.h"
20 #include "clang/Format/Format.h"
31 LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
33 LT_ObjCProperty, // An @property line.
35 LT_PreprocessorDirective,
36 LT_VirtualFunctionDecl
41 AnnotatedLine(const UnwrappedLine &Line)
42 : First(Line.Tokens.front().Tok), Level(Line.Level),
43 InPPDirective(Line.InPPDirective),
44 MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false),
45 IsMultiVariableDeclStmt(false), Affected(false),
46 LeadingEmptyLinesAffected(false), ChildrenAffected(false) {
47 assert(!Line.Tokens.empty());
49 // Calculate Next and Previous for all tokens. Note that we must overwrite
50 // Next and Previous for every token, as previous formatting runs might have
51 // left them in a different state.
52 First->Previous = nullptr;
53 FormatToken *Current = First;
54 for (std::list<UnwrappedLineNode>::const_iterator I = ++Line.Tokens.begin(),
55 E = Line.Tokens.end();
57 const UnwrappedLineNode &Node = *I;
58 Current->Next = I->Tok;
59 I->Tok->Previous = Current;
60 Current = Current->Next;
61 Current->Children.clear();
62 for (const auto &Child : Node.Children) {
63 Children.push_back(new AnnotatedLine(Child));
64 Current->Children.push_back(Children.back());
72 for (unsigned i = 0, e = Children.size(); i != e; ++i) {
75 FormatToken *Current = First;
77 Current->Children.clear();
78 Current->Role.reset();
79 Current = Current->Next;
83 /// \c true if this line starts with the given tokens in order, ignoring
85 template <typename... Ts> bool startsWith(Ts... Tokens) const {
86 return startsWith(First, Tokens...);
89 /// \c true if this line looks like a function definition instead of a
90 /// function declaration. Asserts MightBeFunctionDecl.
91 bool mightBeFunctionDefinition() const {
92 assert(MightBeFunctionDecl);
93 // FIXME: Line.Last points to other characters than tok::semi
95 return !Last->isOneOf(tok::semi, tok::comment);
101 SmallVector<AnnotatedLine *, 0> Children;
106 bool MustBeDeclaration;
107 bool MightBeFunctionDecl;
108 bool IsMultiVariableDeclStmt;
110 /// \c True if this line should be formatted, i.e. intersects directly or
111 /// indirectly with one of the input ranges.
114 /// \c True if the leading empty lines of this line intersect with one of the
116 bool LeadingEmptyLinesAffected;
118 /// \c True if a one of this line's children intersects with an input range.
119 bool ChildrenAffected;
123 AnnotatedLine(const AnnotatedLine &) = delete;
124 void operator=(const AnnotatedLine &) = delete;
126 template <typename A, typename... Ts>
127 bool startsWith(FormatToken *Tok, A K1) const {
128 while (Tok && Tok->is(tok::comment))
130 return Tok && Tok->is(K1);
133 template <typename A, typename... Ts>
134 bool startsWith(FormatToken *Tok, A K1, Ts... Tokens) const {
135 return startsWith(Tok, K1) && startsWith(Tok->Next, Tokens...);
139 /// \brief Determines extra information about the tokens comprising an
140 /// \c UnwrappedLine.
141 class TokenAnnotator {
143 TokenAnnotator(const FormatStyle &Style, const AdditionalKeywords &Keywords)
144 : Style(Style), Keywords(Keywords) {}
146 /// \brief Adapts the indent levels of comment lines to the indent of the
148 // FIXME: Can/should this be done in the UnwrappedLineParser?
149 void setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines);
151 void annotate(AnnotatedLine &Line);
152 void calculateFormattingInformation(AnnotatedLine &Line);
155 /// \brief Calculate the penalty for splitting before \c Tok.
156 unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok,
157 bool InFunctionDecl);
159 bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
160 const FormatToken &Right);
162 bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
164 bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
166 bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
168 bool mustBreakForReturnType(const AnnotatedLine &Line) const;
170 void printDebugInfo(const AnnotatedLine &Line);
172 void calculateUnbreakableTailLengths(AnnotatedLine &Line);
174 const FormatStyle &Style;
176 const AdditionalKeywords &Keywords;
179 } // end namespace format
180 } // end namespace clang