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_FORMAT_TOKEN_ANNOTATOR_H
     17 #define LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_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_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 
     39 class AnnotatedLine {
     40 public:
     41   AnnotatedLine(const UnwrappedLine &Line)
     42       : First(Line.Tokens.front()), Level(Line.Level),
     43         InPPDirective(Line.InPPDirective),
     44         MustBeDeclaration(Line.MustBeDeclaration), MightBeFunctionDecl(false),
     45         StartsDefinition(false) {
     46     assert(!Line.Tokens.empty());
     47     FormatToken *Current = First;
     48     for (std::list<FormatToken *>::const_iterator I = ++Line.Tokens.begin(),
     49                                                   E = Line.Tokens.end();
     50          I != E; ++I) {
     51       Current->Next = *I;
     52       (*I)->Previous = Current;
     53       Current = Current->Next;
     54     }
     55     Last = Current;
     56   }
     57 
     58   FormatToken *First;
     59   FormatToken *Last;
     60 
     61   LineType Type;
     62   unsigned Level;
     63   bool InPPDirective;
     64   bool MustBeDeclaration;
     65   bool MightBeFunctionDecl;
     66   bool StartsDefinition;
     67 };
     68 
     69 /// \brief Determines extra information about the tokens comprising an
     70 /// \c UnwrappedLine.
     71 class TokenAnnotator {
     72 public:
     73   TokenAnnotator(const FormatStyle &Style, IdentifierInfo &Ident_in)
     74       : Style(Style), Ident_in(Ident_in) {}
     75 
     76   void annotate(AnnotatedLine &Line);
     77   void calculateFormattingInformation(AnnotatedLine &Line);
     78 
     79 private:
     80   /// \brief Calculate the penalty for splitting before \c Tok.
     81   unsigned splitPenalty(const AnnotatedLine &Line, const FormatToken &Tok);
     82 
     83   bool spaceRequiredBetween(const AnnotatedLine &Line, const FormatToken &Left,
     84                             const FormatToken &Right);
     85 
     86   bool spaceRequiredBefore(const AnnotatedLine &Line, const FormatToken &Tok);
     87 
     88   bool canBreakBefore(const AnnotatedLine &Line, const FormatToken &Right);
     89 
     90   void printDebugInfo(const AnnotatedLine &Line);
     91 
     92   void calculateUnbreakableTailLengths(AnnotatedLine &Line);
     93 
     94   const FormatStyle &Style;
     95 
     96   // Contextual keywords:
     97   IdentifierInfo &Ident_in;
     98 };
     99 
    100 } // end namespace format
    101 } // end namespace clang
    102 
    103 #endif // LLVM_CLANG_FORMAT_TOKEN_ANNOTATOR_H
    104