summaryrefslogtreecommitdiffstats
path: root/lib/Frontend/TextDiagnostic.cpp
diff options
context:
space:
mode:
authorChandler Carruth <chandlerc@gmail.com>2011-10-16 09:39:09 +0000
committerChandler Carruth <chandlerc@gmail.com>2011-10-16 09:39:09 +0000
commitfe627b0e16e9a735891505325b670d0d3c51d2b9 (patch)
tree11d3f09a3acfecbca4aed0b356f54d4ffe20ac8d /lib/Frontend/TextDiagnostic.cpp
parenta47129e97be2379961046fac4f94f12ac15cb540 (diff)
Hoist the logic I added to compute the macro name into a helper
function. No functionality changed. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@142128 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Frontend/TextDiagnostic.cpp')
-rw-r--r--lib/Frontend/TextDiagnostic.cpp46
1 files changed, 30 insertions, 16 deletions
diff --git a/lib/Frontend/TextDiagnostic.cpp b/lib/Frontend/TextDiagnostic.cpp
index 8dab0afcac..8b901ae310 100644
--- a/lib/Frontend/TextDiagnostic.cpp
+++ b/lib/Frontend/TextDiagnostic.cpp
@@ -389,6 +389,35 @@ static bool printWordWrapped(raw_ostream &OS, StringRef Str,
return Wrapped;
}
+/// \brief Retrieve the name of the immediate macro expansion.
+///
+/// This routine starts from a source location, and finds the name of the macro
+/// responsible for its immediate expansion. It looks through any intervening
+/// macro argument expansions to compute this. It returns a StringRef which
+/// refers to the SourceManager-owned buffer of the source where that macro
+/// name is spelled. Thus, the result shouldn't out-live that SourceManager.
+///
+static StringRef 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);
+}
+
TextDiagnostic::TextDiagnostic(raw_ostream &OS,
const SourceManager &SM,
const LangOptions &LangOpts,
@@ -729,25 +758,10 @@ void TextDiagnostic::emitMacroExpansionsAndCarets(
return;
}
- // Walk past macro argument expanions.
- while (SM.isMacroArgExpansion(MacroLoc))
- MacroLoc = SM.getImmediateExpansionRange(MacroLoc).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.
- MacroLoc = SM.getSpellingLoc(SM.getImmediateExpansionRange(MacroLoc).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(MacroLoc);
- unsigned MacroTokenLength = Lexer::MeasureTokenLength(MacroLoc, SM, LangOpts);
- StringRef ExpansionBuffer = SM.getBufferData(ExpansionInfo.first);
-
llvm::SmallString<100> MessageStorage;
llvm::raw_svector_ostream Message(MessageStorage);
Message << "expanded from macro: "
- << ExpansionBuffer.substr(ExpansionInfo.second, MacroTokenLength);
+ << getImmediateMacroName(MacroLoc, SM, LangOpts);
emitDiagnostic(SM.getSpellingLoc(Loc), DiagnosticsEngine::Note,
Message.str(),
Ranges, ArrayRef<FixItHint>());