blob: ca8b115415766b26e29cecc46e3b74443d8c2d5d [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),
44 StartsDefinition(false) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +000045 assert(!Line.Tokens.empty());
Manuel Klimekd186f0b2013-10-21 08:11:15 +000046
47 // Calculate Next and Previous for all tokens. Note that we must overwrite
48 // Next and Previous for every token, as previous formatting runs might have
49 // left them in a different state.
50 First->Previous = NULL;
Manuel Klimekb3987012013-05-29 14:47:47 +000051 FormatToken *Current = First;
Daniel Jasper567dcf92013-09-05 09:29:45 +000052 for (std::list<UnwrappedLineNode>::const_iterator I = ++Line.Tokens.begin(),
53 E = Line.Tokens.end();
Daniel Jasper32d28ee2013-01-29 21:01:14 +000054 I != E; ++I) {
Daniel Jasper567dcf92013-09-05 09:29:45 +000055 const UnwrappedLineNode &Node = *I;
56 Current->Next = I->Tok;
57 I->Tok->Previous = Current;
Manuel Klimekb3987012013-05-29 14:47:47 +000058 Current = Current->Next;
Daniel Jasper852bce42013-10-24 15:23:11 +000059 Current->Children.clear();
Daniel Jasper567dcf92013-09-05 09:29:45 +000060 for (SmallVectorImpl<UnwrappedLine>::const_iterator
61 I = Node.Children.begin(),
62 E = Node.Children.end();
63 I != E; ++I) {
64 Children.push_back(new AnnotatedLine(*I));
65 Current->Children.push_back(Children.back());
66 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +000067 }
68 Last = Current;
Manuel Klimekd186f0b2013-10-21 08:11:15 +000069 Last->Next = NULL;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000070 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +000071
Daniel Jasper567dcf92013-09-05 09:29:45 +000072 ~AnnotatedLine() {
73 for (unsigned i = 0, e = Children.size(); i != e; ++i) {
74 delete Children[i];
75 }
Daniel Jasper567dcf92013-09-05 09:29:45 +000076 }
77
Manuel Klimekb3987012013-05-29 14:47:47 +000078 FormatToken *First;
79 FormatToken *Last;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000080
Daniel Jasperb77d7412013-09-06 07:54:20 +000081 SmallVector<AnnotatedLine *, 0> Children;
Daniel Jasper567dcf92013-09-05 09:29:45 +000082
Daniel Jasper32d28ee2013-01-29 21:01:14 +000083 LineType Type;
84 unsigned Level;
85 bool InPPDirective;
86 bool MustBeDeclaration;
Daniel Jasper3c08a812013-02-24 18:54:32 +000087 bool MightBeFunctionDecl;
Daniel Jasper53e72cd2013-05-06 08:27:33 +000088 bool StartsDefinition;
Daniel Jasper567dcf92013-09-05 09:29:45 +000089
90private:
91 // Disallow copying.
92 AnnotatedLine(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
93 void operator=(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000094};
95
Daniel Jasper32d28ee2013-01-29 21:01:14 +000096/// \brief Determines extra information about the tokens comprising an
97/// \c UnwrappedLine.
98class TokenAnnotator {
99public:
Alexander Kornienko00895102013-06-05 14:09:10 +0000100 TokenAnnotator(const FormatStyle &Style, IdentifierInfo &Ident_in)
101 : Style(Style), Ident_in(Ident_in) {}
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000102
Daniel Jasperb77d7412013-09-06 07:54:20 +0000103 /// \brief Adapts the indent levels of comment lines to the indent of the
104 /// subsequent line.
105 // FIXME: Can/should this be done in the UnwrappedLineParser?
106 void setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines);
107
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000108 void annotate(AnnotatedLine &Line);
109 void calculateFormattingInformation(AnnotatedLine &Line);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000110
111private:
112 /// \brief Calculate the penalty for splitting before \c Tok.
Manuel Klimekb3987012013-05-29 14:47:47 +0000113 unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000114
Manuel Klimekb3987012013-05-29 14:47:47 +0000115 bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
116 const FormatToken &Right);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000117
Manuel Klimekb3987012013-05-29 14:47:47 +0000118 bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000119
Daniel Jasperebaa1712013-09-17 09:52:48 +0000120 bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
121
Manuel Klimekb3987012013-05-29 14:47:47 +0000122 bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000123
Daniel Jasperbf71ba22013-04-08 20:33:42 +0000124 void printDebugInfo(const AnnotatedLine &Line);
125
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000126 void calculateUnbreakableTailLengths(AnnotatedLine &Line);
127
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000128 const FormatStyle &Style;
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000129
130 // Contextual keywords:
131 IdentifierInfo &Ident_in;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000132};
133
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000134} // end namespace format
135} // end namespace clang
136
137#endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H