Skip to content

Commit 2990ac1

Browse files
committed
[clang-tidy] Enable inline variable definitions in headers
Differential Revision: https://reviews.llvm.org/D34449 llvm-svn: 306538
1 parent 86cf07a commit 2990ac1

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

clang-tools-extra/clang-tidy/misc/DefinitionsInHeadersCheck.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ void DefinitionsInHeadersCheck::check(const MatchFinder::MatchResult &Result) {
139139
// Ignore variable definition within function scope.
140140
if (VD->hasLocalStorage() || VD->isStaticLocal())
141141
return;
142+
// Ignore inline variables.
143+
if (VD->isInline())
144+
return;
142145

143146
diag(VD->getLocation(),
144147
"variable %0 defined in a header file; "

clang-tools-extra/docs/clang-tidy/checks/misc-definitions-in-headers.rst

+8
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ from multiple translation units.
7474
template <typename T>
7575
void B<T>::f1() {}
7676

77+
class CE {
78+
constexpr static int i = 5; // OK: inline variable definition.
79+
};
80+
81+
inline int i = 5; // OK: inline variable definition.
82+
83+
constexpr int k = 1; // OK: constexpr variable has internal linkage.
84+
7785
Options
7886
-------
7987

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %check_clang_tidy %s misc-definitions-in-headers %t -- -- -std=c++1z
2+
3+
class CE {
4+
constexpr static int i = 5; // OK: inline variable definition.
5+
};
6+
7+
inline int i = 5; // OK: inline variable definition.
8+
9+
int b = 1;
10+
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable 'b' defined in a header file; variable definitions in header files can lead to ODR violations [misc-definitions-in-headers]

clang-tools-extra/test/clang-tidy/misc-definitions-in-headers.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %check_clang_tidy %s misc-definitions-in-headers %t
1+
// RUN: %check_clang_tidy %s misc-definitions-in-headers %t -- -- -std=c++11
22

33
int f() {
44
// 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,5 @@ template <typename T>
175175
int CD<T, int>::f() { // OK: partial template specialization.
176176
return 0;
177177
}
178+
179+
constexpr int k = 1; // OK: constexpr variable has internal linkage.

0 commit comments

Comments
 (0)