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       ConstantsAsWritten(false) { }
     55 
     56   /// \brief Adjust this printing policy for cases where it's known that
     57   /// we're printing C++ code (for instance, if AST dumping reaches a
     58   /// C++-only construct). This should not be used if a real LangOptions
     59   /// object is available.
     60   void adjustForCPlusPlus() {
     61     SuppressTagKeyword = true;
     62     Bool = true;
     63     UseVoidForZeroParams = false;
     64   }
     65 
     66   /// \brief The number of spaces to use to indent each line.
     67   unsigned Indentation : 8;
     68 
     69   /// \brief Whether we should suppress printing of the actual specifiers for
     70   /// the given type or declaration.
     71   ///
     72   /// This flag is only used when we are printing declarators beyond
     73   /// the first declarator within a declaration group. For example, given:
     74   ///
     75   /// \code
     76   /// const int *x, *y;
     77   /// \endcode
     78   ///
     79   /// SuppressSpecifiers will be false when printing the
     80   /// declaration for "x", so that we will print "int *x"; it will be
     81   /// \c true when we print "y", so that we suppress printing the
     82   /// "const int" type specifier and instead only print the "*y".
     83   bool SuppressSpecifiers : 1;
     84 
     85   /// \brief Whether type printing should skip printing the tag keyword.
     86   ///
     87   /// This is used when printing the inner type of elaborated types,
     88   /// (as the tag keyword is part of the elaborated type):
     89   ///
     90   /// \code
     91   /// struct Geometry::Point;
     92   /// \endcode
     93   bool SuppressTagKeyword : 1;
     94 
     95   /// \brief When true, include the body of a tag definition.
     96   ///
     97   /// This is used to place the definition of a struct
     98   /// in the middle of another declaration as with:
     99   ///
    100   /// \code
    101   /// typedef struct { int x, y; } Point;
    102   /// \endcode
    103   bool IncludeTagDefinition : 1;
    104 
    105   /// \brief Suppresses printing of scope specifiers.
    106   bool SuppressScope : 1;
    107 
    108   /// \brief Suppress printing parts of scope specifiers that don't need
    109   /// to be written, e.g., for inline or anonymous namespaces.
    110   bool SuppressUnwrittenScope : 1;
    111 
    112   /// \brief Suppress printing of variable initializers.
    113   ///
    114   /// This flag is used when printing the loop variable in a for-range
    115   /// statement. For example, given:
    116   ///
    117   /// \code
    118   /// for (auto x : coll)
    119   /// \endcode
    120   ///
    121   /// SuppressInitializers will be true when printing "auto x", so that the
    122   /// internal initializer constructed for x will not be printed.
    123   bool SuppressInitializers : 1;
    124 
    125   /// \brief Whether we should print the sizes of constant array expressions
    126   /// as written in the sources.
    127   ///
    128   /// This flag determines whether array types declared as
    129   ///
    130   /// \code
    131   /// int a[4+10*10];
    132   /// char a[] = "A string";
    133   /// \endcode
    134   ///
    135   /// will be printed as written or as follows:
    136   ///
    137   /// \code
    138   /// int a[104];
    139   /// char a[9] = "A string";
    140   /// \endcode
    141   bool ConstantArraySizeAsWritten : 1;
    142 
    143   /// \brief When printing an anonymous tag name, also print the location of
    144   /// that entity (e.g., "enum <anonymous at t.h:10:5>"). Otherwise, just
    145   /// prints "(anonymous)" for the name.
    146   bool AnonymousTagLocations : 1;
    147 
    148   /// \brief When true, suppress printing of the __strong lifetime qualifier in
    149   /// ARC.
    150   unsigned SuppressStrongLifetime : 1;
    151 
    152   /// \brief When true, suppress printing of lifetime qualifier in
    153   /// ARC.
    154   unsigned SuppressLifetimeQualifiers : 1;
    155 
    156   /// When true, suppresses printing template arguments in names of C++
    157   /// constructors.
    158   unsigned SuppressTemplateArgsInCXXConstructors : 1;
    159 
    160   /// \brief Whether we can use 'bool' rather than '_Bool' (even if the language
    161   /// doesn't actually have 'bool', because, e.g., it is defined as a macro).
    162   unsigned Bool : 1;
    163 
    164   /// \brief Whether we can use 'restrict' rather than '__restrict'.
    165   unsigned Restrict : 1;
    166 
    167   /// \brief Whether we can use 'alignof' rather than '__alignof'.
    168   unsigned Alignof : 1;
    169 
    170   /// \brief Whether we can use '_Alignof' rather than '__alignof'.
    171   unsigned UnderscoreAlignof : 1;
    172 
    173   /// \brief Whether we should use '(void)' rather than '()' for a function
    174   /// prototype with zero parameters.
    175   unsigned UseVoidForZeroParams : 1;
    176 
    177   /// \brief Provide a 'terse' output.
    178   ///
    179   /// For example, in this mode we don't print function bodies, class members,
    180   /// declarations inside namespaces etc.  Effectively, this should print
    181   /// only the requested declaration.
    182   unsigned TerseOutput : 1;
    183 
    184   /// \brief When true, do certain refinement needed for producing proper
    185   /// declaration tag; such as, do not print attributes attached to the declaration.
    186   ///
    187   unsigned PolishForDeclaration : 1;
    188 
    189   /// \brief When true, print the half-precision floating-point type as 'half'
    190   /// instead of '__fp16'
    191   unsigned Half : 1;
    192 
    193   /// \brief When true, print the built-in wchar_t type as __wchar_t. For use in
    194   /// Microsoft mode when wchar_t is not available.
    195   unsigned MSWChar : 1;
    196 
    197   /// \brief When true, include newlines after statements like "break", etc.
    198   unsigned IncludeNewlines : 1;
    199 
    200   /// \brief Use whitespace and punctuation like MSVC does. In particular, this
    201   /// prints anonymous namespaces as `anonymous namespace' and does not insert
    202   /// spaces after template arguments.
    203   bool MSVCFormatting : 1;
    204 
    205   /// \brief Whether we should print the constant expressions as written in the
    206   /// sources.
    207   ///
    208   /// This flag determines whether constants expressions like
    209   ///
    210   /// \code
    211   /// 0x10
    212   /// 2.5e3
    213   /// \endcode
    214   ///
    215   /// will be printed as written or as follows:
    216   ///
    217   /// \code
    218   /// 0x10
    219   /// 2.5e3
    220   /// \endcode
    221   bool ConstantsAsWritten;
    222 };
    223 
    224 } // end namespace clang
    225 
    226 #endif
    227