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, |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 31 | 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 Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 38 | class AnnotatedLine { |
| 39 | public: |
| 40 | AnnotatedLine(const UnwrappedLine &Line) |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 41 | : First(Line.Tokens.front().Tok), Level(Line.Level), |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 42 | InPPDirective(Line.InPPDirective), |
Daniel Jasper | 53e72cd | 2013-05-06 08:27:33 +0000 | [diff] [blame] | 43 | MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 44 | StartsDefinition(false), Affected(false), |
| 45 | LeadingEmptyLinesAffected(false), ChildrenAffected(false) { |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 46 | assert(!Line.Tokens.empty()); |
Manuel Klimek | d186f0b | 2013-10-21 08:11:15 +0000 | [diff] [blame] | 47 | |
| 48 | // Calculate Next and Previous for all tokens. Note that we must overwrite |
| 49 | // Next and Previous for every token, as previous formatting runs might have |
| 50 | // left them in a different state. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 51 | First->Previous = nullptr; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 52 | FormatToken *Current = First; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 53 | for (std::list<UnwrappedLineNode>::const_iterator I = ++Line.Tokens.begin(), |
| 54 | E = Line.Tokens.end(); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 55 | I != E; ++I) { |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 56 | const UnwrappedLineNode &Node = *I; |
| 57 | Current->Next = I->Tok; |
| 58 | I->Tok->Previous = Current; |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 59 | Current = Current->Next; |
Daniel Jasper | 852bce4 | 2013-10-24 15:23:11 +0000 | [diff] [blame] | 60 | Current->Children.clear(); |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 61 | for (SmallVectorImpl<UnwrappedLine>::const_iterator |
| 62 | I = Node.Children.begin(), |
| 63 | E = Node.Children.end(); |
| 64 | I != E; ++I) { |
| 65 | Children.push_back(new AnnotatedLine(*I)); |
| 66 | Current->Children.push_back(Children.back()); |
| 67 | } |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 68 | } |
| 69 | Last = Current; |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 70 | Last->Next = nullptr; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 71 | } |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 72 | |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 73 | ~AnnotatedLine() { |
| 74 | for (unsigned i = 0, e = Children.size(); i != e; ++i) { |
| 75 | delete Children[i]; |
| 76 | } |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 79 | FormatToken *First; |
| 80 | FormatToken *Last; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 81 | |
Daniel Jasper | b77d741 | 2013-09-06 07:54:20 +0000 | [diff] [blame] | 82 | SmallVector<AnnotatedLine *, 0> Children; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 83 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 84 | LineType Type; |
| 85 | unsigned Level; |
| 86 | bool InPPDirective; |
| 87 | bool MustBeDeclaration; |
Daniel Jasper | 3c08a81 | 2013-02-24 18:54:32 +0000 | [diff] [blame] | 88 | bool MightBeFunctionDecl; |
Daniel Jasper | 53e72cd | 2013-05-06 08:27:33 +0000 | [diff] [blame] | 89 | bool StartsDefinition; |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 90 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 91 | /// \c True if this line should be formatted, i.e. intersects directly or |
| 92 | /// indirectly with one of the input ranges. |
| 93 | bool Affected; |
| 94 | |
| 95 | /// \c True if the leading empty lines of this line intersect with one of the |
| 96 | /// input ranges. |
| 97 | bool LeadingEmptyLinesAffected; |
| 98 | |
| 99 | /// \c True if a one of this line's children intersects with an input range. |
| 100 | bool ChildrenAffected; |
| 101 | |
Daniel Jasper | 567dcf9 | 2013-09-05 09:29:45 +0000 | [diff] [blame] | 102 | private: |
| 103 | // Disallow copying. |
| 104 | AnnotatedLine(const AnnotatedLine &) LLVM_DELETED_FUNCTION; |
| 105 | void operator=(const AnnotatedLine &) LLVM_DELETED_FUNCTION; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 108 | /// \brief Determines extra information about the tokens comprising an |
| 109 | /// \c UnwrappedLine. |
| 110 | class TokenAnnotator { |
| 111 | public: |
Alexander Kornienko | 0089510 | 2013-06-05 14:09:10 +0000 | [diff] [blame] | 112 | TokenAnnotator(const FormatStyle &Style, IdentifierInfo &Ident_in) |
| 113 | : Style(Style), Ident_in(Ident_in) {} |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 114 | |
Daniel Jasper | b77d741 | 2013-09-06 07:54:20 +0000 | [diff] [blame] | 115 | /// \brief Adapts the indent levels of comment lines to the indent of the |
| 116 | /// subsequent line. |
| 117 | // FIXME: Can/should this be done in the UnwrappedLineParser? |
| 118 | void setCommentLineLevels(SmallVectorImpl<AnnotatedLine *> &Lines); |
| 119 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 120 | void annotate(AnnotatedLine &Line); |
| 121 | void calculateFormattingInformation(AnnotatedLine &Line); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 122 | |
| 123 | private: |
| 124 | /// \brief Calculate the penalty for splitting before \c Tok. |
Daniel Jasper | dbfb5f3 | 2013-11-07 17:52:51 +0000 | [diff] [blame] | 125 | unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok, |
| 126 | bool InFunctionDecl); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 127 | |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 128 | bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left, |
| 129 | const FormatToken &Right); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 130 | |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 131 | bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 132 | |
Daniel Jasper | ebaa171 | 2013-09-17 09:52:48 +0000 | [diff] [blame] | 133 | bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right); |
| 134 | |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 135 | bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right); |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 136 | |
Daniel Jasper | bf71ba2 | 2013-04-08 20:33:42 +0000 | [diff] [blame] | 137 | void printDebugInfo(const AnnotatedLine &Line); |
| 138 | |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 139 | void calculateUnbreakableTailLengths(AnnotatedLine &Line); |
| 140 | |
Daniel Jasper | 8ff690a | 2013-02-06 14:22:40 +0000 | [diff] [blame] | 141 | const FormatStyle &Style; |
Nico Weber | c2e6d2a | 2013-02-11 15:32:15 +0000 | [diff] [blame] | 142 | |
| 143 | // Contextual keywords: |
| 144 | IdentifierInfo &Ident_in; |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
Daniel Jasper | 32d28ee | 2013-01-29 21:01:14 +0000 | [diff] [blame] | 147 | } // end namespace format |
| 148 | } // end namespace clang |
| 149 | |
| 150 | #endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H |