blob: 97daaf44ba99e3c97c682afc66f8af9dca35360e [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
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000016#ifndef LLVM_CLANG_LIB_FORMAT_TOKENANNOTATOR_H
17#define LLVM_CLANG_LIB_FORMAT_TOKENANNOTATOR_H
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000018
19#include "UnwrappedLineParser.h"
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000020#include "clang/Format/Format.h"
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000021
22namespace clang {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000023class SourceManager;
24
25namespace format {
26
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000027enum LineType {
28 LT_Invalid,
Daniel Jasper616de8642014-11-23 16:46:28 +000029 LT_ImportStatement,
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000030 LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
31 LT_ObjCMethodDecl,
Daniel Jasper616de8642014-11-23 16:46:28 +000032 LT_ObjCProperty, // An @property line.
33 LT_Other,
34 LT_PreprocessorDirective,
35 LT_VirtualFunctionDecl
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000036};
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 Jasperbea1ab42015-03-01 18:55:26 +000044 IsMultiVariableDeclStmt(false), Affected(false),
45 LeadingEmptyLinesAffected(false), 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 Jaspere285b8d2015-06-17 09:43:56 +000061 for (const auto &Child : Node.Children) {
Daniel Jasperfd725c02015-01-21 17:35:29 +000062 Children.push_back(new AnnotatedLine(Child));
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000063 Current->Children.push_back(Children.back());
64 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000065 }
66 Last = Current;
Craig Topper2145bc02014-05-09 08:15:10 +000067 Last->Next = nullptr;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000068 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000069
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000070 ~AnnotatedLine() {
71 for (unsigned i = 0, e = Children.size(); i != e; ++i) {
72 delete Children[i];
73 }
Daniel Jasperfd725c02015-01-21 17:35:29 +000074 FormatToken *Current = First;
75 while (Current) {
76 Current->Children.clear();
Daniel Jasper04b979d2015-01-21 18:35:47 +000077 Current->Role.reset();
Daniel Jasperfd725c02015-01-21 17:35:29 +000078 Current = Current->Next;
79 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000080 }
81
Daniel Jaspere285b8d2015-06-17 09:43:56 +000082 /// \c true if this line starts with the given tokens in order, ignoring
83 /// comments.
84 template <typename... Ts> bool startsWith(Ts... Tokens) const {
Martin Probst1244ecb2016-05-29 14:41:02 +000085 return First && First->startsSequence(Tokens...);
Daniel Jaspere285b8d2015-06-17 09:43:56 +000086 }
87
Eric Liu4cfb88a2016-04-25 15:09:22 +000088 /// \c true if this line ends with the given tokens in reversed order,
89 /// ignoring comments.
90 /// For example, given tokens [T1, T2, T3, ...], the function returns true if
91 /// this line is like "... T3 T2 T1".
92 template <typename... Ts> bool endsWith(Ts... Tokens) const {
Martin Probst1244ecb2016-05-29 14:41:02 +000093 return Last && Last->endsSequence(Tokens...);
Eric Liu4cfb88a2016-04-25 15:09:22 +000094 }
95
Zachary Turner448592e2015-12-18 22:20:15 +000096 /// \c true if this line looks like a function definition instead of a
97 /// function declaration. Asserts MightBeFunctionDecl.
98 bool mightBeFunctionDefinition() const {
99 assert(MightBeFunctionDecl);
100 // FIXME: Line.Last points to other characters than tok::semi
101 // and tok::lbrace.
102 return !Last->isOneOf(tok::semi, tok::comment);
103 }
104
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000105 FormatToken *First;
106 FormatToken *Last;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000107
Daniel Jasper1c5d9df2013-09-06 07:54:20 +0000108 SmallVector<AnnotatedLine *, 0> Children;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000109
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000110 LineType Type;
111 unsigned Level;
112 bool InPPDirective;
113 bool MustBeDeclaration;
Daniel Jasper26d1b1d2013-02-24 18:54:32 +0000114 bool MightBeFunctionDecl;
Daniel Jasperbea1ab42015-03-01 18:55:26 +0000115 bool IsMultiVariableDeclStmt;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000116
Daniel Jasper5500f612013-11-25 11:08:59 +0000117 /// \c True if this line should be formatted, i.e. intersects directly or
118 /// indirectly with one of the input ranges.
119 bool Affected;
120
121 /// \c True if the leading empty lines of this line intersect with one of the
122 /// input ranges.
123 bool LeadingEmptyLinesAffected;
124
Daniel Jasper9c199562013-11-28 15:58:55 +0000125 /// \c True if a one of this line's children intersects with an input range.
126 bool ChildrenAffected;
127
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000128private:
129 // Disallow copying.
Aaron Ballmanabc18922015-02-15 22:54:08 +0000130 AnnotatedLine(const AnnotatedLine &) = delete;
131 void operator=(const AnnotatedLine &) = delete;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000132};
133
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000134/// \brief Determines extra information about the tokens comprising an
135/// \c UnwrappedLine.
136class TokenAnnotator {
137public:
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000138 TokenAnnotator(const FormatStyle &Style, const AdditionalKeywords &Keywords)
139 : Style(Style), Keywords(Keywords) {}
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000140
Daniel Jasper1c5d9df2013-09-06 07:54:20 +0000141 /// \brief Adapts the indent levels of comment lines to the indent of the
142 /// subsequent line.
143 // FIXME: Can/should this be done in the UnwrappedLineParser?
144 void setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines);
145
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000146 void annotate(AnnotatedLine &Line);
147 void calculateFormattingInformation(AnnotatedLine &Line);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000148
149private:
150 /// \brief Calculate the penalty for splitting before \c Tok.
Daniel Jasper4fcc8b92013-11-07 17:52:51 +0000151 unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok,
152 bool InFunctionDecl);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000153
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000154 bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
155 const FormatToken &Right);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000156
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000157 bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000158
Daniel Jasperfb81b092013-09-17 09:52:48 +0000159 bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
160
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000161 bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000162
Zachary Turner448592e2015-12-18 22:20:15 +0000163 bool mustBreakForReturnType(const AnnotatedLine &Line) const;
164
Daniel Jasper6bee6822013-04-08 20:33:42 +0000165 void printDebugInfo(const AnnotatedLine &Line);
166
Manuel Klimek4fe43002013-05-22 12:51:29 +0000167 void calculateUnbreakableTailLengths(AnnotatedLine &Line);
168
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000169 const FormatStyle &Style;
Nico Weber29f9dea2013-02-11 15:32:15 +0000170
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000171 const AdditionalKeywords &Keywords;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000172};
173
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000174} // end namespace format
175} // end namespace clang
176
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000177#endif