clang  3.7.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 /// \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 #include <string>
22 
23 namespace clang {
24 class SourceManager;
25 
26 namespace format {
27 
28 enum LineType {
31  LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
33  LT_ObjCProperty, // An @property line.
37 };
38 
40 public:
42  : First(Line.Tokens.front().Tok), Level(Line.Level),
47  assert(!Line.Tokens.empty());
48 
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;
54  for (std::list<UnwrappedLineNode>::const_iterator I = ++Line.Tokens.begin(),
55  E = Line.Tokens.end();
56  I != E; ++I) {
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());
65  }
66  }
67  Last = Current;
68  Last->Next = nullptr;
69  }
70 
72  for (unsigned i = 0, e = Children.size(); i != e; ++i) {
73  delete Children[i];
74  }
76  while (Current) {
77  Current->Children.clear();
78  Current->Role.reset();
79  Current = Current->Next;
80  }
81  }
82 
83  /// \c true if this line starts with the given tokens in order, ignoring
84  /// comments.
85  template <typename... Ts> bool startsWith(Ts... Tokens) const {
86  return startsWith(First, Tokens...);
87  }
88 
91 
93 
95  unsigned Level;
100 
101  /// \c True if this line should be formatted, i.e. intersects directly or
102  /// indirectly with one of the input ranges.
103  bool Affected;
104 
105  /// \c True if the leading empty lines of this line intersect with one of the
106  /// input ranges.
108 
109  /// \c True if a one of this line's children intersects with an input range.
111 
112 private:
113  // Disallow copying.
114  AnnotatedLine(const AnnotatedLine &) = delete;
115  void operator=(const AnnotatedLine &) = delete;
116 
117  template <typename A, typename... Ts>
118  bool startsWith(FormatToken *Tok, A K1) const {
119  while (Tok && Tok->is(tok::comment))
120  Tok = Tok->Next;
121  return Tok && Tok->is(K1);
122  }
123 
124  template <typename A, typename... Ts>
125  bool startsWith(FormatToken *Tok, A K1, Ts... Tokens) const {
126  return startsWith(Tok, K1) && startsWith(Tok->Next, Tokens...);
127  }
128 };
129 
130 /// \brief Determines extra information about the tokens comprising an
131 /// \c UnwrappedLine.
133 public:
134  TokenAnnotator(const FormatStyle &Style, const AdditionalKeywords &Keywords)
135  : Style(Style), Keywords(Keywords) {}
136 
137  /// \brief Adapts the indent levels of comment lines to the indent of the
138  /// subsequent line.
139  // FIXME: Can/should this be done in the UnwrappedLineParser?
141 
142  void annotate(AnnotatedLine &Line);
144 
145 private:
146  /// \brief Calculate the penalty for splitting before \c Tok.
147  unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok,
148  bool InFunctionDecl);
149 
150  bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
151  const FormatToken &Right);
152 
153  bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
154 
155  bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
156 
157  bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
158 
159  void printDebugInfo(const AnnotatedLine &Line);
160 
161  void calculateUnbreakableTailLengths(AnnotatedLine &Line);
162 
163  const FormatStyle &Style;
164 
165  const AdditionalKeywords &Keywords;
166 };
167 
168 } // end namespace format
169 } // end namespace clang
170 
171 #endif
SmallVector< UnwrappedLine, 0 > Children
Token Tok
The Token.
Definition: FormatToken.h:116
std::unique_ptr< TokenRole > Role
A token can have a special role that can carry extra information about the token's formatting...
Definition: FormatToken.h:196
void setCommentLineLevels(SmallVectorImpl< AnnotatedLine * > &Lines)
Adapts the indent levels of comment lines to the indent of the subsequent line.
FormatToken * Next
The next token in the unwrapped line.
Definition: FormatToken.h:267
FormatToken * Previous
The previous token in the unwrapped line.
Definition: FormatToken.h:264
bool ChildrenAffected
True if a one of this line's children intersects with an input range.
An unwrapped line is a sequence of Token, that we would like to put on a single line if there was no ...
Determines extra information about the tokens comprising an UnwrappedLine.
std::list< UnwrappedLineNode > Tokens
The Tokens comprising this UnwrappedLine.
SmallVector< AnnotatedLine *, 0 > Children
A wrapper around a Token storing information about the whitespace characters preceding it...
Definition: FormatToken.h:112
TokenAnnotator(const FormatStyle &Style, const AdditionalKeywords &Keywords)
void annotate(AnnotatedLine &Line)
bool startsWith(Ts...Tokens) const
#define false
Definition: stdbool.h:33
AnnotatedLine & Line
Encapsulates keywords that are context sensitive or for languages not properly supported by Clang's l...
Definition: FormatToken.h:522
This file contains the declaration of the UnwrappedLineParser, which turns a stream of tokens into Un...
SmallVector< FormatToken *, 16 > Tokens
Definition: Format.cpp:1214
The FormatStyle is used to configure the formatting to follow specific guidelines.
Definition: Format.h:42
ast_type_traits::DynTypedNode Node
AnnotatedLine(const UnwrappedLine &Line)
void calculateFormattingInformation(AnnotatedLine &Line)
bool is(tok::TokenKind Kind) const
Definition: FormatToken.h:281
SmallVector< AnnotatedLine *, 1 > Children
If this token starts a block, this contains all the unwrapped lines in it.
Definition: FormatToken.h:271
FormatToken * Current