|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +#===- check_clang_tidy.py - ClangTidy Test Helper ------------*- python -*--===# |
| 4 | +# |
| 5 | +# The LLVM Compiler Infrastructure |
| 6 | +# |
| 7 | +# This file is distributed under the University of Illinois Open Source |
| 8 | +# License. See LICENSE.TXT for details. |
| 9 | +# |
| 10 | +#===------------------------------------------------------------------------===# |
| 11 | +# |
| 12 | +# This script is used for unit tests. |
| 13 | +# Usage: |
| 14 | +# ./check_clang_tidy.py <test_file_name>.cpp '<check_names>' |
| 15 | +# Examples: |
| 16 | +# ./check_clang_tidy.py test/aliceO2-aliceO2-namespace-naming.cpp 'aliceO2*' |
| 17 | +# or |
| 18 | +# ./check_clang_tidy.py test/aliceO2-aliceO2-namespace-naming.cpp 'aliceO2-namespace-naming' |
| 19 | +# |
| 20 | + |
| 21 | + |
| 22 | +r""" |
| 23 | +ClangTidy Test Helper |
| 24 | +===================== |
| 25 | +
|
| 26 | +This script runs clang-tidy in fix mode and verify fixes, messages or both. |
| 27 | +
|
| 28 | +Usage: |
| 29 | + check_clang_tidy.py [-resource-dir <resource-dir>] \ |
| 30 | + <source-file> <check-name> \ |
| 31 | + -- [optional clang-tidy arguments] |
| 32 | +
|
| 33 | +Example: |
| 34 | + // RUN: %check_clang_tidy %s llvm-include-order %t -- -- -isystem %S/Inputs |
| 35 | +""" |
| 36 | + |
| 37 | +import argparse |
| 38 | +import re |
| 39 | +import subprocess |
| 40 | +import sys |
| 41 | + |
| 42 | + |
| 43 | +def write_file(file_name, text): |
| 44 | + with open(file_name, 'w') as f: |
| 45 | + f.write(text) |
| 46 | + f.truncate() |
| 47 | + |
| 48 | +def main(): |
| 49 | + parser = argparse.ArgumentParser() |
| 50 | + parser.add_argument('-resource-dir') |
| 51 | + parser.add_argument('input_file_name') |
| 52 | + parser.add_argument('check_name') |
| 53 | + |
| 54 | + args, extra_args = parser.parse_known_args() |
| 55 | + |
| 56 | + resource_dir = args.resource_dir |
| 57 | + input_file_name = args.input_file_name |
| 58 | + check_name = args.check_name |
| 59 | + |
| 60 | + extension = '.cpp' |
| 61 | + if (input_file_name.endswith('.c')): |
| 62 | + extension = '.c' |
| 63 | + if (input_file_name.endswith('.hpp')): |
| 64 | + extension = '.hpp' |
| 65 | + |
| 66 | + temp_file_name = input_file_name.replace(extension, '_result'+extension) |
| 67 | + |
| 68 | + clang_tidy_extra_args = extra_args |
| 69 | + if len(clang_tidy_extra_args) == 0: |
| 70 | + clang_tidy_extra_args = ['--', '--std=c++11'] if extension == '.cpp' \ |
| 71 | + else ['--'] |
| 72 | + if resource_dir is not None: |
| 73 | + clang_tidy_extra_args.append('-resource-dir=%s' % resource_dir) |
| 74 | + |
| 75 | + with open(input_file_name, 'r') as input_file: |
| 76 | + input_text = input_file.read() |
| 77 | + |
| 78 | + has_check_fixes = input_text.find('CHECK-FIXES') >= 0 |
| 79 | + has_check_messages = input_text.find('CHECK-MESSAGES') >= 0 |
| 80 | + |
| 81 | + if not has_check_fixes and not has_check_messages: |
| 82 | + sys.exit('Neither CHECK-FIXES nor CHECK-MESSAGES found in the input') |
| 83 | + |
| 84 | + # Remove the contents of the CHECK lines to avoid CHECKs matching on |
| 85 | + # themselves. We need to keep the comments to preserve line numbers while |
| 86 | + # avoiding empty lines which could potentially trigger formatting-related |
| 87 | + # checks. |
| 88 | + cleaned_test = re.sub('// *CHECK-[A-Z-]*:[^\r\n]*', '//', input_text) |
| 89 | + |
| 90 | + write_file(temp_file_name, cleaned_test) |
| 91 | + |
| 92 | + original_file_name = temp_file_name + ".orig" |
| 93 | + write_file(original_file_name, cleaned_test) |
| 94 | + |
| 95 | + args = ['O2codecheck', temp_file_name, '-fix', '--checks=-*,' + check_name] + \ |
| 96 | + clang_tidy_extra_args |
| 97 | + print('Running ' + repr(args) + '...') |
| 98 | + |
| 99 | + try: |
| 100 | + clang_tidy_output = \ |
| 101 | + subprocess.check_output(args, stderr=subprocess.STDOUT).decode() |
| 102 | + except subprocess.CalledProcessError as e: |
| 103 | + clang_tidy_output = e.output.decode() |
| 104 | + |
| 105 | + try: |
| 106 | + diff_output = subprocess.check_output( |
| 107 | + ['diff', '-u', original_file_name, temp_file_name], |
| 108 | + stderr=subprocess.STDOUT) |
| 109 | + except subprocess.CalledProcessError as e: |
| 110 | + diff_output = e.output |
| 111 | + |
| 112 | + print('------------------------------ Fixes -----------------------------\n' + |
| 113 | + diff_output.decode() + |
| 114 | + '\n------------------------------------------------------------------') |
| 115 | + |
| 116 | + if has_check_fixes: |
| 117 | + try: |
| 118 | + subprocess.check_output( |
| 119 | + ['FileCheck', '-input-file=' + temp_file_name, input_file_name, |
| 120 | + '-check-prefix=CHECK-FIXES', '-strict-whitespace'], |
| 121 | + stderr=subprocess.STDOUT) |
| 122 | + except subprocess.CalledProcessError as e: |
| 123 | + print('FileCheck failed:\n' + e.output.decode()) |
| 124 | + raise |
| 125 | + |
| 126 | + if has_check_messages: |
| 127 | + messages_file = temp_file_name + '.msg' |
| 128 | + write_file(messages_file, clang_tidy_output) |
| 129 | + try: |
| 130 | + subprocess.check_output( |
| 131 | + ['FileCheck', '-input-file=' + messages_file, input_file_name, |
| 132 | + '-check-prefix=CHECK-MESSAGES', |
| 133 | + '-implicit-check-not={{warning|error}}:'], |
| 134 | + stderr=subprocess.STDOUT) |
| 135 | + except subprocess.CalledProcessError as e: |
| 136 | + print('FileCheck failed:\n' + e.output.decode()) |
| 137 | + raise |
| 138 | + |
| 139 | +if __name__ == '__main__': |
| 140 | + main() |
0 commit comments