Skip to content

Commit

Permalink
[analyzer] Remove some code duplication from CodeChecker check command
Browse files Browse the repository at this point in the history
Remove code duplication which is related to epilog from the
`CodeChecker check` command.
  • Loading branch information
csordasmarton committed Mar 2, 2021
1 parent 17b25a6 commit 3aecb82
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 168 deletions.
61 changes: 36 additions & 25 deletions analyzer/codechecker_analyzer/cmd/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,22 @@

LOG = logger.get_logger('system')

_package_root = analyzer_context.get_context().package_root
_severity_map_file = os.path.join(_package_root, 'config',
'checker_severity_map.json')

def get_argparser_ctor_args():
"""
This method returns a dict containing the kwargs for constructing an
argparse.ArgumentParser (either directly or as a subparser).
"""

package_root = analyzer_context.get_context().package_root

return {
'prog': 'CodeChecker analyze',
'formatter_class': arg.RawDescriptionDefaultHelpFormatter,

# Description is shown when the command's help is queried directly
'description': """
Use the previously created JSON Compilation Database to perform an analysis on
the project, outputting analysis results in a machine-readable format.""",

# Epilogue is shown after the arguments when the help is queried
# directly.
'epilog': """
Environment variables
------------------------------------------------
epilog_env_var = f"""
CC_ANALYZERS_FROM_PATH Set to `yes` or `1` to enforce taking the analyzers
from the `PATH` instead of the given binaries.
CC_CLANGSA_PLUGIN_DIR If the CC_ANALYZERS_FROM_PATH environment variable
is set you can configure the plugin directory of the
Clang Static Analyzer by using this environment
variable.
CC_SEVERITY_MAP_FILE Path of the checker-severity mapping config file.
Default: {}
Default: {_severity_map_file}
"""

epilog_issue_hashes = """
Issue hashes
------------------------------------------------
- By default the issue hash calculation method for 'Clang Static Analyzer' is
Expand Down Expand Up @@ -103,20 +86,48 @@ def get_argparser_ctor_args():
For more information see:
https://github.com/Ericsson/codechecker/blob/master/docs/analyzer/report_identification.md
"""

epilog_exit_status = """
Exit status
------------------------------------------------
0 - Successful analysis and no new reports
1 - CodeChecker error
2 - At least one report emitted by an analyzer and there is no analyzer failure
3 - Analysis of at least one translation unit failed
128+signum - Terminating on a fatal signal whose number is signum
"""


def get_argparser_ctor_args():
"""
This method returns a dict containing the kwargs for constructing an
argparse.ArgumentParser (either directly or as a subparser).
"""
return {
'prog': 'CodeChecker analyze',
'formatter_class': arg.RawDescriptionDefaultHelpFormatter,

# Description is shown when the command's help is queried directly
'description': """
Use the previously created JSON Compilation Database to perform an analysis on
the project, outputting analysis results in a machine-readable format.""",

# Epilogue is shown after the arguments when the help is queried
# directly.
'epilog': f"""
Environment variables
------------------------------------------------
{epilog_env_var}
{epilog_issue_hashes}
{epilog_exit_status}
Compilation databases can be created by instrumenting your project's build via
'CodeChecker log'. To transform the results of the analysis to a human-friendly
format, please see the commands 'CodeChecker parse' or 'CodeChecker store'.
""".format(os.path.join(package_root, 'config', 'checker_severity_map.json')),
""",

# Help is shown when the "parent" CodeChecker command lists the
# individual subcommands.
Expand Down
94 changes: 24 additions & 70 deletions analyzer/codechecker_analyzer/cmd/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@
from codechecker_common import arg, logger
from codechecker_common.source_code_comment_handler import REVIEW_STATUS_VALUES

from codechecker_analyzer.cmd.analyze import \
epilog_env_var as analyzer_epilog_env_var, \
epilog_issue_hashes as analyzer_epilog_issue_hashes, \
epilog_exit_status as analyzer_epilog_exit_status

from codechecker_analyzer.cmd.log import \
epilog_env_var as log_epilog_env_var

from codechecker_analyzer.cmd.parse import \
epilog_env_var as parse_epilog_env_var

LOG = logger.get_logger('system')


Expand All @@ -32,9 +43,6 @@ def get_argparser_ctor_args():
This method returns a dict containing the kwargs for constructing an
argparse.ArgumentParser (either directly or as a subparser).
"""

package_root = analyzer_context.get_context().package_root

return {
'prog': 'CodeChecker check',
'formatter_class': arg.RawDescriptionDefaultHelpFormatter,
Expand All @@ -47,75 +55,21 @@ def get_argparser_ctor_args():

# Epilogue is shown after the arguments when the help is queried
# directly.
'epilog': """
'epilog': f"""
Environment variables
------------------------------------------------
CC_ANALYZERS_FROM_PATH Set to `yes` or `1` to enforce taking the analyzers
from the `PATH` instead of the given binaries.
CC_CLANGSA_PLUGIN_DIR If the CC_ANALYZERS_FROM_PATH environment variable
is set you can configure the plugin directory of the
Clang Static Analyzer by using this environment
variable.
CC_SEVERITY_MAP_FILE Path of the checker-severity mapping config file.
Default: {}
CC_LOGGER_DEBUG_FILE If -b and -o flags are used with debug logs, the
logging phase emits its debug logs in
'codechecker.logger.debug' under the output
directory by default. This environment variable
can be given a file path which overrides this
default location.
Issue hashes
------------------------------------------------
- By default the issue hash calculation method for 'Clang Static Analyzer' is
context sensitive. It means the hash will be generated based on the following
information:
* signature of the enclosing function declaration, type declaration or
namespace.
* content of the line where the bug is.
* unique name of the checker.
* position (column) within the line.
- By default the issue hash calculation method for 'Clang Tidy' is context
insensitive. It means the hash will be generated based on the following
information:
* 'file name' from the main diag section.
* 'checker name'.
* 'checker message'.
* 'line content' from the source file if can be read up.
* 'column numbers' from the main diag section.
* 'range column numbers' only from the control diag sections if column number
in the range is not the same as the previous control diag section number in
the bug path. If there are no control sections event section column numbers
are used.
- context-free: there was a bug and for Clang Tidy the default hash was
generated and not the context free hash (kept for backward compatibility). Use
'context-free-v2' instead of this.
- context-free-v2:
* 'file name' from the main diag section.
* 'checker message'.
* 'line content' from the source file if can be read up. All the whitespaces
from the source content are removed.
* 'column numbers' from the main diag sections location.
OUR RECOMMENDATION: we recommend you to use 'context-free-v2' hash because the
hash will not be changed so easily for example on code indentation or when a
checker is renamed.
For more information see:
https://github.com/Ericsson/codechecker/blob/master/docs/analyzer/report_identification.md
Exit status
------------------------------------------------
0 - Successful analysis and no new reports
1 - CodeChecker error
2 - At least one report emitted by an analyzer and there is no analyzer failure
3 - Analysis of at least one translation unit failed
128+signum - Terminating on a fatal signal whose number is signum
Environment variables for 'CodeChecker log' command:
{log_epilog_env_var}
Environment variables for 'CodeChecker analyze' command:
{analyzer_epilog_env_var}
Environment variables for 'CodeChecker parse' command:
{parse_epilog_env_var}
{analyzer_epilog_issue_hashes}
{analyzer_epilog_exit_status}
If you wish to reuse the logfile resulting from executing the build, see
'CodeChecker log'. To keep analysis results for later, see and use
Expand All @@ -125,7 +79,7 @@ def get_argparser_ctor_args():
command actually builds the files -- it is advised to execute builds on empty
trees, aka. after a 'make clean', as CodeChecker only analyzes files that had
been used by the build system.
""".format(os.path.join(package_root, 'config', 'checker_severity_map.json')),
""",

# Help is shown when the "parent" CodeChecker command lists the
# individual subcommands.
Expand Down
68 changes: 37 additions & 31 deletions analyzer/codechecker_analyzer/cmd/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,40 @@
from codechecker_common import arg, logger


epilog_env_var = f"""
CC_LOGGER_ABS_PATH If the environment variable is defined, all relative
paths in the compilation commands after '-I,
-idirafter, -imultilib, -iquote, -isysroot -isystem,
-iwithprefix, -iwithprefixbefore, -sysroot,
--sysroot' will be converted to absolute PATH when
written into the compilation database.
CC_LOGGER_DEBUG_FILE Output file to print log messages. By default if we
run the log command in debug mode it will generate
a 'codechecker.logger.debug' file beside the log
file.
CC_LOGGER_DEF_DIRS If the environment variable is defined, the logger
will extend the compiler argument list in the
compilation database with the pre-configured include
paths of the logged compiler.
CC_LOGGER_GCC_LIKE Set to to a colon separated list to change which
compilers should be logged. For example (default):
export CC_LOGGER_GCC_LIKE="gcc:g++:clang:clang++:
cc:c++". The logger will match any compilers with
'gcc', 'g++', 'clang', 'clang++', 'cc' and 'c++' in
their filenames.
CC_LOGGER_KEEP_LINK If its value is not 'true' then object files will be
removed from the build action. For example in case
of this build command: 'gcc main.c object1.o
object2.so' the 'object1.o' and 'object2.so' will be
removed and only 'gcc main.c' will be captured. If
only object files are provided to the compiler then
the complete build action will be thrown away. This
means that build actions which only perform linking
will not be captured. We consider a file as object
file if its extension is '.o', '.so' or '.a'.
"""


def get_argparser_ctor_args():
"""
This method returns a dict containing the kwargs for constructing an
Expand All @@ -50,39 +84,11 @@ def get_argparser_ctor_args():
""".format('intercept-build' if is_intercept else 'ld-logger',
ldlogger_settings),

'epilog': """
'epilog': f"""
Environment variables
------------------------------------------------
CC_LOGGER_GCC_LIKE Set to to a colon separated list to change which
compilers should be logged. For example (default):
export CC_LOGGER_GCC_LIKE="gcc:g++:clang:clang++:
cc:c++". The logger will match any compilers with
'gcc', 'g++', 'clang', 'clang++', 'cc' and 'c++' in
their filenames.
CC_LOGGER_DEF_DIRS If the environment variable is defined, the logger
will extend the compiler argument list in the
compilation database with the pre-configured include
paths of the logged compiler.
CC_LOGGER_ABS_PATH If the environment variable is defined, all relative
paths in the compilation commands after '-I,
-idirafter, -imultilib, -iquote, -isysroot -isystem,
-iwithprefix, -iwithprefixbefore, -sysroot,
--sysroot' will be converted to absolute PATH when
written into the compilation database.
CC_LOGGER_KEEP_LINK If its value is not 'true' then object files will be
removed from the build action. For example in case
of this build command: 'gcc main.c object1.o
object2.so' the 'object1.o' and 'object2.so' will be
removed and only 'gcc main.c' will be captured. If
only object files are provided to the compiler then
the complete build action will be thrown away. This
means that build actions which only perform linking
will not be captured. We consider a file as object
file if its extension is '.o', '.so' or '.a'.
CC_LOGGER_DEBUG_FILE Output file to print log messages. By default if we
run the log command in debug mode it will generate
a 'codechecker.logger.debug' file beside the log
file.""",
{epilog_env_var}
""",

# Help is shown when the "parent" CodeChecker command lists the
# individual subcommands.
Expand Down
34 changes: 19 additions & 15 deletions analyzer/codechecker_analyzer/cmd/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@

EXPORT_TYPES = ['html', 'json', 'codeclimate', 'gerrit']

_package_root = analyzer_context.get_context().package_root
_severity_map_file = os.path.join(_package_root, 'config',
'checker_severity_map.json')

epilog_env_var = f"""
CC_CHANGED_FILES Path of changed files json from Gerrit. Use it when
generating gerrit output.
CC_REPO_DIR Root directory of the sources, i.e. the directory
where the repository was cloned. Use it when
generating gerrit output.
CC_REPORT_URL URL where the report can be found. Use it when
generating gerrit output.
CC_SEVERITY_MAP_FILE Path of the checker-severity mapping config file.
Default: {_severity_map_file}
"""


class PlistToPlaintextFormatter(object):
"""
Expand Down Expand Up @@ -361,9 +377,6 @@ def get_argparser_ctor_args():
This method returns a dict containing the kwargs for constructing an
argparse.ArgumentParser (either directly or as a subparser).
"""

package_root = analyzer_context.get_context().package_root

return {
'prog': 'CodeChecker parse',
'formatter_class': arg.RawDescriptionDefaultHelpFormatter,
Expand All @@ -375,20 +388,11 @@ def get_argparser_ctor_args():
"false_positive", "suppress" and "intentional" source code comments will not be
printed by the `parse` command.""",

'epilog': """
'epilog': f"""
Environment variables
------------------------------------------------
CC_SEVERITY_MAP_FILE Path of the checker-severity mapping config file.
CC_REPO_DIR Root directory of the sources, i.e. the directory where
the repository was cloned. Use it when generating gerrit
output.
CC_REPORT_URL URL where the report can be found. Use it when generating
gerrit output.
CC_CHANGED_FILES Path of changed files json from Gerrit. Use it when
generating gerrit output.
Default: {}
""".format(os.path.join(package_root, 'config', 'checker_severity_map.json')),
{epilog_env_var}
""",

# Help is shown when the "parent" CodeChecker command lists the
# individual subcommands.
Expand Down
Loading

0 comments on commit 3aecb82

Please sign in to comment.