TokenAnnotator.h revision 263508
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_FORMAT_TOKEN_ANNOTATOR_H
17#define LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H
18
19#include "UnwrappedLineParser.h"
20#include "clang/Format/Format.h"
21#include <string>
22
23namespace clang {
24class SourceManager;
25
26namespace format {
27
28enum LineType {
29  LT_Invalid,
30  LT_Other,
31  LT_PreprocessorDirective,
32  LT_VirtualFunctionDecl,
33  LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
34  LT_ObjCMethodDecl,
35  LT_ObjCProperty // An @property line.
36};
37
38class AnnotatedLine {
39public:
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        StartsDefinition(false) {
45    assert(!Line.Tokens.empty());
46
47    // Calculate Next and Previous for all tokens. Note that we must overwrite
48    // Next and Previous for every token, as previous formatting runs might have
49    // left them in a different state.
50    First->Previous = NULL;
51    FormatToken *Current = First;
52    for (std::list<UnwrappedLineNode>::const_iterator I = ++Line.Tokens.begin(),
53                                                      E = Line.Tokens.end();
54         I != E; ++I) {
55      const UnwrappedLineNode &Node = *I;
56      Current->Next = I->Tok;
57      I->Tok->Previous = Current;
58      Current = Current->Next;
59      Current->Children.clear();
60      for (SmallVectorImpl<UnwrappedLine>::const_iterator
61               I = Node.Children.begin(),
62               E = Node.Children.end();
63           I != E; ++I) {
64        Children.push_back(new AnnotatedLine(*I));
65        Current->Children.push_back(Children.back());
66      }
67    }
68    Last = Current;
69    Last->Next = NULL;
70  }
71
72  ~AnnotatedLine() {
73    for (unsigned i = 0, e = Children.size(); i != e; ++i) {
74      delete Children[i];
75    }
76  }
77
78  FormatToken *First;
79  FormatToken *Last;
80
81  SmallVector<AnnotatedLine *, 0> Children;
82
83  LineType Type;
84  unsigned Level;
85  bool InPPDirective;
86  bool MustBeDeclaration;
87  bool MightBeFunctionDecl;
88  bool StartsDefinition;
89
90private:
91  // Disallow copying.
92  AnnotatedLine(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
93  void operator=(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
94};
95
96/// \brief Determines extra information about the tokens comprising an
97/// \c UnwrappedLine.
98class TokenAnnotator {
99public:
100  TokenAnnotator(const FormatStyle &Style, IdentifierInfo &Ident_in)
101      : Style(Style), Ident_in(Ident_in) {}
102
103  /// \brief Adapts the indent levels of comment lines to the indent of the
104  /// subsequent line.
105  // FIXME: Can/should this be done in the UnwrappedLineParser?
106  void setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines);
107
108  void annotate(AnnotatedLine &Line);
109  void calculateFormattingInformation(AnnotatedLine &Line);
110
111private:
112  /// \brief Calculate the penalty for splitting before \c Tok.
113  unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok,
114                        bool InFunctionDecl);
115
116  bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
117                            const FormatToken &Right);
118
119  bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
120
121  bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
122
123  bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
124
125  void printDebugInfo(const AnnotatedLine &Line);
126
127  void calculateUnbreakableTailLengths(AnnotatedLine &Line);
128
129  const FormatStyle &Style;
130
131  // Contextual keywords:
132  IdentifierInfo &Ident_in;
133};
134
135} // end namespace format
136} // end namespace clang
137
138#endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H
139