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), Affected(false),
45 LeadingEmptyLinesAffected(false) {
46 assert(!Line.Tokens.empty());
48 // Calculate Next and Previous for all tokens. Note that we must overwrite
49 // Next and Previous for every token, as previous formatting runs might have
50 // left them in a different state.
51 First->Previous = NULL;
52 FormatToken *Current = First;
53 for (std::list<UnwrappedLineNode>::const_iterator I = ++Line.Tokens.begin(),
54 E = Line.Tokens.end();
56 const UnwrappedLineNode &Node = *I;
57 Current->Next = I->Tok;
58 I->Tok->Previous = Current;
59 Current = Current->Next;
60 Current->Children.clear();
61 for (SmallVectorImpl<UnwrappedLine>::const_iterator
62 I = Node.Children.begin(),
63 E = Node.Children.end();
65 Children.push_back(new AnnotatedLine(*I));
66 Current->Children.push_back(Children.back());
74 for (unsigned i = 0, e = Children.size(); i != e; ++i) {
82 SmallVector<AnnotatedLine *, 0> Children;
87 bool MustBeDeclaration;
88 bool MightBeFunctionDecl;
89 bool StartsDefinition;
91 /// \c True if this line should be formatted, i.e. intersects directly or
92 /// indirectly with one of the input ranges.
95 /// \c True if the leading empty lines of this line intersect with one of the
97 bool LeadingEmptyLinesAffected;
101 AnnotatedLine(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
102 void operator=(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
105 /// \brief Determines extra information about the tokens comprising an
106 /// \c UnwrappedLine.
107 class TokenAnnotator {
109 TokenAnnotator(const FormatStyle &Style, IdentifierInfo &Ident_in)
110 : Style(Style), Ident_in(Ident_in) {}
112 /// \brief Adapts the indent levels of comment lines to the indent of the
114 // FIXME: Can/should this be done in the UnwrappedLineParser?
115 void setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines);
117 void annotate(AnnotatedLine &Line);
118 void calculateFormattingInformation(AnnotatedLine &Line);
121 /// \brief Calculate the penalty for splitting before \c Tok.
122 unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok,
123 bool InFunctionDecl);
125 bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
126 const FormatToken &Right);
128 bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
130 bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
132 bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
134 void printDebugInfo(const AnnotatedLine &Line);
136 void calculateUnbreakableTailLengths(AnnotatedLine &Line);
138 const FormatStyle &Style;
140 // Contextual keywords:
141 IdentifierInfo &Ident_in;
144 } // end namespace format
145 } // end namespace clang
147 #endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H