-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcccppcheck.lua
284 lines (261 loc) · 9.78 KB
/
gcccppcheck.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
--
-- GCC-CPPCheck - a package/plugin for Zerobrane Studio
--
-- When editing C or C++ files your code is run through GCC and CPPCheck when you save
-- it, and any issues found are annotated to the releveant lines in your editor window.
--
--
-- Copyright 2019+ Paul Reilly (GitHub user paul-reilly), MIT license
--
local file_types =
{
gcc = {
c = "gcc",
cpp = "g++",
h = "gcc",
hpp = "g++",
}
}
local c_family = {
c = true,
cpp = true,
h = true,
hpp = true
}
-- we clear annotations when we start typing, but we want to be able to navigate
-- so don't clear when only these keys are used
local ignored_keys = {
["27"] = "escape",
["306"] = "shift",
["307"] = "alt",
["308"] = "ctrl",
["308"] = "mousewheel",
["314"] = "left arrow",
["315"] = "up arrow",
["316"] = "right arrow",
["317"] = "right arrow",
["366"] = "page up",
["367"] = "page down",
["393"] = "windows",
["395"] = "menu"
}
-- our marker IDs, used to set and delete
local kMARKER_TYPE_WARNING = 10
local kMARKER_TYPE_ERROR = 11
-- existing style numbers to repurpose
local kSTYLE_WARNING = 9
local kSTYLE_ERROR = 7
--
local function stringLinesIterator(s)
if s:sub(-1) ~= "\n" then s = s .. "\n" end
return s:gmatch("(.-)\n")
end
--
local function getArrayOfIssuesFromTextOutput(parse_patterns, file_path, output)
local issues = {}
local file_name = wx.wxFileName(file_path):GetFullName()
if type(parse_patterns) == "string" then
parse_patterns = { parse_patterns }
end
-- CPPCheck can return errors that span several, sepaate lines - for example tracing an uninitialized pointer that
-- gets deferenced. Give these errors an ID number to group them in case more than one of these errors exists, so
-- that it's obvious what error relates to which incidence.
local group_id = 1
for current_line in stringLinesIterator(output) do
for _, parse_pattern in pairs(parse_patterns) do
for fn, line, column, warning in current_line:gmatch("(" .. EscapeMagic(file_name) .. ")" .. parse_pattern) do
if warning then
local trace = ""
-- CPPCheck's multiple line issues are reported on the same line of output text separated by ' -> '. So gather
--- them up and give them their id and a label
local count = 0
for line_number in current_line:gmatch(":(%d-)%] %->") do
count = count + 1
trace = count == 1 and (" [origin] (group ID: " .. group_id .. ")") or (" [trace] (group ID: " .. group_id .. ")")
issues[#issues + 1] = { file_name = fn, line_number = line_number, column = column, warning = warning .. trace }
end
trace = count > 0 and (" [action] (group ID: " .. group_id .. ")") or ""
issues[#issues + 1] = { file_name = fn, line_number = line, column = column, warning = warning .. trace }
if count > 0 then group_id = group_id + 1 end
end
end
end
end
return issues
end
--
local function getDocFromEditorAndCheckExt(config, editor)
local document = ide:GetDocument(editor)
local file_ext = document:GetFileExt()
if not file_types[config.compiler][file_ext] then
return false
else
return true, document
end
end
--
local function clearAnnotationsAndMarkers(editor)
editor:AnnotationClearAll()
editor:MarkerDeleteAll(kMARKER_TYPE_WARNING)
editor:MarkerDeleteAll(kMARKER_TYPE_ERROR)
end
--
local function parseIssuesAndAddAnnotationsAndMarkers(file_path, editor, issues)
local file_name = wx.wxFileName(file_path):GetFullName()
for _, issue in ipairs(issues) do
if issue.file_name == file_name then
-- lines are off by one re wx editor and CPPCheck can give a file warning on line 0
local line_number = tonumber(issue.line_number) > 0 and issue.line_number - 1 or 0
local current_line_text = editor:AnnotationGetText(line_number)
local delim = current_line_text == "" and "" or "\n"
current_line_text = current_line_text .. delim .. issue.warning
editor:AnnotationSetText(line_number, current_line_text)
editor:AnnotationSetVisible(wxstc.wxSTC_ANNOTATION_BOXED)
local is_error = editor:AnnotationGetText(line_number):match("[Ee]rror")
if is_error then
editor:AnnotationSetStyle(line_number, kSTYLE_ERROR)
editor:MarkerAdd(line_number, kMARKER_TYPE_ERROR)
else
editor:AnnotationSetStyle(line_number, kSTYLE_WARNING)
editor:MarkerAdd(line_number, kMARKER_TYPE_WARNING)
end
end
end
end
--
local function runToolsOnCurrentFile(config, document, editor)
local file_path = document:GetFilePath()
local file_ext = document:GetFileExt()
-- enclose in quotes for command line in case of spaces in path
local file_path_enclosed = "\"" .. file_path .. "\""
local contents = editor:GetText()
local compiler = file_types[config.compiler][file_ext]
local compiler_string = compiler .. " -c "
local compiler_parse_pattern = config.compiler_parse_pattern
if c_family[file_ext] then
compiler_string = compiler_string .. " " .. config.compiler_options
.. " " .. config.include_dirs .. " " .. config.libraries .. " "
end
compiler_string = compiler_string .. file_path_enclosed
-- first entry in table we concat to display total output in final callback from
-- Execute Command since it's async and we are running two tools we don't want
-- to print live as we get results
local compiler_total_output = { "\nExecuting GCC: \n", compiler_string }
ide:Print(compiler_string)
-- clearAnnotationsAndMarkers(editor)
ide:ExecuteCommand(
compiler_string,
ide:GetProject()
,
function(compiler_output)
compiler_total_output[#compiler_total_output + 1] = compiler_output
local issues = getArrayOfIssuesFromTextOutput(compiler_parse_pattern, file_path, compiler_output)
parseIssuesAndAddAnnotationsAndMarkers(file_path, editor, issues)
end
,
function()
table.insert(compiler_total_output, "\n")
ide:Print(table.concat(compiler_total_output, "\n"))
end
)
-- run cppcheck
if file_ext == "c" or file_ext == "cpp" then
local cppcheck_string = "cppcheck --inline-suppr --force --enable=warning --enable=information --enable=performance "
.. " " .. config.include_dirs .. file_path_enclosed
local cppcheck_total_output = { "\nExecuting CPPCheck: \n", cppcheck_string }
ide:ExecuteCommand(
cppcheck_string
,
ide:GetProject()
,
function(cppcheck_output)
cppcheck_total_output[#cppcheck_total_output + 1] = cppcheck_output
local issues = getArrayOfIssuesFromTextOutput(config.cppcheck_parse_pattern, file_path, cppcheck_output)
parseIssuesAndAddAnnotationsAndMarkers(file_path, editor, issues)
end
,
function()
table.insert(cppcheck_total_output, "\n")
ide:Print(table.concat(cppcheck_total_output, "\n"))
end
)
end
end
--
local function setConfig(self)
self.config = {}
if self:GetConfig() then
self.config = self:GetConfig()
end
if not self.config then ide:Print("GCC-CPPCheck Package: Config not found - using defaults") end
self.config.compiler = self.config.compiler or "gcc"
if self.config.include_dirs and #self.config.include_dirs > 0 then
self.config.include_dirs = "-I " .. table.concat(self.config.include_dirs, " -I ") .. " "
else
self.config.include_dirs = " "
end
ide:Print("Include dirs: " .. self.config.include_dirs)
if self.config.libraries and #self.config.libraries > 0 then
self.config.libraries = "-L " .. table.concat(self.config.libraries, " -L ") .. " "
else
self.config.libraries = " "
end
ide:Print("Libraries: " .. self.config.libraries)
self.config.compiler_options = self.config.compiler_options or "-Wall -Woverflow -Wextra -fpermissive -fmax-errors=100 "
self.config.compiler_options = " -O " .. self.config.compiler_options
-- patterns that returns line number, column (not used) and warning/error
-- we can have more than one pattern per tool if we put them in a table
self.config.compiler_parse_pattern = self.config.compiler_parse_pattern or ":(%d-):(%d-):(.-)[\n\r]"
self.config.cppcheck_parse_pattern = self.config.cppcheck_parse_pattern or ":(%d-)%]():(.-)[\n\r]"
end
--
local package = {
name = "GCC-CPPCheck",
description = "Annotates C and C++ files with compiler and static analyzer errors/warnings on save.",
author = "Paul Reilly",
version = 0.10,
dependencies = "1.0",
onRegister = function(self)
-- reading config done in onProjectLoad to ensure that Project Settings package
-- config option can be used
end
,
onUnRegister = function(self)
--
end
,
onEditorKeyDown = function(self, editor, event)
local key = tostring(event:GetKeyCode())
if not ignored_keys[key] then
clearAnnotationsAndMarkers(editor)
end
end
,
onEditorSave = function(self, editor)
editor:MarkerDefine(kMARKER_TYPE_WARNING, 1, wx.wxColour(0xcc, 0xcc, 0x00), wx.wxColour(0xcc, 0xcc, 0x00))
editor:MarkerDefine(kMARKER_TYPE_ERROR, 1, wx.wxColour(0xcc, 0x00, 0x00), wx.wxColour(0xcc, 0x00, 0x00))
local ok, doc = getDocFromEditorAndCheckExt(self.config, editor)
if ok then runToolsOnCurrentFile(self.config, doc, editor) end
end
,
onIdleOnce = function(self, event)
--
end
,
onIdle = function(self, event)
-- TODO: maybe do it liveish here?
end
,
onEditorFocusLost = function(self, editor)
--
end
,
onEditorClose = function(self, editor)
clearAnnotationsAndMarkers(editor)
end
,
onProjectLoad = function(self, _)
setConfig(self)
end
}
return package