[clang] [Clang] add fix-it hints for unknown attributes (PR #141305)
Oleksandr T. via cfe-commits
cfe-commits at lists.llvm.org
Fri May 23 16:56:11 PDT 2025
https://github.com/a-tarasyuk created https://github.com/llvm/llvm-project/pull/141305
This patch adds fix-it hints for unknown attribute names when Clang suggests a correction
>From 2b84b21462a4e66b8681e165fcd24e3107612448 Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk <oleksandr.tarasiuk at outlook.com>
Date: Sat, 24 May 2025 02:52:24 +0300
Subject: [PATCH] [Clang] add fix-it hints for unknown attributes
---
clang/docs/ReleaseNotes.rst | 3 +++
clang/lib/Sema/SemaDeclAttr.cpp | 13 +++++-----
clang/test/FixIt/fixit-unknown-attributes.cpp | 26 +++++++++++++++++++
3 files changed, 36 insertions(+), 6 deletions(-)
create mode 100644 clang/test/FixIt/fixit-unknown-attributes.cpp
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"
More information about the cfe-commits
mailing list