[clang] [clang] Add source range to 'use of undeclared identifier' diagnostics (PR #117671)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 26 01:18:00 PST 2024
Timm =?utf-8?q?Bäder?= <tbaeder at redhat.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/117671 at github.com>
https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/117671
>From 1d77f36a1f7fa974d50ff7a5a98b93bbb01ba26c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 26 Nov 2024 08:44:57 +0100
Subject: [PATCH 1/2] [clang] Add source range to 'use of undeclared
identifier' diagnostics
---
clang/lib/Sema/SemaExpr.cpp | 45 ++++++++++++++++++++-----------------
1 file changed, 24 insertions(+), 21 deletions(-)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 6c7472ce92703b..d0d6d981827270 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -2351,20 +2351,22 @@ Sema::DecomposeUnqualifiedId(const UnqualifiedId &Id,
}
}
-static void emitEmptyLookupTypoDiagnostic(
- const TypoCorrection &TC, Sema &SemaRef, const CXXScopeSpec &SS,
- DeclarationName Typo, SourceLocation TypoLoc, ArrayRef<Expr *> Args,
- unsigned DiagnosticID, unsigned DiagnosticSuggestID) {
+static void emitEmptyLookupTypoDiagnostic(const TypoCorrection &TC,
+ Sema &SemaRef, const CXXScopeSpec &SS,
+ DeclarationName Typo,
+ SourceRange TypoRange,
+ unsigned DiagnosticID,
+ unsigned DiagnosticSuggestID) {
DeclContext *Ctx =
SS.isEmpty() ? nullptr : SemaRef.computeDeclContext(SS, false);
if (!TC) {
// Emit a special diagnostic for failed member lookups.
// FIXME: computing the declaration context might fail here (?)
if (Ctx)
- SemaRef.Diag(TypoLoc, diag::err_no_member) << Typo << Ctx
- << SS.getRange();
+ SemaRef.Diag(TypoRange.getBegin(), diag::err_no_member)
+ << Typo << Ctx << TypoRange;
else
- SemaRef.Diag(TypoLoc, DiagnosticID) << Typo;
+ SemaRef.Diag(TypoRange.getBegin(), DiagnosticID) << Typo << TypoRange;
return;
}
@@ -2375,12 +2377,13 @@ static void emitEmptyLookupTypoDiagnostic(
? diag::note_implicit_param_decl
: diag::note_previous_decl;
if (!Ctx)
- SemaRef.diagnoseTypo(TC, SemaRef.PDiag(DiagnosticSuggestID) << Typo,
- SemaRef.PDiag(NoteID));
+ SemaRef.diagnoseTypo(
+ TC, SemaRef.PDiag(DiagnosticSuggestID) << Typo << TypoRange,
+ SemaRef.PDiag(NoteID));
else
- SemaRef.diagnoseTypo(TC, SemaRef.PDiag(diag::err_no_member_suggest)
- << Typo << Ctx << DroppedSpecifier
- << SS.getRange(),
+ SemaRef.diagnoseTypo(TC,
+ SemaRef.PDiag(diag::err_no_member_suggest)
+ << Typo << Ctx << DroppedSpecifier << TypoRange,
SemaRef.PDiag(NoteID));
}
@@ -2449,6 +2452,7 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R,
ArrayRef<Expr *> Args, DeclContext *LookupCtx,
TypoExpr **Out) {
DeclarationName Name = R.getLookupName();
+ SourceRange NameRange = R.getLookupNameInfo().getSourceRange();
unsigned diagnostic = diag::err_undeclared_var_use;
unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest;
@@ -2506,13 +2510,12 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R,
// We didn't find anything, so try to correct for a typo.
TypoCorrection Corrected;
if (S && Out) {
- SourceLocation TypoLoc = R.getNameLoc();
assert(!ExplicitTemplateArgs &&
"Diagnosing an empty lookup with explicit template args!");
*Out = CorrectTypoDelayed(
R.getLookupNameInfo(), R.getLookupKind(), S, &SS, CCC,
[=](const TypoCorrection &TC) {
- emitEmptyLookupTypoDiagnostic(TC, *this, SS, Name, TypoLoc, Args,
+ emitEmptyLookupTypoDiagnostic(TC, *this, SS, Name, NameRange,
diagnostic, diagnostic_suggest);
},
nullptr, CTK_ErrorRecovery, LookupCtx);
@@ -2591,12 +2594,13 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R,
? diag::note_implicit_param_decl
: diag::note_previous_decl;
if (SS.isEmpty())
- diagnoseTypo(Corrected, PDiag(diagnostic_suggest) << Name,
+ diagnoseTypo(Corrected, PDiag(diagnostic_suggest) << Name << NameRange,
PDiag(NoteID), AcceptableWithRecovery);
else
- diagnoseTypo(Corrected, PDiag(diag::err_no_member_suggest)
- << Name << computeDeclContext(SS, false)
- << DroppedSpecifier << SS.getRange(),
+ diagnoseTypo(Corrected,
+ PDiag(diag::err_no_member_suggest)
+ << Name << computeDeclContext(SS, false)
+ << DroppedSpecifier << NameRange,
PDiag(NoteID), AcceptableWithRecovery);
// Tell the callee whether to try to recover.
@@ -2609,13 +2613,12 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R,
// FIXME: computing the declaration context might fail here (?)
if (!SS.isEmpty()) {
Diag(R.getNameLoc(), diag::err_no_member)
- << Name << computeDeclContext(SS, false)
- << SS.getRange();
+ << Name << computeDeclContext(SS, false) << NameRange;
return true;
}
// Give up, we can't recover.
- Diag(R.getNameLoc(), diagnostic) << Name;
+ Diag(R.getNameLoc(), diagnostic) << Name << NameRange;
return true;
}
>From 894748176d95d1141b7c6fb27407c283e9d7a542 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 26 Nov 2024 10:17:08 +0100
Subject: [PATCH 2/2] Let's try something
---
clang/lib/Frontend/DiagnosticRenderer.cpp | 9 ++++++---
clang/lib/Sema/SemaExpr.cpp | 3 ++-
2 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/clang/lib/Frontend/DiagnosticRenderer.cpp b/clang/lib/Frontend/DiagnosticRenderer.cpp
index 8b32af13372579..063af34f58c6b1 100644
--- a/clang/lib/Frontend/DiagnosticRenderer.cpp
+++ b/clang/lib/Frontend/DiagnosticRenderer.cpp
@@ -487,14 +487,17 @@ static bool checkRangeForMacroArgExpansion(CharSourceRange Range,
/// macro argument expansion as Loc.
static bool checkRangesForMacroArgExpansion(FullSourceLoc Loc,
ArrayRef<CharSourceRange> Ranges) {
+ // llvm::errs() << __PRETTY_FUNCTION__ << '\n';
assert(Loc.isMacroID() && "Must be a macro expansion!");
SmallVector<CharSourceRange, 4> SpellingRanges;
- mapDiagnosticRanges(Loc, Ranges, SpellingRanges);
+ // mapDiagnosticRanges(Loc, Ranges, SpellingRanges);
+ // llvm::errs() << "Spelling Ranges: " << SpellingRanges.size() << '\n';
// Count all valid ranges.
- unsigned ValidCount =
- llvm::count_if(Ranges, [](const auto &R) { return R.isValid(); });
+ unsigned ValidCount = 0;
+ // llvm::count_if(Ranges, [](const auto &R) { return R.isValid(); });
+ // llvm::errs() << "ValidCount: " << ValidCount << '\n';
if (ValidCount > SpellingRanges.size())
return false;
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index d0d6d981827270..3a3f73709217cb 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -2452,7 +2452,8 @@ bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R,
ArrayRef<Expr *> Args, DeclContext *LookupCtx,
TypoExpr **Out) {
DeclarationName Name = R.getLookupName();
- SourceRange NameRange = R.getLookupNameInfo().getSourceRange();
+ SourceRange NameRange = SourceRange(R.getNameLoc(), SourceLocation());
+ NameRange = R.getLookupNameInfo().getSourceRange();
unsigned diagnostic = diag::err_undeclared_var_use;
unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest;
More information about the cfe-commits
mailing list