diff options
author | Anna Zaks <ganna@apple.com> | 2012-01-18 20:17:16 +0000 |
---|---|---|
committer | Anna Zaks <ganna@apple.com> | 2012-01-18 20:17:16 +0000 |
commit | c2a8d6cee01fc4845f5409bf5c021a64616ac8c3 (patch) | |
tree | 3d3df55ff0e799e7da1d02e17c89c622ac1fe9a5 /lib/Lex/Lexer.cpp | |
parent | 556b1d0f3a039a691ed4f6dd91b8587435f30b0b (diff) |
Refactor: Pull getImmediateMacroName() out of DiagnosticRenderer and
into Lexer and Preprocessor; making it widely available.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@148410 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Lex/Lexer.cpp')
-rw-r--r-- | lib/Lex/Lexer.cpp | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/lib/Lex/Lexer.cpp b/lib/Lex/Lexer.cpp index c320b3c222..bb1bbbbe7e 100644 --- a/lib/Lex/Lexer.cpp +++ b/lib/Lex/Lexer.cpp @@ -785,6 +785,27 @@ bool Lexer::isAtEndOfMacroExpansion(SourceLocation loc, return isAtEndOfMacroExpansion(expansionLoc, SM, LangOpts); } +StringRef Lexer::getImmediateMacroName(SourceLocation Loc, + const SourceManager &SM, + const LangOptions &LangOpts) { + assert(Loc.isMacroID() && "Only reasonble to call this on macros"); + // Walk past macro argument expanions. + while (SM.isMacroArgExpansion(Loc)) + Loc = SM.getImmediateExpansionRange(Loc).first; + + // Find the spelling location of the start of the non-argument expansion + // range. This is where the macro name was spelled in order to begin + // expanding this macro. + Loc = SM.getSpellingLoc(SM.getImmediateExpansionRange(Loc).first); + + // Dig out the buffer where the macro name was spelled and the extents of the + // name so that we can render it into the expansion note. + std::pair<FileID, unsigned> ExpansionInfo = SM.getDecomposedLoc(Loc); + unsigned MacroTokenLength = Lexer::MeasureTokenLength(Loc, SM, LangOpts); + StringRef ExpansionBuffer = SM.getBufferData(ExpansionInfo.first); + return ExpansionBuffer.substr(ExpansionInfo.second, MacroTokenLength); +} + //===----------------------------------------------------------------------===// // Character information. //===----------------------------------------------------------------------===// |