Skip to content

Commit

Permalink
[fix] Arguments in --cppcheckargs are stronger
Browse files Browse the repository at this point in the history
When a C/C++ standard version is provided both in the original build
command and the --cppcheckargs file, then only the one in --cppcheckargs
should be used in the analyzer command.
  • Loading branch information
bruntib committed Oct 11, 2024
1 parent 893ffd3 commit c965ba3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
13 changes: 12 additions & 1 deletion analyzer/codechecker_analyzer/analyzers/cppcheck/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import xml.etree.ElementTree as ET

from codechecker_common.logger import get_logger
from codechecker_common import util

from codechecker_analyzer import analyzer_context, env
from codechecker_analyzer.env import get_binary_in_path
Expand Down Expand Up @@ -223,7 +224,17 @@ def construct_analyzer_cmd(self, result_handler):
analyzer_cmd.extend(config.analyzer_extra_arguments)

# Pass whitelisted parameters
analyzer_cmd.extend(self.parse_analyzer_config())
params = self.parse_analyzer_config()

def is_std(arg):
return arg.startswith("--std=")

if util.index_of(config.analyzer_extra_arguments, is_std) >= 0:
std_idx = util.index_of(params, is_std)
if std_idx >= 0:
del params[std_idx]

analyzer_cmd.extend(params)

# TODO fix this in a follow up patch, because it is failing
# the macos pypy test.
Expand Down
26 changes: 26 additions & 0 deletions analyzer/tests/functional/analyze/test_analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import shutil
import subprocess
import shlex
import tempfile
import unittest
import zipfile

Expand Down Expand Up @@ -1124,6 +1125,31 @@ def test_cppcheck_standard(self):
self.assertNotIn("iso9899:2017", out)
self.assertIn("--std=c17", out)

# Test if standard version in --cppcheckargs is stronger.
with tempfile.NamedTemporaryFile(mode='w',
encoding='utf-8') as cppcheck_args:
with open(build_json, 'w',
encoding="utf-8", errors="ignore") as outfile:
build_log = [{
"directory": self.test_workspace,
"command": "gcc -c -std=c++0x " + source_file,
"file": source_file}]
json.dump(build_log, outfile)

cppcheck_args.write("--std=c++11")
cppcheck_args.close()

analyze_cmd.extend(['--cppcheckargs', cppcheck_args.name])

out = subprocess.run(analyze_cmd,
cwd=self.test_dir,
# env=self.env,
check=False,
stdout=subprocess.PIPE).stdout.decode()

self.assertIn("--std=c++11", out)
self.assertNotIn("--std=c++0x", out)

def test_makefile_generation(self):
""" Test makefile generation. """
build_json = os.path.join(self.test_workspace, "build_extra_args.json")
Expand Down
10 changes: 10 additions & 0 deletions codechecker_common/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,13 @@ def path_for_fake_root(full_path: str, root_path: str = '/') -> str:
def strtobool(value: str) -> bool:
"""Parse a string value to a boolean."""
return value.lower() in ('y', 'yes', 't', 'true', 'on', '1')

def index_of(iterable, lambda_func) -> int:
"""Return the index of the first element in iterable for which
lambda_func returns True.
"""
for i, item in enumerate(iterable):
if lambda_func(item):
return i

return -1

0 comments on commit c965ba3

Please sign in to comment.