diff options
-rw-r--r-- | lib/snakeyaml-2.4.jar | bin | 0 -> 339838 bytes | |||
-rw-r--r-- | src/cppcheckplus/control/MyContorlUtil.java | 117 | ||||
-rw-r--r-- | src/cppcheckplus/control/UIFrame.java | 31 | ||||
-rw-r--r-- | src/cppcheckplus/control/menubar.xml | 1 | ||||
-rw-r--r-- | src/cppcheckplus/control/ruleSet.yaml | 94 | ||||
-rw-r--r-- | src/cppcheckplus/text/creatTable.java | 228 | ||||
-rw-r--r-- | src/toolsconfig/ToolsConfig.java | 20 | ||||
-rw-r--r-- | src/toolsconfig/clangTidy.java | 191 | ||||
-rw-r--r-- | src/toolsconfig/cppcheck.java | 93 | ||||
-rw-r--r-- | src/toolsconfig/flawfinder.java | 50 | ||||
-rw-r--r-- | src/toolsconfig/ruleSet.java | 93 | ||||
-rw-r--r-- | src/toolsconfig/settings.xml | 8 | ||||
-rw-r--r-- | src/toolsconfig/tools.yaml | 51 |
13 files changed, 565 insertions, 412 deletions
diff --git a/lib/snakeyaml-2.4.jar b/lib/snakeyaml-2.4.jar new file mode 100644 index 0000000..c8bb638 --- /dev/null +++ b/lib/snakeyaml-2.4.jar | |||
Binary files differ | |||
diff --git a/src/cppcheckplus/control/MyContorlUtil.java b/src/cppcheckplus/control/MyContorlUtil.java index e7e3d07..1507663 100644 --- a/src/cppcheckplus/control/MyContorlUtil.java +++ b/src/cppcheckplus/control/MyContorlUtil.java | |||
@@ -22,12 +22,11 @@ import java.awt.TexturePaint; | |||
22 | import java.awt.event.ActionListener; | 22 | import java.awt.event.ActionListener; |
23 | import java.awt.image.BufferedImage; | 23 | import java.awt.image.BufferedImage; |
24 | import java.awt.image.PixelGrabber; | 24 | import java.awt.image.PixelGrabber; |
25 | import java.io.File; | ||
26 | import java.io.FileOutputStream; | ||
27 | import java.io.IOException; | 25 | import java.io.IOException; |
28 | import java.io.InputStream; | 26 | import java.io.InputStream; |
29 | import java.util.ArrayList; | 27 | import java.util.ArrayList; |
30 | import java.util.List; | 28 | import java.util.List; |
29 | import java.util.Map; | ||
31 | import javax.imageio.ImageIO; | 30 | import javax.imageio.ImageIO; |
32 | import javax.swing.BorderFactory; | 31 | import javax.swing.BorderFactory; |
33 | import javax.swing.Box; | 32 | import javax.swing.Box; |
@@ -35,20 +34,15 @@ import javax.swing.Icon; | |||
35 | import javax.swing.ImageIcon; | 34 | import javax.swing.ImageIcon; |
36 | import javax.swing.JLabel; | 35 | import javax.swing.JLabel; |
37 | import javax.swing.JMenuItem; | 36 | import javax.swing.JMenuItem; |
38 | import javax.swing.JOptionPane; | ||
39 | import javax.swing.UIManager; | 37 | import javax.swing.UIManager; |
40 | import javax.swing.plaf.FontUIResource; | 38 | import javax.swing.plaf.FontUIResource; |
41 | import javax.xml.parsers.DocumentBuilder; | 39 | import javax.xml.parsers.DocumentBuilder; |
42 | import javax.xml.parsers.DocumentBuilderFactory; | 40 | import javax.xml.parsers.DocumentBuilderFactory; |
43 | import javax.xml.transform.Transformer; | ||
44 | import javax.xml.transform.TransformerFactory; | ||
45 | import javax.xml.transform.dom.DOMSource; | ||
46 | import javax.xml.transform.stream.StreamResult; | ||
47 | import org.w3c.dom.Document; | 41 | import org.w3c.dom.Document; |
48 | import org.w3c.dom.Element; | 42 | import org.w3c.dom.Element; |
49 | import org.w3c.dom.Node; | 43 | import org.w3c.dom.Node; |
50 | import org.w3c.dom.NodeList; | 44 | import org.w3c.dom.NodeList; |
51 | import toolsconfig.ToolsConfig; | 45 | import org.yaml.snakeyaml.Yaml; |
52 | import toolsconfig.ToolsRegistry; | 46 | import toolsconfig.ToolsRegistry; |
53 | 47 | ||
54 | 48 | ||
@@ -318,116 +312,39 @@ public class MyContorlUtil { | |||
318 | return menuBar; | 312 | return menuBar; |
319 | } | 313 | } |
320 | 314 | ||
321 | |||
322 | public static void fixSettingPath(String xml, String tool, String newPath) { | ||
323 | try { | ||
324 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | ||
325 | DocumentBuilder db = dbf.newDocumentBuilder(); | ||
326 | Document doc = db.parse(MyContorlUtil.getXMLFile(xml)); | ||
327 | Element root = doc.getDocumentElement(); | ||
328 | NodeList tools = root.getChildNodes(); | ||
329 | if (tools != null) { | ||
330 | for (int i = 0; i < tools.getLength(); i++) { | ||
331 | org.w3c.dom.Node menu = tools.item(i); | ||
332 | if (menu.getNodeType() == Node.ELEMENT_NODE) { | ||
333 | if (menu.getNodeName().equalsIgnoreCase("tool")) { | ||
334 | org.w3c.dom.Node att = | ||
335 | menu.getAttributes().getNamedItem("id"); | ||
336 | String toolName = | ||
337 | MyContorlUtil.getStringAttribute(menu, | ||
338 | "toolName"); | ||
339 | if (tool.equals("cppcheck") && | ||
340 | toolName.equals("cppcheck")) { | ||
341 | org.w3c.dom.Node attribute = | ||
342 | menu.getAttributes() | ||
343 | .getNamedItem("filePath"); | ||
344 | if (attribute != null) { | ||
345 | attribute.setNodeValue( | ||
346 | newPath.replaceAll("\\\\", "\\/")); | ||
347 | } | ||
348 | } else if (tool.equals("flawfinder") && | ||
349 | toolName.equals("flawfinder")) { | ||
350 | org.w3c.dom.Node attribute = | ||
351 | menu.getAttributes() | ||
352 | .getNamedItem("filePath"); | ||
353 | if (attribute != null) { | ||
354 | attribute.setNodeValue( | ||
355 | newPath.replaceAll("\\\\", "\\/")); | ||
356 | } | ||
357 | } | ||
358 | } | ||
359 | } | ||
360 | } | ||
361 | } | ||
362 | |||
363 | Transformer transformer = | ||
364 | TransformerFactory.newInstance().newTransformer(); | ||
365 | DOMSource source = new DOMSource(doc); | ||
366 | File file = new File(ClassLoader.getSystemResource(xml).toURI()); | ||
367 | FileOutputStream outstream = | ||
368 | new FileOutputStream("src/cppcheckplus/control/settings.xml"); | ||
369 | StreamResult reslut = new StreamResult(outstream); | ||
370 | transformer.transform(source, reslut); | ||
371 | outstream.close(); | ||
372 | |||
373 | } catch (Exception ex) { | ||
374 | ex.printStackTrace(); | ||
375 | } | ||
376 | } | ||
377 | |||
378 | public static void loadToolsRoad(String xml, UIFrame ui, | 315 | public static void loadToolsRoad(String xml, UIFrame ui, |
379 | ActionListener action) { | 316 | ActionListener action) { |
380 | try { | 317 | Yaml yaml = new Yaml(); |
381 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | ||
382 | DocumentBuilder db = dbf.newDocumentBuilder(); | ||
383 | Document doc = db.parse(MyContorlUtil.getXMLFile(xml)); | ||
384 | Element root = doc.getDocumentElement(); | ||
385 | NodeList tools = root.getChildNodes(); | ||
386 | 318 | ||
319 | try (InputStream input = Main.class.getClassLoader() | ||
320 | .getResourceAsStream(xml)) { | ||
321 | Map<String, Object> data = yaml.load(input); | ||
387 | MyRootMenu checkBar = new MyRootMenu(); | 322 | MyRootMenu checkBar = new MyRootMenu(); |
388 | String toolName, filePath, para; | 323 | data.forEach((toolName, value) -> { |
389 | MyMenuItem menuItem; | 324 | Map<String, Object> tool = (Map<String, Object>) value; |
390 | if (tools == null) { | ||
391 | // 弹窗提示 | ||
392 | JOptionPane.showMessageDialog(null, "Error: 工具配置文件未找到", | ||
393 | "提示", 2); | ||
394 | return; | ||
395 | } | ||
396 | for (int i = 0; i < tools.getLength(); i++) { | ||
397 | org.w3c.dom.Node menu = tools.item(i); | ||
398 | if (menu.getNodeType() != Node.ELEMENT_NODE || | ||
399 | !menu.getNodeName().equalsIgnoreCase("tool")) { | ||
400 | continue; | ||
401 | } | ||
402 | toolName = MyContorlUtil.getStringAttribute(menu, "toolName"); | ||
403 | filePath = MyContorlUtil.getStringAttribute(menu, "filePath"); | ||
404 | para = MyContorlUtil.getStringAttribute(menu, "para"); | ||
405 | |||
406 | ToolsConfig tool = ToolsRegistry.getTool(toolName); | ||
407 | if (tool != null) { | ||
408 | tool.init(filePath, para, ui); | ||
409 | continue; | ||
410 | } | ||
411 | try { | 325 | try { |
412 | Class<?> clazz = Class.forName("toolsconfig." + toolName); | 326 | Class<?> clazz = Class.forName("toolsconfig." + toolName); |
413 | Object obj = clazz.getDeclaredConstructor().newInstance(); | 327 | Object obj = clazz.getDeclaredConstructor().newInstance(); |
414 | ToolsRegistry.getTool(toolName).init(filePath, para, ui); | 328 | ToolsRegistry.getTool(toolName) |
329 | .init(tool.get("path").toString(), | ||
330 | tool.get("params").toString(), ui.ct, | ||
331 | (Map<String, String>) tool.get("ruleMap")); | ||
415 | } catch (Exception e) { | 332 | } catch (Exception e) { |
416 | e.printStackTrace(); | 333 | e.printStackTrace(); |
417 | } | 334 | } |
418 | menuItem = new MyMenuItem(); | 335 | MyMenuItem menuItem = new MyMenuItem(); |
419 | menuItem.setText(toolName); | 336 | menuItem.setText(toolName); |
420 | menuItem.setToolTipText(toolName); | 337 | menuItem.setToolTipText(toolName); |
421 | menuItem.setActionCommand(toolName); | 338 | menuItem.setActionCommand(toolName); |
422 | 339 | ||
423 | menuItem.addActionListener(action); | 340 | menuItem.addActionListener(action); |
424 | checkBar.add(menuItem); | 341 | checkBar.add(menuItem); |
425 | } | 342 | }); |
426 | 343 | ||
427 | checkBar.setText("检工具"); | 344 | checkBar.setText("检工具"); |
428 | ui.mymenubar.add(checkBar, 1); | 345 | ui.mymenubar.add(checkBar, 1); |
429 | } catch (Exception ex) { | 346 | } catch (Exception e) { |
430 | ex.printStackTrace(); | 347 | e.printStackTrace(); |
431 | } | 348 | } |
432 | } | 349 | } |
433 | 350 | ||
diff --git a/src/cppcheckplus/control/UIFrame.java b/src/cppcheckplus/control/UIFrame.java index 499d056..db6a6c7 100644 --- a/src/cppcheckplus/control/UIFrame.java +++ b/src/cppcheckplus/control/UIFrame.java | |||
@@ -34,8 +34,8 @@ import javax.swing.JTextArea; | |||
34 | import javax.swing.event.ListSelectionEvent; | 34 | import javax.swing.event.ListSelectionEvent; |
35 | import javax.swing.event.ListSelectionListener; | 35 | import javax.swing.event.ListSelectionListener; |
36 | import javax.swing.plaf.TabbedPaneUI; | 36 | import javax.swing.plaf.TabbedPaneUI; |
37 | import toolsconfig.ToolsConfig; | ||
38 | import toolsconfig.ToolsRegistry; | 37 | import toolsconfig.ToolsRegistry; |
38 | import toolsconfig.ruleSet; | ||
39 | 39 | ||
40 | // 构造界面 | 40 | // 构造界面 |
41 | public class UIFrame extends JFrame { | 41 | public class UIFrame extends JFrame { |
@@ -43,8 +43,9 @@ public class UIFrame extends JFrame { | |||
43 | // XML配置文件路径 | 43 | // XML配置文件路径 |
44 | private final String menuBarXML = "cppcheckplus/control/menubar.xml"; | 44 | private final String menuBarXML = "cppcheckplus/control/menubar.xml"; |
45 | private final String outlookPanelXML = "cppcheckplus/control/outlook.xml"; | 45 | private final String outlookPanelXML = "cppcheckplus/control/outlook.xml"; |
46 | private final String settingsXML = "toolsconfig/settings.xml"; | 46 | private final String settingFile = "toolsconfig/tools.yaml"; |
47 | private final String stdclangXML = "cppcheckplus/control/stdclang.xml"; | 47 | private final String stdclangXML = "cppcheckplus/control/stdclang.xml"; |
48 | private final String ruleSetFile = "cppcheckplus/control/ruleSet.yaml"; | ||
48 | // 界面组件 | 49 | // 界面组件 |
49 | private final MyContentPanel contentPanel; | 50 | private final MyContentPanel contentPanel; |
50 | private final JFileChooser jfc = new JFileChooser();// 文件选择器 | 51 | private final JFileChooser jfc = new JFileChooser();// 文件选择器 |
@@ -69,10 +70,8 @@ public class UIFrame extends JFrame { | |||
69 | public UIFrame() { | 70 | public UIFrame() { |
70 | projectPath = System.getProperty("user.dir"); | 71 | projectPath = System.getProperty("user.dir"); |
71 | // 文件选择器初始目录 | 72 | // 文件选择器初始目录 |
72 | jfc.setCurrentDirectory(new File("d://")); | 73 | jfc.setCurrentDirectory(new File(".")); |
73 | jfsettings.setCurrentDirectory(new File("d://")); | 74 | jfsettings.setCurrentDirectory(new File(".")); |
74 | |||
75 | ct.initSTDClang(stdclangXML);// 设置clang配置 | ||
76 | 75 | ||
77 | // 加载菜单栏,注册事件监听 | 76 | // 加载菜单栏,注册事件监听 |
78 | mymenubar = MyContorlUtil.loadMenuBar(menuBarXML, new ActionListener() { | 77 | mymenubar = MyContorlUtil.loadMenuBar(menuBarXML, new ActionListener() { |
@@ -150,6 +149,11 @@ public class UIFrame extends JFrame { | |||
150 | JOptionPane.showMessageDialog(null, "cppcheckplus", | 149 | JOptionPane.showMessageDialog(null, "cppcheckplus", |
151 | "提示", 2); | 150 | "提示", 2); |
152 | break; | 151 | break; |
152 | case "onRuleSet": | ||
153 | // 弹出窗口,显示规则集,让用户选择 | ||
154 | JFrame ruleSetFrame = new JFrame(); | ||
155 | ruleSet.getInstance().showRuleSet(ruleSetFrame); | ||
156 | break; | ||
153 | case "onOrderType": | 157 | case "onOrderType": |
154 | // 按类型排序:生成表格并按类型排序 | 158 | // 按类型排序:生成表格并按类型排序 |
155 | ct.makeTable(); | 159 | ct.makeTable(); |
@@ -196,7 +200,7 @@ public class UIFrame extends JFrame { | |||
196 | } | 200 | } |
197 | }); | 201 | }); |
198 | 202 | ||
199 | MyContorlUtil.loadToolsRoad(settingsXML, this, new ActionListener() { | 203 | MyContorlUtil.loadToolsRoad(settingFile, this, new ActionListener() { |
200 | @Override | 204 | @Override |
201 | public void actionPerformed(ActionEvent e) { | 205 | public void actionPerformed(ActionEvent e) { |
202 | String toolName = e.getActionCommand(); | 206 | String toolName = e.getActionCommand(); |
@@ -215,6 +219,14 @@ public class UIFrame extends JFrame { | |||
215 | } | 219 | } |
216 | }); | 220 | }); |
217 | 221 | ||
222 | toolsconfig.clangTidy clangTidy = | ||
223 | (toolsconfig.clangTidy) ToolsRegistry.getTool("clangTidy"); | ||
224 | if (clangTidy != null) { | ||
225 | clangTidy.initSTDClang(stdclangXML); | ||
226 | } | ||
227 | |||
228 | ruleSet.getInstance().loadRuleSet(ruleSetFile); | ||
229 | |||
218 | // 初始化主内容面板 | 230 | // 初始化主内容面板 |
219 | contentPanel = new MyContentPanel(); | 231 | contentPanel = new MyContentPanel(); |
220 | try { | 232 | try { |
@@ -235,14 +247,9 @@ public class UIFrame extends JFrame { | |||
235 | // 菜单栏在最上方 | 247 | // 菜单栏在最上方 |
236 | contentPanel.add(mymenubar, "North"); | 248 | contentPanel.add(mymenubar, "North"); |
237 | 249 | ||
238 | // 除却菜单栏之外的面板 | ||
239 | //JPanel centerPane = new JPanel(new BorderLayout()); | ||
240 | |||
241 | // 中间区域,除了菜单栏之外的所有内容 | 250 | // 中间区域,除了菜单栏之外的所有内容 |
242 | MyPanel centerPane = new MyPanel(new BorderLayout()); | 251 | MyPanel centerPane = new MyPanel(new BorderLayout()); |
243 | centerPane.setOpaque(true); | 252 | centerPane.setOpaque(true); |
244 | //centerPane.setBackground(MyContorlUtil.CONTENT_PANE_BACKGROUND3); | ||
245 | //centerPane.setBackground(MyContorlUtil.getImage("control/images/logo2.png")); | ||
246 | centerPane.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0)); | 253 | centerPane.setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0)); |
247 | contentPanel.add(centerPane, "Center"); | 254 | contentPanel.add(centerPane, "Center"); |
248 | 255 | ||
diff --git a/src/cppcheckplus/control/menubar.xml b/src/cppcheckplus/control/menubar.xml index 0aae26b..17ff0b8 100644 --- a/src/cppcheckplus/control/menubar.xml +++ b/src/cppcheckplus/control/menubar.xml | |||
@@ -9,6 +9,7 @@ | |||
9 | <menuitem text="关闭" tooltip="退出程序" icon="control/images/toolbar/right.png" action="onMenuClose"/> | 9 | <menuitem text="关闭" tooltip="退出程序" icon="control/images/toolbar/right.png" action="onMenuClose"/> |
10 | </menu> | 10 | </menu> |
11 | <menu text="设置"> | 11 | <menu text="设置"> |
12 | <menuitem text="规则集选择" tooltip="检查规则集选择" icon="control/images/add.png" action="onRuleSet"/> | ||
12 | <menu text="结果筛选"> | 13 | <menu text="结果筛选"> |
13 | <menuitem text="按照类别筛选" tooltip="结果按缺陷类别排序" icon="control/images/email.png" action="onOrderType"/> | 14 | <menuitem text="按照类别筛选" tooltip="结果按缺陷类别排序" icon="control/images/email.png" action="onOrderType"/> |
14 | <menuitem text="按照级别筛选" tooltip="结果按缺陷等级排序" icon="control/images/email.png" action="onOrderLevel"/> | 15 | <menuitem text="按照级别筛选" tooltip="结果按缺陷等级排序" icon="control/images/email.png" action="onOrderLevel"/> |
diff --git a/src/cppcheckplus/control/ruleSet.yaml b/src/cppcheckplus/control/ruleSet.yaml new file mode 100644 index 0000000..bb7d11d --- /dev/null +++ b/src/cppcheckplus/control/ruleSet.yaml | |||
@@ -0,0 +1,94 @@ | |||
1 | NestedStructureInit: | ||
2 | name: "嵌套结构体初始化" | ||
3 | description: "结构体初始化的嵌套结构,与结构体定义保持一致" | ||
4 | selected: true | ||
5 | |||
6 | CheckNullPointer: | ||
7 | name: "空指针检查" | ||
8 | description: "指针使用前检查是否为空" | ||
9 | selected: true | ||
10 | |||
11 | CheckSelfCall: | ||
12 | name: "禁止直接或间接自调用" | ||
13 | description: "函数自调用检查" | ||
14 | selected: false | ||
15 | |||
16 | CheckDiamondInheritance: | ||
17 | name: "菱形继承检查" | ||
18 | description: "菱形继承的基类必须为虚基类" | ||
19 | selected: false | ||
20 | |||
21 | CheckCatchFromDerived: | ||
22 | name: "异常捕获检查" | ||
23 | description: "异常捕获时,派生类异常捕获必须在基类异常捕获之前" | ||
24 | selected: true | ||
25 | |||
26 | CheckStringEnd: | ||
27 | name: "字符串结尾检查" | ||
28 | description: "字符串结尾必须为'\0'" | ||
29 | selected: false | ||
30 | |||
31 | CheckConstInsteadOfMacro: | ||
32 | name: "const替代宏定义" | ||
33 | description: "宏定义替换为const常量" | ||
34 | selected: true | ||
35 | |||
36 | CheckEnumUsage: | ||
37 | name: "相关常量使用枚举" | ||
38 | description: "相关整形常量应使用枚举" | ||
39 | selected: true | ||
40 | |||
41 | CheckMemOpForNotPOD: | ||
42 | name: "非POD类型内存操作检查" | ||
43 | description: "禁止使用memcpy、memset等内存操作函数对非POD类型进行操作" | ||
44 | selected: true | ||
45 | |||
46 | CheckCStyleCast: | ||
47 | name: "禁止C风格类型转换" | ||
48 | description: "禁止使用C风格类型转换" | ||
49 | selected: true | ||
50 | |||
51 | CheckInlineInsteadOfMacro: | ||
52 | name: "内联函数替代宏定义" | ||
53 | description: "宏定义替换为内联函数" | ||
54 | selected: true | ||
55 | |||
56 | CheckExplicitForConstructorWithSingleParameter: | ||
57 | name: "单参数构造函数要explicit" | ||
58 | description: "explicit防止传参过程中的隐式转换" | ||
59 | selected: true | ||
60 | |||
61 | CheckReturnValueOfOperatorAssign: | ||
62 | name: "operator=返回*this的引用" | ||
63 | description: "operator=返回*this的引用,以支持连续赋值" | ||
64 | selected: true | ||
65 | |||
66 | CheckAssignToSelf: | ||
67 | name: "operator= 自赋值检查" | ||
68 | description: "operator= 自赋值检查" | ||
69 | selected: true | ||
70 | |||
71 | CheckCallVirtualInConstructor: | ||
72 | name: "禁止构造函数中调用虚函数" | ||
73 | description: "构造函数中调用虚函数" | ||
74 | selected: true | ||
75 | |||
76 | CheckRedefineNonVirtualFunction: | ||
77 | name: "禁止重定义非虚函数" | ||
78 | description: "禁止重定义非虚函数" | ||
79 | selected: true | ||
80 | |||
81 | CheckDeleteArray: | ||
82 | name: "delete[]删除数组" | ||
83 | description: "delete[]替代delete删除数组,防止内存泄漏" | ||
84 | selected: true | ||
85 | |||
86 | CheckReturnLocalObjectPointer: | ||
87 | name: "禁止返回局部对象指针" | ||
88 | description: "返回局部对象指针" | ||
89 | selected: true | ||
90 | |||
91 | ChecCatchByReference: | ||
92 | name: "通过传值方式抛出,引用方式捕获" | ||
93 | description: "通过传值方式抛出,引用方式捕获" | ||
94 | selected: true | ||
diff --git a/src/cppcheckplus/text/creatTable.java b/src/cppcheckplus/text/creatTable.java index f261e3a..cee37e1 100644 --- a/src/cppcheckplus/text/creatTable.java +++ b/src/cppcheckplus/text/creatTable.java | |||
@@ -35,39 +35,6 @@ public class creatTable { | |||
35 | } | 35 | } |
36 | } | 36 | } |
37 | 37 | ||
38 | public void initSTDClang(String xml) { | ||
39 | try { | ||
40 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | ||
41 | DocumentBuilder db = dbf.newDocumentBuilder(); | ||
42 | Document doc = db.parse(MyContorlUtil.getXMLFile(xml)); | ||
43 | Element root = doc.getDocumentElement(); | ||
44 | NodeList stdlist = root.getChildNodes(); | ||
45 | if (stdlist != null) { | ||
46 | for (int i = 0; i < stdlist.getLength(); i++) { | ||
47 | org.w3c.dom.Node menu = stdlist.item(i); | ||
48 | if (menu.getNodeType() == Node.ELEMENT_NODE) { | ||
49 | if (menu.getNodeName().equalsIgnoreCase("clangNode")) { | ||
50 | stdClangType node = new stdClangType(); | ||
51 | node.cwe = | ||
52 | MyContorlUtil.getStringAttribute(menu, "cwe"); | ||
53 | node.level = | ||
54 | MyContorlUtil.getStringAttribute(menu, "level"); | ||
55 | node.target = MyContorlUtil.getStringAttribute(menu, | ||
56 | "target"); | ||
57 | node.len = Integer.valueOf( | ||
58 | MyContorlUtil.getStringAttribute(menu, "len")); | ||
59 | node.type = | ||
60 | MyContorlUtil.getStringAttribute(menu, "type"); | ||
61 | stdclang.add(node); | ||
62 | } | ||
63 | } | ||
64 | } | ||
65 | } | ||
66 | } catch (Exception ex) { | ||
67 | ex.printStackTrace(); | ||
68 | } | ||
69 | } | ||
70 | |||
71 | public void makeTable() {// 统一并输出 | 38 | public void makeTable() {// 统一并输出 |
72 | results.clear(); | 39 | results.clear(); |
73 | for (int i = 0; i < cppcheckplusResults.size(); i++) { | 40 | for (int i = 0; i < cppcheckplusResults.size(); i++) { |
@@ -130,199 +97,4 @@ public class creatTable { | |||
130 | } | 97 | } |
131 | } | 98 | } |
132 | } | 99 | } |
133 | |||
134 | public void toRowsOfCppcheckResult(String input) {// | ||
135 | String[] temp = input.split("\\$"); | ||
136 | int size = temp.length; | ||
137 | String until = null; | ||
138 | String file = null; | ||
139 | String line = null; | ||
140 | String level = null; | ||
141 | String type = null; | ||
142 | String cwe = null; | ||
143 | String description = null; | ||
144 | if (size > 6) { | ||
145 | if (temp[1].length() == 0) { | ||
146 | return; | ||
147 | } | ||
148 | until = temp[0]; | ||
149 | file = temp[1]; | ||
150 | line = temp[2]; | ||
151 | level = temp[3]; | ||
152 | if (temp[3].equals("error")) { | ||
153 | level = "4"; | ||
154 | } else if (temp[3].equals("warning")) { | ||
155 | level = "3"; | ||
156 | } else if (temp[2].equals("style")) { | ||
157 | level = "1"; | ||
158 | } else if (temp[2].equals("information")) { | ||
159 | level = "0"; | ||
160 | } else { | ||
161 | level = "2"; | ||
162 | } | ||
163 | type = temp[4]; | ||
164 | description = temp[5]; | ||
165 | cwe = temp[6]; | ||
166 | DeflectRow node = | ||
167 | new DeflectRow(until, file, line, level, type, cwe, description, | ||
168 | 0); | ||
169 | stdCppcheck(node); | ||
170 | cppcheckplusResults.add(node); | ||
171 | } | ||
172 | } | ||
173 | |||
174 | public void stdCppcheck(DeflectRow node) { | ||
175 | String cwe = node.getCwe(); | ||
176 | int cwenum = Integer.parseInt(cwe); | ||
177 | switch (cwenum) { //cppcheck向flawfinder看齐 | ||
178 | case 134: | ||
179 | node.setLevel("4"); | ||
180 | node.setType("format"); | ||
181 | break; | ||
182 | case 367: | ||
183 | node.setLevel("4"); | ||
184 | node.setType("race"); | ||
185 | break; | ||
186 | case 120: | ||
187 | node.setLevel("4"); | ||
188 | node.setType("buff"); | ||
189 | break; | ||
190 | case 78: | ||
191 | node.setLevel("4"); | ||
192 | node.setType("shell"); | ||
193 | break; | ||
194 | case 327: | ||
195 | node.setLevel("3"); | ||
196 | node.setType("random"); | ||
197 | break; | ||
198 | case 119: | ||
199 | node.setLevel("4"); | ||
200 | node.setType("buffer"); | ||
201 | break; | ||
202 | case 362: | ||
203 | node.setLevel("2"); | ||
204 | node.setType("misc"); | ||
205 | break; | ||
206 | case 190: | ||
207 | node.setLevel("2"); | ||
208 | node.setType("integer"); | ||
209 | break; | ||
210 | default: | ||
211 | ; | ||
212 | } | ||
213 | } | ||
214 | |||
215 | public void toRowsOfClang(String input) { | ||
216 | String[] temp = input.split(":"); | ||
217 | int size = temp.length; | ||
218 | String until = null; | ||
219 | String file = null; | ||
220 | String line = null; | ||
221 | String column = null; | ||
222 | String level = null; | ||
223 | String type = null; | ||
224 | String cwe = null; | ||
225 | String description = null; | ||
226 | if (size > 5) { | ||
227 | until = "clang-tidy"; | ||
228 | file = temp[0] + ":" + temp[1]; | ||
229 | line = temp[2]; | ||
230 | column = temp[3]; | ||
231 | level = temp[4];// warning,note, error | ||
232 | type = ""; | ||
233 | description = temp[5]; | ||
234 | cwe = "0"; | ||
235 | // 标准化 | ||
236 | DeflectRow node = | ||
237 | new DeflectRow(until, file, line, level, type, cwe, description, | ||
238 | 0); | ||
239 | stdClang(node); | ||
240 | clangResults.add(node); | ||
241 | } | ||
242 | } | ||
243 | |||
244 | public void stdClang(DeflectRow node) { | ||
245 | String describ = node.getDescription(); | ||
246 | int len = describ.length(); | ||
247 | boolean isFound = false; | ||
248 | for (int i = 0; i < stdclang.size(); i++) { | ||
249 | if (len > stdclang.elementAt(i).len) { | ||
250 | if (describ.substring(1, stdclang.elementAt(i).len) | ||
251 | .equals(stdclang.elementAt(i).target)) { | ||
252 | node.setCwe(stdclang.elementAt(i).cwe); | ||
253 | node.setLevel(stdclang.elementAt(i).level); | ||
254 | node.setType(stdclang.elementAt(i).type); | ||
255 | isFound = true; | ||
256 | return; | ||
257 | } | ||
258 | } | ||
259 | } | ||
260 | if (!isFound) { | ||
261 | node.setCwe("未知"); | ||
262 | node.setLevel("2"); | ||
263 | node.setType("other"); | ||
264 | } | ||
265 | } | ||
266 | |||
267 | |||
268 | public void toRowsOfFlawfinder(String csvFileName) {// csv | ||
269 | |||
270 | String until = null; | ||
271 | // public String defObject = null; | ||
272 | String file = null; | ||
273 | String location = null; | ||
274 | String level = null; | ||
275 | String type = null; | ||
276 | String function = null; | ||
277 | String description = null; | ||
278 | DeflectRow node = | ||
279 | new DeflectRow(until, file, location, level, type, function, | ||
280 | description, 0); | ||
281 | } | ||
282 | |||
283 | public boolean isInteger(String str) { | ||
284 | Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$"); | ||
285 | return pattern.matcher(str).matches(); | ||
286 | } | ||
287 | |||
288 | public void toRowsOfflawfinder(String input) { | ||
289 | // File,Line,Column,Level,Category,Name,Warning,Suggestion,Note,CWEs,Context,Fingerprint | ||
290 | String[] temp = input.split(","); | ||
291 | int size = temp.length; | ||
292 | String until = null; | ||
293 | String file = null; | ||
294 | String line = null; | ||
295 | String column = null; | ||
296 | String level = null; | ||
297 | String category = null; | ||
298 | String name = null; | ||
299 | String warning = null; | ||
300 | String suggestion = null; | ||
301 | String note = null; | ||
302 | String cwe = null; | ||
303 | String context = null; | ||
304 | String fingerprint = null; | ||
305 | if (size > 11) { | ||
306 | if (!isInteger(temp[1])) { | ||
307 | return; | ||
308 | } | ||
309 | until = "flawfinder"; | ||
310 | file = temp[0]; | ||
311 | line = temp[1]; | ||
312 | column = temp[2]; | ||
313 | level = temp[3]; | ||
314 | category = temp[4]; | ||
315 | name = temp[5]; | ||
316 | warning = temp[6]; | ||
317 | suggestion = temp[7]; | ||
318 | note = temp[8]; | ||
319 | cwe = temp[9]; | ||
320 | context = temp[10]; | ||
321 | fingerprint = temp[11]; | ||
322 | DeflectRow node = | ||
323 | new DeflectRow(until, file, line, level, category, cwe, warning, | ||
324 | 0); | ||
325 | flawfinderResults.add(node); | ||
326 | } | ||
327 | } | ||
328 | } | 100 | } |
diff --git a/src/toolsconfig/ToolsConfig.java b/src/toolsconfig/ToolsConfig.java index fc1b6a0..a027e1f 100644 --- a/src/toolsconfig/ToolsConfig.java +++ b/src/toolsconfig/ToolsConfig.java | |||
@@ -1,8 +1,8 @@ | |||
1 | package toolsconfig; | 1 | package toolsconfig; |
2 | 2 | ||
3 | import cppcheckplus.control.UIFrame; | ||
4 | import cppcheckplus.text.MyTextPanel1; | 3 | import cppcheckplus.text.MyTextPanel1; |
5 | import java.awt.TextArea; | 4 | import java.awt.TextArea; |
5 | import java.util.Map; | ||
6 | 6 | ||
7 | public abstract class ToolsConfig { | 7 | public abstract class ToolsConfig { |
8 | public static final int SUCCESS = 0; // 表示程序执行成功 | 8 | public static final int SUCCESS = 0; // 表示程序执行成功 |
@@ -10,26 +10,34 @@ public abstract class ToolsConfig { | |||
10 | public static final String ERROR_MESSAGE = "程序执行出错:"; | 10 | public static final String ERROR_MESSAGE = "程序执行出错:"; |
11 | public static final String ENTERWindows = "\r\n"; | 11 | public static final String ENTERWindows = "\r\n"; |
12 | public MyTextPanel1 textPanel; | 12 | public MyTextPanel1 textPanel; |
13 | public String name; | ||
13 | protected String path; | 14 | protected String path; |
14 | protected String params; | 15 | protected String params; |
15 | protected UIFrame uiFrame; | 16 | // protected UIFrame uiFrame; |
17 | protected cppcheckplus.text.creatTable ct; | ||
16 | protected TextArea result; | 18 | protected TextArea result; |
17 | public String name; | 19 | protected Map<String, String> toolRuleSet; |
18 | 20 | ||
19 | protected abstract void execute(String src); | 21 | protected abstract void execute(String src); |
20 | 22 | ||
21 | public void init(String path, String params, UIFrame uiFrame) { | 23 | public void init(String path, String params, |
24 | cppcheckplus.text.creatTable ct, | ||
25 | Map<String, String> toolRuleSet) { | ||
22 | this.path = path; | 26 | this.path = path; |
23 | this.params = params; | 27 | this.params = params; |
24 | this.textPanel = new MyTextPanel1(); | 28 | this.textPanel = new MyTextPanel1(); |
25 | this.result = this.textPanel.getTextArea(); | 29 | this.result = this.textPanel.getTextArea(); |
26 | this.uiFrame = uiFrame; | 30 | // this.uiFrame = uiFrame; |
31 | this.ct = ct; | ||
32 | this.toolRuleSet = toolRuleSet; | ||
27 | } | 33 | } |
28 | 34 | ||
29 | public boolean checkInit() { | 35 | public boolean checkInit() { |
30 | return path != null && params != null && uiFrame != null; | 36 | return path != null && params != null && ct != null; |
31 | } | 37 | } |
32 | 38 | ||
39 | abstract public void toRowsOfTable(String input); | ||
40 | |||
33 | public void clear() { | 41 | public void clear() { |
34 | this.result.setText(""); | 42 | this.result.setText(""); |
35 | } | 43 | } |
diff --git a/src/toolsconfig/clangTidy.java b/src/toolsconfig/clangTidy.java index 6e3cb59..2f09d38 100644 --- a/src/toolsconfig/clangTidy.java +++ b/src/toolsconfig/clangTidy.java | |||
@@ -1,5 +1,8 @@ | |||
1 | package toolsconfig; | 1 | package toolsconfig; |
2 | 2 | ||
3 | import cppcheckplus.control.MyContorlUtil; | ||
4 | import cppcheckplus.text.DeflectRow; | ||
5 | import cppcheckplus.text.stdClangType; | ||
3 | import java.io.BufferedReader; | 6 | import java.io.BufferedReader; |
4 | import java.io.IOException; | 7 | import java.io.IOException; |
5 | import java.io.InputStream; | 8 | import java.io.InputStream; |
@@ -7,6 +10,14 @@ import java.io.InputStreamReader; | |||
7 | import java.nio.file.Files; | 10 | import java.nio.file.Files; |
8 | import java.nio.file.Path; | 11 | import java.nio.file.Path; |
9 | import java.nio.file.Paths; | 12 | import java.nio.file.Paths; |
13 | import java.util.Set; | ||
14 | import java.util.Vector; | ||
15 | import javax.xml.parsers.DocumentBuilder; | ||
16 | import javax.xml.parsers.DocumentBuilderFactory; | ||
17 | import org.w3c.dom.Document; | ||
18 | import org.w3c.dom.Element; | ||
19 | import org.w3c.dom.Node; | ||
20 | import org.w3c.dom.NodeList; | ||
10 | 21 | ||
11 | public class clangTidy extends ToolsConfig { | 22 | public class clangTidy extends ToolsConfig { |
12 | public clangTidy() { | 23 | public clangTidy() { |
@@ -19,29 +30,48 @@ public class clangTidy extends ToolsConfig { | |||
19 | // clang-tidy 检查 | 30 | // clang-tidy 检查 |
20 | Runtime runtime = Runtime.getRuntime(); | 31 | Runtime runtime = Runtime.getRuntime(); |
21 | Process p = null; | 32 | Process p = null; |
22 | try { | 33 | |
23 | // 修改为执行 clang-tidy | 34 | // 列举所有启用的检查规则 |
24 | Path srcPath = Paths.get(src); | 35 | String checks = ""; |
25 | String execable; | 36 | Set<String> enabledRules = ruleSet.getInstance().getEnabledRules(); |
26 | // FIXME: | 37 | for (String rule : enabledRules) { |
27 | // 1.这里没能实现递归检查 | 38 | if (checks.isEmpty()) { |
28 | // 2.如何指定特定的检查规则? | 39 | checks = "\"-*,"; // 先清空,再添加 |
29 | // 3.如何指定特定的头文件路径? | ||
30 | // 4.是否需要指定代码标准 | ||
31 | if (Files.isDirectory(srcPath)) { | ||
32 | // 是目录,则检查这个目录下所有cpp文件 | ||
33 | execable = String.format( | ||
34 | "%s %s\\*.cpp --checks=* --header-filter=%s -- %s", path, | ||
35 | src, src, params); | ||
36 | } else { | ||
37 | // 是文件,直接执行 | ||
38 | execable = | ||
39 | String.format("%s %s --checks=* --header-filter=%s -- %s", | ||
40 | path, src, src, params); | ||
41 | } | 40 | } |
42 | //String execable = String.format("clang-tidy %s\\*.cpp --checks='*,misc-no-delete-this,-llvmlibc-restrict-system-libc-headers,-modernize-use-trailing-return-type' -- -std=c++17 -I %s", src, path); | 41 | if (toolRuleSet.containsKey(rule)) { |
43 | System.out.println(execable); | 42 | checks += toolRuleSet.get(rule) + ","; |
43 | } | ||
44 | } | ||
44 | 45 | ||
46 | if (checks.isEmpty()) { | ||
47 | checks = "\"*\""; // 如果没有启用的检查规则,则默认开启所有检查 | ||
48 | } else { | ||
49 | checks = checks.substring(0, checks.length() - 1) + | ||
50 | "\""; // 去掉最后一个逗号,再加上引号 | ||
51 | } | ||
52 | |||
53 | Path srcPath = Paths.get(src); | ||
54 | String execable; | ||
55 | |||
56 | // FIXME: | ||
57 | // 这里没能实现递归检查 | ||
58 | // 如何指定特定的头文件路径? | ||
59 | // 是否需要指定代码标准 | ||
60 | if (Files.isDirectory(srcPath)) { | ||
61 | // 是目录,则检查这个目录下所有cpp文件 | ||
62 | execable = String.format( | ||
63 | "%s %s\\*.cpp --checks=%s --header-filter=%s -- %s", path, | ||
64 | src, checks, src, params); | ||
65 | } else { | ||
66 | // 是文件,直接执行 | ||
67 | execable = | ||
68 | String.format("%s %s --checks=%s --header-filter=%s -- %s", | ||
69 | path, src, checks, src, params); | ||
70 | } | ||
71 | //String execable = String.format("clang-tidy %s\\*.cpp --checks='*,misc-no-delete-this,-llvmlibc-restrict-system-libc-headers,-modernize-use-trailing-return-type' -- -std=c++17 -I %s", src, path); | ||
72 | System.out.println(execable); | ||
73 | |||
74 | try { | ||
45 | // 执行 | 75 | // 执行 |
46 | p = runtime.exec(execable); | 76 | p = runtime.exec(execable); |
47 | final InputStream stdout = p.getInputStream(); | 77 | final InputStream stdout = p.getInputStream(); |
@@ -49,7 +79,7 @@ public class clangTidy extends ToolsConfig { | |||
49 | 79 | ||
50 | result.setText(""); | 80 | result.setText(""); |
51 | 81 | ||
52 | // 处理标准输出 | 82 | // stdout |
53 | new Thread() { | 83 | new Thread() { |
54 | public void run() { | 84 | public void run() { |
55 | BufferedReader br1 = | 85 | BufferedReader br1 = |
@@ -59,7 +89,7 @@ public class clangTidy extends ToolsConfig { | |||
59 | String line1 = null; | 89 | String line1 = null; |
60 | while ((line1 = br1.readLine()) != null) { | 90 | while ((line1 = br1.readLine()) != null) { |
61 | l1.append(line1 + ENTERWindows); | 91 | l1.append(line1 + ENTERWindows); |
62 | uiFrame.ct.toRowsOfClang(line1); | 92 | toRowsOfTable(line1); |
63 | } | 93 | } |
64 | } catch (Exception e) { | 94 | } catch (Exception e) { |
65 | e.printStackTrace(); | 95 | e.printStackTrace(); |
@@ -74,34 +104,6 @@ public class clangTidy extends ToolsConfig { | |||
74 | } | 104 | } |
75 | }.start(); | 105 | }.start(); |
76 | 106 | ||
77 | new Thread() { | ||
78 | public void run() { | ||
79 | BufferedReader br2 = | ||
80 | new BufferedReader(new InputStreamReader(stderr)); | ||
81 | StringBuilder l2 = new StringBuilder(); | ||
82 | try { | ||
83 | String line2 = null; | ||
84 | int i = 0; | ||
85 | l2.append(ENTERWindows + ENTERWindows); | ||
86 | while ((line2 = br2.readLine()) != null) { | ||
87 | l2.append(line2 + ENTERWindows); | ||
88 | uiFrame.ct.toRowsOfClang(line2); | ||
89 | i++; | ||
90 | } | ||
91 | } catch (Exception e) { | ||
92 | e.printStackTrace(); | ||
93 | System.out.println("inputError"); | ||
94 | } finally { | ||
95 | try { | ||
96 | result.append(l2.toString()); | ||
97 | stderr.close(); | ||
98 | } catch (IOException e) { | ||
99 | e.printStackTrace(); | ||
100 | } | ||
101 | } | ||
102 | } | ||
103 | }.start(); | ||
104 | |||
105 | int exitCode = p.waitFor(); | 107 | int exitCode = p.waitFor(); |
106 | if (exitCode == SUCCESS) { | 108 | if (exitCode == SUCCESS) { |
107 | System.out.println(SUCCESS_MESSAGE); | 109 | System.out.println(SUCCESS_MESSAGE); |
@@ -118,4 +120,91 @@ public class clangTidy extends ToolsConfig { | |||
118 | } | 120 | } |
119 | } | 121 | } |
120 | } | 122 | } |
123 | |||
124 | @Override | ||
125 | public void toRowsOfTable(String input) { | ||
126 | String[] temp = input.split(":"); | ||
127 | int size = temp.length; | ||
128 | String until = null; | ||
129 | String file = null; | ||
130 | String line = null; | ||
131 | String column = null; | ||
132 | String level = null; | ||
133 | String type = null; | ||
134 | String cwe = null; | ||
135 | String description = null; | ||
136 | if (size > 5) { | ||
137 | until = "clang-tidy"; | ||
138 | file = temp[0] + ":" + temp[1]; | ||
139 | line = temp[2]; | ||
140 | column = temp[3]; | ||
141 | level = temp[4];// warning,note, error | ||
142 | type = ""; | ||
143 | description = temp[5]; | ||
144 | cwe = "0"; | ||
145 | // 标准化 | ||
146 | DeflectRow node = | ||
147 | new DeflectRow(until, file, line, level, type, cwe, description, | ||
148 | 0); | ||
149 | stdClang(node); | ||
150 | ct.clangResults.add(node); | ||
151 | } | ||
152 | } | ||
153 | |||
154 | public Vector<stdClangType> stdclang = new Vector<stdClangType>(); | ||
155 | public void initSTDClang(String xml) { | ||
156 | try { | ||
157 | DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | ||
158 | DocumentBuilder db = dbf.newDocumentBuilder(); | ||
159 | Document doc = db.parse(MyContorlUtil.getXMLFile(xml)); | ||
160 | Element root = doc.getDocumentElement(); | ||
161 | NodeList stdlist = root.getChildNodes(); | ||
162 | if (stdlist != null) { | ||
163 | for (int i = 0; i < stdlist.getLength(); i++) { | ||
164 | org.w3c.dom.Node menu = stdlist.item(i); | ||
165 | if (menu.getNodeType() == Node.ELEMENT_NODE) { | ||
166 | if (menu.getNodeName().equalsIgnoreCase("clangNode")) { | ||
167 | stdClangType node = new stdClangType(); | ||
168 | node.cwe = | ||
169 | MyContorlUtil.getStringAttribute(menu, "cwe"); | ||
170 | node.level = | ||
171 | MyContorlUtil.getStringAttribute(menu, "level"); | ||
172 | node.target = MyContorlUtil.getStringAttribute(menu, | ||
173 | "target"); | ||
174 | node.len = Integer.valueOf( | ||
175 | MyContorlUtil.getStringAttribute(menu, "len")); | ||
176 | node.type = | ||
177 | MyContorlUtil.getStringAttribute(menu, "type"); | ||
178 | stdclang.add(node); | ||
179 | } | ||
180 | } | ||
181 | } | ||
182 | } | ||
183 | } catch (Exception ex) { | ||
184 | ex.printStackTrace(); | ||
185 | } | ||
186 | } | ||
187 | |||
188 | private void stdClang(DeflectRow node) { | ||
189 | String describ = node.getDescription(); | ||
190 | int len = describ.length(); | ||
191 | boolean isFound = false; | ||
192 | for (int i = 0; i < stdclang.size(); i++) { | ||
193 | if (len > stdclang.elementAt(i).len) { | ||
194 | if (describ.substring(1, stdclang.elementAt(i).len) | ||
195 | .equals(stdclang.elementAt(i).target)) { | ||
196 | node.setCwe(stdclang.elementAt(i).cwe); | ||
197 | node.setLevel(stdclang.elementAt(i).level); | ||
198 | node.setType(stdclang.elementAt(i).type); | ||
199 | isFound = true; | ||
200 | return; | ||
201 | } | ||
202 | } | ||
203 | } | ||
204 | if (!isFound) { | ||
205 | node.setCwe("未知"); | ||
206 | node.setLevel("2"); | ||
207 | node.setType("other"); | ||
208 | } | ||
209 | } | ||
121 | } | 210 | } |
diff --git a/src/toolsconfig/cppcheck.java b/src/toolsconfig/cppcheck.java index ca3be27..4bd7f51 100644 --- a/src/toolsconfig/cppcheck.java +++ b/src/toolsconfig/cppcheck.java | |||
@@ -1,5 +1,6 @@ | |||
1 | package toolsconfig; | 1 | package toolsconfig; |
2 | 2 | ||
3 | import cppcheckplus.text.DeflectRow; | ||
3 | import java.io.BufferedReader; | 4 | import java.io.BufferedReader; |
4 | import java.io.IOException; | 5 | import java.io.IOException; |
5 | import java.io.InputStream; | 6 | import java.io.InputStream; |
@@ -35,7 +36,7 @@ public class cppcheck extends ToolsConfig { | |||
35 | final InputStream[] stderr = | 36 | final InputStream[] stderr = |
36 | {p[0].getErrorStream(), p[1].getErrorStream()}; | 37 | {p[0].getErrorStream(), p[1].getErrorStream()}; |
37 | 38 | ||
38 | // 模板标准输出 | 39 | // stdout |
39 | new Thread() { | 40 | new Thread() { |
40 | @SuppressWarnings("resource") | 41 | @SuppressWarnings("resource") |
41 | public void run() { | 42 | public void run() { |
@@ -45,10 +46,9 @@ public class cppcheck extends ToolsConfig { | |||
45 | try { | 46 | try { |
46 | String line1 = null; | 47 | String line1 = null; |
47 | int i = 0; | 48 | int i = 0; |
48 | uiFrame.textCheck.setText(""); | ||
49 | while ((line1 = br1.readLine()) != null) { | 49 | while ((line1 = br1.readLine()) != null) { |
50 | uiFrame.textCheck.append( | 50 | // uiFrame.textCheck.append( |
51 | i + " " + line1 + ENTERWindows); | 51 | // i + " " + line1 + ENTERWindows); |
52 | i++; | 52 | i++; |
53 | } | 53 | } |
54 | } catch (Exception e) { | 54 | } catch (Exception e) { |
@@ -64,7 +64,7 @@ public class cppcheck extends ToolsConfig { | |||
64 | } | 64 | } |
65 | }.start(); | 65 | }.start(); |
66 | 66 | ||
67 | // 模板错误流,用于生成结果表格 | 67 | // stderr,用于生成结果表格 |
68 | new Thread() { | 68 | new Thread() { |
69 | public void run() { | 69 | public void run() { |
70 | BufferedReader br2 = | 70 | BufferedReader br2 = |
@@ -74,8 +74,8 @@ public class cppcheck extends ToolsConfig { | |||
74 | String line2 = null; | 74 | String line2 = null; |
75 | int i = 0; | 75 | int i = 0; |
76 | while ((line2 = br2.readLine()) != null) { | 76 | while ((line2 = br2.readLine()) != null) { |
77 | uiFrame.ct.toRowsOfCppcheckResult(line2); | ||
78 | l2.append(i + " " + line2 + ENTERWindows); | 77 | l2.append(i + " " + line2 + ENTERWindows); |
78 | toRowsOfTable(line2); | ||
79 | i++; | 79 | i++; |
80 | } | 80 | } |
81 | } catch (Exception e) { | 81 | } catch (Exception e) { |
@@ -143,4 +143,85 @@ public class cppcheck extends ToolsConfig { | |||
143 | } | 143 | } |
144 | } | 144 | } |
145 | } | 145 | } |
146 | |||
147 | public void toRowsOfTable(String input) {// | ||
148 | String[] temp = input.split("\\$"); | ||
149 | int size = temp.length; | ||
150 | String until = null; | ||
151 | String file = null; | ||
152 | String line = null; | ||
153 | String level = null; | ||
154 | String type = null; | ||
155 | String cwe = null; | ||
156 | String description = null; | ||
157 | if (size > 6) { | ||
158 | if (temp[1].length() == 0) { | ||
159 | return; | ||
160 | } | ||
161 | until = temp[0]; | ||
162 | file = temp[1]; | ||
163 | line = temp[2]; | ||
164 | level = temp[3]; | ||
165 | if (temp[3].equals("error")) { | ||
166 | level = "4"; | ||
167 | } else if (temp[3].equals("warning")) { | ||
168 | level = "3"; | ||
169 | } else if (temp[2].equals("style")) { | ||
170 | level = "1"; | ||
171 | } else if (temp[2].equals("information")) { | ||
172 | level = "0"; | ||
173 | } else { | ||
174 | level = "2"; | ||
175 | } | ||
176 | type = temp[4]; | ||
177 | description = temp[5]; | ||
178 | cwe = temp[6]; | ||
179 | DeflectRow node = | ||
180 | new DeflectRow(until, file, line, level, type, cwe, description, | ||
181 | 0); | ||
182 | stdCppcheck(node); | ||
183 | ct.cppcheckplusResults.add(node); | ||
184 | } | ||
185 | } | ||
186 | |||
187 | private void stdCppcheck(DeflectRow node) { | ||
188 | String cwe = node.getCwe(); | ||
189 | int cwenum = Integer.parseInt(cwe); | ||
190 | switch (cwenum) { //cppcheck向flawfinder看齐 | ||
191 | case 134: | ||
192 | node.setLevel("4"); | ||
193 | node.setType("format"); | ||
194 | break; | ||
195 | case 367: | ||
196 | node.setLevel("4"); | ||
197 | node.setType("race"); | ||
198 | break; | ||
199 | case 120: | ||
200 | node.setLevel("4"); | ||
201 | node.setType("buff"); | ||
202 | break; | ||
203 | case 78: | ||
204 | node.setLevel("4"); | ||
205 | node.setType("shell"); | ||
206 | break; | ||
207 | case 327: | ||
208 | node.setLevel("3"); | ||
209 | node.setType("random"); | ||
210 | break; | ||
211 | case 119: | ||
212 | node.setLevel("4"); | ||
213 | node.setType("buffer"); | ||
214 | break; | ||
215 | case 362: | ||
216 | node.setLevel("2"); | ||
217 | node.setType("misc"); | ||
218 | break; | ||
219 | case 190: | ||
220 | node.setLevel("2"); | ||
221 | node.setType("integer"); | ||
222 | break; | ||
223 | default: | ||
224 | ; | ||
225 | } | ||
226 | } | ||
146 | } | 227 | } |
diff --git a/src/toolsconfig/flawfinder.java b/src/toolsconfig/flawfinder.java index bb953ea..9ecadae 100644 --- a/src/toolsconfig/flawfinder.java +++ b/src/toolsconfig/flawfinder.java | |||
@@ -1,9 +1,11 @@ | |||
1 | package toolsconfig; | 1 | package toolsconfig; |
2 | 2 | ||
3 | import cppcheckplus.text.DeflectRow; | ||
3 | import java.io.BufferedReader; | 4 | import java.io.BufferedReader; |
4 | import java.io.IOException; | 5 | import java.io.IOException; |
5 | import java.io.InputStream; | 6 | import java.io.InputStream; |
6 | import java.io.InputStreamReader; | 7 | import java.io.InputStreamReader; |
8 | import java.util.regex.Pattern; | ||
7 | 9 | ||
8 | public class flawfinder extends ToolsConfig { | 10 | public class flawfinder extends ToolsConfig { |
9 | public flawfinder() { | 11 | public flawfinder() { |
@@ -41,7 +43,7 @@ public class flawfinder extends ToolsConfig { | |||
41 | try { | 43 | try { |
42 | String line1 = null; | 44 | String line1 = null; |
43 | while ((line1 = br1.readLine()) != null) { | 45 | while ((line1 = br1.readLine()) != null) { |
44 | uiFrame.ct.toRowsOfflawfinder(line1); | 46 | toRowsOfTable(line1); |
45 | } | 47 | } |
46 | } catch (Exception e) { | 48 | } catch (Exception e) { |
47 | e.printStackTrace(); | 49 | e.printStackTrace(); |
@@ -103,4 +105,50 @@ public class flawfinder extends ToolsConfig { | |||
103 | } | 105 | } |
104 | } | 106 | } |
105 | } | 107 | } |
108 | |||
109 | public void toRowsOfTable(String input) { | ||
110 | // File,Line,Column,Level,Category,Name,Warning,Suggestion,Note,CWEs,Context,Fingerprint | ||
111 | String[] temp = input.split(","); | ||
112 | int size = temp.length; | ||
113 | String until = null; | ||
114 | String file = null; | ||
115 | String line = null; | ||
116 | String column = null; | ||
117 | String level = null; | ||
118 | String category = null; | ||
119 | String name = null; | ||
120 | String warning = null; | ||
121 | String suggestion = null; | ||
122 | String note = null; | ||
123 | String cwe = null; | ||
124 | String context = null; | ||
125 | String fingerprint = null; | ||
126 | if (size > 11) { | ||
127 | if (!isInteger(temp[1])) { | ||
128 | return; | ||
129 | } | ||
130 | until = "flawfinder"; | ||
131 | file = temp[0]; | ||
132 | line = temp[1]; | ||
133 | column = temp[2]; | ||
134 | level = temp[3]; | ||
135 | category = temp[4]; | ||
136 | name = temp[5]; | ||
137 | warning = temp[6]; | ||
138 | suggestion = temp[7]; | ||
139 | note = temp[8]; | ||
140 | cwe = temp[9]; | ||
141 | context = temp[10]; | ||
142 | fingerprint = temp[11]; | ||
143 | DeflectRow node = | ||
144 | new DeflectRow(until, file, line, level, category, cwe, warning, | ||
145 | 0); | ||
146 | ct.flawfinderResults.add(node); | ||
147 | } | ||
148 | } | ||
149 | |||
150 | private boolean isInteger(String str) { | ||
151 | Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$"); | ||
152 | return pattern.matcher(str).matches(); | ||
153 | } | ||
106 | } | 154 | } |
diff --git a/src/toolsconfig/ruleSet.java b/src/toolsconfig/ruleSet.java new file mode 100644 index 0000000..2df9de8 --- /dev/null +++ b/src/toolsconfig/ruleSet.java | |||
@@ -0,0 +1,93 @@ | |||
1 | package toolsconfig; | ||
2 | |||
3 | import java.awt.BorderLayout; | ||
4 | import java.io.InputStream; | ||
5 | import java.util.LinkedHashMap; | ||
6 | import java.util.LinkedHashSet; | ||
7 | import java.util.Map; | ||
8 | import java.util.Set; | ||
9 | import javax.swing.BoxLayout; | ||
10 | import javax.swing.JButton; | ||
11 | import javax.swing.JCheckBox; | ||
12 | import javax.swing.JDialog; | ||
13 | import javax.swing.JFrame; | ||
14 | import javax.swing.JPanel; | ||
15 | import javax.swing.JScrollPane; | ||
16 | import org.yaml.snakeyaml.Yaml; | ||
17 | |||
18 | public class ruleSet { | ||
19 | private static ruleSet instance = new ruleSet(); | ||
20 | private static Map<String, Object> data; | ||
21 | private final Map<String, JCheckBox> checkBoxes = new LinkedHashMap<>(); | ||
22 | private ruleSet() { | ||
23 | } | ||
24 | |||
25 | public static void loadRuleSet(String configFile) { | ||
26 | Yaml yaml = new Yaml(); | ||
27 | try (InputStream input = ruleSet.class.getClassLoader() | ||
28 | .getResourceAsStream(configFile)) { | ||
29 | data = yaml.load(input); | ||
30 | } catch (Exception e) { | ||
31 | e.printStackTrace(); | ||
32 | } | ||
33 | } | ||
34 | |||
35 | public static ruleSet getInstance() { | ||
36 | return instance; | ||
37 | } | ||
38 | |||
39 | public void showRuleSet(JFrame frame) { | ||
40 | JDialog dialog = new JDialog(frame, "规则集选择", true); | ||
41 | dialog.setLayout(new BorderLayout()); | ||
42 | JPanel panel = new JPanel(); | ||
43 | panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); | ||
44 | |||
45 | data.forEach((ruleKey, value) -> { | ||
46 | Map<String, Object> rule = (Map<String, Object>) value; | ||
47 | String name = (String) rule.get("name"); | ||
48 | String description = (String) rule.get("description"); | ||
49 | boolean selected = (boolean) rule.get("selected"); | ||
50 | |||
51 | JCheckBox checkBox = new JCheckBox(name, selected); | ||
52 | checkBox.setToolTipText(description); | ||
53 | checkBoxes.put(ruleKey, checkBox); | ||
54 | panel.add(checkBox); | ||
55 | }); | ||
56 | |||
57 | JPanel buttonPanel = new JPanel(); | ||
58 | JButton okButton = new JButton("确定"); | ||
59 | JButton cancelButton = new JButton("取消"); | ||
60 | |||
61 | okButton.addActionListener(e -> { | ||
62 | data.forEach((k, v) -> { | ||
63 | ((Map<String, Object>) v).put("selected", | ||
64 | checkBoxes.get(k).isSelected()); | ||
65 | }); | ||
66 | dialog.dispose(); | ||
67 | }); | ||
68 | |||
69 | cancelButton.addActionListener(e -> { | ||
70 | dialog.dispose(); | ||
71 | }); | ||
72 | |||
73 | buttonPanel.add(okButton); | ||
74 | buttonPanel.add(cancelButton); | ||
75 | |||
76 | dialog.add(new JScrollPane(panel), BorderLayout.CENTER); | ||
77 | dialog.add(buttonPanel, BorderLayout.SOUTH); | ||
78 | dialog.setSize(400, 300); | ||
79 | dialog.setLocationRelativeTo(frame); | ||
80 | dialog.setVisible(true); | ||
81 | } | ||
82 | |||
83 | public Set<String> getEnabledRules() { | ||
84 | Set<String> enabledRules = new LinkedHashSet<>(); | ||
85 | data.forEach((k, v) -> { | ||
86 | if((boolean)((Map<String, Object>) v).get("selected")){ | ||
87 | enabledRules.add(k); | ||
88 | } | ||
89 | }); | ||
90 | System.out.println(enabledRules); | ||
91 | return enabledRules; | ||
92 | } | ||
93 | } | ||
diff --git a/src/toolsconfig/settings.xml b/src/toolsconfig/settings.xml deleted file mode 100644 index 77dd785..0000000 --- a/src/toolsconfig/settings.xml +++ /dev/null | |||
@@ -1,8 +0,0 @@ | |||
1 | <?xml version="1.0" encoding="UTF-8" standalone="no"?><settings> | ||
2 | <tool filePath="C:\\Users\\we-unite\\Desktop\\Project\\Project\\code\\cppcheck\\bin\\debug\\cppcheck.exe" para="--enable=all" toolName="cppcheck"> | ||
3 | </tool> | ||
4 | <tool filePath="C:\\Users\\we-unite\\Desktop\\Project\\Project\\code\\flawfinder-2.0.19\\flawfinder-2.0.19\\flawfinder.py" para="" toolName="flawfinder"> | ||
5 | </tool> | ||
6 | <tool filePath="clang-tidy.exe" para="-I C:\\Program Files\\LLVM\\lib\\clang\\17" toolName="clangTidy"> | ||
7 | </tool> | ||
8 | </settings> \ No newline at end of file | ||
diff --git a/src/toolsconfig/tools.yaml b/src/toolsconfig/tools.yaml new file mode 100644 index 0000000..08b535c --- /dev/null +++ b/src/toolsconfig/tools.yaml | |||
@@ -0,0 +1,51 @@ | |||
1 | clangTidy: | ||
2 | path: "clang-tidy.exe" | ||
3 | params: "-I C:\\Program Files\\LLVM\\lib\\clang\\17" | ||
4 | ruleMap: | ||
5 | NestedStructureInit: | ||
6 | CheckNullPointer: "clang-analyzer-core.NullDereference" | ||
7 | CheckSelfCall: | ||
8 | CheckDiamondInheritance: | ||
9 | CheckCatchFromDerived: "myrules-TrycatchCheck" | ||
10 | CheckStringEnd: | ||
11 | CheckConstInsteadOfMacro: "cppcoreguidelines-macro-usage" | ||
12 | CheckEnumUsage: "modernize-macro-to-enum" | ||
13 | CheckMemOpForNotPOD: "cppcoreguidelines-pro-type-reinterpret-cast" | ||
14 | CheckCStyleCast: "cppcoreguidelines-pro-type-cstyle-cast" | ||
15 | CheckInlineInsteadOfMacro: "cppcoreguidelines-macro-usage" | ||
16 | CheckExplicitForConstructorWithSingleParameter: "google-explicit-constructor" | ||
17 | CheckReturnValueOfOperatorAssign: "myrules-ReturnvalueofassignoperatorCheck" | ||
18 | CheckAssignToSelf: "cert-oop54-cpp" | ||
19 | CheckCallVirtualInConstructor: "clang-analyzer-optin.cplusplus.VirtualCall" | ||
20 | CheckRedefineNonVirtualFunction: "myrules-RedefineNonVirtualFuncCheck" | ||
21 | CheckDeleteArray: "clang-analyzer-cplusplus.NewDelete" | ||
22 | CheckReturnLocalObjectPointer: | ||
23 | ChecCatchByReference: "myrules-TrycatchCheck" | ||
24 | |||
25 | cppcheck: | ||
26 | path: "C:\\Users\\we-unite\\Desktop\\Project\\Project\\code\\cppcheck\\bin\\debug\\cppcheck.exe" | ||
27 | params: "--enable=all" | ||
28 | ruleMap: | ||
29 | NestedStructureInit: | ||
30 | CheckNullPointer: | ||
31 | CheckSelfCall: | ||
32 | CheckDiamondInheritance: | ||
33 | CheckCatchFromDerived: | ||
34 | CheckStringEnd: | ||
35 | CheckConstInsteadOfMacro: | ||
36 | CheckEnumUsage: | ||
37 | CheckMemOpForNotPOD: | ||
38 | CheckCStyleCast: | ||
39 | CheckInlineInsteadOfMacro: | ||
40 | CheckExplicitForConstructorWithSingleParameter: | ||
41 | CheckReturnValueOfOperatorAssign: | ||
42 | CheckAssignToSelf: | ||
43 | CheckCallVirtualInConstructor: | ||
44 | CheckRedefineNonVirtualFunction: | ||
45 | CheckDeleteArray: | ||
46 | CheckReturnLocalObjectPointer: | ||
47 | ChecCatchByReference: | ||
48 | |||
49 | flawfinder: | ||
50 | path: "C:\\Users\\we-unite\\Desktop\\Project\\Project\\code\\flawfinder-2.0.19\\install\\bin\\test" | ||
51 | params: "" | ||