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_PRETTY_PRINTER_H
     15 #define LLVM_CLANG_AST_PRETTY_PRINTER_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 struct PrintingPolicy {
     36   /// \brief Create a default printing policy for C.
     37   PrintingPolicy(const LangOptions &LO)
     38     : LangOpts(LO), Indentation(2), SuppressSpecifiers(false),
     39       SuppressTagKeyword(false), SuppressTag(false), SuppressScope(false),
     40       SuppressUnwrittenScope(false), SuppressInitializers(false),
     41       ConstantArraySizeAsWritten(false), AnonymousTagLocations(true),
     42       SuppressStrongLifetime(false), Bool(LO.Bool),
     43       TerseOutput(false), PolishForDeclaration(false),
     44       MSWChar(LO.MicrosoftExt && !LO.WChar) { }
     45 
     46   /// \brief What language we're printing.
     47   LangOptions LangOpts;
     48 
     49   /// \brief The number of spaces to use to indent each line.
     50   unsigned Indentation : 8;
     51 
     52   /// \brief Whether we should suppress printing of the actual specifiers for
     53   /// the given type or declaration.
     54   ///
     55   /// This flag is only used when we are printing declarators beyond
     56   /// the first declarator within a declaration group. For example, given:
     57   ///
     58   /// \code
     59   /// const int *x, *y;
     60   /// \endcode
     61   ///
     62   /// SuppressSpecifiers will be false when printing the
     63   /// declaration for "x", so that we will print "int *x"; it will be
     64   /// \c true when we print "y", so that we suppress printing the
     65   /// "const int" type specifier and instead only print the "*y".
     66   bool SuppressSpecifiers : 1;
     67 
     68   /// \brief Whether type printing should skip printing the tag keyword.
     69   ///
     70   /// This is used when printing the inner type of elaborated types,
     71   /// (as the tag keyword is part of the elaborated type):
     72   ///
     73   /// \code
     74   /// struct Geometry::Point;
     75   /// \endcode
     76   bool SuppressTagKeyword : 1;
     77 
     78   /// \brief Whether type printing should skip printing the actual tag type.
     79   ///
     80   /// This is used when the caller needs to print a tag definition in front
     81   /// of the type, as in constructs like the following:
     82   ///
     83   /// \code
     84   /// typedef struct { int x, y; } Point;
     85   /// \endcode
     86   bool SuppressTag : 1;
     87 
     88   /// \brief Suppresses printing of scope specifiers.
     89   bool SuppressScope : 1;
     90 
     91   /// \brief Suppress printing parts of scope specifiers that don't need
     92   /// to be written, e.g., for inline or anonymous namespaces.
     93   bool SuppressUnwrittenScope : 1;
     94 
     95   /// \brief Suppress printing of variable initializers.
     96   ///
     97   /// This flag is used when printing the loop variable in a for-range
     98   /// statement. For example, given:
     99   ///
    100   /// \code
    101   /// for (auto x : coll)
    102   /// \endcode
    103   ///
    104   /// SuppressInitializers will be true when printing "auto x", so that the
    105   /// internal initializer constructed for x will not be printed.
    106   bool SuppressInitializers : 1;
    107 
    108   /// \brief Whether we should print the sizes of constant array expressions
    109   /// as written in the sources.
    110   ///
    111   /// This flag is determines whether arrays types declared as
    112   ///
    113   /// \code
    114   /// int a[4+10*10];
    115   /// char a[] = "A string";
    116   /// \endcode
    117   ///
    118   /// will be printed as written or as follows:
    119   ///
    120   /// \code
    121   /// int a[104];
    122   /// char a[9] = "A string";
    123   /// \endcode
    124   bool ConstantArraySizeAsWritten : 1;
    125 
    126   /// \brief When printing an anonymous tag name, also print the location of
    127   /// that entity (e.g., "enum <anonymous at t.h:10:5>"). Otherwise, just
    128   /// prints "<anonymous>" for the name.
    129   bool AnonymousTagLocations : 1;
    130 
    131   /// \brief When true, suppress printing of the __strong lifetime qualifier in
    132   /// ARC.
    133   unsigned SuppressStrongLifetime : 1;
    134 
    135   /// \brief Whether we can use 'bool' rather than '_Bool', even if the language
    136   /// doesn't actually have 'bool' (because, e.g., it is defined as a macro).
    137   unsigned Bool : 1;
    138 
    139   /// \brief Provide a 'terse' output.
    140   ///
    141   /// For example, in this mode we don't print function bodies, class members,
    142   /// declarations inside namespaces etc.  Effectively, this should print
    143   /// only the requested declaration.
    144   unsigned TerseOutput : 1;
    145 
    146   /// \brief When true, do certain refinement needed for producing proper
    147   /// declaration tag; such as, do not print attributes attached to the declaration.
    148   ///
    149   unsigned PolishForDeclaration : 1;
    150 
    151   /// \brief When true, print the built-in wchar_t type as __wchar_t. For use in
    152   /// Microsoft mode when wchar_t is not available.
    153   unsigned MSWChar : 1;
    154 };
    155 
    156 } // end namespace clang
    157 
    158 #endif
    159