blob: ff8e32a56afc10bccffbd5f105e7fac9952bf2aa [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"
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,
Daniel Jasper616de8642014-11-23 16:46:28 +000030 LT_ImportStatement,
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000031 LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
32 LT_ObjCMethodDecl,
Daniel Jasper616de8642014-11-23 16:46:28 +000033 LT_ObjCProperty, // An @property line.
34 LT_Other,
35 LT_PreprocessorDirective,
36 LT_VirtualFunctionDecl
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000037};
38
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000039class AnnotatedLine {
40public:
41 AnnotatedLine(const UnwrappedLine &Line)
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000042 : First(Line.Tokens.front().Tok), Level(Line.Level),
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000043 InPPDirective(Line.InPPDirective),
Daniel Jasper8e357692013-05-06 08:27:33 +000044 MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false),
Daniel Jasper4355e7f2014-07-09 07:50:33 +000045 Affected(false), LeadingEmptyLinesAffected(false),
46 ChildrenAffected(false) {
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000047 assert(!Line.Tokens.empty());
Manuel Klimek88033d72013-10-21 08:11:15 +000048
49 // Calculate Next and Previous for all tokens. Note that we must overwrite
50 // Next and Previous for every token, as previous formatting runs might have
51 // left them in a different state.
Craig Topper2145bc02014-05-09 08:15:10 +000052 First->Previous = nullptr;
Manuel Klimek6e6310e2013-05-29 14:47:47 +000053 FormatToken *Current = First;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000054 for (std::list<UnwrappedLineNode>::const_iterator I = ++Line.Tokens.begin(),
55 E = Line.Tokens.end();
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000056 I != E; ++I) {
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000057 const UnwrappedLineNode &Node = *I;
58 Current->Next = I->Tok;
59 I->Tok->Previous = Current;
Manuel Klimek6e6310e2013-05-29 14:47:47 +000060 Current = Current->Next;
Daniel Jasper472da862013-10-24 15:23:11 +000061 Current->Children.clear();
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000062 for (SmallVectorImpl<UnwrappedLine>::const_iterator
63 I = Node.Children.begin(),
64 E = Node.Children.end();
65 I != E; ++I) {
66 Children.push_back(new AnnotatedLine(*I));
67 Current->Children.push_back(Children.back());
68 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000069 }
70 Last = Current;
Craig Topper2145bc02014-05-09 08:15:10 +000071 Last->Next = nullptr;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000072 }
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000073
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000074 ~AnnotatedLine() {
75 for (unsigned i = 0, e = Children.size(); i != e; ++i) {
76 delete Children[i];
77 }
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000078 }
79
Manuel Klimek6e6310e2013-05-29 14:47:47 +000080 FormatToken *First;
81 FormatToken *Last;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000082
Daniel Jasper1c5d9df2013-09-06 07:54:20 +000083 SmallVector<AnnotatedLine *, 0> Children;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000084
Daniel Jasper7a6d09b2013-01-29 21:01:14 +000085 LineType Type;
86 unsigned Level;
87 bool InPPDirective;
88 bool MustBeDeclaration;
Daniel Jasper26d1b1d2013-02-24 18:54:32 +000089 bool MightBeFunctionDecl;
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +000090
Daniel Jasper5500f612013-11-25 11:08:59 +000091 /// \c True if this line should be formatted, i.e. intersects directly or
92 /// indirectly with one of the input ranges.
93 bool Affected;
94
95 /// \c True if the leading empty lines of this line intersect with one of the
96 /// input ranges.
97 bool LeadingEmptyLinesAffected;
98
Daniel Jasper9c199562013-11-28 15:58:55 +000099 /// \c True if a one of this line's children intersects with an input range.
100 bool ChildrenAffected;
101
Daniel Jasper9fe0e8d2013-09-05 09:29:45 +0000102private:
103 // Disallow copying.
104 AnnotatedLine(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
105 void operator=(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000106};
107
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000108/// \brief Determines extra information about the tokens comprising an
109/// \c UnwrappedLine.
110class TokenAnnotator {
111public:
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000112 TokenAnnotator(const FormatStyle &Style, const AdditionalKeywords &Keywords)
113 : Style(Style), Keywords(Keywords) {}
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000114
Daniel Jasper1c5d9df2013-09-06 07:54:20 +0000115 /// \brief Adapts the indent levels of comment lines to the indent of the
116 /// subsequent line.
117 // FIXME: Can/should this be done in the UnwrappedLineParser?
118 void setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines);
119
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000120 void annotate(AnnotatedLine &Line);
121 void calculateFormattingInformation(AnnotatedLine &Line);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000122
123private:
124 /// \brief Calculate the penalty for splitting before \c Tok.
Daniel Jasper4fcc8b92013-11-07 17:52:51 +0000125 unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok,
126 bool InFunctionDecl);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000127
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000128 bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
129 const FormatToken &Right);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000130
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000131 bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000132
Daniel Jasperfb81b092013-09-17 09:52:48 +0000133 bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
134
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000135 bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000136
Daniel Jasper6bee6822013-04-08 20:33:42 +0000137 void printDebugInfo(const AnnotatedLine &Line);
138
Manuel Klimek4fe43002013-05-22 12:51:29 +0000139 void calculateUnbreakableTailLengths(AnnotatedLine &Line);
140
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000141 const FormatStyle &Style;
Nico Weber29f9dea2013-02-11 15:32:15 +0000142
Daniel Jasperd0ec0d62014-11-04 12:41:02 +0000143 const AdditionalKeywords &Keywords;
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
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000149#endif