| 1 |
.. title:: clang-tidy - misc-definitions-in-headers |
| 2 |
|
| 3 |
misc-definitions-in-headers |
| 4 |
=========================== |
| 5 |
|
| 6 |
Finds non-extern non-inline function and variable definitions in header files, |
| 7 |
which can lead to potential ODR violations in case these headers are included |
| 8 |
from multiple translation units. |
| 9 |
|
| 10 |
.. code-block:: c++ |
| 11 |
|
| 12 |
// Foo.h |
| 13 |
int a = 1; // Warning: variable definition. |
| 14 |
extern int d; // OK: extern variable. |
| 15 |
|
| 16 |
namespace N { |
| 17 |
int e = 2; // Warning: variable definition. |
| 18 |
} |
| 19 |
|
| 20 |
// Warning: variable definition. |
| 21 |
const char* str = "foo"; |
| 22 |
|
| 23 |
// OK: internal linkage variable definitions are ignored for now. |
| 24 |
// Although these might also cause ODR violations, we can be less certain and |
| 25 |
// should try to keep the false-positive rate down. |
| 26 |
static int b = 1; |
| 27 |
const int c = 1; |
| 28 |
const char* const str2 = "foo"; |
| 29 |
constexpr int k = 1; |
| 30 |
|
| 31 |
// Warning: function definition. |
| 32 |
int g() { |
| 33 |
return 1; |
| 34 |
} |
| 35 |
|
| 36 |
// OK: inline function definition is allowed to be defined multiple times. |
| 37 |
inline int e() { |
| 38 |
return 1; |
| 39 |
} |
| 40 |
|
| 41 |
class A { |
| 42 |
public: |
| 43 |
int f1() { return 1; } // OK: implicitly inline member function definition is allowed. |
| 44 |
int f2(); |
| 45 |
|
| 46 |
static int d; |
| 47 |
}; |
| 48 |
|
| 49 |
// Warning: not an inline member function definition. |
| 50 |
int A::f2() { return 1; } |
| 51 |
|
| 52 |
// OK: class static data member declaration is allowed. |
| 53 |
int A::d = 1; |
| 54 |
|
| 55 |
// OK: function template is allowed. |
| 56 |
template<typename T> |
| 57 |
T f3() { |
| 58 |
T a = 1; |
| 59 |
return a; |
| 60 |
} |
| 61 |
|
| 62 |
// Warning: full specialization of a function template is not allowed. |
| 63 |
template <> |
| 64 |
int f3() { |
| 65 |
int a = 1; |
| 66 |
return a; |
| 67 |
} |
| 68 |
|
| 69 |
template <typename T> |
| 70 |
struct B { |
| 71 |
void f1(); |
| 72 |
}; |
| 73 |
|
| 74 |
// OK: member function definition of a class template is allowed. |
| 75 |
template <typename T> |
| 76 |
void B<T>::f1() {} |
| 77 |
|
| 78 |
class CE { |
| 79 |
constexpr static int i = 5; // OK: inline variable definition. |
| 80 |
}; |
| 81 |
|
| 82 |
inline int i = 5; // OK: inline variable definition. |
| 83 |
|
| 84 |
constexpr int f10() { return 0; } // OK: constexpr function implies inline. |
| 85 |
|
| 86 |
// OK: C++14 variable templates are inline. |
| 87 |
template <class T> |
| 88 |
constexpr T pi = T(3.1415926L); |
| 89 |
|
| 90 |
Options |
| 91 |
------- |
| 92 |
|
| 93 |
.. option:: HeaderFileExtensions |
| 94 |
|
| 95 |
A comma-separated list of filename extensions of header files (the filename |
| 96 |
extensions should not include "." prefix). Default is "h,hh,hpp,hxx". |
| 97 |
For header files without an extension, use an empty string (if there are no |
| 98 |
other desired extensions) or leave an empty element in the list. E.g., |
| 99 |
"h,hh,hpp,hxx," (note the trailing comma). |
| 100 |
|
| 101 |
.. option:: UseHeaderFileExtension |
| 102 |
|
| 103 |
When `true`, the check will use the file extension to distinguish header |
| 104 |
files. Default is `true`. |