blob: e51003bac822a71a3a84457f1ebfe1c88040d4b4 [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 Jasper567dcf92013-09-05 09:29:45 +000059 for (SmallVectorImpl<UnwrappedLine>::const_iterator
60 I = Node.Children.begin(),
61 E = Node.Children.end();
62 I != E; ++I) {
63 Children.push_back(new AnnotatedLine(*I));
64 Current->Children.push_back(Children.back());
65 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +000066 }
67 Last = Current;
Manuel Klimekd186f0b2013-10-21 08:11:15 +000068 Last->Next = NULL;
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 }
Daniel Jasper567dcf92013-09-05 09:29:45 +000075 }
76
Manuel Klimekb3987012013-05-29 14:47:47 +000077 FormatToken *First;
78 FormatToken *Last;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000079
Daniel Jasperb77d7412013-09-06 07:54:20 +000080 SmallVector<AnnotatedLine *, 0> Children;
Daniel Jasper567dcf92013-09-05 09:29:45 +000081
Daniel Jasper32d28ee2013-01-29 21:01:14 +000082 LineType Type;
83 unsigned Level;
84 bool InPPDirective;
85 bool MustBeDeclaration;
Daniel Jasper3c08a812013-02-24 18:54:32 +000086 bool MightBeFunctionDecl;
Daniel Jasper53e72cd2013-05-06 08:27:33 +000087 bool StartsDefinition;
Daniel Jasper567dcf92013-09-05 09:29:45 +000088
89private:
90 // Disallow copying.
91 AnnotatedLine(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
92 void operator=(const AnnotatedLine &) LLVM_DELETED_FUNCTION;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000093};
94
Daniel Jasper32d28ee2013-01-29 21:01:14 +000095/// \brief Determines extra information about the tokens comprising an
96/// \c UnwrappedLine.
97class TokenAnnotator {
98public:
Alexander Kornienko00895102013-06-05 14:09:10 +000099 TokenAnnotator(const FormatStyle &Style, IdentifierInfo &Ident_in)
100 : Style(Style), Ident_in(Ident_in) {}
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000101
Daniel Jasperb77d7412013-09-06 07:54:20 +0000102 /// \brief Adapts the indent levels of comment lines to the indent of the
103 /// subsequent line.
104 // FIXME: Can/should this be done in the UnwrappedLineParser?
105 void setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines);
106
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000107 void annotate(AnnotatedLine &Line);
108 void calculateFormattingInformation(AnnotatedLine &Line);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000109
110private:
111 /// \brief Calculate the penalty for splitting before \c Tok.
Manuel Klimekb3987012013-05-29 14:47:47 +0000112 unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000113
Manuel Klimekb3987012013-05-29 14:47:47 +0000114 bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
115 const FormatToken &Right);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000116
Manuel Klimekb3987012013-05-29 14:47:47 +0000117 bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000118
Daniel Jasperebaa1712013-09-17 09:52:48 +0000119 bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
120
Manuel Klimekb3987012013-05-29 14:47:47 +0000121 bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000122
Daniel Jasperbf71ba22013-04-08 20:33:42 +0000123 void printDebugInfo(const AnnotatedLine &Line);
124
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000125 void calculateUnbreakableTailLengths(AnnotatedLine &Line);
126
Daniel Jasper8ff690a2013-02-06 14:22:40 +0000127 const FormatStyle &Style;
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000128
129 // Contextual keywords:
130 IdentifierInfo &Ident_in;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000131};
132
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000133} // end namespace format
134} // end namespace clang
135
136#endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H