Home | History | Annotate | Download | only in Format
      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_LIB_FORMAT_TOKENANNOTATOR_H
     17 #define LLVM_CLANG_LIB_FORMAT_TOKENANNOTATOR_H
     18 
     19 #include "UnwrappedLineParser.h"
     20 #include "clang/Format/Format.h"
     21 #include <string>
     22 
     23 namespace clang {
     24 class SourceManager;
     25 
     26 namespace format {
     27 
     28 enum LineType {
     29   LT_Invalid,
     30   LT_ImportStatement,
     31   LT_ObjCDecl, // An @interface, @implementation, or @protocol line.
     32   LT_ObjCMethodDecl,
     33   LT_ObjCProperty, // An @property line.
     34   LT_Other,
     35   LT_PreprocessorDirective,
     36   LT_VirtualFunctionDecl
     37 };
     38 
     39 class AnnotatedLine {
     40 public:
     41   AnnotatedLine(const UnwrappedLine &Line)
     42       : First(Line.Tokens.front().Tok), Level(Line.Level),
     43         InPPDirective(Line.InPPDirective),
     44         MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false),
     45         IsMultiVariableDeclStmt(false), Affected(false),
     46         LeadingEmptyLinesAffected(false), ChildrenAffected(false) {
     47     assert(!Line.Tokens.empty());
     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.
     52     First->Previous = nullptr;
     53     FormatToken *Current = First;
     54     for (std::list<UnwrappedLineNode>::const_iterator I = ++Line.Tokens.begin(),
     55                                                       E = Line.Tokens.end();
     56          I != E; ++I) {
     57       const UnwrappedLineNode &Node = *I;
     58       Current->Next = I->Tok;
     59       I->Tok->Previous = Current;
     60       Current = Current->Next;
     61       Current->Children.clear();
     62       for (const auto& Child : Node.Children) {
     63         Children.push_back(new AnnotatedLine(Child));
     64         Current->Children.push_back(Children.back());
     65       }
     66     }
     67     Last = Current;
     68     Last->Next = nullptr;
     69   }
     70 
     71   ~AnnotatedLine() {
     72     for (unsigned i = 0, e = Children.size(); i != e; ++i) {
     73       delete Children[i];
     74     }
     75     FormatToken *Current = First;
     76     while (Current) {
     77       Current->Children.clear();
     78       Current->Role.reset();
     79       Current = Current->Next;
     80     }
     81   }
     82 
     83   FormatToken *First;
     84   FormatToken *Last;
     85 
     86   SmallVector<AnnotatedLine *, 0> Children;
     87 
     88   LineType Type;
     89   unsigned Level;
     90   bool InPPDirective;
     91   bool MustBeDeclaration;
     92   bool MightBeFunctionDecl;
     93   bool IsMultiVariableDeclStmt;
     94 
     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 
    106 private:
    107   // Disallow copying.
    108   AnnotatedLine(const AnnotatedLine &) = delete;
    109   void operator=(const AnnotatedLine &) = delete;
    110 };
    111 
    112 /// \brief Determines extra information about the tokens comprising an
    113 /// \c UnwrappedLine.
    114 class TokenAnnotator {
    115 public:
    116   TokenAnnotator(const FormatStyle &Style, const AdditionalKeywords &Keywords)
    117       : Style(Style), Keywords(Keywords) {}
    118 
    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 
    124   void annotate(AnnotatedLine &Line);
    125   void calculateFormattingInformation(AnnotatedLine &Line);
    126 
    127 private:
    128   /// \brief Calculate the penalty for splitting before \c Tok.
    129   unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok,
    130                         bool InFunctionDecl);
    131 
    132   bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
    133                             const FormatToken &Right);
    134 
    135   bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
    136 
    137   bool mustBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
    138 
    139   bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
    140 
    141   void printDebugInfo(const AnnotatedLine &Line);
    142 
    143   void calculateUnbreakableTailLengths(AnnotatedLine &Line);
    144 
    145   const FormatStyle &Style;
    146 
    147   const AdditionalKeywords &Keywords;
    148 };
    149 
    150 } // end namespace format
    151 } // end namespace clang
    152 
    153 #endif
    154