[PATCH] D150191: [clang][Diagnostics] Provide a source range for 'use of undeclared identifier' diagnostics

Timm Bäder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed May 10 23:40:45 PDT 2023


tbaeder updated this revision to Diff 521215.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D150191/new/

https://reviews.llvm.org/D150191

Files:
  clang/include/clang/Sema/Lookup.h
  clang/lib/Sema/SemaExpr.cpp


Index: clang/lib/Sema/SemaExpr.cpp
===================================================================
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -2173,7 +2173,7 @@
 
 static void emitEmptyLookupTypoDiagnostic(
     const TypoCorrection &TC, Sema &SemaRef, const CXXScopeSpec &SS,
-    DeclarationName Typo, SourceLocation TypoLoc, ArrayRef<Expr *> Args,
+    DeclarationName Typo, SourceRange TypoRange, ArrayRef<Expr *> Args,
     unsigned DiagnosticID, unsigned DiagnosticSuggestID) {
   DeclContext *Ctx =
       SS.isEmpty() ? nullptr : SemaRef.computeDeclContext(SS, false);
@@ -2181,10 +2181,10 @@
     // 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.getEnd(), DiagnosticID) << Typo;
     return;
   }
 
@@ -2198,9 +2198,9 @@
     SemaRef.diagnoseTypo(TC, SemaRef.PDiag(DiagnosticSuggestID) << Typo,
                          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));
 }
 
@@ -2324,16 +2324,16 @@
     DC = DC->getLookupParent();
   }
 
+  SourceRange TypoRange = R.getNameRange();
   // 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, TypoRange, Args,
                                         diagnostic, diagnostic_suggest);
         },
         nullptr, CTK_ErrorRecovery);
@@ -2436,7 +2436,7 @@
   }
 
   // Give up, we can't recover.
-  Diag(R.getNameLoc(), diagnostic) << Name;
+  Diag(R.getNameLoc(), diagnostic) << Name << TypoRange;
   return true;
 }
 
Index: clang/include/clang/Sema/Lookup.h
===================================================================
--- clang/include/clang/Sema/Lookup.h
+++ clang/include/clang/Sema/Lookup.h
@@ -633,6 +633,10 @@
     return NameInfo.getLoc();
   }
 
+  SourceRange getNameRange() const {
+    return SourceRange(NameInfo.getBeginLoc(), NameInfo.getEndLoc());
+  }
+
   /// Get the Sema object that this lookup result is searching
   /// with.
   Sema &getSema() const { return *SemaPtr; }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D150191.521215.patch
Type: text/x-patch
Size: 3194 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230511/75f7bfb0/attachment.bin>


More information about the cfe-commits mailing list