Skip to content

Commit

Permalink
[enhancement] --cppcheckargs flag was missing
Browse files Browse the repository at this point in the history
Analyzer config options can be forwarded to the analyzers by --saargs
and --tidyargs flags. This was missing for Cppcheck, so --cppcheckargs
flag has now been introduced.

Fixes Ericsson#3936
  • Loading branch information
bruntib committed Oct 10, 2023
1 parent 6f57fa2 commit f1ab31b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
20 changes: 19 additions & 1 deletion analyzer/codechecker_analyzer/analyzers/cppcheck/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
import os
import pickle
import re
import shlex
import shutil
import subprocess
import xml.etree.ElementTree as ET

from codechecker_common.logger import get_logger

from codechecker_analyzer import analyzer_context
from codechecker_analyzer import analyzer_context, env
from codechecker_analyzer.env import get_binary_in_path

from .. import analyzer_base
Expand Down Expand Up @@ -225,6 +226,8 @@ def construct_analyzer_cmd(self, result_handler):

analyzer_cmd.append('--plist-output=' + str(output_dir))

analyzer_cmd.extend(config.analyzer_extra_arguments)

analyzer_cmd.append(self.source_file)

return analyzer_cmd
Expand Down Expand Up @@ -389,6 +392,21 @@ def construct_config_handler(cls, args):

handler.analyzer_config = analyzer_config

try:
with open(args.cppcheck_args_cfg_file, 'r', encoding='utf8',
errors='ignore') as sa_cfg:
handler.analyzer_extra_arguments = \
re.sub(r'\$\((.*?)\)',
env.replace_env_var(args.cppcheck_args_cfg_file),
sa_cfg.read().strip())
handler.analyzer_extra_arguments = \
shlex.split(handler.analyzer_extra_arguments)
except IOError as ioerr:
LOG.debug_analyzer(ioerr)
except AttributeError as aerr:
# No cppcheck arguments file was given in the command line.
LOG.debug_analyzer(aerr)

check_env = context.analyzer_env

# Overwrite PATH to contain only the parent of the cppcheck binary.
Expand Down
7 changes: 7 additions & 0 deletions analyzer/codechecker_analyzer/cmd/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,13 @@ def add_arguments_to_parser(parser):

cmd_config.add_option(analyzer_opts)

analyzer_opts.add_argument('--cppcheckargs',
dest="cppcheck_args_cfg_file",
required=False,
default=argparse.SUPPRESS,
help="File containing argument which will be "
"forwarded verbatim for Cppcheck.")

analyzer_opts.add_argument('--saargs',
dest="clangsa_args_cfg_file",
required=False,
Expand Down
39 changes: 39 additions & 0 deletions analyzer/tests/unit/test_checker_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
from distutils import util
import os
import re
import tempfile
import unittest
from argparse import Namespace

from codechecker_analyzer.analyzers.clangsa.analyzer import ClangSA
from codechecker_analyzer.analyzers.clangtidy.analyzer import ClangTidy
from codechecker_analyzer.analyzers.cppcheck.analyzer import Cppcheck
from codechecker_analyzer.analyzers.config_handler import CheckerState
from codechecker_analyzer.analyzers.clangtidy.config_handler \
import is_compiler_warning
Expand Down Expand Up @@ -49,6 +51,9 @@ def occurring_values(self, label):
elif label == 'sei-cert':
return ['rule1', 'rule2']

def checkers(self, analyzer=None):
return []


def create_analyzer_sa():
args = []
Expand Down Expand Up @@ -434,3 +439,37 @@ def test_clang_diags_as_compiler_warnings(self):
# -Wno-unused-value.
self.assertEqual(cmd.count('-Wvla'), 1)
self.assertEqual(cmd.count('-Wvla-extension'), 1)


def create_analyzer_cppcheck(args, workspace):
cfg_handler = Cppcheck.construct_config_handler(args)

action = {
'file': 'main.cpp',
'command': "g++ -o main main.cpp",
'directory': workspace}
build_action = log_parser.parse_options(action)

return Cppcheck(cfg_handler, build_action)


class CheckerHandlingCppcheckTest(unittest.TestCase):
def test_cppcheckargs(self):
"""
Check if the content of --cppcheckargs config file is properly passed
to the analyzer command.
"""
with tempfile.TemporaryDirectory() as tmp_ws:
cppcheckargs = os.path.join(tmp_ws, 'cppcheckargs')
with open(cppcheckargs, 'w',
encoding='utf-8', errors='ignore') as f:
f.write('--max-ctu-depth=42')

args = Namespace()
args.cppcheck_args_cfg_file = cppcheckargs

analyzer = create_analyzer_cppcheck(args, tmp_ws)
result_handler = create_result_handler(analyzer)
cmd = analyzer.construct_analyzer_cmd(result_handler)

self.assertIn('--max-ctu-depth=42', cmd)
8 changes: 4 additions & 4 deletions docs/analyzer/user_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ usage: CodeChecker analyze [-h] [-j JOBS]
[-n NAME] [--analyzers ANALYZER [ANALYZER ...]]
[--capture-analysis-output] [--generate-reproducer]
[--config CONFIG_FILE]
[--cppcheck-args CPPCHECK_ARGS_CFG_FILE]
[--cppcheckargs CPPCHECK_ARGS_CFG_FILE]
[--saargs CLANGSA_ARGS_CFG_FILE]
[--tidyargs TIDY_ARGS_CFG_FILE]
[--timeout TIMEOUT]
Expand Down Expand Up @@ -1122,9 +1122,9 @@ analyzer arguments:
For more information see the docs: https://github.com/
Ericsson/codechecker/tree/master/docs/config_file.md
(default: None)
--cppcheck-args CPPCHECK_ARGS_CFG_FILE
Configuration file to pass cppcheck command line
arguments.
--cppcheckargs CPPCHECK_ARGS_CFG_FILE
File containing argument which will be forwarded
verbatim for Cppcheck.
--saargs CLANGSA_ARGS_CFG_FILE
File containing argument which will be forwarded
verbatim for the Clang Static Analyzer.
Expand Down

0 comments on commit f1ab31b

Please sign in to comment.