1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
// RUN: %check_clang_tidy %s misc-definitions-in-headers %t
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]
// CHECK-MESSAGES: :[[@LINE-2]]:5: note: make as 'inline'
// CHECK-FIXES: inline int f() {
return 1;
}
class CA {
void f1() {} // OK: inline class member function definition.
void f2();
template<typename T>
T f3() {
T a = 1;
return a;
}
template<typename T>
struct CAA {
struct CAB {
void f4();
};
};
};
void CA::f2() { }
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: function 'f2' defined in a header file;
// CHECK-FIXES: inline void CA::f2() {
template <>
int CA::f3() {
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: full function template specialization 'f3<int>' defined in a header file;
// CHECK-FIXES: inline int CA::f3() {
int a = 1;
return a;
}
template <typename T>
void CA::CAA<T>::CAB::f4() {
// OK: member function definition of a nested template class in a class.
}
template <typename T>
struct CB {
void f1();
struct CCA {
void f2(T a);
};
struct CCB; // OK: forward declaration.
static int a; // OK: class static data member declaration.
};
template <typename T>
void CB<T>::f1() { // OK: Member function definition of a class template.
}
template <typename T>
void CB<T>::CCA::f2(T a) {
// OK: member function definition of a nested class in a class template.
}
template <typename T>
struct CB<T>::CCB {
void f3();
};
template <typename T>
void CB<T>::CCB::f3() {
// OK: member function definition of a nested class in a class template.
}
template <typename T>
int CB<T>::a = 2; // OK: static data member definition of a class template.
template class CB<int>; // OK: explicitly instantiated static data member of a class template.
inline int callCB() {
CB<double> cb; // OK: implicitly instantiated static data member of a class template.
return cb.a;
}
template <typename T>
T tf() { // OK: template function definition.
T a;
return a;
}
namespace NA {
int f() { return 1; }
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function 'f' defined in a header file;
// CHECK-FIXES: inline int f() { return 1; }
}
template <typename T>
T f3() {
T a = 1;
return a;
}
template <>
int f3() {
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: full function template specialization 'f3<int>' defined in a header file;
// CHECK-FIXES: inline int f3() {
int a = 1;
return a;
}
int f5(); // OK: function declaration.
inline int f6() { return 1; } // OK: inline function definition.
namespace {
int f7() { return 1; }
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function 'f7' defined in a header file;
}
int f8() = delete; // OK: the function being marked delete is not callable.
template <typename T>
int f9(T t) { return 1; }
inline void callF9() { f9(1); } // OK: implicitly instantiated function.
template int f9(double); // OK: explicitly instantiated function.
int a = 1;
// CHECK-MESSAGES: :[[@LINE-1]]:5: warning: variable 'a' defined in a header file; variable definitions in header files can lead to ODR violations [misc-definitions-in-headers]
CA a1;
// CHECK-MESSAGES: :[[@LINE-1]]:4: warning: variable 'a1' defined in a header file;
namespace NB {
int b = 1;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'b' defined in a header file;
const int c = 1; // OK: internal linkage variable definition.
}
class CC {
static int d; // OK: class static data member declaration.
};
int CC::d = 1;
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable 'd' defined in a header file;
const char* ca = "foo";
// CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable 'ca' defined in a header file;
namespace {
int e = 2;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'e' defined in a header file;
}
const char* const g = "foo"; // OK: internal linkage variable definition.
static int h = 1; // OK: internal linkage variable definition.
const int i = 1; // OK: internal linkage variable definition.
extern int j; // OK: internal linkage variable definition.
template <typename T, typename U>
struct CD {
int f();
};
template <typename T>
struct CD<T, int> {
int f();
};
template <>
struct CD<int, int> {
int f();
};
int CD<int, int>::f() {
// CHECK-MESSAGES: :[[@LINE-1]]:19: warning: function 'f' defined in a header file;
// CHECK-FIXES: inline int CD<int, int>::f() {
return 0;
}
template <typename T>
int CD<T, int>::f() { // OK: partial template specialization.
return 0;
}
constexpr int k = 1; // OK: constexpr variable has internal linkage.
constexpr int f10() { return 0; } // OK: constexpr function definition.
const int f11() { return 0; }
// CHECK-MESSAGES: :[[@LINE-1]]:11: warning: function 'f11' defined in a header file;
// CHECK-FIXES: inline const int f11() { return 0; }
template <typename T>
const T f12();
template <>
const int f12() { return 0; }
// 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; }
|