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

Method analyze

client/tool/cppcheck.py:123–185  ·  view source on GitHub ↗

执行cppcheck分析任务 :param params: 需包含下面键值: 'rules': lint分析的规则列表 'incr_scan' : 是否增量分析 :return: return a :py:class:`IssueResponse`

(self, params)

Source from the content-addressed store, hash-verified

121 return result_list
122
123 def analyze(self, params):
124 """执行cppcheck分析任务
125
126 :param params: 需包含下面键值:
127 'rules': lint分析的规则列表
128 'incr_scan' : 是否增量分析
129
130 :return: return a :py:class:`IssueResponse`
131 """
132 source_dir = params.source_dir
133 work_dir = params.work_dir
134 incr_scan = params["incr_scan"]
135 relpos = len(source_dir) + 1
136 files_path = os.path.join(work_dir, "paths.txt")
137 path_mgr = PathMgr()
138
139 toscans = []
140 want_suffix = [".cpp", ".cxx", ".cc", ".c++", ".c", ".tpp", ".txx"]
141 if incr_scan:
142 diffs = SCMMgr(params).get_scm_diff()
143 toscans = [
144 os.path.join(source_dir, diff.path).replace(os.sep, "/")
145 for diff in diffs
146 if diff.path.endswith(tuple(want_suffix)) and diff.state != "del"
147 ]
148 else:
149 toscans = [path.replace(os.sep, "/") for path in PathMgr().get_dir_files(source_dir, tuple(want_suffix))]
150
151 toscans = FilterPathUtil(params).get_include_files(toscans, relpos)
152
153 if not toscans:
154 LogPrinter.debug("To-be-scanned files is empty ")
155 return []
156
157 with open(files_path, "w", encoding="UTF-8") as f:
158 f.write("\n".join(toscans))
159
160 # 执行cppcheck misra规则分析检查
161 addon_misra_result_list = self._run_misra_addon_analyze(
162 source_dir, params, files_path)
163
164 # 获取剩余的rules给cppcheck使用
165 rules = params["rules"]
166
167 id_severity_map = self._get_id_severity_map() # 获取当前版本cppcheck的 规则名:严重级别 对应关系
168 supported_rules = id_severity_map.keys() # 获取当前版本cppcheck支持的所有规则名
169 # 过滤掉当前版本cppcheck不支持的规则
170 filtered_rules = [r for r in rules if r not in supported_rules]
171 rules = list(set(rules) - set(filtered_rules))
172
173 # 执行 cppcheck 工具
174 scan_result_path = self._run_cppcheck(files_path, rules, id_severity_map)
175
176 if not os.path.exists(scan_result_path):
177 LogPrinter.info("result is empty ")
178 cppcheck_result_list = []
179 else:
180 # 格式化cppcheck结果

Callers

nothing calls this directly

Calls 8

_get_id_severity_mapMethod · 0.95
_run_cppcheckMethod · 0.95
_format_resultMethod · 0.95
get_dir_filesMethod · 0.80
debugMethod · 0.80
writeMethod · 0.45
infoMethod · 0.45

Tested by

no test coverage detected