clang 8.0.0

TokenAnnotator.h
Go to the documentation of this file.
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 /// 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 MatchingClosingBlockLineIndex(Line.MatchingClosingBlockLineIndex),
44 InPPDirective(Line.InPPDirective),
45 MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false),
46 IsMultiVariableDeclStmt(false), Affected(false),
47 LeadingEmptyLinesAffected(false), ChildrenAffected(false),
48 FirstStartColumn(Line.FirstStartColumn) {
49 assert(!Line.Tokens.empty());
50
51 // Calculate Next and Previous for all tokens. Note that we must overwrite
52 // Next and Previous for every token, as previous formatting runs might have
53 // left them in a different state.
54 First->Previous = nullptr;
55 FormatToken *Current = First;
56 for (std::list<UnwrappedLineNode>::const_iterator I = ++Line.Tokens.begin(),
57 E = Line.Tokens.end();
58 I != E; ++I) {
59 const UnwrappedLineNode &Node = *I;
60 Current->Next = I->Tok;
61 I->Tok->Previous = Current;
62 Current = Current->Next;
63 Current->Children.clear();
64 for (const auto &Child : Node.Children) {
65 Children.push_back(new AnnotatedLine(Child));
66 Current->Children.push_back(Children.back());
67 }
68 }
69 Last = Current;
70 Last->Next = nullptr;
71 }
72
73 ~AnnotatedLine() {
74 for (unsigned i = 0, e = Children.size(); i != e; ++i) {
75 delete Children[i];
76 }
77 FormatToken *Current = First;
78 while (Current) {
79 Current->Children.clear();
80 Current->Role.reset();
81 Current = Current->Next;
82 }
83 }
84
85 /// \c true if this line starts with the given tokens in order, ignoring
86 /// comments.
87 template <typename... Ts> bool startsWith(Ts... Tokens) const {
88 return First && First->startsSequence(Tokens...);
89 }
90
91 /// \c true if this line ends with the given tokens in reversed order,
92 /// ignoring comments.
93 /// For example, given tokens [T1, T2, T3, ...], the function returns true if
94 /// this line is like "... T3 T2 T1".
95 template <typename... Ts> bool endsWith(Ts... Tokens) const {
96 return Last && Last->endsSequence(Tokens...);
97 }
98
99 /// \c true if this line looks like a function definition instead of a
100 /// function declaration. Asserts MightBeFunctionDecl.
101 bool mightBeFunctionDefinition() const {
102 assert(MightBeFunctionDecl);
103 // FIXME: Line.Last points to other characters than tok::semi
104 // and tok::lbrace.
105 return !Last->isOneOf(tok::semi, tok::comment);
106 }
107
108 /// \c true if this line starts a namespace definition.
109 bool startsWithNamespace() const {
110 return startsWith(tok::kw_namespace) ||
111 startsWith(tok::kw_inline, tok::kw_namespace) ||
112 startsWith(tok::kw_export, tok::kw_namespace);
113 }
114
115 FormatToken *First;
116 FormatToken *Last;
117
118 SmallVector<AnnotatedLine *, 0> Children;
119
120 LineType Type;
121 unsigned Level;
122 size_t MatchingOpeningBlockLineIndex;
123 size_t MatchingClosingBlockLineIndex;
124 bool InPPDirective;
125 bool MustBeDeclaration;
126 bool MightBeFunctionDecl;
127 bool IsMultiVariableDeclStmt;
128
129 /// \c True if this line should be formatted, i.e. intersects directly or
130 /// indirectly with one of the input ranges.
131 bool Affected;
132
133 /// \c True if the leading empty lines of this line intersect with one of the
134 /// input ranges.
135 bool LeadingEmptyLinesAffected;
136
137 /// \c True if one of this line's children intersects with an input range.
138 bool ChildrenAffected;
139
140 unsigned FirstStartColumn;
141
142 private:
143 // Disallow copying.
144 AnnotatedLine(const AnnotatedLine &) = delete;
145 void operator=(const AnnotatedLine &) = delete;
146 };
147
148 /// Determines extra information about the tokens comprising an
149 /// \c UnwrappedLine.
150 class TokenAnnotator {
151 public:
152 TokenAnnotator(const FormatStyle &Style, const AdditionalKeywords &Keywords)
153 : Style(Style), Keywords(Keywords) {}
154
155 /// Adapts the indent levels of comment lines to the indent of the
156 /// subsequent line.
157 // FIXME: Can/should this be done in the UnwrappedLineParser?
158 void setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines);
159
160 void annotate(AnnotatedLine &Line);
161 void calculateFormattingInformation(AnnotatedLine &Line);
162
163 private:
164 /// Calculate the penalty for splitting before \c Tok.
165 unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok,
166 bool InFunctionDecl);
167
168 bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
169 const FormatToken &Right);
170
171 bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Right);
172
173 bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
174
175 bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
176
177 bool mustBreakForReturnType(const AnnotatedLine &Line) const;
178
179 void printDebugInfo(const AnnotatedLine &Line);
180
181 void calculateUnbreakableTailLengths(AnnotatedLine &Line);
182
183 const FormatStyle &Style;
184
185 const AdditionalKeywords &Keywords;
186 };
187
188 } // end namespace format
189 } // end namespace clang
190
191 #endif
clang::format::FormatToken::endsSequence
bool endsSequence(A K1, Ts... Tokens) const
true if this token ends a sequence with the given tokens in order, following the Previous pointers...
Definition: FormatToken.h:343
clang::format::AnnotatedLine::FirstStartColumn
unsigned FirstStartColumn
Definition: TokenAnnotator.h:140
clang::format::UnwrappedLineNode::Children
SmallVector< UnwrappedLine, 0 > Children
Definition: UnwrappedLineParser.h:288
clang::format::AnnotatedLine::endsWith
bool endsWith(Ts... Tokens) const
true if this line ends with the given tokens in reversed order, ignoring comments.
Definition: TokenAnnotator.h:95
clang::format::FormatToken::Tok
Token Tok
The Token.
Definition: FormatToken.h:128
clang::format::FormatToken::Role
std::unique_ptr< TokenRole > Role
A token can have a special role that can carry extra information about the token&#39;s formatting...
Definition: FormatToken.h:204
clang::format::AnnotatedLine::MustBeDeclaration
bool MustBeDeclaration
Definition: TokenAnnotator.h:125
clang::format::printDebugInfo
static LLVM_ATTRIBUTE_UNUSED void printDebugInfo(const UnwrappedLine &Line, StringRef Prefix="")
Definition: UnwrappedLineParser.cpp:2360
clang::format::AnnotatedLine::LeadingEmptyLinesAffected
bool LeadingEmptyLinesAffected
True if the leading empty lines of this line intersect with one of the input ranges.
Definition: TokenAnnotator.h:135
clang::format::AnnotatedLine::InPPDirective
bool InPPDirective
Definition: TokenAnnotator.h:124
clang::format::AnnotatedLine::Last
FormatToken * Last
Definition: TokenAnnotator.h:116
clang::format::FormatToken::Next
FormatToken * Next
The next token in the unwrapped line.
Definition: FormatToken.h:293
clang::format::AnnotatedLine::MatchingOpeningBlockLineIndex
size_t MatchingOpeningBlockLineIndex
Definition: TokenAnnotator.h:122
clang::format::AnnotatedLine::MatchingClosingBlockLineIndex
size_t MatchingClosingBlockLineIndex
Definition: TokenAnnotator.h:123
clang::format::FormatToken::Previous
FormatToken * Previous
The previous token in the unwrapped line.
Definition: FormatToken.h:290
llvm::SmallVectorImpl
Definition: LLVM.h:37
clang::format::LineType
LineType
Definition: TokenAnnotator.h:27
clang::format::Tok
const FormatToken & Tok
Definition: BreakableToken.h:244
clang::format::AnnotatedLine::ChildrenAffected
bool ChildrenAffected
True if one of this line&#39;s children intersects with an input range.
Definition: TokenAnnotator.h:138
clang::format::AnnotatedLine::~AnnotatedLine
~AnnotatedLine()
Definition: TokenAnnotator.h:73
clang::format::LT_ObjCMethodDecl
Definition: TokenAnnotator.h:31
clang::format::UnwrappedLine
An unwrapped line is a sequence of Token, that we would like to put on a single line if there was no ...
Definition: UnwrappedLineParser.h:37
clang::format::AnnotatedLine::mightBeFunctionDefinition
bool mightBeFunctionDefinition() const
true if this line looks like a function definition instead of a function declaration.
Definition: TokenAnnotator.h:101
clang::format::FormatToken::isOneOf
bool isOneOf(A K1, B K2) const
Definition: FormatToken.h:316
clang::format::AnnotatedLine::Type
LineType Type
Definition: TokenAnnotator.h:120
clang::format::TokenAnnotator
Determines extra information about the tokens comprising an UnwrappedLine.
Definition: TokenAnnotator.h:150
clang::format::UnwrappedLine::Tokens
std::list< UnwrappedLineNode > Tokens
The Tokens comprising this UnwrappedLine.
Definition: UnwrappedLineParser.h:42
Line
const AnnotatedLine * Line
Definition: UsingDeclarationsSorter.cpp:69
clang::format::AnnotatedLine::Children
SmallVector< AnnotatedLine *, 0 > Children
Definition: TokenAnnotator.h:118
clang::format::LT_ImportStatement
Definition: TokenAnnotator.h:29
clang::format::FormatToken
A wrapper around a Token storing information about the whitespace characters preceding it...
Definition: FormatToken.h:124
clang::format::LT_ObjCDecl
Definition: TokenAnnotator.h:30
clang::format::AnnotatedLine
Definition: TokenAnnotator.h:38
clang::format::TokenAnnotator::TokenAnnotator
TokenAnnotator(const FormatStyle &Style, const AdditionalKeywords &Keywords)
Definition: TokenAnnotator.h:152
clang::format::LT_Other
Definition: TokenAnnotator.h:33
clang::format::AnnotatedLine::Level
unsigned Level
Definition: TokenAnnotator.h:121
clang::format::AnnotatedLine::Affected
bool Affected
True if this line should be formatted, i.e.
Definition: TokenAnnotator.h:131
clang::format::LT_Invalid
Definition: TokenAnnotator.h:28
false
#define false
Definition: stdbool.h:33
Format.h
Various functions to configurably format source code.
clang::format::AdditionalKeywords
Encapsulates keywords that are context sensitive or for languages not properly supported by Clang&#39;s l...
Definition: FormatToken.h:668
clang::format::AnnotatedLine::First
FormatToken * First
Definition: TokenAnnotator.h:115
UnwrappedLineParser.h
This file contains the declaration of the UnwrappedLineParser, which turns a stream of tokens into Un...
llvm::SmallVector
Definition: LLVM.h:36
clang::format::AnnotatedLine::startsWith
bool startsWith(Ts... Tokens) const
true if this line starts with the given tokens in order, ignoring comments.
Definition: TokenAnnotator.h:87
clang::format::LT_VirtualFunctionDecl
Definition: TokenAnnotator.h:35
clang::format::FormatStyle
The FormatStyle is used to configure the formatting to follow specific guidelines.
Definition: Format.h:50
clang::format::AnnotatedLine::IsMultiVariableDeclStmt
bool IsMultiVariableDeclStmt
Definition: TokenAnnotator.h:127
clang::format::UnwrappedLineNode
Definition: UnwrappedLineParser.h:283
Node
ast_type_traits::DynTypedNode Node
Definition: ASTMatchFinder.cpp:61
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::format::AnnotatedLine::AnnotatedLine
AnnotatedLine(const UnwrappedLine &Line)
Definition: TokenAnnotator.h:40
clang::format::LT_ObjCProperty
Definition: TokenAnnotator.h:32
clang::format::AnnotatedLine::MightBeFunctionDecl
bool MightBeFunctionDecl
Definition: TokenAnnotator.h:126
clang::format::AnnotatedLine::startsWithNamespace
bool startsWithNamespace() const
true if this line starts a namespace definition.
Definition: TokenAnnotator.h:109
clang::format::FormatToken::Children
SmallVector< AnnotatedLine *, 1 > Children
If this token starts a block, this contains all the unwrapped lines in it.
Definition: FormatToken.h:297
clang::format::FormatToken::startsSequence
bool startsSequence(A K1, Ts... Tokens) const
true if this token starts a sequence with the given tokens in order, following the Next pointers...
Definition: FormatToken.h:336
clang::format::LT_PreprocessorDirective
Definition: TokenAnnotator.h:34
clang::format::Style
const FormatStyle & Style
Definition: BreakableToken.h:247