2019-04-15 16:57:16 +02:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
# Create Cppcheck project file
|
2021-12-19 12:36:11 +01:00
|
|
|
import sys
|
|
|
|
|
|
|
|
|
2019-04-15 20:02:17 +02:00
|
|
|
def create_gui_project_file(project_file, root_path=None, import_project=None, paths=None, exclude_paths=None, suppressions=None, addon=None):
|
2019-04-15 16:57:16 +02:00
|
|
|
cppcheck_xml = ('<?xml version="1.0" encoding="UTF-8"?>\n'
|
|
|
|
'<project version="1">\n')
|
|
|
|
if root_path:
|
|
|
|
cppcheck_xml += ' <root name="' + root_path + '"/>\n'
|
|
|
|
if import_project:
|
|
|
|
cppcheck_xml += ' <importproject>' + import_project + '</importproject>\n'
|
|
|
|
if paths:
|
|
|
|
cppcheck_xml += ' <paths>\n'
|
|
|
|
for path in paths:
|
|
|
|
cppcheck_xml += ' <dir name="' + path + '"/>\n'
|
|
|
|
cppcheck_xml += ' </paths>\n'
|
|
|
|
if exclude_paths:
|
|
|
|
cppcheck_xml += ' <exclude>\n'
|
|
|
|
for path in exclude_paths:
|
|
|
|
cppcheck_xml += ' <path name="' + path + '"/>\n'
|
|
|
|
cppcheck_xml += ' </exclude>\n'
|
|
|
|
if suppressions:
|
|
|
|
cppcheck_xml += ' <suppressions>\n'
|
|
|
|
for suppression in suppressions:
|
|
|
|
cppcheck_xml += ' <suppression'
|
|
|
|
if 'fileName' in suppression:
|
|
|
|
cppcheck_xml += ' fileName="' + suppression['fileName'] + '"'
|
|
|
|
cppcheck_xml += '>' + suppression['id'] + '</suppression>\n'
|
|
|
|
cppcheck_xml += ' </suppressions>\n'
|
2019-04-15 20:02:17 +02:00
|
|
|
if addon:
|
|
|
|
cppcheck_xml += ' <addons>\n'
|
2021-01-31 14:27:11 +01:00
|
|
|
cppcheck_xml += ' <addon>%s</addon>\n' % addon
|
2019-04-15 20:02:17 +02:00
|
|
|
cppcheck_xml += ' </addons>\n'
|
2019-04-15 16:57:16 +02:00
|
|
|
cppcheck_xml += '</project>\n'
|
|
|
|
|
|
|
|
f = open(project_file, 'wt')
|
|
|
|
f.write(cppcheck_xml)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
2023-11-06 19:06:22 +01:00
|
|
|
def __lookup_cppcheck_exe():
|
2021-12-19 12:36:11 +01:00
|
|
|
# path the script is located in
|
|
|
|
script_path = os.path.dirname(os.path.realpath(__file__))
|
|
|
|
|
|
|
|
exe_name = "cppcheck"
|
|
|
|
|
|
|
|
if sys.platform == "win32":
|
|
|
|
exe_name += ".exe"
|
|
|
|
|
2023-12-16 17:03:03 +01:00
|
|
|
exe_path = None
|
2021-12-19 12:36:11 +01:00
|
|
|
|
2023-12-16 17:03:03 +01:00
|
|
|
if 'TEST_CPPCHECK_EXE_LOOKUP_PATH' in os.environ:
|
|
|
|
lookup_paths = [os.environ['TEST_CPPCHECK_EXE_LOOKUP_PATH']]
|
|
|
|
else:
|
|
|
|
lookup_paths = [os.path.join(script_path, '..', '..'), '.']
|
|
|
|
|
|
|
|
for base in lookup_paths:
|
|
|
|
for path in ('', 'bin', os.path.join('bin', 'debug')):
|
|
|
|
tmp_exe_path = os.path.join(base, path, exe_name)
|
|
|
|
if os.path.isfile(tmp_exe_path):
|
|
|
|
exe_path = tmp_exe_path
|
|
|
|
break
|
|
|
|
|
|
|
|
if exe_path:
|
|
|
|
print("using '{}'".format(exe_path))
|
|
|
|
return exe_path
|
2021-12-19 12:36:11 +01:00
|
|
|
|
|
|
|
|
2019-04-15 16:57:16 +02:00
|
|
|
# Run Cppcheck with args
|
2023-12-19 15:55:29 +01:00
|
|
|
def cppcheck(args, env=None, remove_checkers_report=True):
|
2023-11-06 19:06:22 +01:00
|
|
|
exe = __lookup_cppcheck_exe()
|
2021-12-19 12:36:11 +01:00
|
|
|
assert exe is not None, 'no cppcheck binary found'
|
2019-07-09 07:46:53 +02:00
|
|
|
|
2019-06-22 19:20:15 +02:00
|
|
|
logging.info(exe + ' ' + ' '.join(args))
|
2023-08-31 13:33:29 +02:00
|
|
|
p = subprocess.Popen([exe] + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
|
2019-04-15 16:57:16 +02:00
|
|
|
comm = p.communicate()
|
|
|
|
stdout = comm[0].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
|
|
|
|
stderr = comm[1].decode(encoding='utf-8', errors='ignore').replace('\r\n', '\n')
|
2023-12-19 15:55:29 +01:00
|
|
|
if remove_checkers_report:
|
|
|
|
if stderr.find('[checkersReport]\n') > 0:
|
|
|
|
start_id = stderr.find('[checkersReport]\n')
|
|
|
|
start_line = stderr.rfind('\n', 0, start_id)
|
|
|
|
if start_line <= 0:
|
|
|
|
stderr = ''
|
|
|
|
else:
|
|
|
|
stderr = stderr[:start_line + 1]
|
|
|
|
elif stderr.find(': (information) Active checkers: ') >= 0:
|
|
|
|
pos = stderr.find(': (information) Active checkers: ')
|
|
|
|
if pos == 0:
|
|
|
|
stderr = ''
|
|
|
|
elif stderr[pos - 1] == '\n':
|
|
|
|
stderr = stderr[:pos]
|
2019-04-15 16:57:16 +02:00
|
|
|
return p.returncode, stdout, stderr
|
2023-11-06 19:06:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
def assert_cppcheck(args, ec_exp=None, out_exp=None, err_exp=None, env=None):
|
|
|
|
exitcode, stdout, stderr = cppcheck(args, env)
|
|
|
|
if ec_exp is not None:
|
|
|
|
assert exitcode == ec_exp, stdout
|
|
|
|
if out_exp is not None:
|
|
|
|
out_lines = stdout.splitlines()
|
2023-11-09 10:17:30 +01:00
|
|
|
assert out_lines == out_exp, stdout
|
2023-11-06 19:06:22 +01:00
|
|
|
if err_exp is not None:
|
|
|
|
err_lines = stderr.splitlines()
|
2023-11-09 10:17:30 +01:00
|
|
|
assert err_lines == err_exp, stderr
|