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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
|
#!/usr/bin/env python3
import sys
import os
import subprocess
import re
import json
import threading
import multiprocessing
import argparse
import io
import shutil
from threading import Thread
from sys import platform as _platform
import platform
# cd into the folder containing this script
os.chdir(os.path.realpath(os.path.dirname(sys.argv[0])))
_verbose = False
c_headerpath = False
try:
result = subprocess.run(['clang', '-v'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True, text=True)
match = re.search(r'Selected .* installation: (.*)', result.stderr)
if match:
c_headerpath = match.group(1).strip()
except:
pass
def isWindows():
return _platform == 'win32'
def isMacOS():
return _platform == 'darwin'
def isLinux():
return _platform.startswith('linux')
class QtInstallation:
def __init__(self):
self.int_version = 000
self.qmake_header_path = "/usr/include/qt/"
self.qmake_lib_path = "/usr/lib"
def compiler_flags(self, module_includes = False):
extra_includes = ''
if isMacOS():
extra_includes = " -I%s/QtCore.framework/Headers" % self.qmake_lib_path
extra_includes += " -iframework %s" % self.qmake_lib_path
# Also include the modules folders
qt_modules_includes = []
if module_includes:
qt_modules_includes = ["-isystem " + self.qmake_header_path + "/" + f for f in next(os.walk(self.qmake_header_path))[1]];
c_header_option = ""
if c_headerpath:
c_header_option = "-isystem " + c_headerpath + "/include "
return c_header_option + "-isystem " + self.qmake_header_path + ("" if isWindows() else " -fPIC") + " -L " + self.qmake_lib_path + ' ' + extra_includes + ' '.join(qt_modules_includes)
class Test:
def __init__(self, check):
self.filenames = []
self.minimum_qt_version = 500
self.maximum_qt_version = 69999
self.minimum_clang_version = 380
self.minimum_clang_version_for_fixits = 380
self.compare_everything = False
self.link = False # If true we also call the linker
self.check = check
self.expects_failure = False
self.qt_major_versions = [5, 6]
self.env = os.environ
self.checks = []
self.flags = ""
self.must_fail = False
self.blacklist_platforms = []
self.only_qt = False
self.qt_developer = False
self.header_filter = ""
self.ignore_dirs = ""
self.has_fixits = False
self.should_run_fixits_test = False
self.should_run_on_32bit = True
self.cppStandards = ["c++14", "c++17"]
self.extra_definitions = False
self.qt_modules_includes = False
self.fixed_file_base = None
def filename(self):
if len(self.filenames) == 1:
return self.filenames[0]
return ""
def relativeFilename(self):
# example: "auto-unexpected-qstringbuilder/main.cpp"
return self.check.name + "/" + self.filename()
def yamlFilename(self, is_standalone):
# The name of the yaml file with fixits
# example: "auto-unexpected-qstringbuilder/main.cpp.clazy.yaml"
if is_standalone:
return self.relativeFilename() + ".clazy-standalone.yaml"
else:
return self.relativeFilename() + ".clazy.yaml"
def fixedFilename(self, is_standalone):
if is_standalone:
return self.relativeFilename() + ".clazy-standalone.fixed"
else:
return self.relativeFilename() + ".clazy.fixed"
def expectedFixedFilename(self):
return self.relativeFilename() + ".fixed.expected"
def isScript(self):
return self.filename().endswith(".sh")
def dir(self):
return self.check.name
def setQtMajorVersions(self, major_versions):
self.qt_major_versions = major_versions
if 4 in major_versions:
if self.minimum_qt_version >= 500:
self.minimum_qt_version = 400
def envString(self):
result = ""
for key in self.env:
result += key + '="' + self.env[key] + '" '
return result
def setEnv(self, e):
self.env = os.environ.copy()
for key in e:
if type(key) is bytes:
key = key.decode('utf-8')
self.env[key] = e[key]
def printableName(self, cppStandard, qt_major_version, is_standalone, is_tidy, is_fixits):
name = self.check.name
if len(self.check.tests) > 1:
name += "/" + self.filename()
if len(cppStandard) > 0:
name += " (" + cppStandard + ")"
if qt_major_version > 0:
name += " (Qt " + str(qt_major_version) + ")"
if is_fixits and is_standalone:
name += " (standalone, fixits)"
elif is_standalone:
name += " (standalone)"
elif is_tidy:
name += " (clang-tidy)"
elif is_fixits:
name += " (plugin, fixits)"
else:
name += " (plugin)"
return name
def removeYamlFiles(self):
for f in [self.yamlFilename(False), self.yamlFilename(True)]:
if os.path.exists(f):
os.remove(f)
class Check:
def __init__(self, name):
self.name = name
self.minimum_clang_version = 380 # clang 3.8.0
self.minimum_qt_version = 500
self.maximum_qt_version = 69999
self.enabled = True
self.clazy_standalone_only = False
self.tests = []
# -------------------------------------------------------------------------------
# utility functions #1
def get_command_output(cmd: str, test_env=os.environ, cwd=None, ignore_verbose=False):
success = True
try:
if _verbose and not ignore_verbose:
print(cmd)
# Polish up the env to fix "TypeError: environment can only contain strings" exception
str_env = {}
for key in test_env.keys():
str_env[str(key)] = str(test_env[key])
output = subprocess.check_output(
cmd, stderr=subprocess.STDOUT, shell=True, env=str_env, cwd=cwd)
except subprocess.CalledProcessError as e:
output = e.output
success = False
if type(output) is bytes:
output = output.decode('utf-8')
return output, success
def load_json(check_name: str):
check = Check(check_name)
filename = check_name + "/config.json"
if not os.path.exists(filename):
# Ignore this directory
return check
f = open(filename, 'r')
contents = f.read()
f.close()
decoded = json.loads(contents)
check_blacklist_platforms = []
if 'minimum_clang_version' in decoded:
check.minimum_clang_version = decoded['minimum_clang_version']
if 'minimum_qt_version' in decoded:
check.minimum_qt_version = decoded['minimum_qt_version']
if 'maximum_qt_version' in decoded:
check.maximum_qt_version = decoded['maximum_qt_version']
if 'enabled' in decoded:
check.enabled = decoded['enabled']
if 'clazy_standalone_only' in decoded:
check.clazy_standalone_only = decoded['clazy_standalone_only']
if 'blacklist_platforms' in decoded:
check_blacklist_platforms = decoded['blacklist_platforms']
if 'tests' in decoded:
for t in decoded['tests']:
test = Test(check)
test.blacklist_platforms = check_blacklist_platforms
if 'filename' in t:
test.filenames.append(t['filename'])
if 'filenames' in t:
test.filenames += t['filenames']
if 'minimum_qt_version' in t:
test.minimum_qt_version = t['minimum_qt_version']
else:
test.minimum_qt_version = check.minimum_qt_version
if 'maximum_qt_version' in t:
test.maximum_qt_version = t['maximum_qt_version']
else:
test.maximum_qt_version = check.maximum_qt_version
if 'minimum_clang_version' in t:
test.minimum_clang_version = t['minimum_clang_version']
else:
test.minimum_clang_version = check.minimum_clang_version
if 'minimum_clang_version_for_fixits' in t:
test.minimum_clang_version_for_fixits = t['minimum_clang_version_for_fixits']
if 'blacklist_platforms' in t:
test.blacklist_platforms = t['blacklist_platforms']
if 'compare_everything' in t:
test.compare_everything = t['compare_everything']
if 'link' in t:
test.link = t['link']
if 'qt_major_versions' in t:
test.setQtMajorVersions(t['qt_major_versions'])
if 'env' in t:
test.setEnv(t['env'])
if 'checks' in t:
test.checks = t['checks']
if 'flags' in t:
test.flags = t['flags']
if 'must_fail' in t:
test.must_fail = t['must_fail']
if 'has_fixits' in t:
test.has_fixits = t['has_fixits'] and test.minimum_clang_version_for_fixits <= CLANG_VERSION
if 'expects_failure' in t:
test.expects_failure = t['expects_failure']
if 'only_qt' in t:
test.only_qt = t['only_qt']
if 'cppStandards' in t:
test.cppStandards = t['cppStandards']
if 'qt_developer' in t:
test.qt_developer = t['qt_developer']
if 'extra_definitions' in t:
test.extra_definitions = " " + t['extra_definitions'] + " "
if 'header_filter' in t:
test.header_filter = t['header_filter']
if 'ignore_dirs' in t:
test.ignore_dirs = t['ignore_dirs']
if 'should_run_on_32bit' in t:
test.should_run_on_32bit = t['should_run_on_32bit']
if 'qt_modules_includes' in t:
test.qt_modules_includes = t['qt_modules_includes']
if not test.checks:
test.checks.append(test.check.name)
check.tests.append(test)
return check
def find_qt_installation(major_version, qmakes):
installation = QtInstallation()
for qmake in qmakes:
qmake_version_str, success = get_command_output(qmake + " -query QT_VERSION")
if success and qmake_version_str.startswith(str(major_version) + "."):
qmake_header_path = get_command_output(
qmake + " -query QT_INSTALL_HEADERS")[0].strip()
qmake_lib_path = get_command_output(
qmake + " -query QT_INSTALL_LIBS")[0].strip()
if qmake_header_path:
installation.qmake_header_path = qmake_header_path
if qmake_lib_path:
installation.qmake_lib_path = qmake_lib_path
ver = qmake_version_str.split('.')
installation.int_version = int(
ver[0]) * 10000 + int(ver[1]) * 100 + int(ver[2])
if _verbose:
print("Found Qt " + str(installation.int_version) +
" using qmake " + qmake)
break
if installation.int_version == 0:
print("Error: Couldn't find a Qt" + str(major_version) + " installation")
return installation
def libraryName():
if 'CLAZYPLUGIN_CXX' in os.environ: # Running tests uninstalled
return os.environ['CLAZYPLUGIN_CXX']
elif _platform == 'win32':
return 'ClazyPlugin.dll'
elif _platform == 'darwin':
return 'ClazyPlugin.dylib'
else:
return 'ClazyPlugin.so'
def clangTidyPluginName():
if 'CLANGTIDYPLUGIN_CXX' in os.environ: # Running tests uninstalled
return os.environ['CLANGTIDYPLUGIN_CXX']
return "ClazyClangTidy.so"
def link_flags(qt:QtInstallation):
is_qt6 = qt.int_version > 60000
major_version = "6" if is_qt6 else "5"
flags = f"-lQt{major_version}Core -lQt{major_version}Gui -lQt{major_version}Widgets"
if _platform.startswith('linux'):
flags += " -lstdc++"
if is_qt6:
flags += " -lQt6StateMachine"
debian_extra_libs = os.getenv("DEBIAN_EXTRA_LIBS")
if debian_extra_libs:
flags += " " + debian_extra_libs
return flags
def clazy_cpp_args(cppStandard):
return ' -Wno-unused-value -Qunused-arguments -std=' + cppStandard + ' '
def clazy_standalone_binary():
if 'CLAZYSTANDALONE_CXX' in os.environ: # in case we want to use "clazy.AppImage --standalone" instead
return os.environ['CLAZYSTANDALONE_CXX']
return 'clazy-standalone'
def more_clazy_standalone_args():
if 'CLANG_BUILTIN_INCLUDE_DIR' in os.environ:
return ' -I ' + os.environ['CLANG_BUILTIN_INCLUDE_DIR']
return ''
def clazy_standalone_command(test: Test, cppStandard, qt):
result = " -- " + clazy_cpp_args(cppStandard) + \
qt.compiler_flags(test.qt_modules_includes) + " " + test.flags + more_clazy_standalone_args()
result = " -checks=" + ','.join(test.checks) + " " + result + suppress_line_numbers_opt
if test.has_fixits:
result = " -export-fixes=" + \
test.yamlFilename(is_standalone=True) + result
if test.only_qt:
result = " -only-qt " + result
if test.qt_developer:
result = " -qt-developer " + result
if test.header_filter:
result = " -header-filter " + test.header_filter + " " + result
if test.ignore_dirs:
result = " -ignore-dirs " + test.ignore_dirs + " " + result
if test.extra_definitions:
result += test.extra_definitions
return result
def clang_name():
return os.getenv('CLANGXX', 'clang')
def clazy_command(test: Test, cppStandard, qt, filename):
if test.isScript():
return "./" + filename
if 'CLAZY_CXX' in os.environ: # In case we want to use clazy.bat
result = os.environ['CLAZY_CXX']
else:
result = clang_name() + " -Xclang -load -Xclang " + libraryName() + " -Xclang -add-plugin -Xclang clazy "
result += clazy_cpp_args(cppStandard) + qt.compiler_flags(test.qt_modules_includes) + suppress_line_numbers_opt
if test.only_qt:
result = result + " -Xclang -plugin-arg-clazy -Xclang only-qt "
if test.qt_developer:
result = result + " -Xclang -plugin-arg-clazy -Xclang qt-developer "
if test.extra_definitions:
result += test.extra_definitions
# Linking on one platform is enough. Won't waste time on macOS and Windows.
if test.link and _platform.startswith('linux'):
result = result + " " + link_flags(qt)
else:
result = result + " -c "
result = result + test.flags + \
" -Xclang -plugin-arg-clazy -Xclang " + ','.join(test.checks) + " "
if test.has_fixits:
result += _export_fixes_argument + " "
result += filename
return result
def clang_tidy_command(test: Test, cppStandard, qt, filename):
command = os.getenv('CLAZY_CLANG_TIDY', 'clang-tidy')
command += f" {filename}"
# disable all checks, re-enable clazy ones
checks = ','.join("clazy-" + check for check in test.checks)
command += f" -checks='-*,{checks}' -header-filter='.*' -system-headers -load='{clangTidyPluginName()}'"
# Add extra compiler flags
command += " -- "
command += f" {test.flags} "
command += clazy_cpp_args(cppStandard)
command += qt.compiler_flags(test.qt_modules_includes)
command += suppress_line_numbers_opt
if test.extra_definitions:
command += test.extra_definitions
return command
def dump_ast_command(test: Test, cppStandard, qt_major_version):
return "clang -std=" + cppStandard + " -fsyntax-only -Xclang -ast-dump -fno-color-diagnostics -c " + qt_installation(qt_major_version).compiler_flags(test.qt_modules_includes) + " " + test.flags + " " + test.filename()
def compiler_name():
if 'CLAZY_CXX' in os.environ:
return os.environ['CLAZY_CXX'] # so we can set clazy.bat instead
return os.getenv('CLANGXX', 'clang')
# -------------------------------------------------------------------------------
# Setup argparse
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", action='store_true')
parser.add_argument("--no-standalone", action='store_true',
help="Don\'t run clazy-standalone")
parser.add_argument("--no-clang-tidy", action='store_true',
help="Don\'t run clang-tidy")
parser.add_argument("--no-fixits", action='store_true',
help='Don\'t run fixits')
parser.add_argument("--only-standalone", action='store_true',
help='Only run clazy-standalone')
parser.add_argument("--dump-ast", action='store_true',
help='Dump a unit-test AST to file')
parser.add_argument("--qt-versions", type=int, choices=[5, 6], nargs='+', default=[5, 6],
help='Specify one or more Qt versions to use (default: 5 and 6)')
parser.add_argument(
"--exclude", help='Comma separated list of checks to ignore')
parser.add_argument("-j", "--jobs", type=int, default=multiprocessing.cpu_count(),
help='Parallel jobs to run (defaults to %(default)s)')
parser.add_argument("check_names", nargs='*',
help="The name of the check whose unit-tests will be run. Defaults to running all checks.")
args = parser.parse_args()
if args.only_standalone and args.no_standalone:
print("Error: --only-standalone is incompatible with --no-standalone")
sys.exit(1)
# -------------------------------------------------------------------------------
# Global variables
_export_fixes_argument = "-Xclang -plugin-arg-clazy -Xclang export-fixes"
_dump_ast = args.dump_ast
_verbose = args.verbose
_no_standalone = args.no_standalone
_no_clang_tidy = args.no_clang_tidy
_no_fixits = args.no_fixits
_only_standalone = args.only_standalone
_num_threads = args.jobs
_lock = threading.Lock()
_was_successful = True
if 6 in args.qt_versions:
_qt6_installation = find_qt_installation(6, ["QT_SELECT=6 qmake", "qmake-qt6", "qmake", "qmake6"])
else:
_qt6_installation = None
if 5 in args.qt_versions:
_qt5_installation = find_qt_installation(5, ["QT_SELECT=5 qmake", "qmake-qt5", "qmake", "qmake5"])
else:
_qt5_installation = None
if (_qt5_installation is None or _qt5_installation.int_version == 0) \
and (_qt6_installation is None or _qt6_installation.int_version == 0):
sys.exit(1)
_excluded_checks = args.exclude.split(',') if args.exclude is not None else []
# -------------------------------------------------------------------------------
# utility functions #2
version, success = get_command_output(compiler_name() + ' --version')
match = re.search('clang version ([^\\s-]+)', version)
try:
version = match.group(1)
except:
# Now try the Clazy.AppImage way
match = re.search('clang version: (.*)', version)
try:
version = match.group(1)
except:
splitted = version.split()
if len(splitted) > 2:
version = splitted[2]
else:
print("Could not determine clang version, is it in PATH?")
sys.exit(-1)
if _verbose:
print('Found clang version: ' + str(version))
CLANG_VERSION = int(version.replace('git', '').replace('.', ''))
suppress_line_numbers_opt = " -fno-diagnostics-show-line-numbers"
def qt_installation(major_version):
if major_version == 6:
return _qt6_installation
elif major_version == 5:
return _qt5_installation
return None
def run_command(cmd, output_file="", test_env=os.environ, cwd=None, ignore_verbose_command=False, is_tidy=False):
lines, success = get_command_output(cmd, test_env, cwd=cwd, ignore_verbose=ignore_verbose_command)
# Hack for Windows, we have std::_Vector_base in the expected data
lines = lines.replace("std::_Container_base0", "std::_Vector_base")
lines = lines.replace("std::__1::__vector_base_common",
"std::_Vector_base") # Hack for macOS
lines = lines.replace("std::_Vector_alloc", "std::_Vector_base")
# clang-tidy prints the tags slightly different
if is_tidy:
lines = lines.replace("[clazy", "[-Wclazy")
if not success and not output_file:
print(lines)
return False
if _verbose:
print("Running: " + cmd)
print("output_file=" + output_file)
lines = lines.replace('\r\n', '\n')
if lines.endswith('\n\n'):
lines = lines[:-1] # remove *only* one extra newline, all testfiles have a newline based on unix convention
if output_file:
f = io.open(output_file, 'w', encoding='utf8')
f.writelines(lines)
f.close()
elif len(lines) > 0:
print(lines)
return success
def files_are_equal(file1, file2):
try:
f = io.open(file1, 'r', encoding='utf-8')
lines1 = f.readlines()
f.close()
f = io.open(file2, 'r', encoding='utf-8')
lines2 = f.readlines()
f.close()
return lines1 == lines2
except Exception as ex:
print("Error comparing files:" + str(ex))
return False
def compare_files(expects_failure, expected_file, result_file, message):
success = files_are_equal(expected_file, result_file)
if expects_failure:
if success:
print("[XOK] " + message)
return False
else:
print("[XFAIL] " + message)
print_differences(expected_file, result_file)
return True
else:
if success:
print("[OK] " + message)
return True
else:
print("[FAIL] " + message)
print_differences(expected_file, result_file)
return False
def get_check_names():
return list(filter(lambda entry: os.path.isdir(entry), os.listdir(".")))
# The yaml file references the test file in our git repo, but we don't want
# to rewrite that one, as we would need to discard git changes afterwards,
# so patch the yaml file and add a ".fixed" suffix to those files
def patch_fixit_yaml_file(test, is_standalone):
yamlfilename = test.yamlFilename(is_standalone)
fixedfilename = test.fixedFilename(is_standalone)
f = open(yamlfilename, 'r')
lines = f.readlines()
f.close()
f = open(yamlfilename, 'w')
possible_headerfile = test.relativeFilename().replace(".cpp", ".h")
for line in lines:
stripped = line.strip()
if stripped.startswith('MainSourceFile') or stripped.startswith("FilePath") or stripped.startswith("- FilePath"):
line = line.replace(test.relativeFilename(), fixedfilename)
# For Windows:
line = line.replace(test.relativeFilename().replace(
'/', '\\'), fixedfilename.replace('/', '\\'))
# Some tests also apply fix their to their headers:
if not test.relativeFilename().endswith(".hh"):
line = line.replace(possible_headerfile,
fixedfilename.replace(".cpp", ".h"))
f.write(line)
f.close()
shutil.copyfile(test.relativeFilename(), fixedfilename)
if os.path.exists(possible_headerfile):
shutil.copyfile(possible_headerfile,
fixedfilename.replace(".cpp", ".h"))
return True
def run_clang_apply_replacements(check):
command = os.getenv('CLAZY_CLANG_APPLY_REPLACEMENTS',
'clang-apply-replacements')
return run_command(command + ' ' + check.name)
def cleanup_fixit_files(checks):
for check in checks:
filestodelete = list(filter(lambda entry: entry.endswith(
'.fixed') or entry.endswith('.yaml'), os.listdir(check.name)))
for f in filestodelete:
os.remove(check.name + '/' + f)
def print_differences(file1, file2):
# Returns true if the the files are equal
return run_command("diff -Naur --strip-trailing-cr {} {}".format(file1, file2))
def normalizedCwd():
if _platform.startswith('linux'):
return subprocess.check_output("pwd -L", shell=True, universal_newlines=True).rstrip('\n')
else:
return os.getcwd().replace('\\', '/')
def extract_word(word, in_file, out_file):
in_f = io.open(in_file, 'r', encoding='utf-8')
out_f = io.open(out_file, 'w', encoding='utf-8')
for line in in_f:
if '[-Wdeprecated-declarations]' in line:
continue
if word in line:
line = line.replace('\\', '/')
# clazy-standalone prints the complete cpp file path for some reason. Normalize it so it compares OK with the expected output.
line = line.replace(f"{normalizedCwd()}/", "")
out_f.write(line)
in_f.close()
out_f.close()
def print_file(filename):
f = open(filename, 'r')
print(f.read())
f.close()
def file_contains(filename, text):
f = io.open(filename, 'r', encoding='utf-8')
contents = f.read()
f.close()
return text in contents
def is32Bit():
return platform.architecture()[0] == '32bit'
def run_unit_test(test, is_standalone, is_tidy, cppStandard, qt_major_version):
if test.check.clazy_standalone_only and not is_standalone:
return True
qt = qt_installation(qt_major_version)
if qt is None:
return True # silently skip
if is_tidy and (test.isScript() or test.only_qt or test.qt_developer):
print("Options not supported with clang-tidy")
return True
if _verbose:
print("Qt major versions required by the test: " + str(test.qt_major_versions))
print("Currently considering Qt major version: " + str(qt_major_version))
print("Qt versions required by the test: min=" + str(test.minimum_qt_version) + " max=" + str(test.maximum_qt_version))
print("Qt int version: " + str(qt.int_version))
print("Qt headers: " + qt.qmake_header_path)
printableName = test.printableName(cppStandard, qt_major_version, is_standalone, is_tidy, False)
if qt.int_version < test.minimum_qt_version or qt.int_version > test.maximum_qt_version or CLANG_VERSION < test.minimum_clang_version:
if (_verbose):
print(f"Skipping {printableName}because required version is not available")
return True
if _platform in test.blacklist_platforms:
if (_verbose):
print(f"Skipping {printableName} because it is blacklisted for this platform")
return True
if not test.should_run_on_32bit and is32Bit():
if (_verbose):
print(f"Skipping {printableName} because it is blacklisted on 32bit")
return True
checkname = test.check.name
filename = checkname + "/" + test.filename()
output_file = filename + ".out"
result_file = filename + ".result"
expected_file = filename + ".expected"
expected_file_tidy = filename + ".expected.tidy"
if is_tidy and os.path.exists(expected_file_tidy):
expected_file = expected_file_tidy
elif not os.path.exists(expected_file):
expected_file = filename + ".qt" + str(qt_major_version) + ".expected"
# Some tests have different output on 32 bit
if is32Bit() and os.path.exists(expected_file + '.x86'):
expected_file = expected_file + '.x86'
if is_standalone and test.isScript():
return True
if is_standalone:
cmd_to_run = clazy_standalone_binary() + " " + filename + " " + \
clazy_standalone_command(test, cppStandard, qt)
elif is_tidy:
cmd_to_run = clang_tidy_command(test, cppStandard, qt, filename)
else:
cmd_to_run = clazy_command(test, cppStandard, qt, filename)
if test.compare_everything:
result_file = output_file
must_fail = test.must_fail
cmd_success = run_command(cmd_to_run, output_file, test.env, ignore_verbose_command=True, is_tidy=is_tidy)
if file_contains(output_file, 'Invalid check: '):
return True
if (not cmd_success and not must_fail) or (cmd_success and must_fail):
print(f"[FAIL] {printableName} (Failed to build test. Check {output_file} for details)")
print("-------------------")
print(f"Contents of {output_file}:")
print_file(output_file)
print("-------------------")
return False
if not test.compare_everything:
word_to_grep = "warning:" if not must_fail else "error:"
extract_word(word_to_grep, output_file, result_file)
# Check that it printed the expected warnings
if not compare_files(test.expects_failure, expected_file, result_file, printableName):
return False
if test.has_fixits and not is_tidy:
# The normal tests succeeded, we can run the respective fixits then
test.should_run_fixits_test = True
return True
def run_unit_test_for_each_configuration(test, is_standalone, is_tidy=False):
if test.check.clazy_standalone_only and not is_standalone:
return True
result = True
for qt_major_version in test.qt_major_versions:
for cppStandard in test.cppStandards:
if cppStandard == "c++14" and qt_major_version == 6: # Qt6 requires C++17
continue
if cppStandard == "c++17" and qt_major_version == 5 and len(test.cppStandards) > 1: # valid combination but let's skip it unless it was the only specified standard
continue
result = result and run_unit_test(test, is_standalone, is_tidy, cppStandard, qt_major_version)
return result
def run_unit_tests(tests):
result = True
for test in tests:
test_result = True
if not _only_standalone:
test_result = run_unit_test_for_each_configuration(test, False)
result = result and test_result
if not _no_standalone:
test_result = test_result and run_unit_test_for_each_configuration(test, True)
result = result and test_result
if not _no_clang_tidy:
test_result = test_result and run_unit_test_for_each_configuration(test, False, True)
result = result and test_result
if not test_result:
test.removeYamlFiles()
global _was_successful, _lock
with _lock:
_was_successful = _was_successful and result
def patch_yaml_files(requested_checks, is_standalone):
if (is_standalone and _no_standalone) or (not is_standalone and _only_standalone):
# Nothing to do
return True
success = True
for check in requested_checks:
for test in check.tests:
if test.should_run_fixits_test:
yamlfilename = test.yamlFilename(is_standalone)
if not os.path.exists(yamlfilename):
print("[FAIL] " + yamlfilename + " is missing!!")
success = False
continue
if not patch_fixit_yaml_file(test, is_standalone):
print("[FAIL] Could not patch " + yamlfilename)
success = False
continue
return success
def compare_fixit_results(test, is_standalone):
if (is_standalone and _no_standalone) or (not is_standalone and _only_standalone):
# Nothing to do
return True
# Check that the rewritten file is identical to the expected one
if not compare_files(False, test.expectedFixedFilename(), test.fixedFilename(is_standalone), test.printableName("", 0, is_standalone, False, True)):
return False
# Some fixed cpp files have an header that was also fixed. Compare it here too.
possible_headerfile_expected = test.expectedFixedFilename().replace('.cpp', '.h')
if os.path.exists(possible_headerfile_expected):
possible_headerfile = test.fixedFilename(is_standalone).replace('.cpp', '.h')
if not compare_files(False, possible_headerfile_expected, possible_headerfile, test.printableName("", 0, is_standalone, False, True).replace('.cpp', '.h')):
return False
return True
# This is run sequentially, due to races. As clang-apply-replacements just applies all .yaml files it can find.
# We run a single clang-apply-replacements invocation, which changes all files in the tests/ directory.
def run_fixit_tests(requested_checks):
success = patch_yaml_files(requested_checks, is_standalone=False)
success = patch_yaml_files(
requested_checks, is_standalone=True) and success
for check in requested_checks:
if not any(map(lambda test: test.should_run_fixits_test, check.tests)):
continue
# Call clazy-apply-replacements[.exe]
if not run_clang_apply_replacements(check):
return False
# Now compare all the *.fixed files with the *.fixed.expected counterparts
for test in check.tests:
if test.should_run_fixits_test:
# Check that the rewritten file is identical to the expected one
if not compare_fixit_results(test, is_standalone=False):
success = False
continue
if not compare_fixit_results(test, is_standalone=True):
success = False
continue
return success
def dump_ast(check):
for test in check.tests:
for cppStandard in test.cppStandards:
for version in test.qt_major_versions:
if version == 6 and cppStandard == "c++14":
continue # Qt6 requires C++17
ast_filename = test.filename() + f"_{cppStandard}_{version}.ast"
run_command(dump_ast_command(test, cppStandard, version) + " > " + ast_filename)
print("Dumped AST to " + os.getcwd() + "/" + ast_filename)
def load_checks(all_check_names):
checks = []
for name in all_check_names:
try:
check = load_json(name)
if check.enabled:
checks.append(check)
except:
print("Error while loading " + name)
raise
return checks
if 'CLAZY_NO_WERROR' in os.environ:
del os.environ['CLAZY_NO_WERROR']
os.environ['CLAZY_CHECKS'] = ''
all_check_names = get_check_names()
all_checks = load_checks(all_check_names)
requested_check_names = args.check_names
requested_check_names = list(
map(lambda x: x.strip("/\\"), requested_check_names))
for check_name in requested_check_names:
if check_name not in all_check_names:
print("Unknown check: " + check_name)
sys.exit(-1)
if not requested_check_names:
requested_check_names = all_check_names
requested_checks = list(filter(
lambda check: check.name in requested_check_names and check.name not in _excluded_checks, all_checks))
requested_checks = list(filter(
lambda check: check.minimum_clang_version <= CLANG_VERSION, requested_checks))
threads = []
if _dump_ast:
for check in requested_checks:
os.chdir(check.name)
dump_ast(check)
os.chdir("..")
else:
cleanup_fixit_files(requested_checks)
# Each list is a list of Test to be worked on by a thread
list_of_chunks = [[] for _ in range(_num_threads)]
i = _num_threads
for check in requested_checks:
for test in check.tests:
i = (i + 1) % _num_threads
list_of_chunks[i].append(test)
for tests in list_of_chunks:
if not tests:
continue
t = Thread(target=run_unit_tests, args=(tests,))
t.start()
threads.append(t)
for thread in threads:
thread.join()
if not _no_fixits and not run_fixit_tests(requested_checks):
_was_successful = False
if _was_successful:
print("SUCCESS")
sys.exit(0)
else:
print("FAIL")
sys.exit(-1)
|