Skip to content

Commit

Permalink
[analyzer] Add --inferargs flag
Browse files Browse the repository at this point in the history
Analyzers can be given further flags that are forwarded verbatim to the
analyzer invocation. These flags are provided in a text file which is
given to --<analyzer>args flag. This flag was missing at Facebook Infer
plugin.
  • Loading branch information
bruntib committed Feb 4, 2025
1 parent 92f1af6 commit 529b8c8
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 15 deletions.
19 changes: 18 additions & 1 deletion analyzer/codechecker_analyzer/analyzers/infer/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
"""
from collections import defaultdict
# TODO distutils will be removed in python3.12
import re
import shlex
import subprocess
import json
from pathlib import Path

from codechecker_common.logger import get_logger

from codechecker_analyzer import analyzer_context
from codechecker_analyzer import analyzer_context, env

from .. import analyzer_base
from ..config_handler import CheckerState
Expand Down Expand Up @@ -77,6 +78,7 @@ def construct_analyzer_cmd(self, result_handler):
result_handler.buildaction_hash)
output_dir.mkdir(exist_ok=True, parents=True)
analyzer_cmd.extend(['-o', str(output_dir)])
analyzer_cmd.extend(config.analyzer_extra_arguments)
analyzer_cmd.append('--')

cmd_filtered = []
Expand Down Expand Up @@ -247,4 +249,19 @@ def construct_config_handler(cls, args):
cmdline_checkers,
'enable_all' in args and args.enable_all)

try:
with open(args.infer_args_cfg_file, 'r', encoding='utf-8',
errors='ignore') as infer_cfg:
handler.analyzer_extra_arguments = \
re.sub(r'\$\((.*?)\)',
env.replace_env_var(args.infer_args_cfg_file),
infer_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 infer arguments file was given in the command line.
LOG.debug_analyzer(aerr)

return handler
7 changes: 7 additions & 0 deletions analyzer/codechecker_analyzer/cmd/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,13 @@ def add_arguments_to_parser(parser):
help="File containing argument which will be "
"forwarded verbatim for Clang-Tidy.")

analyzer_opts.add_argument('--inferargs',
dest="infer_args_cfg_file",
required=False,
default=argparse.SUPPRESS,
help="File containing argument which will be "
"forwarded verbatim for Facebook Infer.")

analyzer_opts.add_argument('--tidy-config',
dest='tidy_config',
required=False,
Expand Down
8 changes: 8 additions & 0 deletions analyzer/codechecker_analyzer/cmd/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,13 @@ def add_arguments_to_parser(parser):
"forwarded verbatim for the Clang-Tidy "
"analyzer.")

analyzer_opts.add_argument('--inferargs',
dest="infer_args_cfg_file",
required=False,
default=argparse.SUPPRESS,
help="File containing argument which will be "
"forwarded verbatim for Facebook Infer.")

analyzer_opts.add_argument('--tidy-config',
dest='tidy_config',
required=False,
Expand Down Expand Up @@ -928,6 +935,7 @@ def __update_if_key_exists(source, target, key):
'cppcheck_args_cfg_file',
'clangsa_args_cfg_file',
'tidy_args_cfg_file',
'infer_args_cfg_file',
'analyzer_config',
'checker_config',
'capture_analysis_output',
Expand Down
36 changes: 22 additions & 14 deletions docs/analyzer/user_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ usage: CodeChecker check [-h] [-o OUTPUT_DIR] [-t {plist}] [-q]
[--config CONFIG_FILE]
[--saargs CLANGSA_ARGS_CFG_FILE]
[--tidyargs TIDY_ARGS_CFG_FILE]
[--inferargs INFER_ARGS_CFG_FILE]
[--analyzer-config [ANALYZER_CONFIG [ANALYZER_CONFIG ...]]]
[--checker-config [CHECKER_CONFIG [CHECKER_CONFIG ...]]]
[--timeout TIMEOUT]
Expand Down Expand Up @@ -289,6 +290,9 @@ analyzer arguments:
--tidyargs TIDY_ARGS_CFG_FILE
File containing argument which will be forwarded
verbatim for the Clang-Tidy analyzer.
--inferargs INFER_ARGS_CFG_FILE
File containing argument which will be forwarded
verbatim for the Facebook Infer.
--analyzer-config [ANALYZER_CONFIG [ANALYZER_CONFIG ...]]
Analyzer configuration options in the following
format: analyzer:key=value. The collection of the
Expand Down Expand Up @@ -391,18 +395,18 @@ checker configuration:
Checker prefix groups
------------------------------------------------
Checker prefix groups allow you to enable checkers that share a common
prefix in their names. Checkers within a prefix group will have names that
start with the same identifier, making it easier to manage and reference
Checker prefix groups allow you to enable checkers that share a common
prefix in their names. Checkers within a prefix group will have names that
start with the same identifier, making it easier to manage and reference
related checkers.
You can enable/disable checkers belonging to a checker prefix group:
You can enable/disable checkers belonging to a checker prefix group:
'-e <label>:<value>', e.g. '-e prefix:security'.
Note: The 'prefix' label is mandatory when there is ambiguity between the
name of a checker prefix group and a checker profile or a guideline. This
name of a checker prefix group and a checker profile or a guideline. This
prevents conflicts and ensures the correct checkers are applied.
See "CodeChecker checkers --help" to learn more.
Checker labels
Expand All @@ -413,9 +417,9 @@ checker configuration:
You can enable/disable checkers belonging to a label: '-e <label>:<value>',
e.g. '-e profile:default'.
Note: The 'profile' label is mandatory when there is ambiguity between the
name of a checker profile and a checker prefix group or a guideline. This
name of a checker profile and a checker prefix group or a guideline. This
prevents conflicts and ensures the correct checkers are applied.
See "CodeChecker checkers --help" to learn more.
Expand All @@ -432,9 +436,9 @@ checker configuration:
Guidelines are labels themselves, and can be used as a label:
'-e guideline:<value>', e.g. '-e guideline:sei-cert'.
Note: The 'guideline' label is mandatory when there is ambiguity between the
name of a guideline and a checker prefix group or a checker profile. This
name of a guideline and a checker prefix group or a checker profile. This
prevents conflicts and ensures the correct checkers are applied.
Batch enabling/disabling checkers
Expand Down Expand Up @@ -992,6 +996,7 @@ usage: CodeChecker analyze [-h] [-j JOBS]
[--cppcheckargs CPPCHECK_ARGS_CFG_FILE]
[--saargs CLANGSA_ARGS_CFG_FILE]
[--tidyargs TIDY_ARGS_CFG_FILE]
[--inferargs INFER_ARGS_CFG_FILE]
[--timeout TIMEOUT]
[--ctu | --ctu-collect | --ctu-analyze]
[--ctu-ast-mode {load-from-pch, parse-on-demand}]
Expand Down Expand Up @@ -1213,6 +1218,9 @@ analyzer arguments:
--tidyargs TIDY_ARGS_CFG_FILE
File containing argument which will be forwarded
verbatim for Clang-Tidy.
--inferargs TIDY_ARGS_CFG_FILE
File containing argument which will be forwarded
verbatim for Facebook Infer.
--analyzer-config [ANALYZER_CONFIG [ANALYZER_CONFIG ...]]
Analyzer configuration options in the following
format: analyzer:key=value. The collection of the
Expand Down Expand Up @@ -1551,12 +1559,12 @@ and disabled flags starting from the bigger groups and going inwards. For
example

```sh
--enable prefix:clang-diagnostic-unused
--enable prefix:clang-diagnostic-unused
--disable checker:clang-diagnostic-unused-parameter
```
or
```sh
--enable prefix:clang-diagnostic-unused
--enable prefix:clang-diagnostic-unused
--disable clang-diagnostic-unused-parameter
```
will enable every `unused` warnings except `unused-parameter`. To turn off a
Expand Down

0 comments on commit 529b8c8

Please sign in to comment.