blob: 8bdf758bc1befefe450824b383cce5a0e195298f [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 Jasper5500f612013-11-25 11:08:59 +000044 StartsDefinition(false), Affected(false),
45 LeadingEmptyLinesAffected(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.
51 First->Previous = NULL;
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;
Manuel Klimek88033d72013-10-21 08:11:15 +000070 Last->Next = NULL;
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 Jasper8e357692013-05-06 08:27:33 +000089 bool StartsDefinition;
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 Jasper9fe0e8d2013-09-05 09:29:45 +000099private:
100 // Disallow copying.
101 AnnotatedLine(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
102 void operator=(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000103};
104
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000105/// \brief Determines extra information about the tokens comprising an
106/// \c UnwrappedLine.
107class TokenAnnotator {
108public:
Alexander Kornienkoffcc0102013-06-05 14:09:10 +0000109 TokenAnnotator(const FormatStyle &Style, IdentifierInfo &Ident_in)
110 : Style(Style), Ident_in(Ident_in) {}
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000111
Daniel Jasper1c5d9df2013-09-06 07:54:20 +0000112 /// \brief Adapts the indent levels of comment lines to the indent of the
113 /// subsequent line.
114 // FIXME: Can/should this be done in the UnwrappedLineParser?
115 void setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines);
116
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000117 void annotate(AnnotatedLine &Line);
118 void calculateFormattingInformation(AnnotatedLine &Line);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000119
120private:
121 /// \brief Calculate the penalty for splitting before \c Tok.
Daniel Jasper4fcc8b92013-11-07 17:52:51 +0000122 unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok,
123 bool InFunctionDecl);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000124
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000125 bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
126 const FormatToken &Right);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000127
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000128 bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000129
Daniel Jasperfb81b092013-09-17 09:52:48 +0000130 bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
131
Manuel Klimek6e6310e2013-05-29 14:47:47 +0000132 bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000133
Daniel Jasper6bee6822013-04-08 20:33:42 +0000134 void printDebugInfo(const AnnotatedLine &Line);
135
Manuel Klimek4fe43002013-05-22 12:51:29 +0000136 void calculateUnbreakableTailLengths(AnnotatedLine &Line);
137
Daniel Jasper7fce3ab2013-02-06 14:22:40 +0000138 const FormatStyle &Style;
Nico Weber29f9dea2013-02-11 15:32:15 +0000139
140 // Contextual keywords:
141 IdentifierInfo &Ident_in;
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000142};
143
Daniel Jasper7a6d09b2013-01-29 21:01:14 +0000144} // end namespace format
145} // end namespace clang
146
147#endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H