def __init__(self, project_dir, config, envname, options): self.config = config self.envname = envname self.options = options self.cpp_defines = [] self.cpp_flags = [] self.cpp_includes = [] self._defects = [] self._on_defect_callback = None self._bad_input = False self._load_cpp_data(project_dir, envname) # detect all defects by default if not self.options.get("severity"): self.options["severity"] = [ DefectItem.SEVERITY_LOW, DefectItem.SEVERITY_MEDIUM, DefectItem.SEVERITY_HIGH, ] # cast to severity by ids self.options["severity"] = [ s if isinstance(s, int) else DefectItem.severity_to_int(s) for s in self.options["severity"] ]
def parse_defect(self, raw_line): 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)
def parse_defects(self, output_file): defects = [] report = self._demangle_report(output_file) if not report: self._bad_input = True return [] try: defects_data = fromstring(report) except: # pylint: disable=bare-except click.echo("Error: Couldn't decode generated report!") self._bad_input = True return [] for table in defects_data.iter("PVS-Studio_Analysis_Log"): message = table.find("Message").text category = table.find("ErrorType").text line = table.find("Line").text file_ = table.find("File").text defect_id = table.find("ErrorCode").text cwe = table.find("CWECode") cwe_id = None if cwe is not None: cwe_id = cwe.text.lower().replace("cwe-", "") misra = table.find("MISRA") if misra is not None: message += " [%s]" % misra.text severity = DefectItem.SEVERITY_LOW if category == "error": severity = DefectItem.SEVERITY_HIGH elif category == "warning": severity = DefectItem.SEVERITY_MEDIUM defects.append( DefectItem(severity, category, message, file_, line, id=defect_id, cwe=cwe_id)) return defects
def parse_defect(self, raw_line): if self._field_delimiter not in raw_line: return None self._buffer += raw_line if any(f not in self._buffer for f in self.defect_fields): return None args = dict() for field in self._buffer.split(self._field_delimiter): field = field.strip().replace('"', "") name, value = field.split("=", 1) args[name] = value args["category"] = args["severity"] if args["severity"] == "error": args["severity"] = DefectItem.SEVERITY_HIGH elif args["severity"] == "warning": args["severity"] = DefectItem.SEVERITY_MEDIUM else: args["severity"] = DefectItem.SEVERITY_LOW # Skip defects found in third-party software, but keep in mind that such defects # might break checking process so defects from project files are not reported breaking_defect_ids = ("preprocessorErrorDirective", "syntaxError") if ( args.get("file", "") .lower() .startswith(self.config.get_optional_dir("packages").lower()) ): if args["id"] in breaking_defect_ids: if self.options.get("verbose"): click.echo( "Error: Found a breaking defect '%s' in %s:%s\n" "Please note: check results might not be valid!\n" "Try adding --skip-packages" % (args.get("message"), args.get("file"), args.get("line")) ) click.echo() self._bad_input = True self._buffer = "" return None self._buffer = "" return DefectItem(**args)
def parse_defect(self, raw_line): if "<&PIO&>" not in raw_line or any(f not in raw_line for f in self.defect_fields): return None args = dict() for field in raw_line.split("<&PIO&>"): field = field.strip().replace('"', "") name, value = field.split("=", 1) args[name] = value args["category"] = args["severity"] if args["severity"] == "error": args["severity"] = DefectItem.SEVERITY_HIGH elif args["severity"] == "warning": args["severity"] = DefectItem.SEVERITY_MEDIUM else: args["severity"] = DefectItem.SEVERITY_LOW return DefectItem(**args)