blob: a0d680c91dfe28dbdfbda5a1fed4e04036ad8b21 [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 {
24class Lexer;
25class SourceManager;
26
27namespace format {
28
Daniel Jasper32d28ee2013-01-29 21:01:14 +000029enum LineType {
30 LT_Invalid,
31 LT_Other,
32 LT_BuilderTypeCall,
33 LT_PreprocessorDirective,
34 LT_VirtualFunctionDecl,
35 LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
36 LT_ObjCMethodDecl,
37 LT_ObjCProperty // An @property line.
38};
39
Daniel Jasper32d28ee2013-01-29 21:01:14 +000040class AnnotatedLine {
41public:
42 AnnotatedLine(const UnwrappedLine &Line)
43 : First(Line.Tokens.front()), Level(Line.Level),
44 InPPDirective(Line.InPPDirective),
Daniel Jasper53e72cd2013-05-06 08:27:33 +000045 MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false),
46 StartsDefinition(false) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +000047 assert(!Line.Tokens.empty());
Manuel Klimekb3987012013-05-29 14:47:47 +000048 FormatToken *Current = First;
Manuel Klimekdcb3f2a2013-05-28 13:42:28 +000049 for (std::list<FormatToken *>::const_iterator I = ++Line.Tokens.begin(),
50 E = Line.Tokens.end();
Daniel Jasper32d28ee2013-01-29 21:01:14 +000051 I != E; ++I) {
Manuel Klimekb3987012013-05-29 14:47:47 +000052 Current->Next = *I;
53 (*I)->Previous = Current;
54 Current = Current->Next;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000055 }
56 Last = Current;
57 }
Daniel Jasper32d28ee2013-01-29 21:01:14 +000058
Manuel Klimekb3987012013-05-29 14:47:47 +000059 FormatToken *First;
60 FormatToken *Last;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000061
62 LineType Type;
63 unsigned Level;
64 bool InPPDirective;
65 bool MustBeDeclaration;
Daniel Jasper3c08a812013-02-24 18:54:32 +000066 bool MightBeFunctionDecl;
Daniel Jasper53e72cd2013-05-06 08:27:33 +000067 bool StartsDefinition;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000068};
69
Daniel Jasper32d28ee2013-01-29 21:01:14 +000070/// \brief Determines extra information about the tokens comprising an
71/// \c UnwrappedLine.
72class TokenAnnotator {
73public:
Nico Weberc2e6d2a2013-02-11 15:32:15 +000074 TokenAnnotator(const FormatStyle &Style, SourceManager &SourceMgr, Lexer &Lex,
75 IdentifierInfo &Ident_in)
76 : Style(Style), SourceMgr(SourceMgr), Lex(Lex), Ident_in(Ident_in) {
Daniel Jasper32d28ee2013-01-29 21:01:14 +000077 }
78
Daniel Jasper8ff690a2013-02-06 14:22:40 +000079 void annotate(AnnotatedLine &Line);
80 void calculateFormattingInformation(AnnotatedLine &Line);
Daniel Jasper32d28ee2013-01-29 21:01:14 +000081
82private:
83 /// \brief Calculate the penalty for splitting before \c Tok.
Manuel Klimekb3987012013-05-29 14:47:47 +000084 unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +000085
Manuel Klimekb3987012013-05-29 14:47:47 +000086 bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
87 const FormatToken &Right);
Daniel Jasper32d28ee2013-01-29 21:01:14 +000088
Manuel Klimekb3987012013-05-29 14:47:47 +000089 bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
Daniel Jasper32d28ee2013-01-29 21:01:14 +000090
Manuel Klimekb3987012013-05-29 14:47:47 +000091 bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
Daniel Jasper32d28ee2013-01-29 21:01:14 +000092
Daniel Jasperbf71ba22013-04-08 20:33:42 +000093 void printDebugInfo(const AnnotatedLine &Line);
94
Manuel Klimeke573c3f2013-05-22 12:51:29 +000095 void calculateUnbreakableTailLengths(AnnotatedLine &Line);
96
Daniel Jasper8ff690a2013-02-06 14:22:40 +000097 const FormatStyle &Style;
Daniel Jasper32d28ee2013-01-29 21:01:14 +000098 SourceManager &SourceMgr;
99 Lexer &Lex;
Nico Weberc2e6d2a2013-02-11 15:32:15 +0000100
101 // Contextual keywords:
102 IdentifierInfo &Ident_in;
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000103};
104
Daniel Jasper32d28ee2013-01-29 21:01:14 +0000105} // end namespace format
106} // end namespace clang
107
108#endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H