Skip to content

Commit

Permalink
Merge branch 'Ericsson:master' into branch-2-backup
Browse files Browse the repository at this point in the history
  • Loading branch information
feyruzb authored Nov 27, 2024
2 parents 3f774da + 53655b1 commit 8cd4793
Show file tree
Hide file tree
Showing 13 changed files with 395 additions and 54 deletions.
42 changes: 41 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 @@ -138,6 +139,34 @@ def parse_analyzer_config(self):
# * -std c99
# * -stdlib=libc++
std_regex = re.compile("-(-std$|-?std=.*)")

# Mapping is needed, because, if a standard version not known by
# cppcheck is used, then it will assume the latest available version
# before cppcheck-2.15 or fail the analysis from cppcheck-2.15.
# https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html#index-std-1
standard_mapping = {
"c90": "c89",
"c18": "c17",
"iso9899:2017": "c17",
"iso9899:2018": "c17",
"iso9899:1990": "c89",
"iso9899:199409": "c89", # Good enough
"c9x": "c99",
"iso9899:1999": "c99",
"iso9899:199x": "c99",
"c1x": "c11",
"iso9899:2011": "c11",
"c2x": "c23",
"iso9899:2024": "c23",
"c++98": "c++03",
"c++0x": "c++11",
"c++1y": "c++14",
"c++1z": "c++17",
"c++2a": "c++20",
"c++2b": "c++23",
"c++2c": "c++26"
}

for i, analyzer_option in enumerate(self.buildaction.analyzer_options):
if interesting_option.match(analyzer_option):
params.extend([analyzer_option])
Expand All @@ -161,6 +190,7 @@ def parse_analyzer_config(self):
else:
standard = self.buildaction.analyzer_options[i+1]
standard = standard.lower().replace("gnu", "c")
standard = standard_mapping.get(standard, standard)
params.extend(["--std=" + standard])
return params

Expand Down Expand Up @@ -194,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
48 changes: 46 additions & 2 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 @@ -1083,7 +1084,7 @@ def test_cppcheck_standard(self):
stdout=subprocess.PIPE).stdout.decode()

# Test correct handover.
self.assertTrue("--std=c99" in out)
self.assertIn("--std=c99", out)

# Cppcheck does not support gnu variants of the standards,
# These are transformed into their respective c and c++
Expand All @@ -1104,7 +1105,50 @@ def test_cppcheck_standard(self):
stdout=subprocess.PIPE).stdout.decode()

# Test if the standard is correctly transformed
self.assertTrue("--std=c99" in out)
self.assertIn("--std=c99", out)

build_log = [{"directory": self.test_workspace,
"command": "gcc -c -std=iso9899:2017 " + source_file,
"file": source_file
}]

with open(build_json, 'w',
encoding="utf-8", errors="ignore") as outfile:
json.dump(build_log, outfile)

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

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. """
Expand Down
11 changes: 11 additions & 0 deletions codechecker_common/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,14 @@ 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
85 changes: 85 additions & 0 deletions config/labels/analyzers/clang-tidy.json
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/android/comparison-in-temp-failure-retry.html",
"severity:MEDIUM"
],
"boost-use-ranges": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/boost/use-ranges.html",
"profile:extreme",
"severity:LOW"
],
"boost-use-to-string": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/boost/use-to-string.html",
"profile:extreme",
Expand Down Expand Up @@ -194,6 +199,14 @@
"sei-cert:pos44-c",
"severity:MEDIUM"
],
"bugprone-bitwise-pointer-cast": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/bugprone/bitwise-pointer-cast.html",
"profile:default",
"profile:extreme",
"profile:security",
"profile:sensitive",
"severity:MEDIUM"
],
"bugprone-bool-pointer-implicit-conversion": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/bugprone/bool-pointer-implicit-conversion.html",
"profile:default",
Expand Down Expand Up @@ -453,6 +466,13 @@
"profile:sensitive",
"severity:MEDIUM"
],
"bugprone-nondeterministic-pointer-iteration-order": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/bugprone/nondeterministic-pointer-iteration-order.html",
"profile:default",
"profile:extreme",
"profile:sensitive",
"severity:MEDIUM"
],
"bugprone-optional-value-conversion": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/bugprone/optional-value-conversion.html",
"profile:default",
Expand All @@ -466,6 +486,14 @@
"profile:sensitive",
"severity:MEDIUM"
],
"bugprone-pointer-arithmetic-on-polymorphic-object": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/bugprone/pointer-arithmetic-on-polymorphic-object.html",
"guideline:sei-cert",
"profile:extreme",
"profile:security",
"sei-cert:ctr56-cpp",
"severity:HIGH"
],
"bugprone-posix-return": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/bugprone/posix-return.html",
"profile:extreme",
Expand All @@ -487,6 +515,12 @@
"sei-cert:dcl51-cpp",
"severity:LOW"
],
"bugprone-return-const-ref-from-parameter": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/bugprone/return-const-ref-from-parameter.html",
"profile:extreme",
"profile:sensitive",
"severity:MEDIUM"
],
"bugprone-shared-ptr-array-mismatch": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/bugprone/shared-ptr-array-mismatch.html",
"guideline:sei-cert",
Expand Down Expand Up @@ -643,6 +677,13 @@
"profile:sensitive",
"severity:MEDIUM"
],
"bugprone-tagged-union-member-count": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/bugprone/tagged-union-member-count.html",
"profile:default",
"profile:extreme",
"profile:sensitive",
"severity:MEDIUM"
],
"bugprone-suspicious-stringview-data-usage": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/bugprone/suspicious-stringview-data-usage.html",
"profile:default",
Expand Down Expand Up @@ -788,6 +829,10 @@
"profile:sensitive",
"severity:MEDIUM"
],
"cert-arr39-c": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/cert/arr39-c.html",
"severtiy:HIGH"
],
"cert-con36-c": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/cert/con36-c.html",
"severity:MEDIUM"
Expand All @@ -796,6 +841,10 @@
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/cert/con54-cpp.html",
"severity:MEDIUM"
],
"cert-ctr56-cpp": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/cert/ctr56-cpp.html",
"severity:HIGH"
],
"cert-dcl03-c": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/cert/dcl03-c.html",
"guideline:sei-cert",
Expand Down Expand Up @@ -6591,9 +6640,12 @@
],
"misc-sizeof-expression": [
"doc_url:https://releases.llvm.org/6.0.1/tools/clang/tools/extra/docs/clang-tidy/checks/misc-sizeof-expression.html",
"guideline:sei-cert",
"profile:default",
"profile:extreme",
"profile:security",
"profile:sensitive",
"sei-cert:arr39-c",
"severity:HIGH"
],
"misc-static-assert": [
Expand Down Expand Up @@ -6732,6 +6784,11 @@
"profile:sensitive",
"severity:LOW"
],
"misc-use-internal-linkage": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/misc/use-internal-linkage.html",
"profile:extreme",
"severity:LOW"
],
"misc-virtual-near-miss": [
"doc_url:https://releases.llvm.org/6.0.1/tools/clang/tools/extra/docs/clang-tidy/checks/misc-virtual-near-miss.html",
"profile:default",
Expand Down Expand Up @@ -6785,6 +6842,13 @@
"profile:extreme",
"severity:LOW"
],
"modernize-min-max-use-initializer-list": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/modernize/min-max-use-initializer-list.html",
"profile:default",
"profile:extreme",
"profile:sensitive",
"severity:LOW"
],
"modernize-pass-by-value": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/modernize/pass-by-value.html",
"profile:extreme",
Expand Down Expand Up @@ -6897,11 +6961,21 @@
"profile:extreme",
"severity:LOW"
],
"modernize-use-ranges": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/modernize/use-ranges.html",
"profile:extreme",
"severity:LOW"
],
"modernize-use-starts-ends-with": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/modernize/use-starts-ends-with.html",
"profile:extreme",
"severity:LOW"
],
"modernize-use-std-format": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/modernize/use-std-format.html",
"profile:extreme",
"severity:LOW"
],
"modernize-use-std-numbers": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/modernize/use-std-numbers.html",
"profile:extreme",
Expand Down Expand Up @@ -7117,6 +7191,17 @@
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/portability/std-allocator-const.html",
"severity:STYLE"
],
"portability-template-virtual-member-function": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/portability/template-virtual-member-function.html",
"profile:extreme",
"profile:sensitive",
"severity:MEDIUM"
],
"readability-math-missing-parentheses": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/readability/math-missing-parentheses.html",
"profile:extreme",
"severity:LOW"
],
"readability-avoid-const-params-in-decls": [
"doc_url:https://clang.llvm.org/extra/clang-tidy/checks/readability/avoid-const-params-in-decls.html",
"severity:STYLE"
Expand Down
Loading

0 comments on commit 8cd4793

Please sign in to comment.