Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 1 | //===--- 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 Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 20 | #include "clang/Format/Format.h" |
| 21 | #include <string> |
| 22 | |
| 23 | namespace clang { |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 24 | class SourceManager; |
| 25 | |
| 26 | namespace format { |
| 27 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 28 | enum 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 Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 39 | class AnnotatedLine { |
| 40 | public: |
| 41 | AnnotatedLine(const UnwrappedLine &Line) |
| 42 | : First(Line.Tokens.front()), Level(Line.Level), |
| 43 | InPPDirective(Line.InPPDirective), |
Daniel Jasper | 53e72cd | 2013-05-06 08:27:33 +0000 | [diff] [blame] | 44 | MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false), |
| 45 | StartsDefinition(false) { |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 46 | assert(!Line.Tokens.empty()); |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 47 | FormatToken *Current = First; |
Manuel Klimek | dcb3f2a | 2013-05-28 13:42:28 +0000 | [diff] [blame] | 48 | for (std::list<FormatToken *>::const_iterator I = ++Line.Tokens.begin(), |
| 49 | E = Line.Tokens.end(); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 50 | I != E; ++I) { |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 51 | Current->Next = *I; |
| 52 | (*I)->Previous = Current; |
| 53 | Current = Current->Next; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 54 | } |
| 55 | Last = Current; |
| 56 | } |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 57 | |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 58 | FormatToken *First; |
| 59 | FormatToken *Last; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 60 | |
| 61 | LineType Type; |
| 62 | unsigned Level; |
| 63 | bool InPPDirective; |
| 64 | bool MustBeDeclaration; |
Daniel Jasper | 3c08a81 | 2013-02-24 18:54:32 +0000 | [diff] [blame] | 65 | bool MightBeFunctionDecl; |
Daniel Jasper | 53e72cd | 2013-05-06 08:27:33 +0000 | [diff] [blame] | 66 | bool StartsDefinition; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 69 | /// \brief Determines extra information about the tokens comprising an |
| 70 | /// \c UnwrappedLine. |
| 71 | class TokenAnnotator { |
| 72 | public: |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame^] | 73 | TokenAnnotator(const FormatStyle &Style, IdentifierInfo &Ident_in) |
| 74 | : Style(Style), Ident_in(Ident_in) {} |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 75 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 76 | void annotate(AnnotatedLine &Line); |
| 77 | void calculateFormattingInformation(AnnotatedLine &Line); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 78 | |
| 79 | private: |
| 80 | /// \brief Calculate the penalty for splitting before \c Tok. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 81 | unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 82 | |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 83 | bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left, |
| 84 | const FormatToken &Right); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 85 | |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 86 | bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 87 | |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 88 | bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 89 | |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 90 | void printDebugInfo(const AnnotatedLine &Line); |
| 91 | |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 92 | void calculateUnbreakableTailLengths(AnnotatedLine &Line); |
| 93 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 94 | const FormatStyle &Style; |
Nico Weber | c2e6d2a | 2013-02-11 15:32:15 +0000 | [diff] [blame] | 95 | |
| 96 | // Contextual keywords: |
| 97 | IdentifierInfo &Ident_in; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 100 | } // end namespace format |
| 101 | } // end namespace clang |
| 102 | |
| 103 | #endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H |