[clang-tools-extra] [clang-tidy] add fixhint for misc-use-internal-linkage (PR #96203)
Congcong Cai via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 20 08:06:21 PDT 2024
https://github.com/HerrCai0907 created https://github.com/llvm/llvm-project/pull/96203
None
>From 669205c0f659239c58a3bde3ddadabb0a8ecbad8 Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Thu, 20 Jun 2024 15:05:57 +0000
Subject: [PATCH] [clang-tidy] add fixhint for misc-use-internal-linkage
---
.../clang-tidy/misc/UseInternalLinkageCheck.cpp | 12 ++++++++++--
.../checkers/misc/use-internal-linkage-func.cpp | 1 +
.../checkers/misc/use-internal-linkage-var.cpp | 1 +
3 files changed, 12 insertions(+), 2 deletions(-)
diff --git a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
index 70d0281df28fa..e36ccdba42ef1 100644
--- a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
@@ -82,11 +82,19 @@ static constexpr StringRef Message =
void UseInternalLinkageCheck::check(const MatchFinder::MatchResult &Result) {
if (const auto *FD = Result.Nodes.getNodeAs<FunctionDecl>("fn")) {
- diag(FD->getLocation(), Message) << "function" << FD;
+ DiagnosticBuilder DB = diag(FD->getLocation(), Message) << "function" << FD;
+ SourceLocation FixLoc = FD->getTypeSpecStartLoc();
+ if (FixLoc.isInvalid() || FixLoc.isMacroID())
+ return;
+ DB << FixItHint::CreateInsertion(FixLoc, "static ");
return;
}
if (const auto *VD = Result.Nodes.getNodeAs<VarDecl>("var")) {
- diag(VD->getLocation(), Message) << "variable" << VD;
+ DiagnosticBuilder DB = diag(VD->getLocation(), Message) << "variable" << VD;
+ SourceLocation FixLoc = VD->getTypeSpecStartLoc();
+ if (FixLoc.isInvalid() || FixLoc.isMacroID())
+ return;
+ DB << FixItHint::CreateInsertion(FixLoc, "static ");
return;
}
llvm_unreachable("");
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-func.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-func.cpp
index c6c513fe0b0c0..c244f32db8e96 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-func.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-func.cpp
@@ -4,6 +4,7 @@
void func() {}
// CHECK-MESSAGES: :[[@LINE-1]]:6: warning: function 'func'
+// CHECK-FIXES: static void func() {}
template<class T>
void func_template() {}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp b/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp
index bd5ef5431de6c..b076e9125fc35 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp
@@ -4,6 +4,7 @@
int global;
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable 'global'
+// CHECK-FIXES: static int global;
template<class T>
T global_template;
More information about the cfe-commits
mailing list