Home | History | Annotate | Download | only in AST
      1 //===--- PrettyPrinter.h - Classes for aiding with AST printing -*- 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 //  This file defines the PrinterHelper interface.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_CLANG_AST_PRETTYPRINTER_H
     15 #define LLVM_CLANG_AST_PRETTYPRINTER_H
     16 
     17 #include "clang/Basic/LLVM.h"
     18 #include "clang/Basic/LangOptions.h"
     19 
     20 namespace clang {
     21 
     22 class LangOptions;
     23 class SourceManager;
     24 class Stmt;
     25 class TagDecl;
     26 
     27 class PrinterHelper {
     28 public:
     29   virtual ~PrinterHelper();
     30   virtual bool handledStmt(Stmt* E, raw_ostream& OS) = 0;
     31 };
     32 
     33 /// \brief Describes how types, statements, expressions, and
     34 /// declarations should be printed.
     35 ///
     36 /// This type is intended to be small and suitable for passing by value.
     37 /// It is very frequently copied.
     38 struct PrintingPolicy {
     39   /// \brief Create a default printing policy for the specified language.
     40   PrintingPolicy(const LangOptions &LO)
     41     : Indentation(2), SuppressSpecifiers(false),
     42       SuppressTagKeyword(LO.CPlusPlus),
     43       IncludeTagDefinition(false), SuppressScope(false),
     44       SuppressUnwrittenScope(false), SuppressInitializers(false),
     45       ConstantArraySizeAsWritten(false), AnonymousTagLocations(true),
     46       SuppressStrongLifetime(false), SuppressLifetimeQualifiers(false),
     47       SuppressTemplateArgsInCXXConstructors(false),
     48       Bool(LO.Bool), Restrict(LO.C99),
     49       Alignof(LO.CPlusPlus11), UnderscoreAlignof(LO.C11),
     50       UseVoidForZeroParams(!LO.CPlusPlus),
     51       TerseOutput(false), PolishForDeclaration(false),
     52       Half(LO.Half), MSWChar(LO.MicrosoftExt && !LO.WChar),
     53       IncludeNewlines(true), MSVCFormatting(false) { }
     54 
     55   /// \brief Adjust this printing policy for cases where it's known that
     56   /// we're printing C++ code (for instance, if AST dumping reaches a
     57   /// C++-only construct). This should not be used if a real LangOptions
     58   /// object is available.
     59   void adjustForCPlusPlus() {
     60     SuppressTagKeyword = true;
     61     Bool = true;
     62     UseVoidForZeroParams = false;
     63   }
     64 
     65   /// \brief The number of spaces to use to indent each line.
     66   unsigned Indentation : 8;
     67 
     68   /// \brief Whether we should suppress printing of the actual specifiers for
     69   /// the given type or declaration.
     70   ///
     71   /// This flag is only used when we are printing declarators beyond
     72   /// the first declarator within a declaration group. For example, given:
     73   ///
     74   /// \code
     75   /// const int *x, *y;
     76   /// \endcode
     77   ///
     78   /// SuppressSpecifiers will be false when printing the
     79   /// declaration for "x", so that we will print "int *x"; it will be
     80   /// \c true when we print "y", so that we suppress printing the
     81   /// "const int" type specifier and instead only print the "*y".
     82   bool SuppressSpecifiers : 1;
     83 
     84   /// \brief Whether type printing should skip printing the tag keyword.
     85   ///
     86   /// This is used when printing the inner type of elaborated types,
     87   /// (as the tag keyword is part of the elaborated type):
     88   ///
     89   /// \code
     90   /// struct Geometry::Point;
     91   /// \endcode
     92   bool SuppressTagKeyword : 1;
     93 
     94   /// \brief When true, include the body of a tag definition.
     95   ///
     96   /// This is used to place the definition of a struct
     97   /// in the middle of another declaration as with:
     98   ///
     99   /// \code
    100   /// typedef struct { int x, y; } Point;
    101   /// \endcode
    102   bool IncludeTagDefinition : 1;
    103 
    104   /// \brief Suppresses printing of scope specifiers.
    105   bool SuppressScope : 1;
    106 
    107   /// \brief Suppress printing parts of scope specifiers that don't need
    108   /// to be written, e.g., for inline or anonymous namespaces.
    109   bool SuppressUnwrittenScope : 1;
    110 
    111   /// \brief Suppress printing of variable initializers.
    112   ///
    113   /// This flag is used when printing the loop variable in a for-range
    114   /// statement. For example, given:
    115   ///
    116   /// \code
    117   /// for (auto x : coll)
    118   /// \endcode
    119   ///
    120   /// SuppressInitializers will be true when printing "auto x", so that the
    121   /// internal initializer constructed for x will not be printed.
    122   bool SuppressInitializers : 1;
    123 
    124   /// \brief Whether we should print the sizes of constant array expressions
    125   /// as written in the sources.
    126   ///
    127   /// This flag determines whether array types declared as
    128   ///
    129   /// \code
    130   /// int a[4+10*10];
    131   /// char a[] = "A string";
    132   /// \endcode
    133   ///
    134   /// will be printed as written or as follows:
    135   ///
    136   /// \code
    137   /// int a[104];
    138   /// char a[9] = "A string";
    139   /// \endcode
    140   bool ConstantArraySizeAsWritten : 1;
    141 
    142   /// \brief When printing an anonymous tag name, also print the location of
    143   /// that entity (e.g., "enum <anonymous at t.h:10:5>"). Otherwise, just
    144   /// prints "(anonymous)" for the name.
    145   bool AnonymousTagLocations : 1;
    146 
    147   /// \brief When true, suppress printing of the __strong lifetime qualifier in
    148   /// ARC.
    149   unsigned SuppressStrongLifetime : 1;
    150 
    151   /// \brief When true, suppress printing of lifetime qualifier in
    152   /// ARC.
    153   unsigned SuppressLifetimeQualifiers : 1;
    154 
    155   /// When true, suppresses printing template arguments in names of C++
    156   /// constructors.
    157   unsigned SuppressTemplateArgsInCXXConstructors : 1;
    158 
    159   /// \brief Whether we can use 'bool' rather than '_Bool' (even if the language
    160   /// doesn't actually have 'bool', because, e.g., it is defined as a macro).
    161   unsigned Bool : 1;
    162 
    163   /// \brief Whether we can use 'restrict' rather than '__restrict'.
    164   unsigned Restrict : 1;
    165 
    166   /// \brief Whether we can use 'alignof' rather than '__alignof'.
    167   unsigned Alignof : 1;
    168 
    169   /// \brief Whether we can use '_Alignof' rather than '__alignof'.
    170   unsigned UnderscoreAlignof : 1;
    171 
    172   /// \brief Whether we should use '(void)' rather than '()' for a function
    173   /// prototype with zero parameters.
    174   unsigned UseVoidForZeroParams : 1;
    175 
    176   /// \brief Provide a 'terse' output.
    177   ///
    178   /// For example, in this mode we don't print function bodies, class members,
    179   /// declarations inside namespaces etc.  Effectively, this should print
    180   /// only the requested declaration.
    181   unsigned TerseOutput : 1;
    182 
    183   /// \brief When true, do certain refinement needed for producing proper
    184   /// declaration tag; such as, do not print attributes attached to the declaration.
    185   ///
    186   unsigned PolishForDeclaration : 1;
    187 
    188   /// \brief When true, print the half-precision floating-point type as 'half'
    189   /// instead of '__fp16'
    190   unsigned Half : 1;
    191 
    192   /// \brief When true, print the built-in wchar_t type as __wchar_t. For use in
    193   /// Microsoft mode when wchar_t is not available.
    194   unsigned MSWChar : 1;
    195 
    196   /// \brief When true, include newlines after statements like "break", etc.
    197   unsigned IncludeNewlines : 1;
    198 
    199   /// \brief Use whitespace and punctuation like MSVC does. In particular, this
    200   /// prints anonymous namespaces as `anonymous namespace' and does not insert
    201   /// spaces after template arguments.
    202   bool MSVCFormatting : 1;
    203 };
    204 
    205 } // end namespace clang
    206 
    207 #endif
    208