class MacroExpander

Declaration

class MacroExpander { /* full declaration omitted */ };

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

Declared at: clang/lib/Format/Macros.h:86

Member Variables

private clang::SourceManager& SourceMgr
private const clang::format::FormatStyle& Style
private llvm::SpecificBumpPtrAllocator<FormatToken>& Allocator
private clang::IdentifierTable& IdentTable
private SmallVector<std::unique_ptr<llvm::MemoryBuffer>> Buffers
private llvm::StringMap<Definition> Definitions

Method Overview

  • public MacroExpander(const std::vector<std::string> & Macros, clang::SourceManager & SourceMgr, const clang::format::FormatStyle & Style, llvm::SpecificBumpPtrAllocator<FormatToken> & Allocator, clang::IdentifierTable & IdentTable)
  • public bool defined(llvm::StringRef Name) const
  • public llvm::SmallVector<FormatToken *, 8> expand(clang::format::FormatToken * ID, clang::format::MacroExpander::ArgsList Args) const
  • public bool objectLike(llvm::StringRef Name) const
  • private void parseDefinition(const std::string & Macro)
  • public ~MacroExpander()

Methods

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

Description

Construct a macro expander from a set of macro definitions. Macro definitions must be encoded as UTF-8. Each entry in \p 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.

Declared at: clang/lib/Format/Macros.h:103

Parameters

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

bool defined(llvm::StringRef Name) const

Description

Returns whether a macro \p Name is defined.

Declared at: clang/lib/Format/Macros.h:110

Parameters

llvm::StringRef Name

llvm::SmallVector<FormatToken*, 8> expand(
    clang::format::FormatToken* ID,
    clang::format::MacroExpander::ArgsList Args)
    const

Description

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

Declared at: clang/lib/Format/Macros.h:118

Parameters

clang::format::FormatToken* ID
clang::format::MacroExpander::ArgsList Args

bool objectLike(llvm::StringRef Name) const

Description

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

Declared at: clang/lib/Format/Macros.h:114

Parameters

llvm::StringRef Name

void parseDefinition(const std::string& Macro)

Declared at: clang/lib/Format/Macros.h:125

Parameters

const std::string& Macro

~MacroExpander()

Declared at: clang/lib/Format/Macros.h:107