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"
33 LT_PreprocessorDirective,
34 LT_VirtualFunctionDecl,
35 LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
37 LT_ObjCProperty // An @property line.
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();
53 (*I)->Previous = Current;
54 Current = Current->Next;
65 bool MustBeDeclaration;
66 bool MightBeFunctionDecl;
67 bool StartsDefinition;
70 /// \brief Determines extra information about the tokens comprising an
72 class TokenAnnotator {
74 TokenAnnotator(const FormatStyle &Style, SourceManager &SourceMgr, Lexer &Lex,
75 IdentifierInfo &Ident_in)
76 : Style(Style), SourceMgr(SourceMgr), Lex(Lex), Ident_in(Ident_in) {
79 void annotate(AnnotatedLine &Line);
80 void calculateFormattingInformation(AnnotatedLine &Line);
83 /// \brief Calculate the penalty for splitting before \c Tok.
84 unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok);
86 bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
87 const FormatToken &Right);
89 bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
91 bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
93 void printDebugInfo(const AnnotatedLine &Line);
95 void calculateUnbreakableTailLengths(AnnotatedLine &Line);
97 const FormatStyle &Style;
98 SourceManager &SourceMgr;
101 // Contextual keywords:
102 IdentifierInfo &Ident_in;
105 } // end namespace format
106 } // end namespace clang
108 #endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H