[clang] [clang][Sema] Don't emit 'declared here' note for builtin functions with no decl in source (PR #93394)

Youngsuk Kim via cfe-commits cfe-commits at lists.llvm.org
Tue May 28 07:59:39 PDT 2024


https://github.com/JOE1994 updated https://github.com/llvm/llvm-project/pull/93394

>From 00c93043fca8f8a1f20634ea1b281c60926bd571 Mon Sep 17 00:00:00 2001
From: Youngsuk Kim <youngsuk.kim at hpe.com>
Date: Sat, 25 May 2024 20:42:43 -0500
Subject: [PATCH 1/5] [clang][Sema] Don't emit 'declared here' note for builtin
 functions with no decl in source

Fixes #93369
---
 clang/lib/Sema/SemaLookup.cpp               | 10 ++++++++++
 clang/test/SemaCXX/invalid-if-constexpr.cpp |  2 +-
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp
index ef0a655b631ab..348f9e5b8f530 100644
--- a/clang/lib/Sema/SemaLookup.cpp
+++ b/clang/lib/Sema/SemaLookup.cpp
@@ -5897,6 +5897,16 @@ void Sema::diagnoseTypo(const TypoCorrection &Correction,
 
   NamedDecl *ChosenDecl =
       Correction.isKeyword() ? nullptr : Correction.getFoundDecl();
+
+  // For builtin functions which aren't declared anywhere in source,
+  // don't emit the "declared here" note.
+  if (auto *FD = dyn_cast_or_null<FunctionDecl>(ChosenDecl);
+      FD && FD->getBuiltinID() &&
+      PrevNote.getDiagID() == diag::note_previous_decl &&
+      Correction.getCorrectionRange().getBegin() == FD->getBeginLoc()) {
+    ChosenDecl = nullptr;
+  }
+
   if (PrevNote.getDiagID() && ChosenDecl)
     Diag(ChosenDecl->getLocation(), PrevNote)
       << CorrectedQuotedStr << (ErrorRecovery ? FixItHint() : FixTypo);
diff --git a/clang/test/SemaCXX/invalid-if-constexpr.cpp b/clang/test/SemaCXX/invalid-if-constexpr.cpp
index 7643c47488f05..16d422880e8a9 100644
--- a/clang/test/SemaCXX/invalid-if-constexpr.cpp
+++ b/clang/test/SemaCXX/invalid-if-constexpr.cpp
@@ -5,7 +5,7 @@ void similar() { // expected-note {{'similar' declared here}}
   if constexpr (similer<>) {} // expected-error {{use of undeclared identifier 'similer'; did you mean 'similar'?}}
 }
 void a() { if constexpr (__adl_swap<>) {}} // expected-error{{use of undeclared identifier '__adl_swap'; did you mean '__sync_swap'?}} \
-                                           // expected-note {{'__sync_swap' declared here}}
+                                           // not-expected-note {{'__sync_swap' declared here}}
 
 int AA() { return true;} // expected-note {{'AA' declared here}}
 

>From 42bbff26459b71e9d77c4d907703930e12db8958 Mon Sep 17 00:00:00 2001
From: Youngsuk Kim <joseph942010 at gmail.com>
Date: Sun, 26 May 2024 05:53:03 -0400
Subject: [PATCH 2/5] Use dyn_cast_if_present

Co-authored-by: Timm Baeder <tbaeder at redhat.com>
---
 clang/lib/Sema/SemaLookup.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp
index 348f9e5b8f530..be6ea20a956a3 100644
--- a/clang/lib/Sema/SemaLookup.cpp
+++ b/clang/lib/Sema/SemaLookup.cpp
@@ -5900,7 +5900,7 @@ void Sema::diagnoseTypo(const TypoCorrection &Correction,
 
   // For builtin functions which aren't declared anywhere in source,
   // don't emit the "declared here" note.
-  if (auto *FD = dyn_cast_or_null<FunctionDecl>(ChosenDecl);
+  if (const auto *FD = dyn_cast_if_present<FunctionDecl>(ChosenDecl);
       FD && FD->getBuiltinID() &&
       PrevNote.getDiagID() == diag::note_previous_decl &&
       Correction.getCorrectionRange().getBegin() == FD->getBeginLoc()) {

>From e4f190be4f59dec78c8a184d9603ac8856da3900 Mon Sep 17 00:00:00 2001
From: Youngsuk Kim <youngsuk.kim at hpe.com>
Date: Sun, 26 May 2024 10:28:02 -0500
Subject: [PATCH 3/5] Add dedicated test

---
 clang/test/SemaCXX/invalid-if-constexpr.cpp         | 1 -
 clang/test/SemaCXX/typo-correction-builtin-func.cpp | 8 ++++++++
 2 files changed, 8 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/SemaCXX/typo-correction-builtin-func.cpp

diff --git a/clang/test/SemaCXX/invalid-if-constexpr.cpp b/clang/test/SemaCXX/invalid-if-constexpr.cpp
index 16d422880e8a9..1fe0bc3415c84 100644
--- a/clang/test/SemaCXX/invalid-if-constexpr.cpp
+++ b/clang/test/SemaCXX/invalid-if-constexpr.cpp
@@ -5,7 +5,6 @@ void similar() { // expected-note {{'similar' declared here}}
   if constexpr (similer<>) {} // expected-error {{use of undeclared identifier 'similer'; did you mean 'similar'?}}
 }
 void a() { if constexpr (__adl_swap<>) {}} // expected-error{{use of undeclared identifier '__adl_swap'; did you mean '__sync_swap'?}} \
-                                           // not-expected-note {{'__sync_swap' declared here}}
 
 int AA() { return true;} // expected-note {{'AA' declared here}}
 
diff --git a/clang/test/SemaCXX/typo-correction-builtin-func.cpp b/clang/test/SemaCXX/typo-correction-builtin-func.cpp
new file mode 100644
index 0000000000000..8d369034d1be3
--- /dev/null
+++ b/clang/test/SemaCXX/typo-correction-builtin-func.cpp
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+// Test that clang does not emit 'declared here' note for builtin functions that don't have a declaration in source.
+
+void t0() {
+  constexpr float A = __builtin_isinfinity(); // expected-error {{use of undeclared identifier '__builtin_isinfinity'; did you mean '__builtin_isfinite'?}}
+                                              // expected-error at -1 {{too few arguments to function call, expected 1, have 0}}
+}

>From 0ea81c7c1af010c0a78027a77e11322914f38a13 Mon Sep 17 00:00:00 2001
From: Youngsuk Kim <youngsuk.kim at hpe.com>
Date: Mon, 27 May 2024 07:33:46 -0500
Subject: [PATCH 4/5] Add release note item

---
 clang/docs/ReleaseNotes.rst | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index d023f53754cb3..1a5843f799c69 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -541,6 +541,9 @@ Improvements to Clang's diagnostics
 - Clang emits a ``-Wparentheses`` warning for expressions with consecutive comparisons like ``x < y < z``.
   Fixes #GH20456.
 
+- Clang no longer emits a "declared here" note for a builtin function that has no declaration in source.
+  Fixes #GH93369.
+
 Improvements to Clang's time-trace
 ----------------------------------
 

>From ff17cdfe6de71f54eb4f68375cd26318761fc979 Mon Sep 17 00:00:00 2001
From: Youngsuk Kim <joseph942010 at gmail.com>
Date: Tue, 28 May 2024 10:59:30 -0400
Subject: [PATCH 5/5] Remove unneeded backtick character from test

Co-authored-by: S. B. Tam <cpplearner at outlook.com>
---
 clang/test/SemaCXX/invalid-if-constexpr.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/SemaCXX/invalid-if-constexpr.cpp b/clang/test/SemaCXX/invalid-if-constexpr.cpp
index 1fe0bc3415c84..0007f2739cbbd 100644
--- a/clang/test/SemaCXX/invalid-if-constexpr.cpp
+++ b/clang/test/SemaCXX/invalid-if-constexpr.cpp
@@ -4,7 +4,7 @@ namespace GH61885 {
 void similar() { // expected-note {{'similar' declared here}}
   if constexpr (similer<>) {} // expected-error {{use of undeclared identifier 'similer'; did you mean 'similar'?}}
 }
-void a() { if constexpr (__adl_swap<>) {}} // expected-error{{use of undeclared identifier '__adl_swap'; did you mean '__sync_swap'?}} \
+void a() { if constexpr (__adl_swap<>) {}} // expected-error{{use of undeclared identifier '__adl_swap'; did you mean '__sync_swap'?}}
 
 int AA() { return true;} // expected-note {{'AA' declared here}}
 



More information about the cfe-commits mailing list