Skip to content

Conversation

a-tarasyuk
Copy link
Member

This patch adds fix-it hints for unknown attribute names when Clang suggests a correction

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 23, 2025
@llvmbot
Copy link
Member

llvmbot commented May 23, 2025

@llvm/pr-subscribers-hlsl
@llvm/pr-subscribers-clang-modules

@llvm/pr-subscribers-clang

Author: Oleksandr T. (a-tarasyuk)

Changes

This patch adds fix-it hints for unknown attribute names when Clang suggests a correction


Full diff: https://github.com/llvm/llvm-project/pull/141305.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+3)
  • (modified) clang/lib/Sema/SemaDeclAttr.cpp (+7-6)
  • (added) clang/test/FixIt/fixit-unknown-attributes.cpp (+26)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b93fa33acc2a0..02cca0bb357f9 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -592,6 +592,9 @@ Improvements to Clang's diagnostics
   trigger a ``'Blue' is deprecated`` warning, which can be turned off with
   ``-Wno-deprecated-switch-case``.
 
+- Clang now emits fix-it hints for unknown attributes when a spelling
+  correction is suggested.
+
 Improvements to Clang's time-trace
 ----------------------------------
 
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 8ce51cc2882bf..05b8e7d5a75f0 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -7867,15 +7867,16 @@ void Sema::checkUnusedDeclAttributes(Declarator &D) {
 
 void Sema::DiagnoseUnknownAttribute(const ParsedAttr &AL) {
   std::string NormalizedFullName = '\'' + AL.getNormalizedFullName() + '\'';
+  SourceRange NR = AL.getNormalizedRange();
+  SourceLocation Loc = NR.getBegin();
+
   if (auto CorrectedFullName =
           AL.getCorrectedFullName(Context.getTargetInfo(), getLangOpts())) {
-    Diag(AL.getNormalizedRange().getBegin(),
-         diag::warn_unknown_attribute_ignored_suggestion)
-        << NormalizedFullName << *CorrectedFullName << AL.getNormalizedRange();
+    Diag(Loc, diag::warn_unknown_attribute_ignored_suggestion)
+        << NormalizedFullName << *CorrectedFullName
+        << FixItHint::CreateReplacement(NR, *CorrectedFullName) << NR;
   } else {
-    Diag(AL.getNormalizedRange().getBegin(),
-         diag::warn_unknown_attribute_ignored)
-        << NormalizedFullName << AL.getNormalizedRange();
+    Diag(Loc, diag::warn_unknown_attribute_ignored) << NormalizedFullName << NR;
   }
 }
 
diff --git a/clang/test/FixIt/fixit-unknown-attributes.cpp b/clang/test/FixIt/fixit-unknown-attributes.cpp
new file mode 100644
index 0000000000000..6b74650942d0a
--- /dev/null
+++ b/clang/test/FixIt/fixit-unknown-attributes.cpp
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -Wunknown-attributes -fsyntax-only -verify %s
+// RUN: %clang_cc1 -Wunknown-attributes -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
+
+[[gmu::deprected]] // expected-warning {{unknown attribute 'gmu::deprected' ignored; did you mean 'gnu::deprecated'?}}
+int f1(void) {
+  return 0;
+}
+// CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:3-[[@LINE-4]]:17}:"gnu::deprecated"
+
+[[gmu::deprecated]] // expected-warning {{unknown attribute 'gmu::deprecated' ignored; did you mean 'gnu::deprecated'?}}
+int f2(void) {
+  return 0;
+}
+// CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:3-[[@LINE-4]]:18}:"gnu::deprecated"
+
+[[gnu::deprected]] // expected-warning {{unknown attribute 'gnu::deprected' ignored; did you mean 'gnu::deprecated'?}}
+int f3(void) {
+  return 0;
+}
+// CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:3-[[@LINE-4]]:17}:"gnu::deprecated"
+
+[[deprected]] // expected-warning {{unknown attribute 'deprected' ignored; did you mean 'deprecated'?}}
+int f4(void) {
+  return 0;
+}
+// CHECK: fix-it:"{{.*}}":{[[@LINE-4]]:3-[[@LINE-4]]:12}:"deprecated"

@zwuis
Copy link
Contributor

zwuis commented May 24, 2025

Can you handle this case [[using gnu : deprected]]? (or add tests)

@llvmbot llvmbot added the clang:modules C++20 modules and Clang Header Modules label May 27, 2025
Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm really in favor of this direction, this is great, thank you!

I have a few suggestions here (and if there is good reason, feel free to ignore the ones re return-type & setting invalid in particular), but this is pretty good for me.

@a-tarasyuk a-tarasyuk marked this pull request as draft May 30, 2025 23:46
@a-tarasyuk a-tarasyuk force-pushed the fix/fixit-unknown-attributes branch 13 times, most recently from db4a85b to 50e590f Compare May 31, 2025 18:12
@a-tarasyuk a-tarasyuk removed the clang:modules C++20 modules and Clang Header Modules label May 31, 2025
@a-tarasyuk a-tarasyuk force-pushed the fix/fixit-unknown-attributes branch from 50e590f to 9d2139c Compare May 31, 2025 20:28
@a-tarasyuk a-tarasyuk force-pushed the fix/fixit-unknown-attributes branch from 9d2139c to 77641f8 Compare May 31, 2025 22:26
@llvmbot llvmbot added clang:modules C++20 modules and Clang Header Modules HLSL HLSL Language Support labels Jun 4, 2025
@a-tarasyuk a-tarasyuk removed clang:modules C++20 modules and Clang Header Modules HLSL HLSL Language Support labels Jun 4, 2025
@llvmbot llvmbot added clang:modules C++20 modules and Clang Header Modules HLSL HLSL Language Support labels Jun 5, 2025
@a-tarasyuk a-tarasyuk requested a review from erichkeane June 6, 2025 11:46
Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a strict improvement, and I think looks correct to me.

Copy link

github-actions bot commented Jun 6, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@a-tarasyuk a-tarasyuk removed clang:modules C++20 modules and Clang Header Modules HLSL HLSL Language Support labels Jun 12, 2025
Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@a-tarasyuk a-tarasyuk merged commit 0ff95c9 into llvm:main Jun 14, 2025
8 checks passed
akuhlens pushed a commit to akuhlens/llvm-project that referenced this pull request Jun 24, 2025
This patch adds fix-it hints for unknown attribute names when Clang
suggests a correction
AnthonyLatsis added a commit to swiftlang/swift that referenced this pull request Jul 19, 2025
AnthonyLatsis added a commit to swiftlang/swift that referenced this pull request Jul 19, 2025
MacDue added a commit that referenced this pull request Jul 24, 2025
In #141305, the attribute argument was (unintentionally) removed from
the diagnostic emission.

Fixes #150237
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Jul 28, 2025
In llvm#141305, the attribute argument was (unintentionally) removed from
the diagnostic emission.

Fixes llvm#150237
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants