blob: 36de010fc9402336c44402f5806de2a4412883ed [file] [log] [blame]
Daniel Jasper7a6d09b2013-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 Jasper7a6d09b2013-01-29 21:01:14 +000020#include "clang/Format/Format.h"
21#include <string>
22
23namespace clang {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000024class SourceManager;
25
26namespace format {
27
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000028enum LineType {
29 LT_Invalid,
30 LT_Other,
Daniel Jasper7a6d09b2013-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 Jasper7a6d09b2013-01-29 21:01:14 +000038class AnnotatedLine {
39public:
40 AnnotatedLine(const UnwrappedLine &Line)
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000041 : First(Line.Tokens.front().Tok), Level(Line.Level),
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000042 InPPDirective(Line.InPPDirective),
Daniel Jasper8e357692013-05-06 08:27:33 +000043 MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false),
Daniel Jasper4355e7f2014-07-09 07:50:33 +000044 Affected(false), LeadingEmptyLinesAffected(false),
45 ChildrenAffected(false) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000046 assert(!Line.Tokens.empty());
Manuel Klimek88033d72013-10-21 08:11:15 +000047
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.
Craig Topper2145bc02014-05-09 08:15:10 +000051 First->Previous = nullptr;
Manuel Klimek6e6310e2013-05-29 14:47:47 +000052 FormatToken *Current = First;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000053 for (std::list<UnwrappedLineNode>::const_iterator I = ++Line.Tokens.begin(),
54 E = Line.Tokens.end();
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000055 I != E; ++I) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000056 const UnwrappedLineNode &Node = *I;
57 Current->Next = I->Tok;
58 I->Tok->Previous = Current;
Manuel Klimek6e6310e2013-05-29 14:47:47 +000059 Current = Current->Next;
Daniel Jasper472da862013-10-24 15:23:11 +000060 Current->Children.clear();
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000061 for (SmallVectorImpl<UnwrappedLine>::const_iterator
62 I = Node.Children.begin(),
63 E = Node.Children.end();
64 I != E; ++I) {
65 Children.push_back(new AnnotatedLine(*I));
66 Current->Children.push_back(Children.back());
67 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000068 }
69 Last = Current;
Craig Topper2145bc02014-05-09 08:15:10 +000070 Last->Next = nullptr;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000071 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000072
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000073 ~AnnotatedLine() {
74 for (unsigned i = 0, e = Children.size(); i != e; ++i) {
75 delete Children[i];
76 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000077 }
78
Manuel Klimek6e6310e2013-05-29 14:47:47 +000079 FormatToken *First;
80 FormatToken *Last;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000081
Daniel Jasper1c5d9df2013-09-06 07:54:20 +000082 SmallVector<AnnotatedLine *, 0> Children;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000083
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000084 LineType Type;
85 unsigned Level;
86 bool InPPDirective;
87 bool MustBeDeclaration;
Daniel Jasper26d1b1d2013-02-24 18:54:32 +000088 bool MightBeFunctionDecl;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000089
Daniel Jasper5500f612013-11-25 11:08:59 +000090 /// \c True if this line should be formatted, i.e. intersects directly or
91 /// indirectly with one of the input ranges.
92 bool Affected;
93
94 /// \c True if the leading empty lines of this line intersect with one of the
95 /// input ranges.
96 bool LeadingEmptyLinesAffected;
97
Daniel Jasper9c199562013-11-28 15:58:55 +000098 /// \c True if a one of this line's children intersects with an input range.
99 bool ChildrenAffected;
100
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000101private:
102 // Disallow copying.
103 AnnotatedLine(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
104 void operator=(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000105};
106
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000107/// \brief Determines extra information about the tokens comprising an
108/// \c UnwrappedLine.
109class TokenAnnotator {
110public:
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000111 TokenAnnotator(const FormatStyle &Style, IdentifierInfo &Ident_in)
112 : Style(Style), Ident_in(Ident_in) {}
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000113
Daniel Jasper1c5d9df2013-09-06 07:54:20 +0000114 /// \brief Adapts the indent levels of comment lines to the indent of the
115 /// subsequent line.
116 // FIXME: Can/should this be done in the UnwrappedLineParser?
117 void setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines);
118
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000119 void annotate(AnnotatedLine &Line);
120 void calculateFormattingInformation(AnnotatedLine &Line);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000121
122private:
123 /// \brief Calculate the penalty for splitting before \c Tok.
Daniel Jasper4fcc8b92013-11-07 17:52:51 +0000124 unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok,
125 bool InFunctionDecl);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000126
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000127 bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
128 const FormatToken &Right);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000129
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000130 bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000131
Daniel Jasperfb81b092013-09-17 09:52:48 +0000132 bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
133
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000134 bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000135
Daniel Jasper6bee6822013-04-08 20:33:42 +0000136 void printDebugInfo(const AnnotatedLine &Line);
137
Manuel Klimek4fe43002013-05-22 12:51:29 +0000138 void calculateUnbreakableTailLengths(AnnotatedLine &Line);
139
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000140 const FormatStyle &Style;
Nico Weber29f9dea2013-02-11 15:32:15 +0000141
142 // Contextual keywords:
143 IdentifierInfo &Ident_in;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000144};
145
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000146} // end namespace format
147} // end namespace clang
148
149#endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H