clang 19.0.0git
Classes | Public Types | Public Member Functions | List of all members
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 = llvm::ArrayRef< llvm::SmallVector< FormatToken *, 8 > >
 

Public Member Functions

 MacroExpander (const std::vector< std::string > &Macros, clang::SourceManager &SourceMgr, const FormatStyle &Style, llvm::SpecificBumpPtrAllocator< FormatToken > &Allocator, IdentifierTable &IdentTable)
 Construct a macro expander from a set of macro definitions.
 
 ~MacroExpander ()
 
bool defined (llvm::StringRef Name) const
 Returns whether any macro Name is defined, regardless of overloads.
 
bool objectLike (llvm::StringRef Name) const
 Returns whetherh there is an object-like overload, i.e.
 
bool hasArity (llvm::StringRef Name, unsigned Arity) const
 Returns whether macro Name provides an overload with the given arity.
 
llvm::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:

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

Definition at line 82 of file Macros.h.

Constructor & Destructor Documentation

◆ MacroExpander()

clang::format::MacroExpander::MacroExpander ( const std::vector< std::string > &  Macros,
clang::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 121 of file MacroExpander.cpp.

◆ ~MacroExpander()

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

Member Function Documentation

◆ defined()

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

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

Definition at line 153 of file MacroExpander.cpp.

◆ expand()

llvm::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 167 of file MacroExpander.cpp.

References clang::format::MacroExpander::Definition::ArgMap, clang::format::MacroExpander::Definition::Body, clang::format::FormatToken::copyFrom(), hasArity(), ID, clang::format::FormatToken::MacroCtx, clang::format::MR_ExpandedArg, clang::format::MR_Hidden, objectLike(), and clang::Result.

◆ hasArity()

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

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

Definition at line 161 of file MacroExpander.cpp.

Referenced by expand().

◆ objectLike()

bool clang::format::MacroExpander::objectLike ( llvm::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 157 of file MacroExpander.cpp.

Referenced by expand().


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