diff options
Diffstat (limited to 'src/toolsconfig/clangTidy.java')
-rw-r--r-- | src/toolsconfig/clangTidy.java | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/src/toolsconfig/clangTidy.java b/src/toolsconfig/clangTidy.java new file mode 100644 index 0000000..d197393 --- /dev/null +++ b/src/toolsconfig/clangTidy.java | |||
@@ -0,0 +1,123 @@ | |||
1 | package toolsconfig; | ||
2 | |||
3 | import cppcheckplus.control.UIFrame; | ||
4 | import cppcheckplus.text.ViewPanel; | ||
5 | import java.io.BufferedReader; | ||
6 | import java.io.IOException; | ||
7 | import java.io.InputStream; | ||
8 | import java.io.InputStreamReader; | ||
9 | import java.nio.file.Files; | ||
10 | import java.nio.file.Path; | ||
11 | import java.nio.file.Paths; | ||
12 | |||
13 | public class clangTidy extends ToolsConfig { | ||
14 | public clangTidy() { | ||
15 | this.name = "clangTidy"; | ||
16 | ToolsRegistry.registerTool(this.name, this); | ||
17 | } | ||
18 | |||
19 | @Override | ||
20 | public void execute(String src) { | ||
21 | // clang-tidy 检查 | ||
22 | Runtime runtime = Runtime.getRuntime(); | ||
23 | Process p = null; | ||
24 | try { | ||
25 | // 修改为执行 clang-tidy | ||
26 | Path srcPath = Paths.get(src); | ||
27 | String execable; | ||
28 | // FIXME: | ||
29 | // 1.这里没能实现递归检查 | ||
30 | // 2.如何指定特定的检查规则? | ||
31 | // 3.如何指定特定的头文件路径? | ||
32 | // 4.是否需要指定代码标准 | ||
33 | if (Files.isDirectory(srcPath)) { | ||
34 | // 是目录,则检查这个目录下所有cpp文件 | ||
35 | execable = String.format( | ||
36 | "%s %s\\*.cpp --checks=* --header-filter=%s -- %s", path, | ||
37 | src, src, params); | ||
38 | } else { | ||
39 | // 是文件,直接执行 | ||
40 | execable = | ||
41 | String.format("%s %s --checks=* --header-filter=%s -- %s", | ||
42 | path, src, src, params); | ||
43 | } | ||
44 | //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); | ||
45 | System.out.println(execable); | ||
46 | |||
47 | // 执行 | ||
48 | p = runtime.exec(execable); | ||
49 | final InputStream stdout = p.getInputStream(); | ||
50 | final InputStream stderr = p.getErrorStream(); | ||
51 | |||
52 | result.setText(""); | ||
53 | |||
54 | // 处理标准输出 | ||
55 | new Thread() { | ||
56 | public void run() { | ||
57 | BufferedReader br1 = | ||
58 | new BufferedReader(new InputStreamReader(stdout)); | ||
59 | StringBuilder l1 = new StringBuilder(); | ||
60 | try { | ||
61 | String line1 = null; | ||
62 | while ((line1 = br1.readLine()) != null) { | ||
63 | l1.append(line1 + ENTERWindows); | ||
64 | uiFrame.ct.toRowsOfClang(line1); | ||
65 | } | ||
66 | } catch (Exception e) { | ||
67 | e.printStackTrace(); | ||
68 | } finally { | ||
69 | try { | ||
70 | result.append(l1.toString()); | ||
71 | stdout.close(); | ||
72 | } catch (IOException e) { | ||
73 | e.printStackTrace(); | ||
74 | } | ||
75 | } | ||
76 | } | ||
77 | }.start(); | ||
78 | |||
79 | new Thread() { | ||
80 | public void run() { | ||
81 | BufferedReader br2 = | ||
82 | new BufferedReader(new InputStreamReader(stderr)); | ||
83 | StringBuilder l2 = new StringBuilder(); | ||
84 | try { | ||
85 | String line2 = null; | ||
86 | int i = 0; | ||
87 | l2.append(ENTERWindows + ENTERWindows); | ||
88 | while ((line2 = br2.readLine()) != null) { | ||
89 | l2.append(line2 + ENTERWindows); | ||
90 | uiFrame.ct.toRowsOfClang(line2); | ||
91 | i++; | ||
92 | } | ||
93 | } catch (Exception e) { | ||
94 | e.printStackTrace(); | ||
95 | System.out.println("inputError"); | ||
96 | } finally { | ||
97 | try { | ||
98 | result.append(l2.toString()); | ||
99 | stderr.close(); | ||
100 | } catch (IOException e) { | ||
101 | e.printStackTrace(); | ||
102 | } | ||
103 | } | ||
104 | } | ||
105 | }.start(); | ||
106 | |||
107 | int exitCode = p.waitFor(); | ||
108 | if (exitCode == SUCCESS) { | ||
109 | System.out.println(SUCCESS_MESSAGE); | ||
110 | } else { | ||
111 | System.err.println(ERROR_MESSAGE + exitCode); | ||
112 | } | ||
113 | p.destroy(); | ||
114 | } catch (Exception e) { | ||
115 | try { | ||
116 | p.getInputStream().close(); | ||
117 | p.getOutputStream().close(); | ||
118 | p.getErrorStream().close(); | ||
119 | } catch (Exception ee) { | ||
120 | } | ||
121 | } | ||
122 | } | ||
123 | } | ||