xazax.hun created this revision.
xazax.hun added a project: clang-tools-extra.
Herald added a subscriber: whisperity.

Constexpr variable definitions should be ok in headers.

https://stackoverflow.com/questions/34445336/constexpr-global-constants-in-a-header-file-and-odr


Repository:
  rL LLVM

https://reviews.llvm.org/D34449

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


Index: test/clang-tidy/misc-definitions-in-headers.hpp
===================================================================
--- test/clang-tidy/misc-definitions-in-headers.hpp
+++ test/clang-tidy/misc-definitions-in-headers.hpp
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy %s misc-definitions-in-headers %t
+// RUN: %check_clang_tidy %s misc-definitions-in-headers %t -- -- -std=c++1z
 
 int f() {
 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function 'f' defined in a header 
file; function definitions in header files can lead to ODR violations 
[misc-definitions-in-headers]
@@ -175,3 +175,7 @@
 int CD<T, int>::f() { // OK: partial template specialization.
   return 0;
 }
+
+class CE {
+  constexpr static int i = 5; // OK: constexpr definition.
+};
Index: clang-tidy/misc/DefinitionsInHeadersCheck.cpp
===================================================================
--- clang-tidy/misc/DefinitionsInHeadersCheck.cpp
+++ clang-tidy/misc/DefinitionsInHeadersCheck.cpp
@@ -54,7 +54,7 @@
     return;
   auto DefinitionMatcher =
       anyOf(functionDecl(isDefinition(), unless(isDeleted())),
-            varDecl(isDefinition()));
+            varDecl(isDefinition(), unless(isConstexpr())));
   if (UseHeaderFileExtension) {
     Finder->addMatcher(namedDecl(DefinitionMatcher,
                                  usesHeaderFileExtension(HeaderFileExtensions))


Index: test/clang-tidy/misc-definitions-in-headers.hpp
===================================================================
--- test/clang-tidy/misc-definitions-in-headers.hpp
+++ test/clang-tidy/misc-definitions-in-headers.hpp
@@ -1,4 +1,4 @@
-// RUN: %check_clang_tidy %s misc-definitions-in-headers %t
+// RUN: %check_clang_tidy %s misc-definitions-in-headers %t -- -- -std=c++1z
 
 int f() {
 // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function 'f' defined in a header file; function definitions in header files can lead to ODR violations [misc-definitions-in-headers]
@@ -175,3 +175,7 @@
 int CD<T, int>::f() { // OK: partial template specialization.
   return 0;
 }
+
+class CE {
+  constexpr static int i = 5; // OK: constexpr definition.
+};
Index: clang-tidy/misc/DefinitionsInHeadersCheck.cpp
===================================================================
--- clang-tidy/misc/DefinitionsInHeadersCheck.cpp
+++ clang-tidy/misc/DefinitionsInHeadersCheck.cpp
@@ -54,7 +54,7 @@
     return;
   auto DefinitionMatcher =
       anyOf(functionDecl(isDefinition(), unless(isDeleted())),
-            varDecl(isDefinition()));
+            varDecl(isDefinition(), unless(isConstexpr())));
   if (UseHeaderFileExtension) {
     Finder->addMatcher(namedDecl(DefinitionMatcher,
                                  usesHeaderFileExtension(HeaderFileExtensions))
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to