[clang-tools-extra] [clang-tidy] Do not warn on const variables in misc-use-internal-linkage (PR #97969)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Jul 7 12:13:56 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tidy
Author: Carlos Galvez (carlosgalvezp)
<details>
<summary>Changes</summary>
Since in C++ they already have implicit internal linkage.
Fixes #<!-- -->97947
---
Full diff: https://github.com/llvm/llvm-project/pull/97969.diff
2 Files Affected:
- (modified) clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp (+6)
- (modified) clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp (+3)
``````````diff
diff --git a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
index 44ccc2bc906a5b..c086e7721e02bd 100644
--- a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
@@ -121,6 +121,12 @@ void UseInternalLinkageCheck::check(const MatchFinder::MatchResult &Result) {
return;
}
if (const auto *VD = Result.Nodes.getNodeAs<VarDecl>("var")) {
+ // In C++, const variables at file scope have implicit internal linkage,
+ // so we should not warn there. This is not the case in C.
+ // https://eel.is/c++draft/diff#basic-3
+ if (getLangOpts().CPlusPlus && VD->getType().isConstQualified())
+ return;
+
DiagnosticBuilder DB = diag(VD->getLocation(), Message) << "variable" << VD;
SourceLocation FixLoc = VD->getTypeSpecStartLoc();
if (FixLoc.isInvalid() || FixLoc.isMacroID())
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;
``````````
</details>
https://github.com/llvm/llvm-project/pull/97969
More information about the cfe-commits
mailing list