]> granicus.if.org Git - clang/blob - lib/Format/TokenAnnotator.h
The second step in the token refactoring.
[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_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
23 namespace clang {
24 class Lexer;
25 class SourceManager;
26
27 namespace format {
28
29 enum LineType {
30   LT_Invalid,
31   LT_Other,
32   LT_BuilderTypeCall,
33   LT_PreprocessorDirective,
34   LT_VirtualFunctionDecl,
35   LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
36   LT_ObjCMethodDecl,
37   LT_ObjCProperty // An @property line.
38 };
39
40 class AnnotatedLine {
41 public:
42   AnnotatedLine(const UnwrappedLine &Line)
43       : First(Line.Tokens.front()), Level(Line.Level),
44         InPPDirective(Line.InPPDirective),
45         MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false),
46         StartsDefinition(false) {
47     assert(!Line.Tokens.empty());
48     FormatToken *Current = First;
49     for (std::list<FormatToken *>::const_iterator I = ++Line.Tokens.begin(),
50                                                   E = Line.Tokens.end();
51          I != E; ++I) {
52       Current->Next = *I;
53       (*I)->Previous = Current;
54       Current = Current->Next;
55     }
56     Last = Current;
57   }
58
59   FormatToken *First;
60   FormatToken *Last;
61
62   LineType Type;
63   unsigned Level;
64   bool InPPDirective;
65   bool MustBeDeclaration;
66   bool MightBeFunctionDecl;
67   bool StartsDefinition;
68 };
69
70 /// \brief Determines extra information about the tokens comprising an
71 /// \c UnwrappedLine.
72 class TokenAnnotator {
73 public:
74   TokenAnnotator(const FormatStyle &Style, SourceManager &SourceMgr, Lexer &Lex,
75                  IdentifierInfo &Ident_in)
76       : Style(Style), SourceMgr(SourceMgr), Lex(Lex), Ident_in(Ident_in) {
77   }
78
79   void annotate(AnnotatedLine &Line);
80   void calculateFormattingInformation(AnnotatedLine &Line);
81
82 private:
83   /// \brief Calculate the penalty for splitting before \c Tok.
84   unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok);
85
86   bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
87                             const FormatToken &Right);
88
89   bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
90
91   bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
92
93   void printDebugInfo(const AnnotatedLine &Line);
94
95   void calculateUnbreakableTailLengths(AnnotatedLine &Line);
96
97   const FormatStyle &Style;
98   SourceManager &SourceMgr;
99   Lexer &Lex;
100
101   // Contextual keywords:
102   IdentifierInfo &Ident_in;
103 };
104
105 } // end namespace format
106 } // end namespace clang
107
108 #endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H