[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
Tue May 9 04:12:49 PDT 2023


tbaeder created this revision.
tbaeder added reviewers: cjdb, aaron.ballman, shafik.
Herald added a project: All.
tbaeder requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Just a small improvement.

Before:

  array.cpp:1279:9: error: use of undeclared identifier 'foo'
   1279 | int a = foo();
        |         ^
  array.cpp:1280:9: error: use of undeclared identifier 'foo'
   1280 | int b = foo;
        |         ^
  2 errors generated.

After:

  array.cpp:1279:9: error: use of undeclared identifier 'foo'
   1279 | int a = foo();
        |         ^~~
  array.cpp:1280:9: error: use of undeclared identifier 'foo'
   1280 | int b = foo;
        |         ^~~
  2 errors generated.


Repository:
  rG LLVM Github Monorepo

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 << TypoRange;
     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.520652.patch
Type: text/x-patch
Size: 3207 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230509/0c0d104e/attachment.bin>


More information about the cfe-commits mailing list