Skip to content

[Clang] add fix-it hints for unknown attributes #141305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

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
@a-tarasyuk a-tarasyuk marked this pull request as ready for review June 3, 2025 18:13
@llvmbot llvmbot added clang:modules C++20 modules and Clang Header Modules HLSL HLSL Language Support labels Jun 3, 2025
@a-tarasyuk a-tarasyuk removed clang:modules C++20 modules and Clang Header Modules HLSL HLSL Language Support labels Jun 3, 2025
@a-tarasyuk a-tarasyuk requested a review from AaronBallman June 4, 2025 17: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.

@@ -0,0 +1,35 @@
#ifndef LLVM_CLANG_BASIC_ATTRIBUTESCOPEINFO_H
Copy link
Collaborator

Choose a reason for hiding this comment

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

This needs a file header.

Copy link
Member Author

Choose a reason for hiding this comment

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

@erichkeane Thanks for the feedback. I've added a header

Copy link

github-actions bot commented Jun 6, 2025

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

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:modules C++20 modules and Clang Header Modules clang Clang issues not falling into any other category HLSL HLSL Language Support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants