Skip to content

Commit 9d4162f

Browse files
committed
[clang-tidy] Add fix-it support to llvmlibc-inline-function-decl
This is very simplistic and could be more thorough by replacing an existing `LIBC_INLINE` in the wrong location or a redunant `inline` when inserting the right macro use. But as is this suffices to automatically apply fixes for most or all of the instances in the libc tree today and get working results (despite some superfluous `inline` keywords left behind). Reviewed By: abrachet Differential Revision: https://reviews.llvm.org/D157164
1 parent 0ba9aec commit 9d4162f

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

clang-tools-extra/clang-tidy/llvmlibc/InlineFunctionDeclCheck.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void InlineFunctionDeclCheck::check(const MatchFinder::MatchResult &Result) {
8888

8989
diag(SrcBegin, "%0 must be tagged with the LIBC_INLINE macro; the macro "
9090
"should be placed at the beginning of the declaration")
91-
<< FuncDecl;
91+
<< FuncDecl << FixItHint::CreateInsertion(Loc, "LIBC_INLINE ");
9292
}
9393

9494
} // namespace clang::tidy::llvm_libc

clang-tools-extra/test/clang-tidy/checkers/llvmlibc/inline-function-decl.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ LIBC_INLINE constexpr long addl(long a, long b) {
1717

1818
constexpr long long addll(long long a, long long b) {
1919
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: 'addll' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
20+
// CHECK-FIXES: LIBC_INLINE constexpr long long addll(long long a, long long b) {
2021
return a + b;
2122
}
2223

2324
inline unsigned long addul(unsigned long a, unsigned long b) {
2425
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: 'addul' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
26+
// CHECK-FIXES: LIBC_INLINE inline unsigned long addul(unsigned long a, unsigned long b) {
2527
return a + b;
2628
}
2729

@@ -30,18 +32,21 @@ class MyClass {
3032
public:
3133
MyClass() : A(123) {}
3234
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'MyClass' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
35+
// CHECK-FIXES: LIBC_INLINE MyClass() : A(123) {}
3336

3437
LIBC_INLINE MyClass(int V) : A(V) {}
3538

3639
constexpr operator int() const { return A; }
3740
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'operator int' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
41+
// CHECK-FIXES: LIBC_INLINE constexpr operator int() const { return A; }
3842

3943
LIBC_INLINE bool operator==(const MyClass &RHS) {
4044
return RHS.A == A;
4145
}
4246

4347
static int getVal(const MyClass &V) {
4448
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'getVal' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
49+
// CHECK-FIXES: LIBC_INLINE static int getVal(const MyClass &V) {
4550
return V.A;
4651
}
4752

@@ -51,6 +56,7 @@ class MyClass {
5156

5257
constexpr static int addInt(MyClass &V, int A) {
5358
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: 'addInt' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
59+
// CHECK-FIXES: LIBC_INLINE constexpr static int addInt(MyClass &V, int A) {
5460
return V.A += A;
5561
}
5662

@@ -78,6 +84,7 @@ LIBC_INLINE void goodSimpleFunction() {}
7884

7985
inline void badSimpleFunction() {}
8086
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: 'badSimpleFunction' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
87+
// CHECK-FIXES: LIBC_INLINE inline void badSimpleFunction() {}
8188

8289
void LIBC_INLINE badSimpleFunctionWrongLocation() {}
8390
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: 'badSimpleFunctionWrongLocation' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
@@ -93,6 +100,7 @@ template <typename T> LIBC_INLINE void goodTemplateFunction() {}
93100

94101
template <typename T> inline void badTemplateFunction() {}
95102
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: 'badTemplateFunction' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
103+
// CHECK-FIXES: template <typename T> LIBC_INLINE inline void badTemplateFunction() {}
96104

97105
template <typename T> void LIBC_INLINE badTemplateFunctionWrongLocation() {}
98106
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: 'badTemplateFunctionWrongLocation' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
@@ -108,9 +116,11 @@ template <typename... Ts> LIBC_INLINE void goodVariadicFunction() {}
108116

109117
template <typename... Ts> inline void badVariadicFunction() {}
110118
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 'badVariadicFunction' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
119+
// CHECK-FIXES: template <typename... Ts> LIBC_INLINE inline void badVariadicFunction() {}
111120

112121
template <typename... Ts> void LIBC_INLINE badVariadicFunctionWrongLocation() {}
113122
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 'badVariadicFunctionWrongLocation' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
123+
// CHECK-FIXES: template <typename... Ts> LIBC_INLINE void LIBC_INLINE badVariadicFunctionWrongLocation() {}
114124

115125
struct NoTemplate {
116126
void goodNoTemplate();
@@ -137,22 +147,27 @@ LIBC_INLINE void NoTemplate::goodNoTemplate() {}
137147

138148
inline void NoTemplate::badNoTemplate() {}
139149
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: 'badNoTemplate' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
150+
// CHECK-FIXES: LIBC_INLINE inline void NoTemplate::badNoTemplate() {}
140151

141152
void LIBC_INLINE NoTemplate::badNoTemplateWrongLocation() {}
142153
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: 'badNoTemplateWrongLocation' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
154+
// CHECK-FIXES: LIBC_INLINE void LIBC_INLINE NoTemplate::badNoTemplateWrongLocation() {}
143155

144156
template <typename T> LIBC_INLINE void NoTemplate::goodNestedTemplate() {}
145157

146158
template <typename T> inline void NoTemplate::badNestedTemplate() {}
147159
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: 'badNestedTemplate' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
160+
// CHECK-FIXES: template <typename T> LIBC_INLINE inline void NoTemplate::badNestedTemplate() {}
148161

149162
template <typename T> void LIBC_INLINE NoTemplate::badNestedTemplateWrongLocation() {}
150163
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: 'badNestedTemplateWrongLocation' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
164+
// CHECK-FIXES: template <typename T> LIBC_INLINE void LIBC_INLINE NoTemplate::badNestedTemplateWrongLocation() {}
151165

152166
template <typename... Ts> LIBC_INLINE void NoTemplate::goodVariadicTemplate() {}
153167

154168
template <typename... Ts> void inline NoTemplate::badVariadicTemplate() {}
155169
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 'badVariadicTemplate' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
170+
// CHECK-FIXES: template <typename... Ts> LIBC_INLINE void inline NoTemplate::badVariadicTemplate() {}
156171

157172
template <typename... Ts> void LIBC_INLINE NoTemplate::badVariadicTemplateWrongLocation() {}
158173
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 'badVariadicTemplateWrongLocation' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
@@ -182,6 +197,7 @@ template <typename T> LIBC_INLINE void SimpleTemplate<T>::goodSimpleTemplate() {
182197

183198
template <typename T> inline void SimpleTemplate<T>::badSimpleTemplate() {}
184199
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: 'badSimpleTemplate' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
200+
// CHECK-FIXES: template <typename T> LIBC_INLINE inline void SimpleTemplate<T>::badSimpleTemplate() {}
185201

186202
template <typename T> void LIBC_INLINE SimpleTemplate<T>::badSimpleTemplateWrongLocation() {}
187203
// CHECK-MESSAGES: :[[@LINE-1]]:23: warning: 'badSimpleTemplateWrongLocation' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
@@ -190,6 +206,7 @@ template <typename T> template <typename U> LIBC_INLINE void SimpleTemplate<T>::
190206

191207
template <typename T> template <typename U> inline void SimpleTemplate<T>::badNestedTemplate() {}
192208
// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: 'badNestedTemplate' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
209+
// CHECK-FIXES: template <typename T> template <typename U> LIBC_INLINE inline void SimpleTemplate<T>::badNestedTemplate() {}
193210

194211
template <typename T> template <typename U> void LIBC_INLINE SimpleTemplate<T>::badNestedTemplateWrongLocation() {}
195212
// CHECK-MESSAGES: :[[@LINE-1]]:45: warning: 'badNestedTemplateWrongLocation' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
@@ -198,6 +215,7 @@ template <typename T> template <typename... Ts> LIBC_INLINE void SimpleTemplate<
198215

199216
template <typename T> template <typename... Ts> inline void SimpleTemplate<T>::badNestedVariadicTemplate() {}
200217
// CHECK-MESSAGES: :[[@LINE-1]]:49: warning: 'badNestedVariadicTemplate' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
218+
// CHECK-FIXES: template <typename T> template <typename... Ts> LIBC_INLINE inline void SimpleTemplate<T>::badNestedVariadicTemplate() {}
201219

202220
template <typename T> template <typename... Ts> void LIBC_INLINE SimpleTemplate<T>::badNestedVariadicTemplateWrongLocation() {}
203221
// CHECK-MESSAGES: :[[@LINE-1]]:49: warning: 'badNestedVariadicTemplateWrongLocation' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
@@ -227,6 +245,7 @@ template <typename... Ts> LIBC_INLINE void VariadicTemplate<Ts...>::goodVariadic
227245

228246
template <typename... Ts> inline void VariadicTemplate<Ts...>::badVariadicTemplate() {}
229247
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 'badVariadicTemplate' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
248+
// CHECK-FIXES: template <typename... Ts> LIBC_INLINE inline void VariadicTemplate<Ts...>::badVariadicTemplate() {}
230249

231250
template <typename... Ts> void LIBC_INLINE VariadicTemplate<Ts...>::badVariadicTemplateWrongLocation() {}
232251
// CHECK-MESSAGES: :[[@LINE-1]]:27: warning: 'badVariadicTemplateWrongLocation' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
@@ -235,6 +254,7 @@ template <typename... Ts> template <typename U> LIBC_INLINE void VariadicTemplat
235254

236255
template <typename... Ts> template <typename U> inline void VariadicTemplate<Ts...>::badNestedTemplate() {}
237256
// CHECK-MESSAGES: :[[@LINE-1]]:49: warning: 'badNestedTemplate' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
257+
// CHECK-FIXES: template <typename... Ts> template <typename U> LIBC_INLINE inline void VariadicTemplate<Ts...>::badNestedTemplate() {}
238258

239259
template <typename... Ts> template <typename U> void LIBC_INLINE VariadicTemplate<Ts...>::badNestedTemplateWrongLocation() {}
240260
// CHECK-MESSAGES: :[[@LINE-1]]:49: warning: 'badNestedTemplateWrongLocation' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
@@ -243,6 +263,7 @@ template <typename... Ts> template <typename... Us> LIBC_INLINE void VariadicTem
243263

244264
template <typename... Ts> template <typename... Us> inline void VariadicTemplate<Ts...>::badNestedVariadicTemplate() {}
245265
// CHECK-MESSAGES: :[[@LINE-1]]:53: warning: 'badNestedVariadicTemplate' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]
266+
// CHECK-FIXES: template <typename... Ts> template <typename... Us> LIBC_INLINE inline void VariadicTemplate<Ts...>::badNestedVariadicTemplate() {}
246267

247268
template <typename... Ts> template <typename... Us> void LIBC_INLINE VariadicTemplate<Ts...>::badNestedVariadicTemplateWrongLocation() {}
248269
// CHECK-MESSAGES: :[[@LINE-1]]:53: warning: 'badNestedVariadicTemplateWrongLocation' must be tagged with the LIBC_INLINE macro; the macro should be placed at the beginning of the declaration [llvmlibc-inline-function-decl]

0 commit comments

Comments
 (0)