blob: 480681ed2f32ebb20a6e2b37e90ccbc30b3a0253 [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)
41 : First(Line.Tokens.front()), Level(Line.Level),
42 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;
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +000047 for (std::list<FormatToken *>::const_iterator I = ++Line.Tokens.begin(),
48 E = Line.Tokens.end();
Daniel Jasper32d28ee2013-01-29 21:01:14 +000049 I != E; ++I) {
Manuel Klimekb3987012013-05-29 14:47:47 +000050 Current->Next = *I;
51 (*I)->Previous = Current;
52 Current = Current->Next;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000053 }
54 Last = Current;
55 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +000056
Manuel Klimekb3987012013-05-29 14:47:47 +000057 FormatToken *First;
58 FormatToken *Last;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000059
60 LineType Type;
61 unsigned Level;
62 bool InPPDirective;
63 bool MustBeDeclaration;
Daniel Jasper3c08a812013-02-24 18:54:32 +000064 bool MightBeFunctionDecl;
Daniel Jasper53e72cd2013-05-06 08:27:33 +000065 bool StartsDefinition;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000066};
67
Daniel Jasper32d28ee2013-01-29 21:01:14 +000068/// \brief Determines extra information about the tokens comprising an
69/// \c UnwrappedLine.
70class TokenAnnotator {
71public:
Alexander Kornienko00895102013-06-05 14:09:10 +000072 TokenAnnotator(const FormatStyle &Style, IdentifierInfo &Ident_in)
73 : Style(Style), Ident_in(Ident_in) {}
Daniel Jasper32d28ee2013-01-29 21:01:14 +000074
Daniel Jasper8ff690a2013-02-06 14:22:40 +000075 void annotate(AnnotatedLine &Line);
76 void calculateFormattingInformation(AnnotatedLine &Line);
Daniel Jasper32d28ee2013-01-29 21:01:14 +000077
78private:
79 /// \brief Calculate the penalty for splitting before \c Tok.
Manuel Klimekb3987012013-05-29 14:47:47 +000080 unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +000081
Manuel Klimekb3987012013-05-29 14:47:47 +000082 bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
83 const FormatToken &Right);
Daniel Jasper32d28ee2013-01-29 21:01:14 +000084
Manuel Klimekb3987012013-05-29 14:47:47 +000085 bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +000086
Manuel Klimekb3987012013-05-29 14:47:47 +000087 bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
Daniel Jasper32d28ee2013-01-29 21:01:14 +000088
Daniel Jasperbf71ba22013-04-08 20:33:42 +000089 void printDebugInfo(const AnnotatedLine &Line);
90
Manuel Klimeke573c3f2013-05-22 12:51:29 +000091 void calculateUnbreakableTailLengths(AnnotatedLine &Line);
92
Daniel Jasper8ff690a2013-02-06 14:22:40 +000093 const FormatStyle &Style;
Nico Weberc2e6d2a2013-02-11 15:32:15 +000094
95 // Contextual keywords:
96 IdentifierInfo &Ident_in;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000097};
98
Daniel Jasper32d28ee2013-01-29 21:01:14 +000099} // end namespace format
100} // end namespace clang
101
102#endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H