Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 1 | // RUN: %check_clang_tidy %s misc-definitions-in-headers %t |
| 2 | |
| 3 | int f() { |
| 4 | // 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] |
| 5 | // CHECK-FIXES: inline int f() { |
| 6 | return 1; |
| 7 | } |
| 8 | |
| 9 | class CA { |
| 10 | void f1() {} // OK: inline class member function definition. |
| 11 | void f2(); |
| 12 | template<typename T> |
| 13 | T f3() { |
| 14 | T a = 1; |
| 15 | return a; |
| 16 | } |
| 17 | template<typename T> |
| 18 | struct CAA { |
| 19 | struct CAB { |
| 20 | void f4(); |
| 21 | }; |
| 22 | }; |
| 23 | }; |
| 24 | |
| 25 | void CA::f2() { } |
| 26 | // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: function 'f2' defined in a header file; |
| 27 | // CHECK-FIXES: inline void CA::f2() { |
| 28 | |
| 29 | template <> |
| 30 | int CA::f3() { |
Benjamin Kramer | a62e223 | 2016-04-07 14:55:25 +0000 | [diff] [blame] | 31 | // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: function 'f3<int>' defined in a header file; |
Haojian Wu | 0b067c1 | 2016-07-13 13:55:29 +0000 | [diff] [blame] | 32 | // CHECK-FIXES: inline int CA::f3() { |
Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 33 | int a = 1; |
| 34 | return a; |
| 35 | } |
| 36 | |
| 37 | template <typename T> |
| 38 | void CA::CAA<T>::CAB::f4() { |
| 39 | // OK: member function definition of a nested template class in a class. |
| 40 | } |
| 41 | |
| 42 | template <typename T> |
| 43 | struct CB { |
| 44 | void f1(); |
| 45 | struct CCA { |
| 46 | void f2(T a); |
| 47 | }; |
| 48 | struct CCB; // OK: forward declaration. |
| 49 | static int a; // OK: class static data member declaration. |
| 50 | }; |
| 51 | |
| 52 | template <typename T> |
| 53 | void CB<T>::f1() { // OK: Member function definition of a class template. |
| 54 | } |
| 55 | |
| 56 | template <typename T> |
| 57 | void CB<T>::CCA::f2(T a) { |
| 58 | // OK: member function definition of a nested class in a class template. |
| 59 | } |
| 60 | |
| 61 | template <typename T> |
| 62 | struct CB<T>::CCB { |
| 63 | void f3(); |
| 64 | }; |
| 65 | |
| 66 | template <typename T> |
| 67 | void CB<T>::CCB::f3() { |
| 68 | // OK: member function definition of a nested class in a class template. |
| 69 | } |
| 70 | |
| 71 | template <typename T> |
| 72 | int CB<T>::a = 2; // OK: static data member definition of a class template. |
| 73 | |
| 74 | template <typename T> |
| 75 | T tf() { // OK: template function definition. |
| 76 | T a; |
| 77 | return a; |
| 78 | } |
| 79 | |
| 80 | |
| 81 | namespace NA { |
| 82 | int f() { return 1; } |
| 83 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function 'f' defined in a header file; |
| 84 | // CHECK-FIXES: inline int f() { return 1; } |
| 85 | } |
| 86 | |
| 87 | template <typename T> |
| 88 | T f3() { |
| 89 | T a = 1; |
| 90 | return a; |
| 91 | } |
| 92 | |
| 93 | template <> |
Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 94 | int f3() { |
Haojian Wu | 0b067c1 | 2016-07-13 13:55:29 +0000 | [diff] [blame] | 95 | // CHECK-MESSAGES: :[[@LINE-1]]:5: warning: function 'f3<int>' defined in a header file; |
| 96 | // CHECK-FIXES: inline int f3() { |
Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 97 | int a = 1; |
| 98 | return a; |
| 99 | } |
| 100 | |
| 101 | int f5(); // OK: function declaration. |
| 102 | inline int f6() { return 1; } // OK: inline function definition. |
| 103 | namespace { |
| 104 | int f7() { return 1; } |
| 105 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: function 'f7' defined in a header file; |
| 106 | } |
| 107 | |
Haojian Wu | ba992cf | 2016-06-07 08:55:38 +0000 | [diff] [blame] | 108 | int f8() = delete; // OK: the function being marked delete is not callable. |
| 109 | |
Alexander Kornienko | b816ba0 | 2016-01-08 16:37:11 +0000 | [diff] [blame] | 110 | int a = 1; |
| 111 | // 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] |
| 112 | CA a1; |
| 113 | // CHECK-MESSAGES: :[[@LINE-1]]:4: warning: variable 'a1' defined in a header file; |
| 114 | |
| 115 | namespace NB { |
| 116 | int b = 1; |
| 117 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'b' defined in a header file; |
| 118 | const int c = 1; // OK: internal linkage variable definition. |
| 119 | } |
| 120 | |
| 121 | class CC { |
| 122 | static int d; // OK: class static data member declaration. |
| 123 | }; |
| 124 | |
| 125 | int CC::d = 1; |
| 126 | // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: variable 'd' defined in a header file; |
| 127 | |
| 128 | const char* ca = "foo"; |
| 129 | // CHECK-MESSAGES: :[[@LINE-1]]:13: warning: variable 'ca' defined in a header file; |
| 130 | |
| 131 | namespace { |
| 132 | int e = 2; |
| 133 | // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: variable 'e' defined in a header file; |
| 134 | } |
| 135 | |
| 136 | const char* const g = "foo"; // OK: internal linkage variable definition. |
| 137 | static int h = 1; // OK: internal linkage variable definition. |
| 138 | const int i = 1; // OK: internal linkage variable definition. |
| 139 | extern int j; // OK: internal linkage variable definition. |
Haojian Wu | 29634fe | 2016-02-03 12:10:27 +0000 | [diff] [blame] | 140 | |
| 141 | template <typename T, typename U> |
| 142 | struct CD { |
| 143 | int f(); |
| 144 | }; |
| 145 | |
| 146 | template <typename T> |
| 147 | struct CD<T, int> { |
| 148 | int f(); |
| 149 | }; |
| 150 | |
| 151 | template <> |
| 152 | struct CD<int, int> { |
| 153 | int f(); |
| 154 | }; |
| 155 | |
| 156 | int CD<int, int>::f() { |
| 157 | // CHECK-MESSAGES: :[[@LINE-1]]:19: warning: function 'f' defined in a header file; |
Haojian Wu | 0b067c1 | 2016-07-13 13:55:29 +0000 | [diff] [blame] | 158 | // CHECK-FIXES: inline int CD<int, int>::f() { |
Haojian Wu | 29634fe | 2016-02-03 12:10:27 +0000 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | template <typename T> |
| 163 | int CD<T, int>::f() { // OK: partial template specialization. |
| 164 | return 0; |
| 165 | } |