]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Format/TokenAnnotator.h
Merge bmake-20170301
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Format / TokenAnnotator.h
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
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         InPPDirective(Line.InPPDirective),
43         MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false),
44         IsMultiVariableDeclStmt(false), Affected(false),
45         LeadingEmptyLinesAffected(false), 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 (const auto &Child : Node.Children) {
62         Children.push_back(new AnnotatedLine(Child));
63         Current->Children.push_back(Children.back());
64       }
65     }
66     Last = Current;
67     Last->Next = nullptr;
68   }
69
70   ~AnnotatedLine() {
71     for (unsigned i = 0, e = Children.size(); i != e; ++i) {
72       delete Children[i];
73     }
74     FormatToken *Current = First;
75     while (Current) {
76       Current->Children.clear();
77       Current->Role.reset();
78       Current = Current->Next;
79     }
80   }
81
82   /// \c true if this line starts with the given tokens in order, ignoring
83   /// comments.
84   template <typename... Ts> bool startsWith(Ts... Tokens) const {
85     return First && First->startsSequence(Tokens...);
86   }
87
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...);
94   }
95
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
101     // and tok::lbrace.
102     return !Last->isOneOf(tok::semi, tok::comment);
103   }
104
105   FormatToken *First;
106   FormatToken *Last;
107
108   SmallVector<AnnotatedLine *, 0> Children;
109
110   LineType Type;
111   unsigned Level;
112   bool InPPDirective;
113   bool MustBeDeclaration;
114   bool MightBeFunctionDecl;
115   bool IsMultiVariableDeclStmt;
116
117   /// \c True if this line should be formatted, i.e. intersects directly or
118   /// indirectly with one of the input ranges.
119   bool Affected;
120
121   /// \c True if the leading empty lines of this line intersect with one of the
122   /// input ranges.
123   bool LeadingEmptyLinesAffected;
124
125   /// \c True if a one of this line's children intersects with an input range.
126   bool ChildrenAffected;
127
128 private:
129   // Disallow copying.
130   AnnotatedLine(const AnnotatedLine &) = delete;
131   void operator=(const AnnotatedLine &) = delete;
132 };
133
134 /// \brief Determines extra information about the tokens comprising an
135 /// \c UnwrappedLine.
136 class TokenAnnotator {
137 public:
138   TokenAnnotator(const FormatStyle &Style, const AdditionalKeywords &Keywords)
139       : Style(Style), Keywords(Keywords) {}
140
141   /// \brief Adapts the indent levels of comment lines to the indent of the
142   /// subsequent line.
143   // FIXME: Can/should this be done in the UnwrappedLineParser?
144   void setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines);
145
146   void annotate(AnnotatedLine &Line);
147   void calculateFormattingInformation(AnnotatedLine &Line);
148
149 private:
150   /// \brief Calculate the penalty for splitting before \c Tok.
151   unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok,
152                         bool InFunctionDecl);
153
154   bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
155                             const FormatToken &Right);
156
157   bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
158
159   bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
160
161   bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
162
163   bool mustBreakForReturnType(const AnnotatedLine &Line) const;
164
165   void printDebugInfo(const AnnotatedLine &Line);
166
167   void calculateUnbreakableTailLengths(AnnotatedLine &Line);
168
169   const FormatStyle &Style;
170
171   const AdditionalKeywords &Keywords;
172 };
173
174 } // end namespace format
175 } // end namespace clang
176
177 #endif