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());
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();
55 const UnwrappedLineNode &Node = *I;
56 Current->Next = I->Tok;
57 I->Tok->Previous = Current;
58 Current = Current->Next;
59 for (SmallVectorImpl<UnwrappedLine>::const_iterator
60 I = Node.Children.begin(),
61 E = Node.Children.end();
63 Children.push_back(new AnnotatedLine(*I));
64 Current->Children.push_back(Children.back());
72 for (unsigned i = 0, e = Children.size(); i != e; ++i) {
80 SmallVector<AnnotatedLine *, 0> Children;
85 bool MustBeDeclaration;
86 bool MightBeFunctionDecl;
87 bool StartsDefinition;
91 AnnotatedLine(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
92 void operator=(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
95 /// \brief Determines extra information about the tokens comprising an
97 class TokenAnnotator {
99 TokenAnnotator(const FormatStyle &Style, IdentifierInfo &Ident_in)
100 : Style(Style), Ident_in(Ident_in) {}
102 /// \brief Adapts the indent levels of comment lines to the indent of the
104 // FIXME: Can/should this be done in the UnwrappedLineParser?
105 void setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines);
107 void annotate(AnnotatedLine &Line);
108 void calculateFormattingInformation(AnnotatedLine &Line);
111 /// \brief Calculate the penalty for splitting before \c Tok.
112 unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok);
114 bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
115 const FormatToken &Right);
117 bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
119 bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
121 bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
123 void printDebugInfo(const AnnotatedLine &Line);
125 void calculateUnbreakableTailLengths(AnnotatedLine &Line);
127 const FormatStyle &Style;
129 // Contextual keywords:
130 IdentifierInfo &Ident_in;
133 } // end namespace format
134 } // end namespace clang
136 #endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H