[clang-tools-extra] [clang-tidy] Do not warn on const variables in misc-use-internal-linkage (PR #97969)
Carlos Galvez via cfe-commits
cfe-commits at lists.llvm.org
Mon Jul 8 04:54:32 PDT 2024
https://github.com/carlosgalvezp updated https://github.com/llvm/llvm-project/pull/97969
>From a1fec907b5d3920d5dda8761b6e173e153b7f281 Mon Sep 17 00:00:00 2001
From: Carlos Galvez <carlosgalvezp at gmail.com>
Date: Sun, 7 Jul 2024 21:11:54 +0200
Subject: [PATCH] [clang-tidy] Do not warn on const variables in
misc-use-internal-linkage
Since in C++ they already have implicit internal linkage.
https://eel.is/c++draft/diff#basic-3
Also, ensure that the check is only active in C++ code, since the
warning message presents anonymous namespaces as a solution, which
is not applicable to C.
Fixes #97947
---
.../clang-tidy/misc/UseInternalLinkageCheck.cpp | 5 ++++-
clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.h | 3 +++
.../clang-tidy/checkers/misc/use-internal-linkage-var.cpp | 3 +++
3 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
index 44ccc2bc906a5b..bc3fc50ca75dff 100644
--- a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
@@ -103,7 +103,10 @@ void UseInternalLinkageCheck::registerMatchers(MatchFinder *Finder) {
functionDecl(Common, unless(cxxMethodDecl()), unless(isMain()))
.bind("fn"),
this);
- Finder->addMatcher(varDecl(Common, hasGlobalStorage()).bind("var"), this);
+ Finder->addMatcher(
+ varDecl(Common, hasGlobalStorage(), unless(hasType(isConstQualified())))
+ .bind("var"),
+ this);
}
static constexpr StringRef Message =
diff --git a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.h b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.h
index 0d6c3e43aa9457..1ad28333ddc49d 100644
--- a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.h
+++ b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.h
@@ -21,6 +21,9 @@ namespace clang::tidy::misc {
class UseInternalLinkageCheck : public ClangTidyCheck {
public:
UseInternalLinkageCheck(StringRef Name, ClangTidyContext *Context);
+ bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
+ return LangOpts.CPlusPlus;
+ }
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
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 01b8d28e612301..6777ce4bb0265e 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
@@ -42,3 +42,6 @@ int global_in_extern_c_1;
}
extern "C" int global_in_extern_c_2;
+
+const int const_global = 123;
+constexpr int constexpr_global = 123;
More information about the cfe-commits
mailing list