]> granicus.if.org Git - clang/blob - lib/Format/TokenAnnotator.h
clang-format: Enable formatting of nested blocks.
[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 SourceManager;
25
26 namespace format {
27
28 enum 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
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         StartsDefinition(false) {
45     assert(!Line.Tokens.empty());
46     FormatToken *Current = First;
47     for (std::list<UnwrappedLineNode>::const_iterator I = ++Line.Tokens.begin(),
48                                                       E = Line.Tokens.end();
49          I != E; ++I) {
50       const UnwrappedLineNode &Node = *I;
51       Current->Next = I->Tok;
52       I->Tok->Previous = Current;
53       Current = Current->Next;
54       for (SmallVectorImpl<UnwrappedLine>::const_iterator
55                I = Node.Children.begin(),
56                E = Node.Children.end();
57            I != E; ++I) {
58         Children.push_back(new AnnotatedLine(*I));
59         Current->Children.push_back(Children.back());
60       }
61     }
62     Last = Current;
63   }
64
65   ~AnnotatedLine() {
66     for (unsigned i = 0, e = Children.size(); i != e; ++i) {
67       delete Children[i];
68     }
69     Children.clear();
70   }
71
72   FormatToken *First;
73   FormatToken *Last;
74
75   std::vector<AnnotatedLine *> Children;
76
77   LineType Type;
78   unsigned Level;
79   bool InPPDirective;
80   bool MustBeDeclaration;
81   bool MightBeFunctionDecl;
82   bool StartsDefinition;
83
84 private:
85   // Disallow copying.
86   AnnotatedLine(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
87   void operator=(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
88 };
89
90 /// \brief Determines extra information about the tokens comprising an
91 /// \c UnwrappedLine.
92 class TokenAnnotator {
93 public:
94   TokenAnnotator(const FormatStyle &Style, IdentifierInfo &Ident_in)
95       : Style(Style), Ident_in(Ident_in) {}
96
97   void annotate(AnnotatedLine &Line);
98   void calculateFormattingInformation(AnnotatedLine &Line);
99
100 private:
101   /// \brief Calculate the penalty for splitting before \c Tok.
102   unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok);
103
104   bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
105                             const FormatToken &Right);
106
107   bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
108
109   bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
110
111   void printDebugInfo(const AnnotatedLine &Line);
112
113   void calculateUnbreakableTailLengths(AnnotatedLine &Line);
114
115   const FormatStyle &Style;
116
117   // Contextual keywords:
118   IdentifierInfo &Ident_in;
119 };
120
121 } // end namespace format
122 } // end namespace clang
123
124 #endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H