[clang-tools-extra] 29896db - [clang-tidy] Do not warn on const variables in misc-use-internal-linkage (#97969)

via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 9 03:20:47 PDT 2024


Author: Carlos Galvez
Date: 2024-07-09T12:20:44+02:00
New Revision: 29896db09c81c79c211e33dc471e23e68ad16e23

URL: https://github.com/llvm/llvm-project/commit/29896db09c81c79c211e33dc471e23e68ad16e23
DIFF: https://github.com/llvm/llvm-project/commit/29896db09c81c79c211e33dc471e23e68ad16e23.diff

LOG: [clang-tidy] Do not warn on const variables in misc-use-internal-linkage (#97969)

Since in C++ they already have implicit internal linkage.

Fixes #97947

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
    clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
index 44ccc2bc906a5..c086e7721e02b 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 01b8d28e61230..6777ce4bb0265 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