[PATCH] D119098: [clang-tidy] Ignore variable template partial specializations in `misc-definitions-in-headers`

Evgeny Shulgin via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Feb 6 17:01:49 PST 2022


Izaron created this revision.
Izaron added reviewers: njames93, hokein, LegalizeAdulthood.
Herald added subscribers: carlosgalvezp, xazax.hun.
Izaron requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

Variable template partial specializations are inline and can't lead
to ODR-violations. The checker now ignores them.

Fixes https://github.com/llvm/llvm-project/issues/53519


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D119098

Files:
  clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp
  clang-tools-extra/test/clang-tidy/checkers/misc-definitions-in-headers.hpp


Index: clang-tools-extra/test/clang-tidy/checkers/misc-definitions-in-headers.hpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/misc-definitions-in-headers.hpp
+++ clang-tools-extra/test/clang-tidy/checkers/misc-definitions-in-headers.hpp
@@ -193,6 +193,16 @@
 // CHECK-MESSAGES: :[[@LINE-1]]:11: warning: full function template specialization 'f12<int>' defined in a header file;
 // CHECK-FIXES: inline const int f12() { return 0; }
 
+template <typename T1, typename T2>
+constexpr bool f13 = false;
+
+template <typename T>
+constexpr bool f13<T, int> = true; // OK: template partial specialization
+
+template <>
+constexpr bool f13<void, int> = false;
+// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: variable 'f13<void, int>' defined in a header file;
+
 int main() {}
 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function 'main' defined in a header file;
 // CHECK-FIXES: {{^}}int main() {
Index: clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp
+++ clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp
@@ -149,6 +149,9 @@
     // Ignore inline variables.
     if (VD->isInline())
       return;
+    // Ignore partial specializations.
+    if (isa<VarTemplatePartialSpecializationDecl>(VD))
+      return;
 
     diag(VD->getLocation(),
          "variable %0 defined in a header file; "


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119098.406301.patch
Type: text/x-patch
Size: 1525 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220207/fb1ed767/attachment.bin>


More information about the cfe-commits mailing list