blob: 28d55a007c2080491ca843b0f57411ebfea1fb73 [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,
31 LT_BuilderTypeCall,
32 LT_PreprocessorDirective,
33 LT_VirtualFunctionDecl,
34 LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
35 LT_ObjCMethodDecl,
36 LT_ObjCProperty // An @property line.
37};
38
Daniel Jasper32d28ee2013-01-29 21:01:14 +000039class AnnotatedLine {
40public:
41 AnnotatedLine(const UnwrappedLine &Line)
42 : First(Line.Tokens.front()), Level(Line.Level),
43 InPPDirective(Line.InPPDirective),
Daniel Jasper53e72cd2013-05-06 08:27:33 +000044 MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false),
45 StartsDefinition(false) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +000046 assert(!Line.Tokens.empty());
Manuel Klimekb3987012013-05-29 14:47:47 +000047 FormatToken *Current = First;
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +000048 for (std::list<FormatToken *>::const_iterator I = ++Line.Tokens.begin(),
49 E = Line.Tokens.end();
Daniel Jasper32d28ee2013-01-29 21:01:14 +000050 I != E; ++I) {
Manuel Klimekb3987012013-05-29 14:47:47 +000051 Current->Next = *I;
52 (*I)->Previous = Current;
53 Current = Current->Next;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000054 }
55 Last = Current;
56 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +000057
Manuel Klimekb3987012013-05-29 14:47:47 +000058 FormatToken *First;
59 FormatToken *Last;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000060
61 LineType Type;
62 unsigned Level;
63 bool InPPDirective;
64 bool MustBeDeclaration;
Daniel Jasper3c08a812013-02-24 18:54:32 +000065 bool MightBeFunctionDecl;
Daniel Jasper53e72cd2013-05-06 08:27:33 +000066 bool StartsDefinition;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000067};
68
Daniel Jasper32d28ee2013-01-29 21:01:14 +000069/// \brief Determines extra information about the tokens comprising an
70/// \c UnwrappedLine.
71class TokenAnnotator {
72public:
Alexander Kornienko00895102013-06-05 14:09:10 +000073 TokenAnnotator(const FormatStyle &Style, IdentifierInfo &Ident_in)
74 : Style(Style), Ident_in(Ident_in) {}
Daniel Jasper32d28ee2013-01-29 21:01:14 +000075
Daniel Jasper8ff690a2013-02-06 14:22:40 +000076 void annotate(AnnotatedLine &Line);
77 void calculateFormattingInformation(AnnotatedLine &Line);
Daniel Jasper32d28ee2013-01-29 21:01:14 +000078
79private:
80 /// \brief Calculate the penalty for splitting before \c Tok.
Manuel Klimekb3987012013-05-29 14:47:47 +000081 unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +000082
Manuel Klimekb3987012013-05-29 14:47:47 +000083 bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
84 const FormatToken &Right);
Daniel Jasper32d28ee2013-01-29 21:01:14 +000085
Manuel Klimekb3987012013-05-29 14:47:47 +000086 bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +000087
Manuel Klimekb3987012013-05-29 14:47:47 +000088 bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
Daniel Jasper32d28ee2013-01-29 21:01:14 +000089
Daniel Jasperbf71ba22013-04-08 20:33:42 +000090 void printDebugInfo(const AnnotatedLine &Line);
91
Manuel Klimeke573c3f2013-05-22 12:51:29 +000092 void calculateUnbreakableTailLengths(AnnotatedLine &Line);
93
Daniel Jasper8ff690a2013-02-06 14:22:40 +000094 const FormatStyle &Style;
Nico Weberc2e6d2a2013-02-11 15:32:15 +000095
96 // Contextual keywords:
97 IdentifierInfo &Ident_in;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000098};
99
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000100} // end namespace format
101} // end namespace clang
102
103#endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H