clang  14.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. More...
 
void registerForPreprocessor (Preprocessor &PP)
 Register the necessary callbacks to the Preprocessor to record the expansion events and the generated tokens. More...
 
Optional< StringRef > getExpandedText (SourceLocation MacroExpansionLoc) const
 
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 85 of file MacroExpansionContext.cpp.

Member Function Documentation

◆ dumpExpandedTexts()

void MacroExpansionContext::dumpExpandedTexts ( ) const

Definition at line 137 of file MacroExpansionContext.cpp.

References dumpExpandedTextsToStream().

◆ dumpExpandedTextsToStream()

void MacroExpansionContext::dumpExpandedTextsToStream ( raw_ostream &  OS) const

Definition at line 159 of file MacroExpansionContext.cpp.

Referenced by dumpExpandedTexts().

◆ dumpExpansionRanges()

void MacroExpansionContext::dumpExpansionRanges ( ) const

Definition at line 134 of file MacroExpansionContext.cpp.

References dumpExpansionRangesToStream().

◆ dumpExpansionRangesToStream()

void MacroExpansionContext::dumpExpansionRangesToStream ( raw_ostream &  OS) const

Definition at line 141 of file MacroExpansionContext.cpp.

Referenced by dumpExpansionRanges().

◆ getExpandedText()

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 llvm::None.

Definition at line 100 of file MacroExpansionContext.cpp.

References clang::SourceLocation::isMacroID().

Referenced by getExpandedMacro().

◆ getOriginalText()

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 llvm::None.

Definition at line 118 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 88 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 103 of file MacroExpansionContext.h.


The documentation for this class was generated from the following files:
x
IRgen optimization opportunities The common pattern of short x
Definition: README.txt:7