Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:40

0001 //===--- PrettyPrinter.h - Classes for aiding with AST printing -*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 //  This file defines helper types for AST pretty-printing.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_CLANG_AST_PRETTYPRINTER_H
0014 #define LLVM_CLANG_AST_PRETTYPRINTER_H
0015 
0016 #include "clang/Basic/LLVM.h"
0017 #include "clang/Basic/LangOptions.h"
0018 
0019 namespace clang {
0020 
0021 class DeclContext;
0022 class LangOptions;
0023 class Stmt;
0024 
0025 class PrinterHelper {
0026 public:
0027   virtual ~PrinterHelper();
0028   virtual bool handledStmt(Stmt* E, raw_ostream& OS) = 0;
0029 };
0030 
0031 /// Callbacks to use to customize the behavior of the pretty-printer.
0032 class PrintingCallbacks {
0033 protected:
0034   ~PrintingCallbacks() = default;
0035 
0036 public:
0037   /// Remap a path to a form suitable for printing.
0038   virtual std::string remapPath(StringRef Path) const {
0039     return std::string(Path);
0040   }
0041 
0042   /// When printing type to be inserted into code in specific context, this
0043   /// callback can be used to avoid printing the redundant part of the
0044   /// qualifier. For example, when inserting code inside namespace foo, we
0045   /// should print bar::SomeType instead of foo::bar::SomeType.
0046   /// To do this, shouldPrintScope should return true on "foo" NamespaceDecl.
0047   /// The printing stops at the first isScopeVisible() == true, so there will
0048   /// be no calls with outer scopes.
0049   virtual bool isScopeVisible(const DeclContext *DC) const { return false; }
0050 };
0051 
0052 /// Describes how types, statements, expressions, and declarations should be
0053 /// printed.
0054 ///
0055 /// This type is intended to be small and suitable for passing by value.
0056 /// It is very frequently copied.
0057 struct PrintingPolicy {
0058   enum SuppressInlineNamespaceMode : uint8_t { None, Redundant, All };
0059 
0060   /// Create a default printing policy for the specified language.
0061   PrintingPolicy(const LangOptions &LO)
0062       : Indentation(2), SuppressSpecifiers(false),
0063         SuppressTagKeyword(LO.CPlusPlus), IncludeTagDefinition(false),
0064         SuppressScope(false), SuppressUnwrittenScope(false),
0065         SuppressInlineNamespace(SuppressInlineNamespaceMode::Redundant),
0066         SuppressElaboration(false), SuppressInitializers(false),
0067         ConstantArraySizeAsWritten(false), AnonymousTagLocations(true),
0068         SuppressStrongLifetime(false), SuppressLifetimeQualifiers(false),
0069         SuppressTemplateArgsInCXXConstructors(false),
0070         SuppressDefaultTemplateArgs(true), Bool(LO.Bool),
0071         Nullptr(LO.CPlusPlus11 || LO.C23), NullptrTypeInNamespace(LO.CPlusPlus),
0072         Restrict(LO.C99), Alignof(LO.CPlusPlus11), UnderscoreAlignof(LO.C11),
0073         UseVoidForZeroParams(!LO.CPlusPlus),
0074         SplitTemplateClosers(!LO.CPlusPlus11), TerseOutput(false),
0075         PolishForDeclaration(false), Half(LO.Half),
0076         MSWChar(LO.MicrosoftExt && !LO.WChar), IncludeNewlines(true),
0077         MSVCFormatting(false), ConstantsAsWritten(false),
0078         SuppressImplicitBase(false), FullyQualifiedName(false),
0079         PrintCanonicalTypes(false), PrintInjectedClassNameWithArguments(true),
0080         UsePreferredNames(true), AlwaysIncludeTypeForTemplateArgument(false),
0081         CleanUglifiedParameters(false), EntireContentsOfLargeArray(true),
0082         UseEnumerators(true), UseHLSLTypes(LO.HLSL) {}
0083 
0084   /// Adjust this printing policy for cases where it's known that we're
0085   /// printing C++ code (for instance, if AST dumping reaches a C++-only
0086   /// construct). This should not be used if a real LangOptions object is
0087   /// available.
0088   void adjustForCPlusPlus() {
0089     SuppressTagKeyword = true;
0090     Bool = true;
0091     UseVoidForZeroParams = false;
0092   }
0093 
0094   /// The number of spaces to use to indent each line.
0095   unsigned Indentation : 8;
0096 
0097   /// Whether we should suppress printing of the actual specifiers for
0098   /// the given type or declaration.
0099   ///
0100   /// This flag is only used when we are printing declarators beyond
0101   /// the first declarator within a declaration group. For example, given:
0102   ///
0103   /// \code
0104   /// const int *x, *y;
0105   /// \endcode
0106   ///
0107   /// SuppressSpecifiers will be false when printing the
0108   /// declaration for "x", so that we will print "int *x"; it will be
0109   /// \c true when we print "y", so that we suppress printing the
0110   /// "const int" type specifier and instead only print the "*y".
0111   LLVM_PREFERRED_TYPE(bool)
0112   unsigned SuppressSpecifiers : 1;
0113 
0114   /// Whether type printing should skip printing the tag keyword.
0115   ///
0116   /// This is used when printing the inner type of elaborated types,
0117   /// (as the tag keyword is part of the elaborated type):
0118   ///
0119   /// \code
0120   /// struct Geometry::Point;
0121   /// \endcode
0122   LLVM_PREFERRED_TYPE(bool)
0123   unsigned SuppressTagKeyword : 1;
0124 
0125   /// When true, include the body of a tag definition.
0126   ///
0127   /// This is used to place the definition of a struct
0128   /// in the middle of another declaration as with:
0129   ///
0130   /// \code
0131   /// typedef struct { int x, y; } Point;
0132   /// \endcode
0133   LLVM_PREFERRED_TYPE(bool)
0134   unsigned IncludeTagDefinition : 1;
0135 
0136   /// Suppresses printing of scope specifiers.
0137   LLVM_PREFERRED_TYPE(bool)
0138   unsigned SuppressScope : 1;
0139 
0140   /// Suppress printing parts of scope specifiers that are never
0141   /// written, e.g., for anonymous namespaces.
0142   LLVM_PREFERRED_TYPE(bool)
0143   unsigned SuppressUnwrittenScope : 1;
0144 
0145   /// Suppress printing parts of scope specifiers that correspond
0146   /// to inline namespaces.
0147   /// If Redudant, where the name is unambiguous with the specifier removed.
0148   /// If All, even if the name is ambiguous with the specifier
0149   /// removed.
0150   LLVM_PREFERRED_TYPE(SuppressInlineNamespaceMode)
0151   unsigned SuppressInlineNamespace : 2;
0152 
0153   /// Ignore qualifiers and tag keywords as specified by elaborated type sugar,
0154   /// instead letting the underlying type print as normal.
0155   LLVM_PREFERRED_TYPE(bool)
0156   unsigned SuppressElaboration : 1;
0157 
0158   /// Suppress printing of variable initializers.
0159   ///
0160   /// This flag is used when printing the loop variable in a for-range
0161   /// statement. For example, given:
0162   ///
0163   /// \code
0164   /// for (auto x : coll)
0165   /// \endcode
0166   ///
0167   /// SuppressInitializers will be true when printing "auto x", so that the
0168   /// internal initializer constructed for x will not be printed.
0169   LLVM_PREFERRED_TYPE(bool)
0170   unsigned SuppressInitializers : 1;
0171 
0172   /// Whether we should print the sizes of constant array expressions as written
0173   /// in the sources.
0174   ///
0175   /// This flag determines whether array types declared as
0176   ///
0177   /// \code
0178   /// int a[4+10*10];
0179   /// char a[] = "A string";
0180   /// \endcode
0181   ///
0182   /// will be printed as written or as follows:
0183   ///
0184   /// \code
0185   /// int a[104];
0186   /// char a[9] = "A string";
0187   /// \endcode
0188   LLVM_PREFERRED_TYPE(bool)
0189   unsigned ConstantArraySizeAsWritten : 1;
0190 
0191   /// When printing an anonymous tag name, also print the location of that
0192   /// entity (e.g., "enum <anonymous at t.h:10:5>"). Otherwise, just prints
0193   /// "(anonymous)" for the name.
0194   LLVM_PREFERRED_TYPE(bool)
0195   unsigned AnonymousTagLocations : 1;
0196 
0197   /// When true, suppress printing of the __strong lifetime qualifier in ARC.
0198   LLVM_PREFERRED_TYPE(bool)
0199   unsigned SuppressStrongLifetime : 1;
0200 
0201   /// When true, suppress printing of lifetime qualifier in ARC.
0202   LLVM_PREFERRED_TYPE(bool)
0203   unsigned SuppressLifetimeQualifiers : 1;
0204 
0205   /// When true, suppresses printing template arguments in names of C++
0206   /// constructors.
0207   LLVM_PREFERRED_TYPE(bool)
0208   unsigned SuppressTemplateArgsInCXXConstructors : 1;
0209 
0210   /// When true, attempt to suppress template arguments that match the default
0211   /// argument for the parameter.
0212   LLVM_PREFERRED_TYPE(bool)
0213   unsigned SuppressDefaultTemplateArgs : 1;
0214 
0215   /// Whether we can use 'bool' rather than '_Bool' (even if the language
0216   /// doesn't actually have 'bool', because, e.g., it is defined as a macro).
0217   LLVM_PREFERRED_TYPE(bool)
0218   unsigned Bool : 1;
0219 
0220   /// Whether we should use 'nullptr' rather than '0' as a null pointer
0221   /// constant.
0222   LLVM_PREFERRED_TYPE(bool)
0223   unsigned Nullptr : 1;
0224 
0225   /// Whether 'nullptr_t' is in namespace 'std' or not.
0226   LLVM_PREFERRED_TYPE(bool)
0227   unsigned NullptrTypeInNamespace : 1;
0228 
0229   /// Whether we can use 'restrict' rather than '__restrict'.
0230   LLVM_PREFERRED_TYPE(bool)
0231   unsigned Restrict : 1;
0232 
0233   /// Whether we can use 'alignof' rather than '__alignof'.
0234   LLVM_PREFERRED_TYPE(bool)
0235   unsigned Alignof : 1;
0236 
0237   /// Whether we can use '_Alignof' rather than '__alignof'.
0238   LLVM_PREFERRED_TYPE(bool)
0239   unsigned UnderscoreAlignof : 1;
0240 
0241   /// Whether we should use '(void)' rather than '()' for a function prototype
0242   /// with zero parameters.
0243   LLVM_PREFERRED_TYPE(bool)
0244   unsigned UseVoidForZeroParams : 1;
0245 
0246   /// Whether nested templates must be closed like 'a\<b\<c\> \>' rather than
0247   /// 'a\<b\<c\>\>'.
0248   LLVM_PREFERRED_TYPE(bool)
0249   unsigned SplitTemplateClosers : 1;
0250 
0251   /// Provide a 'terse' output.
0252   ///
0253   /// For example, in this mode we don't print function bodies, class members,
0254   /// declarations inside namespaces etc.  Effectively, this should print
0255   /// only the requested declaration.
0256   LLVM_PREFERRED_TYPE(bool)
0257   unsigned TerseOutput : 1;
0258 
0259   /// When true, do certain refinement needed for producing proper declaration
0260   /// tag; such as, do not print attributes attached to the declaration.
0261   ///
0262   LLVM_PREFERRED_TYPE(bool)
0263   unsigned PolishForDeclaration : 1;
0264 
0265   /// When true, print the half-precision floating-point type as 'half'
0266   /// instead of '__fp16'
0267   LLVM_PREFERRED_TYPE(bool)
0268   unsigned Half : 1;
0269 
0270   /// When true, print the built-in wchar_t type as __wchar_t. For use in
0271   /// Microsoft mode when wchar_t is not available.
0272   LLVM_PREFERRED_TYPE(bool)
0273   unsigned MSWChar : 1;
0274 
0275   /// When true, include newlines after statements like "break", etc.
0276   LLVM_PREFERRED_TYPE(bool)
0277   unsigned IncludeNewlines : 1;
0278 
0279   /// Use whitespace and punctuation like MSVC does. In particular, this prints
0280   /// anonymous namespaces as `anonymous namespace' and does not insert spaces
0281   /// after template arguments.
0282   LLVM_PREFERRED_TYPE(bool)
0283   unsigned MSVCFormatting : 1;
0284 
0285   /// Whether we should print the constant expressions as written in the
0286   /// sources.
0287   ///
0288   /// This flag determines whether constants expressions like
0289   ///
0290   /// \code
0291   /// 0x10
0292   /// 2.5e3
0293   /// \endcode
0294   ///
0295   /// will be printed as written or as follows:
0296   ///
0297   /// \code
0298   /// 0x10
0299   /// 2.5e3
0300   /// \endcode
0301   LLVM_PREFERRED_TYPE(bool)
0302   unsigned ConstantsAsWritten : 1;
0303 
0304   /// When true, don't print the implicit 'self' or 'this' expressions.
0305   LLVM_PREFERRED_TYPE(bool)
0306   unsigned SuppressImplicitBase : 1;
0307 
0308   /// When true, print the fully qualified name of function declarations.
0309   /// This is the opposite of SuppressScope and thus overrules it.
0310   LLVM_PREFERRED_TYPE(bool)
0311   unsigned FullyQualifiedName : 1;
0312 
0313   /// Whether to print types as written or canonically.
0314   LLVM_PREFERRED_TYPE(bool)
0315   unsigned PrintCanonicalTypes : 1;
0316 
0317   /// Whether to print an InjectedClassNameType with template arguments or as
0318   /// written. When a template argument is unnamed, printing it results in
0319   /// invalid C++ code.
0320   LLVM_PREFERRED_TYPE(bool)
0321   unsigned PrintInjectedClassNameWithArguments : 1;
0322 
0323   /// Whether to use C++ template preferred_name attributes when printing
0324   /// templates.
0325   LLVM_PREFERRED_TYPE(bool)
0326   unsigned UsePreferredNames : 1;
0327 
0328   /// Whether to use type suffixes (eg: 1U) on integral non-type template
0329   /// parameters.
0330   LLVM_PREFERRED_TYPE(bool)
0331   unsigned AlwaysIncludeTypeForTemplateArgument : 1;
0332 
0333   /// Whether to strip underscores when printing reserved parameter names.
0334   /// e.g. std::vector<class _Tp> becomes std::vector<class Tp>.
0335   /// This only affects parameter names, and so describes a compatible API.
0336   LLVM_PREFERRED_TYPE(bool)
0337   unsigned CleanUglifiedParameters : 1;
0338 
0339   /// Whether to print the entire array initializers, especially on non-type
0340   /// template parameters, no matter how many elements there are.
0341   LLVM_PREFERRED_TYPE(bool)
0342   unsigned EntireContentsOfLargeArray : 1;
0343 
0344   /// Whether to print enumerator non-type template parameters with a matching
0345   /// enumerator name or via cast of an integer.
0346   LLVM_PREFERRED_TYPE(bool)
0347   unsigned UseEnumerators : 1;
0348 
0349   /// Whether or not we're printing known HLSL code and should print HLSL
0350   /// sugared types when possible.
0351   LLVM_PREFERRED_TYPE(bool)
0352   unsigned UseHLSLTypes : 1;
0353 
0354   /// Callbacks to use to allow the behavior of printing to be customized.
0355   const PrintingCallbacks *Callbacks = nullptr;
0356 };
0357 
0358 } // end namespace clang
0359 
0360 #endif