blob: 0df70a0d02c1e4a157a4c294230e682fb4e8dd75 [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),
Stephen Hines651f13c2014-04-23 16:59:28 -070044 StartsDefinition(false), Affected(false),
45 LeadingEmptyLinesAffected(false), ChildrenAffected(false) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +000046 assert(!Line.Tokens.empty());
Manuel Klimekd186f0b2013-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.
Stephen Hines6bcf27b2014-05-29 04:14:42 -070051 First->Previous = nullptr;
Manuel Klimekb3987012013-05-29 14:47:47 +000052 FormatToken *Current = First;
Daniel Jasper567dcf92013-09-05 09:29:45 +000053 for (std::list<UnwrappedLineNode>::const_iterator I = ++Line.Tokens.begin(),
54 E = Line.Tokens.end();
Daniel Jasper32d28ee2013-01-29 21:01:14 +000055 I != E; ++I) {
Daniel Jasper567dcf92013-09-05 09:29:45 +000056 const UnwrappedLineNode &Node = *I;
57 Current->Next = I->Tok;
58 I->Tok->Previous = Current;
Manuel Klimekb3987012013-05-29 14:47:47 +000059 Current = Current->Next;
Daniel Jasper852bce42013-10-24 15:23:11 +000060 Current->Children.clear();
Daniel Jasper567dcf92013-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 Jasper32d28ee2013-01-29 21:01:14 +000068 }
69 Last = Current;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070070 Last->Next = nullptr;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000071 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +000072
Daniel Jasper567dcf92013-09-05 09:29:45 +000073 ~AnnotatedLine() {
74 for (unsigned i = 0, e = Children.size(); i != e; ++i) {
75 delete Children[i];
76 }
Daniel Jasper567dcf92013-09-05 09:29:45 +000077 }
78
Manuel Klimekb3987012013-05-29 14:47:47 +000079 FormatToken *First;
80 FormatToken *Last;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000081
Daniel Jasperb77d7412013-09-06 07:54:20 +000082 SmallVector<AnnotatedLine *, 0> Children;
Daniel Jasper567dcf92013-09-05 09:29:45 +000083
Daniel Jasper32d28ee2013-01-29 21:01:14 +000084 LineType Type;
85 unsigned Level;
86 bool InPPDirective;
87 bool MustBeDeclaration;
Daniel Jasper3c08a812013-02-24 18:54:32 +000088 bool MightBeFunctionDecl;
Daniel Jasper53e72cd2013-05-06 08:27:33 +000089 bool StartsDefinition;
Daniel Jasper567dcf92013-09-05 09:29:45 +000090
Stephen Hines651f13c2014-04-23 16:59:28 -070091 /// \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
99 /// \c True if a one of this line's children intersects with an input range.
100 bool ChildrenAffected;
101
Daniel Jasper567dcf92013-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 Jasper32d28ee2013-01-29 21:01:14 +0000106};
107
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000108/// \brief Determines extra information about the tokens comprising an
109/// \c UnwrappedLine.
110class TokenAnnotator {
111public:
Alexander Kornienko00895102013-06-05 14:09:10 +0000112 TokenAnnotator(const FormatStyle &Style, IdentifierInfo &Ident_in)
113 : Style(Style), Ident_in(Ident_in) {}
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000114
Daniel Jasperb77d7412013-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 Jasper8ff690a2013-02-06 14:22:40 +0000120 void annotate(AnnotatedLine &Line);
121 void calculateFormattingInformation(AnnotatedLine &Line);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000122
123private:
124 /// \brief Calculate the penalty for splitting before \c Tok.
Daniel Jasperdbfb5f32013-11-07 17:52:51 +0000125 unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok,
126 bool InFunctionDecl);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000127
Manuel Klimekb3987012013-05-29 14:47:47 +0000128 bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
129 const FormatToken &Right);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000130
Manuel Klimekb3987012013-05-29 14:47:47 +0000131 bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000132
Daniel Jasperebaa1712013-09-17 09:52:48 +0000133 bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
134
Manuel Klimekb3987012013-05-29 14:47:47 +0000135 bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000136
Daniel Jasperbf71ba22013-04-08 20:33:42 +0000137 void printDebugInfo(const AnnotatedLine &Line);
138
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000139 void calculateUnbreakableTailLengths(AnnotatedLine &Line);
140
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000141 const FormatStyle &Style;
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000142
143 // Contextual keywords:
144 IdentifierInfo &Ident_in;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000145};
146
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000147} // end namespace format
148} // end namespace clang
149
150#endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H