blob: 8e8d076819edaa68b77ee8245f7dd44951f1cc50 [file] [log] [blame]
Daniel Jasper32d28ee2013-01-29 21:01:14 +00001//===--- 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"
Daniel Jasper32d28ee2013-01-29 21:01:14 +000020#include "clang/Format/Format.h"
21#include <string>
22
23namespace clang {
Daniel Jasper32d28ee2013-01-29 21:01:14 +000024class SourceManager;
25
26namespace format {
27
Daniel Jasper32d28ee2013-01-29 21:01:14 +000028enum LineType {
29 LT_Invalid,
30 LT_Other,
Daniel Jasper32d28ee2013-01-29 21:01:14 +000031 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
Daniel Jasper32d28ee2013-01-29 21:01:14 +000038class AnnotatedLine {
39public:
40 AnnotatedLine(const UnwrappedLine &Line)
Daniel Jasper567dcf92013-09-05 09:29:45 +000041 : First(Line.Tokens.front().Tok), Level(Line.Level),
Daniel Jasper32d28ee2013-01-29 21:01:14 +000042 InPPDirective(Line.InPPDirective),
Daniel Jasper53e72cd2013-05-06 08:27:33 +000043 MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false),
44 StartsDefinition(false) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +000045 assert(!Line.Tokens.empty());
Manuel Klimekb3987012013-05-29 14:47:47 +000046 FormatToken *Current = First;
Daniel Jasper567dcf92013-09-05 09:29:45 +000047 for (std::list<UnwrappedLineNode>::const_iterator I = ++Line.Tokens.begin(),
48 E = Line.Tokens.end();
Daniel Jasper32d28ee2013-01-29 21:01:14 +000049 I != E; ++I) {
Daniel Jasper567dcf92013-09-05 09:29:45 +000050 const UnwrappedLineNode &Node = *I;
51 Current->Next = I->Tok;
52 I->Tok->Previous = Current;
Manuel Klimekb3987012013-05-29 14:47:47 +000053 Current = Current->Next;
Daniel Jasper567dcf92013-09-05 09:29:45 +000054 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 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +000061 }
62 Last = Current;
63 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +000064
Daniel Jasper567dcf92013-09-05 09:29:45 +000065 ~AnnotatedLine() {
66 for (unsigned i = 0, e = Children.size(); i != e; ++i) {
67 delete Children[i];
68 }
69 Children.clear();
70 }
71
Manuel Klimekb3987012013-05-29 14:47:47 +000072 FormatToken *First;
73 FormatToken *Last;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000074
Daniel Jasper567dcf92013-09-05 09:29:45 +000075 std::vector<AnnotatedLine *> Children;
76
Daniel Jasper32d28ee2013-01-29 21:01:14 +000077 LineType Type;
78 unsigned Level;
79 bool InPPDirective;
80 bool MustBeDeclaration;
Daniel Jasper3c08a812013-02-24 18:54:32 +000081 bool MightBeFunctionDecl;
Daniel Jasper53e72cd2013-05-06 08:27:33 +000082 bool StartsDefinition;
Daniel Jasper567dcf92013-09-05 09:29:45 +000083
84private:
85 // Disallow copying.
86 AnnotatedLine(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
87 void operator=(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000088};
89
Daniel Jasper32d28ee2013-01-29 21:01:14 +000090/// \brief Determines extra information about the tokens comprising an
91/// \c UnwrappedLine.
92class TokenAnnotator {
93public:
Alexander Kornienko00895102013-06-05 14:09:10 +000094 TokenAnnotator(const FormatStyle &Style, IdentifierInfo &Ident_in)
95 : Style(Style), Ident_in(Ident_in) {}
Daniel Jasper32d28ee2013-01-29 21:01:14 +000096
Daniel Jasper8ff690a2013-02-06 14:22:40 +000097 void annotate(AnnotatedLine &Line);
98 void calculateFormattingInformation(AnnotatedLine &Line);
Daniel Jasper32d28ee2013-01-29 21:01:14 +000099
100private:
101 /// \brief Calculate the penalty for splitting before \c Tok.
Manuel Klimekb3987012013-05-29 14:47:47 +0000102 unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000103
Manuel Klimekb3987012013-05-29 14:47:47 +0000104 bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
105 const FormatToken &Right);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000106
Manuel Klimekb3987012013-05-29 14:47:47 +0000107 bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000108
Manuel Klimekb3987012013-05-29 14:47:47 +0000109 bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000110
Daniel Jasperbf71ba22013-04-08 20:33:42 +0000111 void printDebugInfo(const AnnotatedLine &Line);
112
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000113 void calculateUnbreakableTailLengths(AnnotatedLine &Line);
114
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000115 const FormatStyle &Style;
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000116
117 // Contextual keywords:
118 IdentifierInfo &Ident_in;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000119};
120
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000121} // end namespace format
122} // end namespace clang
123
124#endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H