clang 22.0.0git
clang::format::MacroExpander Class Reference

Takes a set of macro definitions as strings and allows expanding calls to those macros. More...

#include "/home/buildbot/as-worker-4/publish-doxygen-docs/llvm-project/clang/lib/Format/Macros.h"

Classes

struct  Definition
class  DefinitionParser

Public Types

using ArgsList = ArrayRef<SmallVector<FormatToken *, 8>>

Public Member Functions

 MacroExpander (const std::vector< std::string > &Macros, SourceManager &SourceMgr, const FormatStyle &Style, llvm::SpecificBumpPtrAllocator< FormatToken > &Allocator, IdentifierTable &IdentTable)
 Construct a macro expander from a set of macro definitions.
 ~MacroExpander ()
bool defined (StringRef Name) const
 Returns whether any macro Name is defined, regardless of overloads.
bool objectLike (StringRef Name) const
 Returns whetherh there is an object-like overload, i.e.
bool hasArity (StringRef Name, unsigned Arity) const
 Returns whether macro Name provides an overload with the given arity.
SmallVector< FormatToken *, 8 > expand (FormatToken *ID, std::optional< ArgsList > OptionalArgs) const
 Returns the expanded stream of format tokens for ID, where each element in Args is a positional argument to the macro call.

Detailed Description

Takes a set of macro definitions as strings and allows expanding calls to those macros.

For example: Definition: A(x, y)=x + y Call : A(int a = 1, 2) Expansion : int a = 1 + 2

Expansion does not check arity of the definition. If fewer arguments than expected are provided, the remaining parameters are considered empty: Call : A(a) Expansion: a + If more arguments than expected are provided, they will be discarded.

The expander does not support:

  • recursive expansion
  • stringification
  • concatenation
  • variadic macros

Furthermore, only a single expansion of each macro argument is supported, so that we cannot get conflicting formatting decisions from different expansions. Definition: A(x)=x+x Call : A(id) Expansion : id+x

Definition at line 80 of file Macros.h.

Member Typedef Documentation

◆ ArgsList

Constructor & Destructor Documentation

◆ MacroExpander()

clang::format::MacroExpander::MacroExpander ( const std::vector< std::string > & Macros,
SourceManager & SourceMgr,
const FormatStyle & Style,
llvm::SpecificBumpPtrAllocator< FormatToken > & Allocator,
IdentifierTable & IdentTable )

Construct a macro expander from a set of macro definitions.

Macro definitions must be encoded as UTF-8.

Each entry in Macros must conform to the following simple macro-definition language: <definition> ::= <id> <expansion> | <id> "(" <params> ")" <expansion> <params> ::= <id-list> | "" <id-list> ::= <id> | <id> "," <params> <expansion> ::= "=" <tail> | <eof> <tail> ::= <tok> <tail> | <eof>

Macros that cannot be parsed will be silently discarded.

Definition at line 118 of file MacroExpander.cpp.

References clang::Macro, and clang::Macros.

◆ ~MacroExpander()

clang::format::MacroExpander::~MacroExpander ( )
default

References clang::Macro.

Member Function Documentation

◆ defined()

bool clang::format::MacroExpander::defined ( StringRef Name) const

Returns whether any macro Name is defined, regardless of overloads.

Definition at line 150 of file MacroExpander.cpp.

◆ expand()

SmallVector< FormatToken *, 8 > clang::format::MacroExpander::expand ( FormatToken * ID,
std::optional< ArgsList > OptionalArgs ) const

Returns the expanded stream of format tokens for ID, where each element in Args is a positional argument to the macro call.

If Args is not set, the object-like overload is used. If Args is set, the overload with the arity equal to Args.size() is used.

Definition at line 164 of file MacroExpander.cpp.

References clang::format::MacroExpander::Definition::ArgMap, clang::format::MacroExpander::Definition::Body, hasArity(), clang::format::MR_ExpandedArg, clang::format::MR_Hidden, New, objectLike(), clang::Result, and Tok.

◆ hasArity()

bool clang::format::MacroExpander::hasArity ( StringRef Name,
unsigned Arity ) const

Returns whether macro Name provides an overload with the given arity.

Definition at line 158 of file MacroExpander.cpp.

Referenced by expand().

◆ objectLike()

bool clang::format::MacroExpander::objectLike ( StringRef Name) const

Returns whetherh there is an object-like overload, i.e.

where the macro has no arguments and should not consume subsequent parentheses.

Definition at line 154 of file MacroExpander.cpp.

Referenced by expand().


The documentation for this class was generated from the following files: