[clang-tools-extra] [clang-tidy] add fixhint for misc-use-internal-linkage (PR #96203)
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 Date: Thu, 20 Jun 2024 15:05:57 + 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("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("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 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 T global_template; ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add fixhint for misc-use-internal-linkage (PR #96203)
llvmbot wrote: @llvm/pr-subscribers-clang-tidy Author: Congcong Cai (HerrCai0907) Changes --- Full diff: https://github.com/llvm/llvm-project/pull/96203.diff 3 Files Affected: - (modified) clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp (+10-2) - (modified) clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-func.cpp (+1) - (modified) clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp (+1) ``diff 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("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("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 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 T global_template; `` https://github.com/llvm/llvm-project/pull/96203 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add fixhint for misc-use-internal-linkage (PR #96203)
5chmidti wrote: The other two functions have fixes as well. I'd argue that template declarations don't need to be inside anonymous namespaces/`static`, but that's outside the scope of this pr. Either way, this pr should check that both have an attached fixit: `// CHECK-FIXES: static void func_template() {}` `// CHECK-FIXES: static void func_cpp_inc();` https://github.com/llvm/llvm-project/pull/96203 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add fixhint for misc-use-internal-linkage (PR #96203)
@@ -82,11 +82,19 @@ static constexpr StringRef Message = void UseInternalLinkageCheck::check(const MatchFinder::MatchResult &Result) { if (const auto *FD = Result.Nodes.getNodeAs("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("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 "); 5chmidti wrote: I think that this fix should be behind an option, so that projects that want to use anonymous namespaces have the choice to leave the option off or disable it (depending on the default). https://github.com/llvm/llvm-project/pull/96203 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add fixhint for misc-use-internal-linkage (PR #96203)
https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/96203 >From 669205c0f659239c58a3bde3ddadabb0a8ecbad8 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Thu, 20 Jun 2024 15:05:57 + Subject: [PATCH 1/2] [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("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("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 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 T global_template; >From d5bb05e2af683a2dcb08c6e8cabc448001c07414 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Mon, 24 Jun 2024 21:58:56 +0800 Subject: [PATCH 2/2] add FixMode option --- .../misc/UseInternalLinkageCheck.cpp | 36 +-- .../clang-tidy/misc/UseInternalLinkageCheck.h | 11 -- .../checks/misc/use-internal-linkage.rst | 13 +++ .../use-internal-linkage-fix-mode-none.cpp| 10 ++ .../misc/use-internal-linkage-func.cpp| 2 ++ .../misc/use-internal-linkage-var.cpp | 2 ++ 6 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-fix-mode-none.cpp diff --git a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp index e36ccdba42ef1..44ccc2bc906a5 100644 --- a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp @@ -18,6 +18,26 @@ using namespace clang::ast_matchers; +namespace clang::tidy { + +template <> +struct OptionEnumMapping { + static llvm::ArrayRef< + std::pair> + getEnumMapping() { +static constexpr std::pair +Mapping[] = { +{misc::UseInternalLinkageCheck::FixModeKind::None, "None"}, +{misc::UseInternalLinkageCheck::FixModeKind::UseStatic, + "UseStatic"}, +}; +return {Mapping}; + } +}; + +} // namespace clang::tidy + namespace clang::tidy::misc { namespace { @@ -57,6 +77,16 @@ AST_POLYMORPHIC_MATCHER(isExternStorageClass, } // namespace +UseInternalLinkageCheck::UseInternalLinkageCheck(StringRef Name, + ClangTidyContext *Context) +: ClangTidyCheck(Name, Context), + HeaderFileExtensions(Context->getHeaderFileExtensions()), + FixMode(Options.get("FixMode", FixModeKind::UseStatic)) {} + +void UseInternalLinkageCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "FixMode", FixMode);
[clang-tools-extra] [clang-tidy] add fixhint for misc-use-internal-linkage (PR #96203)
https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/96203 >From 669205c0f659239c58a3bde3ddadabb0a8ecbad8 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Thu, 20 Jun 2024 15:05:57 + Subject: [PATCH 1/3] [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("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("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 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 T global_template; >From d5bb05e2af683a2dcb08c6e8cabc448001c07414 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Mon, 24 Jun 2024 21:58:56 +0800 Subject: [PATCH 2/3] add FixMode option --- .../misc/UseInternalLinkageCheck.cpp | 36 +-- .../clang-tidy/misc/UseInternalLinkageCheck.h | 11 -- .../checks/misc/use-internal-linkage.rst | 13 +++ .../use-internal-linkage-fix-mode-none.cpp| 10 ++ .../misc/use-internal-linkage-func.cpp| 2 ++ .../misc/use-internal-linkage-var.cpp | 2 ++ 6 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-fix-mode-none.cpp diff --git a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp index e36ccdba42ef1..44ccc2bc906a5 100644 --- a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp @@ -18,6 +18,26 @@ using namespace clang::ast_matchers; +namespace clang::tidy { + +template <> +struct OptionEnumMapping { + static llvm::ArrayRef< + std::pair> + getEnumMapping() { +static constexpr std::pair +Mapping[] = { +{misc::UseInternalLinkageCheck::FixModeKind::None, "None"}, +{misc::UseInternalLinkageCheck::FixModeKind::UseStatic, + "UseStatic"}, +}; +return {Mapping}; + } +}; + +} // namespace clang::tidy + namespace clang::tidy::misc { namespace { @@ -57,6 +77,16 @@ AST_POLYMORPHIC_MATCHER(isExternStorageClass, } // namespace +UseInternalLinkageCheck::UseInternalLinkageCheck(StringRef Name, + ClangTidyContext *Context) +: ClangTidyCheck(Name, Context), + HeaderFileExtensions(Context->getHeaderFileExtensions()), + FixMode(Options.get("FixMode", FixModeKind::UseStatic)) {} + +void UseInternalLinkageCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "FixMode", FixMode);
[clang-tools-extra] [clang-tidy] add fixhint for misc-use-internal-linkage (PR #96203)
https://github.com/5chmidti approved this pull request. LGTM from my side https://github.com/llvm/llvm-project/pull/96203 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add fixhint for misc-use-internal-linkage (PR #96203)
https://github.com/5chmidti edited https://github.com/llvm/llvm-project/pull/96203 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add fixhint for misc-use-internal-linkage (PR #96203)
@@ -25,3 +25,16 @@ Example: } // already declared as extern extern int v2; + +Options +--- + +.. option:: FixMode + + Selects the fix mode when fixing automatically. 5chmidti wrote: Not sure if it is better: `Selects what kind of a fix the check should provide`? https://github.com/llvm/llvm-project/pull/96203 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add fixhint for misc-use-internal-linkage (PR #96203)
@@ -25,3 +25,16 @@ Example: } // already declared as extern extern int v2; + +Options +--- + +.. option:: FixMode + + Selects the fix mode when fixing automatically. HerrCai0907 wrote: ```suggestion Selects what kind of a fix the check should provide. ``` https://github.com/llvm/llvm-project/pull/96203 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add fixhint for misc-use-internal-linkage (PR #96203)
https://github.com/HerrCai0907 updated https://github.com/llvm/llvm-project/pull/96203 >From 669205c0f659239c58a3bde3ddadabb0a8ecbad8 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Thu, 20 Jun 2024 15:05:57 + Subject: [PATCH 1/4] [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("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("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 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 T global_template; >From d5bb05e2af683a2dcb08c6e8cabc448001c07414 Mon Sep 17 00:00:00 2001 From: Congcong Cai Date: Mon, 24 Jun 2024 21:58:56 +0800 Subject: [PATCH 2/4] add FixMode option --- .../misc/UseInternalLinkageCheck.cpp | 36 +-- .../clang-tidy/misc/UseInternalLinkageCheck.h | 11 -- .../checks/misc/use-internal-linkage.rst | 13 +++ .../use-internal-linkage-fix-mode-none.cpp| 10 ++ .../misc/use-internal-linkage-func.cpp| 2 ++ .../misc/use-internal-linkage-var.cpp | 2 ++ 6 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-fix-mode-none.cpp diff --git a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp index e36ccdba42ef1..44ccc2bc906a5 100644 --- a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp @@ -18,6 +18,26 @@ using namespace clang::ast_matchers; +namespace clang::tidy { + +template <> +struct OptionEnumMapping { + static llvm::ArrayRef< + std::pair> + getEnumMapping() { +static constexpr std::pair +Mapping[] = { +{misc::UseInternalLinkageCheck::FixModeKind::None, "None"}, +{misc::UseInternalLinkageCheck::FixModeKind::UseStatic, + "UseStatic"}, +}; +return {Mapping}; + } +}; + +} // namespace clang::tidy + namespace clang::tidy::misc { namespace { @@ -57,6 +77,16 @@ AST_POLYMORPHIC_MATCHER(isExternStorageClass, } // namespace +UseInternalLinkageCheck::UseInternalLinkageCheck(StringRef Name, + ClangTidyContext *Context) +: ClangTidyCheck(Name, Context), + HeaderFileExtensions(Context->getHeaderFileExtensions()), + FixMode(Options.get("FixMode", FixModeKind::UseStatic)) {} + +void UseInternalLinkageCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { + Options.store(Opts, "FixMode", FixMode);
[clang-tools-extra] [clang-tidy] add fixhint for misc-use-internal-linkage (PR #96203)
https://github.com/HerrCai0907 closed https://github.com/llvm/llvm-project/pull/96203 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add fixhint for misc-use-internal-linkage (PR #96203)
@@ -25,3 +25,16 @@ Example: } // already declared as extern extern int v2; + +Options +--- + +.. option:: FixMode PiotrZSL wrote: should say what is default value https://github.com/llvm/llvm-project/pull/96203 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang-tools-extra] [clang-tidy] add fixhint for misc-use-internal-linkage (PR #96203)
@@ -25,3 +25,16 @@ Example: } // already declared as extern extern int v2; + +Options +--- + +.. option:: FixMode HerrCai0907 wrote: #96779 https://github.com/llvm/llvm-project/pull/96203 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits