From 0e7a2b314157a38c256eab29db7496b5ee8eaaa6 Mon Sep 17 00:00:00 2001 From: valeros Date: Fri, 25 Oct 2019 20:08:04 +0300 Subject: [PATCH] Automatically detect source files language when invoking cppcheck --- platformio/commands/check/tools/base.py | 11 +++++++++++ platformio/commands/check/tools/cppcheck.py | 3 +++ 2 files changed, 14 insertions(+) diff --git a/platformio/commands/check/tools/base.py b/platformio/commands/check/tools/base.py index 78eac5dd..d199f131 100644 --- a/platformio/commands/check/tools/base.py +++ b/platformio/commands/check/tools/base.py @@ -13,6 +13,7 @@ # limitations under the License. import click +import os from platformio import fs, proc from platformio.commands.check.defect import DefectItem @@ -126,6 +127,16 @@ class CheckToolBase(object): # pylint: disable=too-many-instance-attributes get_project_dir(), self.options.get("filter"), file_extensions ) + def get_source_language(self): + with fs.cd(get_project_dir()): + for _, __, files in os.walk(self.config.get_optional_dir("src")): + for name in files: + if "." not in name: + continue + if os.path.splitext(name)[1].lower() in (".cpp", ".cxx", ".ino"): + return "c++" + return "c" + def check(self, on_defect_callback=None): self._on_defect_callback = on_defect_callback cmd = self.configure_command() diff --git a/platformio/commands/check/tools/cppcheck.py b/platformio/commands/check/tools/cppcheck.py index 9eef560f..b9a8578b 100644 --- a/platformio/commands/check/tools/cppcheck.py +++ b/platformio/commands/check/tools/cppcheck.py @@ -90,6 +90,9 @@ class CppcheckCheckTool(CheckToolBase): % "<&PIO&>".join(["{0}={{{0}}}".format(f) for f in self.defect_fields]) ) + if self.get_source_language() == "c++": + cmd.append("--language=c++") + flags = self.get_flags("cppcheck") if not self.is_flag_set("--platform", flags): cmd.append("--platform=unspecified")