1 //===--- TokenAnnotator.h - Format C++ code ---------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief This file implements a token annotator, i.e. creates
12 /// \c AnnotatedTokens out of \c FormatTokens with required extra information.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H
17 #define LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H
19 #include "UnwrappedLineParser.h"
20 #include "clang/Format/Format.h"
31 LT_PreprocessorDirective,
32 LT_VirtualFunctionDecl,
33 LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
35 LT_ObjCProperty // An @property line.
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();
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();
58 Children.push_back(new AnnotatedLine(*I));
59 Current->Children.push_back(Children.back());
66 for (unsigned i = 0, e = Children.size(); i != e; ++i) {
74 std::vector<AnnotatedLine *> Children;
79 bool MustBeDeclaration;
80 bool MightBeFunctionDecl;
81 bool StartsDefinition;
85 AnnotatedLine(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
86 void operator=(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
89 /// \brief Determines extra information about the tokens comprising an
91 class TokenAnnotator {
93 TokenAnnotator(const FormatStyle &Style, IdentifierInfo &Ident_in)
94 : Style(Style), Ident_in(Ident_in) {}
96 void annotate(AnnotatedLine &Line);
97 void calculateFormattingInformation(AnnotatedLine &Line);
100 /// \brief Calculate the penalty for splitting before \c Tok.
101 unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok);
103 bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
104 const FormatToken &Right);
106 bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
108 bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
110 void printDebugInfo(const AnnotatedLine &Line);
112 void calculateUnbreakableTailLengths(AnnotatedLine &Line);
114 const FormatStyle &Style;
116 // Contextual keywords:
117 IdentifierInfo &Ident_in;
120 } // end namespace format
121 } // end namespace clang
123 #endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H