Skip to content

Commit b392044

Browse files
committed
Rewrite check* functions in DiagnosticRenderer.cpp
In rangeInsideSameMacroArgExpansion, check the given Ranges instead of the SpellingRanges.
1 parent c9d6813 commit b392044

File tree

1 file changed

+27
-48
lines changed

1 file changed

+27
-48
lines changed

clang/lib/Frontend/DiagnosticRenderer.cpp

Lines changed: 27 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -454,62 +454,41 @@ void DiagnosticRenderer::emitSingleMacroExpansion(
454454
SpellingRanges, {});
455455
}
456456

457-
/// Check that the macro argument location of Loc starts with ArgumentLoc.
458-
/// The starting location of the macro expansions is used to differeniate
459-
/// different macro expansions.
460-
static bool checkLocForMacroArgExpansion(SourceLocation Loc,
461-
const SourceManager &SM,
462-
SourceLocation ArgumentLoc) {
463-
SourceLocation MacroLoc;
464-
if (SM.isMacroArgExpansion(Loc, &MacroLoc)) {
465-
if (ArgumentLoc == MacroLoc) return true;
466-
}
467-
468-
return false;
469-
}
470-
471-
/// Check if all the locations in the range have the same macro argument
472-
/// expansion, and that the expansion starts with ArgumentLoc.
473-
static bool checkRangeForMacroArgExpansion(CharSourceRange Range,
474-
const SourceManager &SM,
475-
SourceLocation ArgumentLoc) {
476-
SourceLocation BegLoc = Range.getBegin(), EndLoc = Range.getEnd();
477-
while (BegLoc != EndLoc) {
478-
if (!checkLocForMacroArgExpansion(BegLoc, SM, ArgumentLoc))
479-
return false;
480-
BegLoc.getLocWithOffset(1);
481-
}
482-
483-
return checkLocForMacroArgExpansion(BegLoc, SM, ArgumentLoc);
484-
}
485-
486457
/// A helper function to check if the current ranges are all inside the same
487458
/// macro argument expansion as Loc.
488-
static bool checkRangesForMacroArgExpansion(FullSourceLoc Loc,
489-
ArrayRef<CharSourceRange> Ranges) {
459+
static bool
460+
rangesInsideSameMacroArgExpansion(FullSourceLoc Loc,
461+
ArrayRef<CharSourceRange> Ranges) {
490462
assert(Loc.isMacroID() && "Must be a macro expansion!");
491463

492-
SmallVector<CharSourceRange, 4> SpellingRanges;
464+
SmallVector<CharSourceRange> SpellingRanges;
493465
mapDiagnosticRanges(Loc, Ranges, SpellingRanges);
494466

495-
// Count all valid ranges.
496467
unsigned ValidCount =
497468
llvm::count_if(Ranges, [](const auto &R) { return R.isValid(); });
498-
499469
if (ValidCount > SpellingRanges.size())
500470
return false;
501471

502-
// To store the source location of the argument location.
503-
FullSourceLoc ArgumentLoc;
472+
const SourceManager &SM = Loc.getManager();
473+
for (const auto &R : Ranges) {
474+
// All positions in the range need to point to Loc.
475+
SourceLocation Begin = R.getBegin();
476+
if (Begin == R.getEnd()) {
477+
if (!SM.isMacroArgExpansion(Begin))
478+
return false;
479+
continue;
480+
}
504481

505-
// Set the ArgumentLoc to the beginning location of the expansion of Loc
506-
// so to check if the ranges expands to the same beginning location.
507-
if (!Loc.isMacroArgExpansion(&ArgumentLoc))
508-
return false;
482+
while (Begin != R.getEnd()) {
483+
SourceLocation MacroLoc;
484+
if (!SM.isMacroArgExpansion(Begin, &MacroLoc))
485+
return false;
486+
if (MacroLoc != Loc)
487+
return false;
509488

510-
for (const auto &Range : SpellingRanges)
511-
if (!checkRangeForMacroArgExpansion(Range, Loc.getManager(), ArgumentLoc))
512-
return false;
489+
Begin = Begin.getLocWithOffset(1);
490+
}
491+
}
513492

514493
return true;
515494
}
@@ -539,13 +518,13 @@ void DiagnosticRenderer::emitMacroExpansions(FullSourceLoc Loc,
539518
while (L.isMacroID()) {
540519
// If this is the expansion of a macro argument, point the caret at the
541520
// use of the argument in the definition of the macro, not the expansion.
542-
if (SM.isMacroArgExpansion(L))
521+
if (SM.isMacroArgExpansion(L)) {
543522
LocationStack.push_back(SM.getImmediateExpansionRange(L).getBegin());
544-
else
545-
LocationStack.push_back(L);
546523

547-
if (checkRangesForMacroArgExpansion(FullSourceLoc(L, SM), Ranges))
548-
IgnoredEnd = LocationStack.size();
524+
if (rangesInsideSameMacroArgExpansion(FullSourceLoc(L, SM), Ranges))
525+
IgnoredEnd = LocationStack.size();
526+
} else
527+
LocationStack.push_back(L);
549528

550529
L = SM.getImmediateMacroCallerLoc(L);
551530

0 commit comments

Comments
 (0)