diff options
author | Eli Friedman <eli.friedman@gmail.com> | 2012-11-03 03:36:51 +0000 |
---|---|---|
committer | Eli Friedman <eli.friedman@gmail.com> | 2012-11-03 03:36:51 +0000 |
commit | 9cb1c3de9df7d944f0c5cccaf03fa414036487eb (patch) | |
tree | 8ca10be605f7f116373d583a65a8e81ee03e4398 /lib/Frontend | |
parent | 8501b7a1c4c4a9ba0ea6cb8e500e601ef3759deb (diff) |
Add a proper algorithm to compute accurate source ranges for diagnostics with
caret locations and source ranges in macros. Makes ranges more accurate
in some cases, and fixes an assertion failure.
Fixes <rdar://problem/12472249>.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@167353 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Frontend')
-rw-r--r-- | lib/Frontend/DiagnosticRenderer.cpp | 76 | ||||
-rw-r--r-- | lib/Frontend/TextDiagnostic.cpp | 12 |
2 files changed, 62 insertions, 26 deletions
diff --git a/lib/Frontend/DiagnosticRenderer.cpp b/lib/Frontend/DiagnosticRenderer.cpp index d70a11b9c9..359b82be60 100644 --- a/lib/Frontend/DiagnosticRenderer.cpp +++ b/lib/Frontend/DiagnosticRenderer.cpp @@ -18,6 +18,7 @@ #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Support/ErrorHandling.h" +#include "llvm/ADT/SmallSet.h" #include "llvm/ADT/SmallString.h" #include <algorithm> using namespace clang; @@ -218,6 +219,53 @@ void DiagnosticRenderer::emitIncludeStackRecursively(SourceLocation Loc, emitIncludeLocation(Loc, PLoc, SM); } +// Helper function to fix up source ranges. It takes in an array of ranges, +// and outputs an array of ranges where we want to draw the range highlighting +// around the location specified by CaretLoc. +// +// To find locations which correspond to the caret, we crawl the macro caller +// chain for the beginning and end of each range. If the caret location +// is in a macro expansion, we search each chain for a location +// in the same expansion as the caret; otherwise, we crawl to the top of +// each chain. Two locations are part of the same macro expansion +// iff the FileID is the same. +static void mapDiagnosticRanges( + SourceLocation CaretLoc, + const SmallVectorImpl<CharSourceRange>& Ranges, + SmallVectorImpl<CharSourceRange>& SpellingRanges, + const SourceManager *SM) { + FileID CaretLocFileID = SM->getFileID(CaretLoc); + + for (SmallVectorImpl<CharSourceRange>::const_iterator I = Ranges.begin(), + E = Ranges.end(); + I != E; ++I) { + SourceLocation Begin = I->getBegin(), End = I->getEnd(); + bool IsTokenRange = I->isTokenRange(); + + // Search the macro caller chain for the beginning of the range. + while (Begin.isMacroID() && SM->getFileID(Begin) != CaretLocFileID) + Begin = SM->getImmediateMacroCallerLoc(Begin); + + // Search the macro caller chain for the beginning of the range. + while (End.isMacroID() && SM->getFileID(End) != CaretLocFileID) { + // The computation of the next End is an inlined version of + // getImmediateMacroCallerLoc, except it chooses the end of an + // expansion range. + if (SM->isMacroArgExpansion(End)) { + End = SM->getImmediateSpellingLoc(End); + } else { + End = SM->getImmediateExpansionRange(End).second; + } + } + + // Return the spelling location of the beginning and end of the range. + Begin = SM->getSpellingLoc(Begin); + End = SM->getSpellingLoc(End); + SpellingRanges.push_back(CharSourceRange(SourceRange(Begin, End), + IsTokenRange)); + } +} + /// \brief Recursively emit notes for each macro expansion and caret /// diagnostics where appropriate. /// @@ -245,9 +293,13 @@ void DiagnosticRenderer::emitMacroExpansionsAndCarets( // If this is a file source location, directly emit the source snippet and // caret line. Also record the macro depth reached. if (Loc.isFileID()) { + // Map the ranges. + SmallVector<CharSourceRange, 4> SpellingRanges; + mapDiagnosticRanges(Loc, Ranges, SpellingRanges, &SM); + assert(MacroDepth == 0 && "We shouldn't hit a leaf node twice!"); MacroDepth = OnMacroInst; - emitCodeContext(Loc, Level, Ranges, Hints, SM); + emitCodeContext(Loc, Level, SpellingRanges, Hints, SM); return; } // Otherwise recurse through each macro expansion layer. @@ -257,8 +309,7 @@ void DiagnosticRenderer::emitMacroExpansionsAndCarets( Loc = SM.skipToMacroArgExpansion(Loc); SourceLocation OneLevelUp = SM.getImmediateMacroCallerLoc(Loc); - - // FIXME: Map ranges? + emitMacroExpansionsAndCarets(OneLevelUp, Level, Ranges, Hints, SM, MacroDepth, OnMacroInst + 1); @@ -280,17 +331,6 @@ void DiagnosticRenderer::emitMacroExpansionsAndCarets( bool Suppressed = (OnMacroInst >= MacroSkipStart && OnMacroInst < MacroSkipEnd); - // Map the ranges. - for (SmallVectorImpl<CharSourceRange>::iterator I = Ranges.begin(), - E = Ranges.end(); - I != E; ++I) { - SourceLocation Start = I->getBegin(), End = I->getEnd(); - if (Start.isMacroID()) - I->setBegin(SM.getImmediateMacroCalleeLoc(Start)); - if (End.isMacroID()) - I->setEnd(SM.getImmediateMacroCalleeLoc(End)); - } - if (Suppressed) { // Tell the user that we've skipped contexts. if (OnMacroInst == MacroSkipStart) { @@ -303,14 +343,18 @@ void DiagnosticRenderer::emitMacroExpansionsAndCarets( } return; } - + + // Map the ranges. + SmallVector<CharSourceRange, 4> SpellingRanges; + mapDiagnosticRanges(MacroLoc, Ranges, SpellingRanges, &SM); + SmallString<100> MessageStorage; llvm::raw_svector_ostream Message(MessageStorage); Message << "expanded from macro '" << getImmediateMacroName(MacroLoc, SM, LangOpts) << "'"; emitDiagnostic(SM.getSpellingLoc(Loc), DiagnosticsEngine::Note, Message.str(), - Ranges, ArrayRef<FixItHint>(), &SM); + SpellingRanges, ArrayRef<FixItHint>(), &SM); } DiagnosticNoteRenderer::~DiagnosticNoteRenderer() {} diff --git a/lib/Frontend/TextDiagnostic.cpp b/lib/Frontend/TextDiagnostic.cpp index a421f9ac34..d622fb35f0 100644 --- a/lib/Frontend/TextDiagnostic.cpp +++ b/lib/Frontend/TextDiagnostic.cpp @@ -1049,16 +1049,8 @@ void TextDiagnostic::highlightRange(const CharSourceRange &R, const SourceManager &SM) { if (!R.isValid()) return; - SourceLocation Begin = SM.getExpansionLoc(R.getBegin()); - SourceLocation End = SM.getExpansionLoc(R.getEnd()); - - // If the End location and the start location are the same and are a macro - // location, then the range was something that came from a macro expansion - // or _Pragma. If this is an object-like macro, the best we can do is to - // highlight the range. If this is a function-like macro, we'd also like to - // highlight the arguments. - if (Begin == End && R.getEnd().isMacroID()) - End = SM.getExpansionRange(R.getEnd()).second; + SourceLocation Begin = R.getBegin(); + SourceLocation End = R.getEnd(); unsigned StartLineNo = SM.getExpansionLineNumber(Begin); if (StartLineNo > LineNo || SM.getFileID(Begin) != FID) |