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