blob: a948cdb1c419459acaef7d3ba384a419ff30e663 [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
Stephen Hines176edba2014-12-01 14:53:08 -080016#ifndef LLVM_CLANG_LIB_FORMAT_TOKENANNOTATOR_H
17#define LLVM_CLANG_LIB_FORMAT_TOKENANNOTATOR_H
Daniel Jasper32d28ee2013-01-29 21:01:14 +000018
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,
Stephen Hines0e2c34f2015-03-23 12:09:02 -070030 LT_ImportStatement,
Daniel Jasper32d28ee2013-01-29 21:01:14 +000031 LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
32 LT_ObjCMethodDecl,
Stephen Hines0e2c34f2015-03-23 12:09:02 -070033 LT_ObjCProperty, // An @property line.
34 LT_Other,
35 LT_PreprocessorDirective,
36 LT_VirtualFunctionDecl
Daniel Jasper32d28ee2013-01-29 21:01:14 +000037};
38
Daniel Jasper32d28ee2013-01-29 21:01:14 +000039class AnnotatedLine {
40public:
41 AnnotatedLine(const UnwrappedLine &Line)
Daniel Jasper567dcf92013-09-05 09:29:45 +000042 : First(Line.Tokens.front().Tok), Level(Line.Level),
Daniel Jasper32d28ee2013-01-29 21:01:14 +000043 InPPDirective(Line.InPPDirective),
Daniel Jasper53e72cd2013-05-06 08:27:33 +000044 MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false),
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070045 IsMultiVariableDeclStmt(false), Affected(false),
46 LeadingEmptyLinesAffected(false), ChildrenAffected(false) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +000047 assert(!Line.Tokens.empty());
Manuel Klimekd186f0b2013-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.
Stephen Hines6bcf27b2014-05-29 04:14:42 -070052 First->Previous = nullptr;
Manuel Klimekb3987012013-05-29 14:47:47 +000053 FormatToken *Current = First;
Daniel Jasper567dcf92013-09-05 09:29:45 +000054 for (std::list<UnwrappedLineNode>::const_iterator I = ++Line.Tokens.begin(),
55 E = Line.Tokens.end();
Daniel Jasper32d28ee2013-01-29 21:01:14 +000056 I != E; ++I) {
Daniel Jasper567dcf92013-09-05 09:29:45 +000057 const UnwrappedLineNode &Node = *I;
58 Current->Next = I->Tok;
59 I->Tok->Previous = Current;
Manuel Klimekb3987012013-05-29 14:47:47 +000060 Current = Current->Next;
Daniel Jasper852bce42013-10-24 15:23:11 +000061 Current->Children.clear();
Stephen Hines0e2c34f2015-03-23 12:09:02 -070062 for (const auto& Child : Node.Children) {
63 Children.push_back(new AnnotatedLine(Child));
Daniel Jasper567dcf92013-09-05 09:29:45 +000064 Current->Children.push_back(Children.back());
65 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +000066 }
67 Last = Current;
Stephen Hines6bcf27b2014-05-29 04:14:42 -070068 Last->Next = nullptr;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000069 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +000070
Daniel Jasper567dcf92013-09-05 09:29:45 +000071 ~AnnotatedLine() {
72 for (unsigned i = 0, e = Children.size(); i != e; ++i) {
73 delete Children[i];
74 }
Stephen Hines0e2c34f2015-03-23 12:09:02 -070075 FormatToken *Current = First;
76 while (Current) {
77 Current->Children.clear();
78 Current->Role.reset();
79 Current = Current->Next;
80 }
Daniel Jasper567dcf92013-09-05 09:29:45 +000081 }
82
Manuel Klimekb3987012013-05-29 14:47:47 +000083 FormatToken *First;
84 FormatToken *Last;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000085
Daniel Jasperb77d7412013-09-06 07:54:20 +000086 SmallVector<AnnotatedLine *, 0> Children;
Daniel Jasper567dcf92013-09-05 09:29:45 +000087
Daniel Jasper32d28ee2013-01-29 21:01:14 +000088 LineType Type;
89 unsigned Level;
90 bool InPPDirective;
91 bool MustBeDeclaration;
Daniel Jasper3c08a812013-02-24 18:54:32 +000092 bool MightBeFunctionDecl;
Pirama Arumuga Nainar3ea9e332015-04-08 08:57:32 -070093 bool IsMultiVariableDeclStmt;
Daniel Jasper567dcf92013-09-05 09:29:45 +000094
Stephen Hines651f13c2014-04-23 16:59:28 -070095 /// \c True if this line should be formatted, i.e. intersects directly or
96 /// indirectly with one of the input ranges.
97 bool Affected;
98
99 /// \c True if the leading empty lines of this line intersect with one of the
100 /// input ranges.
101 bool LeadingEmptyLinesAffected;
102
103 /// \c True if a one of this line's children intersects with an input range.
104 bool ChildrenAffected;
105
Daniel Jasper567dcf92013-09-05 09:29:45 +0000106private:
107 // Disallow copying.
Stephen Hines0e2c34f2015-03-23 12:09:02 -0700108 AnnotatedLine(const AnnotatedLine &) = delete;
109 void operator=(const AnnotatedLine &) = delete;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000110};
111
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000112/// \brief Determines extra information about the tokens comprising an
113/// \c UnwrappedLine.
114class TokenAnnotator {
115public:
Stephen Hines176edba2014-12-01 14:53:08 -0800116 TokenAnnotator(const FormatStyle &Style, const AdditionalKeywords &Keywords)
117 : Style(Style), Keywords(Keywords) {}
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000118
Daniel Jasperb77d7412013-09-06 07:54:20 +0000119 /// \brief Adapts the indent levels of comment lines to the indent of the
120 /// subsequent line.
121 // FIXME: Can/should this be done in the UnwrappedLineParser?
122 void setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines);
123
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000124 void annotate(AnnotatedLine &Line);
125 void calculateFormattingInformation(AnnotatedLine &Line);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000126
127private:
128 /// \brief Calculate the penalty for splitting before \c Tok.
Daniel Jasperdbfb5f32013-11-07 17:52:51 +0000129 unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok,
130 bool InFunctionDecl);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000131
Manuel Klimekb3987012013-05-29 14:47:47 +0000132 bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
133 const FormatToken &Right);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000134
Manuel Klimekb3987012013-05-29 14:47:47 +0000135 bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000136
Daniel Jasperebaa1712013-09-17 09:52:48 +0000137 bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
138
Manuel Klimekb3987012013-05-29 14:47:47 +0000139 bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000140
Daniel Jasperbf71ba22013-04-08 20:33:42 +0000141 void printDebugInfo(const AnnotatedLine &Line);
142
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000143 void calculateUnbreakableTailLengths(AnnotatedLine &Line);
144
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000145 const FormatStyle &Style;
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000146
Stephen Hines176edba2014-12-01 14:53:08 -0800147 const AdditionalKeywords &Keywords;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000148};
149
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000150} // end namespace format
151} // end namespace clang
152
Stephen Hines176edba2014-12-01 14:53:08 -0800153#endif