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"
30 LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
32 LT_ObjCProperty, // An @property line.
34 LT_PreprocessorDirective,
35 LT_VirtualFunctionDecl
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 IsMultiVariableDeclStmt(false), Affected(false),
45 LeadingEmptyLinesAffected(false), ChildrenAffected(false) {
46 assert(!Line.Tokens.empty());
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();
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 (const auto &Child : Node.Children) {
62 Children.push_back(new AnnotatedLine(Child));
63 Current->Children.push_back(Children.back());
71 for (unsigned i = 0, e = Children.size(); i != e; ++i) {
74 FormatToken *Current = First;
76 Current->Children.clear();
77 Current->Role.reset();
78 Current = Current->Next;
82 /// \c true if this line starts with the given tokens in order, ignoring
84 template <typename... Ts> bool startsWith(Ts... Tokens) const {
85 return First && First->startsSequence(Tokens...);
88 /// \c true if this line ends with the given tokens in reversed order,
89 /// ignoring comments.
90 /// For example, given tokens [T1, T2, T3, ...], the function returns true if
91 /// this line is like "... T3 T2 T1".
92 template <typename... Ts> bool endsWith(Ts... Tokens) const {
93 return Last && Last->endsSequence(Tokens...);
96 /// \c true if this line looks like a function definition instead of a
97 /// function declaration. Asserts MightBeFunctionDecl.
98 bool mightBeFunctionDefinition() const {
99 assert(MightBeFunctionDecl);
100 // FIXME: Line.Last points to other characters than tok::semi
102 return !Last->isOneOf(tok::semi, tok::comment);
108 SmallVector<AnnotatedLine *, 0> Children;
113 bool MustBeDeclaration;
114 bool MightBeFunctionDecl;
115 bool IsMultiVariableDeclStmt;
117 /// \c True if this line should be formatted, i.e. intersects directly or
118 /// indirectly with one of the input ranges.
121 /// \c True if the leading empty lines of this line intersect with one of the
123 bool LeadingEmptyLinesAffected;
125 /// \c True if a one of this line's children intersects with an input range.
126 bool ChildrenAffected;
130 AnnotatedLine(const AnnotatedLine &) = delete;
131 void operator=(const AnnotatedLine &) = delete;
134 /// \brief Determines extra information about the tokens comprising an
135 /// \c UnwrappedLine.
136 class TokenAnnotator {
138 TokenAnnotator(const FormatStyle &Style, const AdditionalKeywords &Keywords)
139 : Style(Style), Keywords(Keywords) {}
141 /// \brief Adapts the indent levels of comment lines to the indent of the
143 // FIXME: Can/should this be done in the UnwrappedLineParser?
144 void setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines);
146 void annotate(AnnotatedLine &Line);
147 void calculateFormattingInformation(AnnotatedLine &Line);
150 /// \brief Calculate the penalty for splitting before \c Tok.
151 unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok,
152 bool InFunctionDecl);
154 bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
155 const FormatToken &Right);
157 bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
159 bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
161 bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
163 bool mustBreakForReturnType(const AnnotatedLine &Line) const;
165 void printDebugInfo(const AnnotatedLine &Line);
167 void calculateUnbreakableTailLengths(AnnotatedLine &Line);
169 const FormatStyle &Style;
171 const AdditionalKeywords &Keywords;
174 } // end namespace format
175 } // end namespace clang