2019-09-27 17:21:35 +03:00
|
|
|
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
|
2019-09-23 23:44:42 +03:00
|
|
|
#
|
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
|
#
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
#
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
from os.path import join
|
|
|
|
|
|
2022-05-31 17:16:55 +03:00
|
|
|
from platformio.check.defect import DefectItem
|
|
|
|
|
from platformio.check.tools.base import CheckToolBase
|
2019-09-23 23:44:42 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class ClangtidyCheckTool(CheckToolBase):
|
2022-03-27 22:34:22 +03:00
|
|
|
def tool_output_filter(self, line): # pylint: disable=arguments-differ
|
2019-09-23 23:44:42 +03:00
|
|
|
if not self.options.get("verbose") and "[clang-diagnostic-error]" in line:
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
if "[CommonOptionsParser]" in line:
|
|
|
|
|
self._bad_input = True
|
|
|
|
|
return line
|
|
|
|
|
|
|
|
|
|
if any(d in line for d in ("note: ", "error: ", "warning: ")):
|
|
|
|
|
return line
|
|
|
|
|
|
|
|
|
|
return ""
|
|
|
|
|
|
2022-03-27 22:34:22 +03:00
|
|
|
def parse_defect(self, raw_line): # pylint: disable=arguments-differ
|
2019-09-23 23:44:42 +03:00
|
|
|
match = re.match(r"^(.*):(\d+):(\d+):\s+([^:]+):\s(.+)\[([^]]+)\]$", raw_line)
|
|
|
|
|
if not match:
|
|
|
|
|
return raw_line
|
|
|
|
|
|
|
|
|
|
file_, line, column, category, message, defect_id = match.groups()
|
|
|
|
|
|
|
|
|
|
severity = DefectItem.SEVERITY_LOW
|
|
|
|
|
if category == "error":
|
|
|
|
|
severity = DefectItem.SEVERITY_HIGH
|
|
|
|
|
elif category == "warning":
|
|
|
|
|
severity = DefectItem.SEVERITY_MEDIUM
|
|
|
|
|
|
|
|
|
|
return DefectItem(severity, category, message, file_, line, column, defect_id)
|
|
|
|
|
|
2020-12-22 00:21:32 +02:00
|
|
|
@staticmethod
|
|
|
|
|
def is_check_successful(cmd_result):
|
|
|
|
|
# Note: Clang-Tidy returns 1 for not critical compilation errors,
|
|
|
|
|
# so 0 and 1 are only acceptable values
|
|
|
|
|
return cmd_result["returncode"] < 2
|
|
|
|
|
|
2019-09-23 23:44:42 +03:00
|
|
|
def configure_command(self):
|
2022-04-01 22:05:30 +03:00
|
|
|
tool_path = join(self.get_tool_dir("tool-clangtidy"), "clang-tidy")
|
2019-09-23 23:44:42 +03:00
|
|
|
|
|
|
|
|
cmd = [tool_path, "--quiet"]
|
|
|
|
|
flags = self.get_flags("clangtidy")
|
2022-03-30 04:47:14 -04:00
|
|
|
if not (
|
|
|
|
|
self.is_flag_set("--checks", flags) or self.is_flag_set("--config", flags)
|
|
|
|
|
):
|
2019-09-23 23:44:42 +03:00
|
|
|
cmd.append("--checks=*")
|
|
|
|
|
|
2023-03-19 00:45:59 +02:00
|
|
|
project_files = self.get_project_target_files(
|
|
|
|
|
self.project_dir, self.options["src_filters"]
|
|
|
|
|
)
|
2020-04-26 00:10:41 +03:00
|
|
|
|
|
|
|
|
src_files = []
|
2022-07-02 19:19:48 +03:00
|
|
|
for items in project_files.values():
|
|
|
|
|
src_files.extend(items)
|
2020-04-26 00:10:41 +03:00
|
|
|
|
2020-08-25 21:19:21 +03:00
|
|
|
cmd.extend(flags + src_files + ["--"])
|
2020-04-26 00:10:41 +03:00
|
|
|
cmd.extend(
|
|
|
|
|
["-D%s" % d for d in self.cpp_defines + self.toolchain_defines["c++"]]
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
includes = []
|
|
|
|
|
for inc in self.cpp_includes:
|
|
|
|
|
if self.options.get("skip_packages") and inc.lower().startswith(
|
2021-10-24 18:19:40 +03:00
|
|
|
self.config.get("platformio", "packages_dir").lower()
|
2020-04-26 00:10:41 +03:00
|
|
|
):
|
|
|
|
|
continue
|
|
|
|
|
includes.append(inc)
|
|
|
|
|
|
2020-08-25 21:19:21 +03:00
|
|
|
cmd.extend(["-I%s" % inc for inc in includes])
|
2019-09-23 23:44:42 +03:00
|
|
|
|
|
|
|
|
return cmd
|