Skip to content

Commit

Permalink
Properly handle in-progress C++ standards when invoking Cppcheck // R…
Browse files Browse the repository at this point in the history
…esolve #3944
  • Loading branch information
valeros committed Sep 27, 2021
1 parent b6f7836 commit 0c360ee
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ PlatformIO Core 5
- Allowed to override a default library builder via a new ``builder`` field in a ``build`` group of `library.json <https://docs.platformio.org/page/librarymanager/config.html#build>`__ manifest (`issue #3957 <https://github.com/platformio/platformio-core/issues/3957>`_)
- Fixed a "KeyError: Invalid board option 'build.cpu'" when using a precompiled library with a board that does not have a CPU field in the manifest (`issue #4056 <https://github.com/platformio/platformio-core/issues/4056>`_)
- Fixed a "FileExist" error when the `platformio ci <https://docs.platformio.org/en/latest/userguide/cmd_ci.html>`__ command is used in pair with the ``--keep-build-dir`` option (`issue #4011 <https://github.com/platformio/platformio-core/issues/4011>`_)
- Fixed an issue with draft values of C++ language standards that broke static analysis via Cppcheck (`issue #3944 <https://github.com/platformio/platformio-core/issues/3944>`_)

5.2.0 (2021-09-13)
~~~~~~~~~~~~~~~~~~
Expand Down
22 changes: 18 additions & 4 deletions platformio/commands/check/tools/cppcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,11 @@ def configure_command(self, language, src_file): # pylint: disable=arguments-di

build_flags = self.cxx_flags if language == "c++" else self.cc_flags

for flag in build_flags:
if "-std" in flag:
# Standards with GNU extensions are not allowed
cmd.append("-" + flag.replace("gnu", "c"))
if not self.is_flag_set("--std", flags):
# Try to guess the standard version from the build flags
for flag in build_flags:
if "-std" in flag:
cmd.append("--std=" + self.convert_cpp_language_standard(flag))

cmd.extend(
["-D%s" % d for d in self.cpp_defines + self.toolchain_defines[language]]
Expand Down Expand Up @@ -224,6 +225,19 @@ def is_check_successful(cmd_result):
# Cppcheck is configured to return '3' if a defect is found
return cmd_result["returncode"] in (0, 3)

@staticmethod
def convert_cpp_language_standard(standard):
cpp_standards_map = {
"0x": "11",
"1y": "14",
"1z": "17",
"2a": "20",
}

version = standard[-2:]
# Note: GNU extensions are not supported and converted to regular standards
return "c++" + cpp_standards_map.get(version, version)

def check(self, on_defect_callback=None):
self._on_defect_callback = on_defect_callback

Expand Down
30 changes: 30 additions & 0 deletions tests/commands/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,36 @@ def test_check_language_standard_definition_passed(clirunner, tmpdir):
assert "--std=c++17" in result.output


def test_check_language_standard_option_is_converted(clirunner, tmpdir):
config = (
DEFAULT_CONFIG
+ """
build_flags = -std=gnu++1y
"""
)
tmpdir.join("platformio.ini").write(config)
tmpdir.mkdir("src").join("main.cpp").write(TEST_CODE)
result = clirunner.invoke(cmd_check, ["--project-dir", str(tmpdir), "-v"])

assert "--std=c++14" in result.output


def test_check_language_standard_is_prioritized_over_build_flags(clirunner, tmpdir):
config = (
DEFAULT_CONFIG
+ """
check_flags = --std=c++03
build_flags = -std=c++17
"""
)
tmpdir.join("platformio.ini").write(config)
tmpdir.mkdir("src").join("main.cpp").write(TEST_CODE)
result = clirunner.invoke(cmd_check, ["--project-dir", str(tmpdir), "-v"])

assert "--std=c++03" in result.output
assert "--std=c++17" not in result.output


def test_check_severity_threshold(clirunner, validate_cliresult, check_dir):
result = clirunner.invoke(
cmd_check, ["--project-dir", str(check_dir), "--severity=high"]
Expand Down

0 comments on commit 0c360ee

Please sign in to comment.