diff options
author | Richard Trieu <rtrieu@google.com> | 2015-08-12 18:24:59 +0000 |
---|---|---|
committer | Richard Trieu <rtrieu@google.com> | 2015-08-12 18:24:59 +0000 |
commit | 2c6990c424e1418dbf151417a708a493b21de093 (patch) | |
tree | 61822ed460d024c71105c282f0fa7a2a967972df /lib/Frontend/DiagnosticRenderer.cpp | |
parent | ba4c8c85a8e2d9e3ab7b8bf7b5efc801660903f9 (diff) |
Stop printing macro backtraces that don't help diagnostics.
When displaying the macro backtrace, ignore some of the backtraces that do not
provide extra information to the diagnostic. Typically, if the problem is
entirely contained within a macro argument, the macro expansion is often not
needed. Also take into account SourceRange's attached to the diagnostic when
selecting which backtraces to ignore. Two previous test cases have also been
updated.
Patch by Zhengkai Wu, with minor formatting fixes.
Differential Revision: http://reviews.llvm.org/D11778
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@244788 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Frontend/DiagnosticRenderer.cpp')
-rw-r--r-- | lib/Frontend/DiagnosticRenderer.cpp | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/lib/Frontend/DiagnosticRenderer.cpp b/lib/Frontend/DiagnosticRenderer.cpp index 46bef1937a..393d379048 100644 --- a/lib/Frontend/DiagnosticRenderer.cpp +++ b/lib/Frontend/DiagnosticRenderer.cpp @@ -423,6 +423,38 @@ void DiagnosticRenderer::emitSingleMacroExpansion( SpellingRanges, None, &SM); } +static bool checkRangeForMacroArgExpansion(CharSourceRange Range, + const SourceManager &SM) { + SourceLocation BegLoc = Range.getBegin(), EndLoc = Range.getEnd(); + while (BegLoc != EndLoc) { + if (!SM.isMacroArgExpansion(BegLoc)) + return false; + BegLoc.getLocWithOffset(1); + } + + return SM.isMacroArgExpansion(BegLoc); +} + +/// A helper function to check if the current ranges are all inside +/// the macro expansions. +static bool checkRangesForMacroArgExpansion(SourceLocation Loc, + ArrayRef<CharSourceRange> Ranges, + const SourceManager &SM) { + assert(Loc.isMacroID() && "Must be a macro expansion!"); + + SmallVector<CharSourceRange, 4> SpellingRanges; + mapDiagnosticRanges(Loc, Ranges, SpellingRanges, &SM); + + if (!SM.isMacroArgExpansion(Loc)) + return false; + + for (auto I = SpellingRanges.begin(), E = SpellingRanges.end(); I != E; ++I) + if (!checkRangeForMacroArgExpansion(*I, SM)) + return false; + + return true; +} + /// \brief Recursively emit notes for each macro expansion and caret /// diagnostics where appropriate. /// @@ -443,12 +475,19 @@ void DiagnosticRenderer::emitMacroExpansions(SourceLocation Loc, // Produce a stack of macro backtraces. SmallVector<SourceLocation, 8> LocationStack; + unsigned IgnoredEnd = 0; while (Loc.isMacroID()) { LocationStack.push_back(Loc); + if (checkRangesForMacroArgExpansion(Loc, Ranges, SM)) + IgnoredEnd = LocationStack.size(); + Loc = SM.getImmediateMacroCallerLoc(Loc); assert(!Loc.isInvalid() && "must have a valid source location here"); } + LocationStack.erase(LocationStack.begin(), + LocationStack.begin() + IgnoredEnd); + unsigned MacroDepth = LocationStack.size(); unsigned MacroLimit = DiagOpts->MacroBacktraceLimit; if (MacroDepth <= MacroLimit || MacroLimit == 0) { |