blob: 06f335215d120823eaae3335bbcc024c84e57950 [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 Klimekb3987012013-05-29 14:47:47 +000046 FormatToken *Current = First;
Daniel Jasper567dcf92013-09-05 09:29:45 +000047 for (std::list<UnwrappedLineNode>::const_iterator I = ++Line.Tokens.begin(),
48 E = Line.Tokens.end();
Daniel Jasper32d28ee2013-01-29 21:01:14 +000049 I != E; ++I) {
Daniel Jasper567dcf92013-09-05 09:29:45 +000050 const UnwrappedLineNode &Node = *I;
51 Current->Next = I->Tok;
52 I->Tok->Previous = Current;
Manuel Klimekb3987012013-05-29 14:47:47 +000053 Current = Current->Next;
Daniel Jasper567dcf92013-09-05 09:29:45 +000054 for (SmallVectorImpl<UnwrappedLine>::const_iterator
55 I = Node.Children.begin(),
56 E = Node.Children.end();
57 I != E; ++I) {
58 Children.push_back(new AnnotatedLine(*I));
59 Current->Children.push_back(Children.back());
60 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +000061 }
62 Last = Current;
63 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +000064
Daniel Jasper567dcf92013-09-05 09:29:45 +000065 ~AnnotatedLine() {
66 for (unsigned i = 0, e = Children.size(); i != e; ++i) {
67 delete Children[i];
68 }
Daniel Jasper567dcf92013-09-05 09:29:45 +000069 }
70
Manuel Klimekb3987012013-05-29 14:47:47 +000071 FormatToken *First;
72 FormatToken *Last;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000073
Daniel Jasperb77d7412013-09-06 07:54:20 +000074 SmallVector<AnnotatedLine *, 0> Children;
Daniel Jasper567dcf92013-09-05 09:29:45 +000075
Daniel Jasper32d28ee2013-01-29 21:01:14 +000076 LineType Type;
77 unsigned Level;
78 bool InPPDirective;
79 bool MustBeDeclaration;
Daniel Jasper3c08a812013-02-24 18:54:32 +000080 bool MightBeFunctionDecl;
Daniel Jasper53e72cd2013-05-06 08:27:33 +000081 bool StartsDefinition;
Daniel Jasper567dcf92013-09-05 09:29:45 +000082
83private:
84 // Disallow copying.
85 AnnotatedLine(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
86 void operator=(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000087};
88
Daniel Jasper32d28ee2013-01-29 21:01:14 +000089/// \brief Determines extra information about the tokens comprising an
90/// \c UnwrappedLine.
91class TokenAnnotator {
92public:
Alexander Kornienko00895102013-06-05 14:09:10 +000093 TokenAnnotator(const FormatStyle &Style, IdentifierInfo &Ident_in)
94 : Style(Style), Ident_in(Ident_in) {}
Daniel Jasper32d28ee2013-01-29 21:01:14 +000095
Daniel Jasperb77d7412013-09-06 07:54:20 +000096 /// \brief Adapts the indent levels of comment lines to the indent of the
97 /// subsequent line.
98 // FIXME: Can/should this be done in the UnwrappedLineParser?
99 void setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines);
100
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000101 void annotate(AnnotatedLine &Line);
102 void calculateFormattingInformation(AnnotatedLine &Line);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000103
104private:
105 /// \brief Calculate the penalty for splitting before \c Tok.
Manuel Klimekb3987012013-05-29 14:47:47 +0000106 unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000107
Manuel Klimekb3987012013-05-29 14:47:47 +0000108 bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
109 const FormatToken &Right);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000110
Manuel Klimekb3987012013-05-29 14:47:47 +0000111 bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000112
Manuel Klimekb3987012013-05-29 14:47:47 +0000113 bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000114
Daniel Jasperbf71ba22013-04-08 20:33:42 +0000115 void printDebugInfo(const AnnotatedLine &Line);
116
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000117 void calculateUnbreakableTailLengths(AnnotatedLine &Line);
118
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000119 const FormatStyle &Style;
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000120
121 // Contextual keywords:
122 IdentifierInfo &Ident_in;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000123};
124
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000125} // end namespace format
126} // end namespace clang
127
128#endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H