clang 20.0.0git
Public Member Functions | Friends | List of all members
clang::MacroExpansionContext Class Reference

MacroExpansionContext tracks the macro expansions processed by the Preprocessor. More...

#include "clang/Analysis/MacroExpansionContext.h"

Public Member Functions

 MacroExpansionContext (const LangOptions &LangOpts)
 Creates a MacroExpansionContext.
 
void registerForPreprocessor (Preprocessor &PP)
 Register the necessary callbacks to the Preprocessor to record the expansion events and the generated tokens.
 
std::optional< StringRef > getExpandedText (SourceLocation MacroExpansionLoc) const
 
std::optional< StringRef > getOriginalText (SourceLocation MacroExpansionLoc) const
 
LLVM_DUMP_METHOD void dumpExpansionRangesToStream (raw_ostream &OS) const
 
LLVM_DUMP_METHOD void dumpExpandedTextsToStream (raw_ostream &OS) const
 
LLVM_DUMP_METHOD void dumpExpansionRanges () const
 
LLVM_DUMP_METHOD void dumpExpandedTexts () const
 

Friends

class detail::MacroExpansionRangeRecorder
 

Detailed Description

MacroExpansionContext tracks the macro expansions processed by the Preprocessor.

It means that it can track source locations from a single translation unit. For every macro expansion it can tell you what text will be substituted.

It was designed to deal with:

void bar();
#define retArg(x) x
#define retArgUnclosed retArg(bar()
#define BB CC
#define applyInt BB(int)
#define CC(x) retArgUnclosed
void unbalancedMacros() {
applyInt );
//^~~~~~~~~~^ is the substituted range
// Substituted text is "applyInt )"
// Expanded text is "bar()"
}
#define expandArgUnclosedCommaExpr(x) (x, bar(), 1
#define f expandArgUnclosedCommaExpr
void unbalancedMacros2() {
int x = f(f(1)) )); // Look at the parenthesis!
// ^~~~~~^ is the substituted range
// Substituted text is "f(f(1))"
// Expanded text is "((1,bar(),1,bar(),1"
}
Remarks
Currently we don't respect the whitespaces between expanded tokens, so the output for this example might differ from the -E compiler invocation.
All whitespaces are consumed while constructing the expansion. After all identifier a single space inserted to produce a valid C code even if identifier follows an other identifiers such as variable declarations.
MacroExpansionContext object must outlive the Preprocessor parameter.

Definition at line 73 of file MacroExpansionContext.h.

Constructor & Destructor Documentation

◆ MacroExpansionContext()

MacroExpansionContext::MacroExpansionContext ( const LangOptions LangOpts)
explicit

Creates a MacroExpansionContext.

Remarks
You must call registerForPreprocessor to set the required onTokenLexed callback and the PPCallbacks.

Definition at line 86 of file MacroExpansionContext.cpp.

Member Function Documentation

◆ dumpExpandedTexts()

void MacroExpansionContext::dumpExpandedTexts ( ) const

Definition at line 138 of file MacroExpansionContext.cpp.

References dumpExpandedTextsToStream().

◆ dumpExpandedTextsToStream()

void MacroExpansionContext::dumpExpandedTextsToStream ( raw_ostream &  OS) const

Definition at line 160 of file MacroExpansionContext.cpp.

Referenced by dumpExpandedTexts().

◆ dumpExpansionRanges()

void MacroExpansionContext::dumpExpansionRanges ( ) const

Definition at line 135 of file MacroExpansionContext.cpp.

References dumpExpansionRangesToStream().

◆ dumpExpansionRangesToStream()

void MacroExpansionContext::dumpExpansionRangesToStream ( raw_ostream &  OS) const

Definition at line 142 of file MacroExpansionContext.cpp.

Referenced by dumpExpansionRanges().

◆ getExpandedText()

std::optional< StringRef > MacroExpansionContext::getExpandedText ( SourceLocation  MacroExpansionLoc) const
Parameters
MacroExpansionLocMust be the expansion location of a macro.
Returns
The textual representation of the token sequence which was substituted in place of the macro after the preprocessing. If no macro was expanded at that location, returns std::nullopt.

Definition at line 101 of file MacroExpansionContext.cpp.

References clang::SourceLocation::isMacroID().

Referenced by getExpandedMacro().

◆ getOriginalText()

std::optional< StringRef > MacroExpansionContext::getOriginalText ( SourceLocation  MacroExpansionLoc) const
Parameters
MacroExpansionLocMust be the expansion location of a macro.
Returns
The text from the original source code which were substituted by the macro expansion chain from the given location. If no macro was expanded at that location, returns std::nullopt.

Definition at line 119 of file MacroExpansionContext.cpp.

References clang::CharSourceRange::getCharRange(), clang::Lexer::getSourceText(), and clang::SourceLocation::isMacroID().

◆ registerForPreprocessor()

void MacroExpansionContext::registerForPreprocessor ( Preprocessor PP)

Register the necessary callbacks to the Preprocessor to record the expansion events and the generated tokens.

Must ensure that this object outlives the given Preprocessor.

Definition at line 89 of file MacroExpansionContext.cpp.

References clang::Preprocessor::addPPCallbacks(), clang::Preprocessor::getSourceManager(), and clang::Preprocessor::setTokenWatcher().

Friends And Related Function Documentation

◆ detail::MacroExpansionRangeRecorder

Definition at line 105 of file MacroExpansionContext.h.


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