8
8
#
9
9
# ===------------------------------------------------------------------------===#
10
10
11
- r """
11
+ """
12
12
ClangTidy Test Helper
13
13
=====================
14
14
15
- This script runs clang-tidy in fix mode and verify fixes, messages or both.
15
+ This script is used to simplify writing, running, and debugging tests compatible
16
+ with llvm-lit. By default it runs clang-tidy in fix mode and uses FileCheck to
17
+ verify messages and/or fixes.
18
+
19
+ For debugging, with --export, the tool simply exports fixes to a provided file
20
+ and does not run FileCheck.
21
+
22
+ Extra arguments, those after the first -- if any, are passed to either
23
+ clang-tidy or clang:
24
+ * Arguments between the first -- and second -- are clang-tidy arguments.
25
+ * May be only whitespace if there are no clang-tidy arguments.
26
+ * clang-tidy's --config would go here.
27
+ * Arguments after the second -- are clang arguments
16
28
17
- Usage:
18
- check_clang_tidy.py [-resource-dir=<resource-dir>] \
19
- [-assume-filename=<file-with-source-extension>] \
20
- [-check-suffix=<comma-separated-file-check-suffixes>] \
21
- [-check-suffixes=<comma-separated-file-check-suffixes>] \
22
- [-std=c++(98|11|14|17|20)[-or-later]] \
23
- <source-file> <check-name> <temp-file> \
24
- -- [optional clang-tidy arguments]
29
+ Examples
30
+ --------
25
31
26
- Example:
27
32
// RUN: %check_clang_tidy %s llvm-include-order %t -- -- -isystem %S/Inputs
28
33
29
- Notes:
34
+ or
35
+
36
+ // RUN: %check_clang_tidy %s llvm-include-order --export=fixes.yaml %t -std=c++20
37
+
38
+ Notes
39
+ -----
30
40
-std=c++(98|11|14|17|20)-or-later:
31
41
This flag will cause multiple runs within the same check_clang_tidy
32
42
execution. Make sure you don't have shared state across these runs.
33
43
"""
34
44
35
45
import argparse
36
46
import os
47
+ import pathlib
37
48
import re
38
49
import subprocess
39
50
import sys
@@ -88,6 +99,7 @@ def __init__(self, args, extra_args):
88
99
self .has_check_fixes = False
89
100
self .has_check_messages = False
90
101
self .has_check_notes = False
102
+ self .export = args .export
91
103
self .fixes = MessagePrefix ("CHECK-FIXES" )
92
104
self .messages = MessagePrefix ("CHECK-MESSAGES" )
93
105
self .notes = MessagePrefix ("CHECK-NOTES" )
@@ -181,7 +193,9 @@ def run_clang_tidy(self):
181
193
[
182
194
"clang-tidy" ,
183
195
self .temp_file_name ,
184
- "-fix" ,
196
+ ]
197
+ + ["-fix" if self .export is None else "--export-fixes=" + self .export ]
198
+ + [
185
199
"--checks=-*," + self .check_name ,
186
200
]
187
201
+ self .clang_tidy_extra_args
@@ -255,12 +269,14 @@ def check_notes(self, clang_tidy_output):
255
269
256
270
def run (self ):
257
271
self .read_input ()
258
- self .get_prefixes ()
272
+ if self .export is None :
273
+ self .get_prefixes ()
259
274
self .prepare_test_inputs ()
260
275
clang_tidy_output = self .run_clang_tidy ()
261
- self .check_fixes ()
262
- self .check_messages (clang_tidy_output )
263
- self .check_notes (clang_tidy_output )
276
+ if self .export is None :
277
+ self .check_fixes ()
278
+ self .check_messages (clang_tidy_output )
279
+ self .check_notes (clang_tidy_output )
264
280
265
281
266
282
def expand_std (std ):
@@ -284,7 +300,11 @@ def csv(string):
284
300
285
301
286
302
def parse_arguments ():
287
- parser = argparse .ArgumentParser ()
303
+ parser = argparse .ArgumentParser (
304
+ prog = pathlib .Path (__file__ ).stem ,
305
+ description = __doc__ ,
306
+ formatter_class = argparse .RawDescriptionHelpFormatter ,
307
+ )
288
308
parser .add_argument ("-expect-clang-tidy-error" , action = "store_true" )
289
309
parser .add_argument ("-resource-dir" )
290
310
parser .add_argument ("-assume-filename" )
@@ -298,7 +318,19 @@ def parse_arguments():
298
318
type = csv ,
299
319
help = "comma-separated list of FileCheck suffixes" ,
300
320
)
301
- parser .add_argument ("-std" , type = csv , default = ["c++11-or-later" ])
321
+ parser .add_argument (
322
+ "-export" ,
323
+ default = None ,
324
+ type = str ,
325
+ metavar = "file" ,
326
+ help = "A file to export fixes into instead of fixing." ,
327
+ )
328
+ parser .add_argument (
329
+ "-std" ,
330
+ type = csv ,
331
+ default = ["c++11-or-later" ],
332
+ help = "Passed to clang. Special -or-later values are expanded." ,
333
+ )
302
334
return parser .parse_known_args ()
303
335
304
336
0 commit comments