Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality to validate analyzer and checker options #4204

Merged
merged 6 commits into from
Apr 23, 2024

Conversation

noraz31
Copy link
Collaborator

@noraz31 noraz31 commented Mar 28, 2024

When specifying invalid checker and analyzer config, the CodeChecker analyze command used to give a warning but continued running.
This functionality throws an error and prevents the analysis from running in this case.
CODECHKR-5784

@noraz31 noraz31 requested review from bruntib and vodorok as code owners March 28, 2024 14:50
@bruntib bruntib added this to the release 6.24.0 milestone Mar 28, 2024
@bruntib bruntib added enhancement 🌟 CLI 💻 Related to the command-line interface, such as the cmd, store, etc. commands labels Mar 28, 2024
@noraz31 noraz31 force-pushed the validate_parameters branch 3 times, most recently from 670041a to cdb5ec7 Compare April 3, 2024 09:27
@noraz31 noraz31 force-pushed the validate_parameters branch from cdb5ec7 to fadef54 Compare April 5, 2024 12:02
Copy link
Contributor

@Szelethus Szelethus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check the analyzer name as well, and clear up the wording in the error msg.

analyzer/codechecker_analyzer/cmd/analyze.py Outdated Show resolved Hide resolved
Comment on lines 813 to 814
if 'analyzer_config' in args and isinstance(args.analyzer_config,
list):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

COuld decrease indentation here as well with an early return.

Comment on lines 818 to 823
LOG.error(f"Invalid --analyzer-config parameter: "
f"{analyzer_class.ANALYZER_NAME} has no "
f"analyzer named {cfg.option}. Use the "
f"'CodeChecker analyzers --analyzer-config "
f"{analyzer_class.ANALYZER_NAME}' "
f"command to check available analyzers.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wording is off here: under analyzer, we mean the actual plugins that CodeChecker implements around a static analyzer tool: clangsa, clang-tidy, cppcheck and gcc. While we should definitely check if the user gave a wrong analyzer name, but at this point you are checking the name of the config, not the analyzer. Your message states

$ CodeChecker check -b "clang++ -c test.cpp -o results/prune_paths_full -c --analyzer-config clangsa:hahaha=false --analyzers=clangsa
[ERROR 2024-04-09 11:21] - Invalid --analyzer-config parameter: clangsa has no analyzer named hahaha. Use the 'CodeChecker analyzers --analyzer-config clangsa' command to check available analyzers.

But should instead state:

$ CodeChecker check -b "clang++ -c test.cpp -o results/prune_paths_full -c --analyzer-config clangsa:hahaha=false --analyzers=clangsa
[ERROR 2024-04-09 11:21] - Invalid --analyzer-config parameter: clangsa has no config named hahaha. Use the 'CodeChecker analyzers --analyzer-config clangsa' command to check available configs.

@@ -797,6 +795,57 @@ def add_arguments_to_parser(parser):
func=main, func_process_config_file=cmd_config.process_config_file)


def validate_analyzer_parameter(args, analyzer):
Copy link
Contributor

@Szelethus Szelethus Apr 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't take args as a parmeter.

"args", which contains everything you need to get CodeChecker going (user invocation, default values, etc). If you want to create a unit test for this function, it would look something like this:

args = {'analyzer_config' : ['clangsa:HAHAHA=true']}
self.assertFalse(validate_analyzer_parameter(args, 'clangsa'))

It is not impossible to conjure up an args object to pass to this function for testing, but its fragile: if we ever need to read more values from args after a new feature or refactoring, all tests needs changing as well. Consider the following alterative:

# analyzer/tests/unit/test_checker_handling.py
self.assertFalse(validate_analyzer_parameter(["clangsa:HAHAHA=true"]))

This philosophy is one of the reasons behind that have soooo many function tests instead of unit tests. I may be painting a greater evil than what is actually present in the patch, but the idea stands.

Prefer having an easy-to-test boolean function

On another issue, its fine if you have a non-returning validator function, but it'd be nice for testing purposes and clarify if you had a boolean function as well for both clarity and testing pusposes.

I'd imagine the main function to look like this (which would be more in line with the existing input testing):

for analyzer_config in args.analyzer_configs:
  if not is_valid_analyzer_config(analyzer_config):
    LOG.error("SORT YOUR INPUT OUT!")
    sys.exit(1);
for checker_config in args.checker_configs:
  if not is_valid_checker_config(checker_config):
    LOG.error("SORT YOUR INPUT OUT!")
    sys.exit(1);

And everything else should happen behind the scenes.

@noraz31 noraz31 force-pushed the validate_parameters branch from fadef54 to 895a846 Compare April 10, 2024 08:23
When specifying invalid checker and analyzer config,
the CodeChecker analyze command used to give a warning
but continued running.
This functionality throws an error and prevents the
analysis from running in this case.
@noraz31 noraz31 force-pushed the validate_parameters branch from 895a846 to 1603968 Compare April 10, 2024 08:35
Copy link
Contributor

@Szelethus Szelethus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Analyzer value checking is still a little off. My other comments are just nitpicking.

analyzer/codechecker_analyzer/cmd/analyze.py Outdated Show resolved Hide resolved
analyzer/codechecker_analyzer/cmd/analyze.py Outdated Show resolved Hide resolved
analyzer/codechecker_analyzer/cmd/analyze.py Outdated Show resolved Hide resolved
analyzer/codechecker_analyzer/cmd/analyze.py Outdated Show resolved Hide resolved
analyzer/tests/unit/test_checker_handling.py Outdated Show resolved Hide resolved
Ensure that the analyzer_config parameter is set to a valid value
by verifying if it belongs to the set of allowed values.
"""
analyzer = analyzer_conf.__getitem__(0).analyzer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why didn't you use the bracket ([]) operator? Also, you check all analyzer configs here, but only check the very first analyzer config for a valid analyzer field?

For this example, the second analyzer config this has an invalid analyzer field, but you miss it:

CodeChecker check -b "g++ test.cpp" --analyzer-config clangsa:prune-paths=false --analyzer-config:OOGABOOGA:no-false-positives=true

noraz31 and others added 4 commits April 11, 2024 10:00
Co-authored-by: Kristóf Umann <dkszelethus@gmail.com>
Co-authored-by: Kristóf Umann <dkszelethus@gmail.com>
Co-authored-by: Kristóf Umann <dkszelethus@gmail.com>
Co-authored-by: Kristóf Umann <dkszelethus@gmail.com>
@noraz31 noraz31 force-pushed the validate_parameters branch from a529225 to 86e9191 Compare April 11, 2024 12:02
Copy link
Contributor

@Szelethus Szelethus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there!

Comment on lines 811 to 813
LOG.error(f"Invalid --analyzer-config parameter: {analyzer} is"
f" not a supported analyzer. The supported analyzers are"
f" {', '.join(a for a in supported_analyzers)}.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fun fact:

def func(a, b, c = 5): <--- these are parameters
  pass

func("a", 3) <--- these are arguments
Suggested change
LOG.error(f"Invalid --analyzer-config parameter: {analyzer} is"
f" not a supported analyzer. The supported analyzers are"
f" {', '.join(a for a in supported_analyzers)}.")
LOG.error(f"Invalid argument to --analyzer-config: {analyzer} "
f"is not a supported analyzer. Supported analyzers are :"
f" {', '.join(a for a in supported_analyzers)}.")

Comment on lines 827 to 831
LOG.error(f"Invalid --analyzer-config parameter: "
f"{analyzer_class.ANALYZER_NAME} has no config named "
f"{cfg.option}. Use the 'CodeChecker analyzers "
f"--analyzer-config {analyzer_class.ANALYZER_NAME}' "
f"command to check available configs.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

analyzer/codechecker_analyzer/cmd/analyze.py Show resolved Hide resolved
analyzer/codechecker_analyzer/cmd/analyze.py Show resolved Hide resolved
analyzer/tests/unit/test_checker_handling.py Outdated Show resolved Hide resolved
@noraz31 noraz31 force-pushed the validate_parameters branch from 86e9191 to 4d70afc Compare April 16, 2024 13:09
analyzer/codechecker_analyzer/cmd/analyze.py Outdated Show resolved Hide resolved
analyzer/codechecker_analyzer/cmd/analyze.py Outdated Show resolved Hide resolved

if False in config_validator_res \
and 'no_missing_checker_error' not in args:
sys.exit(1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the validation failed and this suppression flag isn't present, try to advertise it, somewhat like what I've done in #3866:

Suggested change
sys.exit(1)
LOG.info("Although it is not reccomended, if you want to "
"suppress errors relating to unknown "
"analyzer/checker configs, consider using the option "
"'--no-missing-checker-error'")
sys.exit(1)

@Szelethus
Copy link
Contributor

Also, please create tests for the interaction of the suppression flag and this validation checking.

When specifying invalid checker and analyzer config,
the CodeChecker analyze command used to give a warning
but continued running.
This functionality throws an error and prevents the
analysis from running in this case.

Co-authored-by: Kristóf Umann <dkszelethus@gmail.com>
@noraz31 noraz31 force-pushed the validate_parameters branch from 4d70afc to 4f23852 Compare April 19, 2024 15:35
Copy link
Contributor

@Szelethus Szelethus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, very well done!

@Szelethus Szelethus merged commit c720463 into Ericsson:master Apr 23, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLI 💻 Related to the command-line interface, such as the cmd, store, etc. commands enhancement 🌟
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants