MCPcopy Index your code
hub / github.com/Tencent/CodeAnalysis / _run_cppcheck

Method _run_cppcheck

client/tool/cppcheck.py:216–289  ·  view source on GitHub ↗

执行cppcheck分析工具 :param files_path: :param rules: :param id_severity_map: :return:

(self, files_path, rules, id_severity_map)

Source from the content-addressed store, hash-verified

214 return {id_severity_map[rule_name] for rule_name in rule_list} - {"error"}
215
216 def _run_cppcheck(self, files_path, rules, id_severity_map):
217 """
218 执行cppcheck分析工具
219 :param files_path:
220 :param rules:
221 :param id_severity_map:
222 :return:
223 """
224 CPPCHECK_HOME = os.environ["CPPCHECK_HOME"]
225 LogPrinter.info("使用 cppcheck 为 %s" % CPPCHECK_HOME)
226 path_mgr = PathMgr()
227 cmd_args = [
228 "cppcheck",
229 "--quiet",
230 '--template="{file}[CODEDOG]{line}[CODEDOG]{id}[CODEDOG]{severity}[CODEDOG]{message}"',
231 "--inconclusive",
232 ]
233 # LogPrinter.info(f'rules after filtering: {rules}')
234 if not rules:
235 # cmd_args.append('--enable=all')
236 cmd_args.append("--enable=warning,style,information")
237 cmd_args.append("-j %s" % str(psutil.cpu_count()))
238 else:
239 visitors = self._get_needed_visitors(id_severity_map, rules)
240 if visitors:
241 cmd_args.append("--enable=%s" % ",".join(visitors))
242 # rules里出现unusedFunction 才不会开启并行检查
243 if "unusedFunction" not in rules:
244 cmd_args.append("-j %s" % psutil.cpu_count())
245
246 # 添加自定义正则表达式规则--rule-file
247 custom_rules = path_mgr.get_dir_files(os.path.join(CPPCHECK_HOME, "custom_plugins"), ".xml")
248 custom_rules = ["--rule-file=" + rule for rule in custom_rules]
249 cmd_args.extend(custom_rules)
250 # 添加代码补丁配置cfg --library
251 custom_cfgs = path_mgr.get_dir_files(os.path.join(CPPCHECK_HOME, "custom_cfg"), ".cfg")
252 custom_cfgs = ["--library=" + cfg for cfg in custom_cfgs]
253 cmd_args.extend(custom_cfgs)
254
255 # 指定分析文件
256 cmd_args.append("--file-list=%s" % files_path)
257 scan_result_path = "cppcheck_result.xml"
258 self.print_log(f"cmd: {' '.join(cmd_args)}")
259 cmd_args = path_mgr.format_cmd_arg_list(cmd_args)
260 SubProcController(
261 cmd_args,
262 cwd=CPPCHECK_HOME,
263 stderr_filepath=scan_result_path,
264 stderr_line_callback=self._error_callback,
265 stdout_line_callback=self.print_log,
266 ).wait()
267 # if not os.path.exists(scan_result_path):
268 # return scan_result_path
269 # try:
270 # if sys.platform == "win32":
271 # result_content = open(scan_result_path, 'r', encoding='gbk').read()
272 # open(scan_result_path, 'w', encoding='utf-8').write(result_content)
273 # with open(scan_result_path, 'r', encoding='utf-8') as rf:

Callers 1

analyzeMethod · 0.95

Calls 4

_get_needed_visitorsMethod · 0.95
get_dir_filesMethod · 0.80
infoMethod · 0.45
print_logMethod · 0.45

Tested by

no test coverage detected