summaryrefslogtreecommitdiffstats
path: root/src/CheckBasicStyle.lua
blob: 79b4a761d77c26a8bcbc3cb0ec472ecbb9ceb854 (plain) (blame)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#!/usr/bin/env lua

-- CheckBasicStyle.lua

--[[
Checks that all source files (*.cpp, *.h) use the basic style requirements of the project:
	- Tabs for indentation, spaces for alignment
	- Trailing whitespace on non-empty lines
	- Two spaces between code and line-end comment ("//")
	- Spaces after comma, not before
	- Opening braces not at the end of a code line
	- Spaces after if, for, while
	- Line dividers (////...) exactly 80 slashes
	- Multi-level indent change
	- (TODO) Spaces before *, /, &
	- (TODO) Hex numbers with even digit length
	- (TODO) Hex numbers in lowercase
	- (TODO) Not using "* "-style doxy comment continuation lines

Violations that cannot be checked easily:
	- Spaces around "+" (there are things like "a++", "++a", "a += 1", "X+", "stack +1" and ascii-drawn tables)

Reports all violations on stdout in a form that is readable by Visual Studio's parser, so that dblclicking
the line brings the editor directly to the violation.

Returns 0 on success, 1 on internal failure, 2 if any violations found

--]]





-- The list of file extensions that are processed:
local g_ShouldProcessExt =
{
	["h"]   = true,
	["cpp"] = true,
}

--- The list of files not to be processed:
local g_IgnoredFiles =
{
	"Bindings/Bindings.cpp",
	"Bindings/Bindings.h",
	"Bindings/LuaState_Implementation.cpp",
	"Registries/BlockStates.cpp",
	"Registries/BlockStates.h"
}

--- The list of files not to be processed, as a dictionary (filename => true), built from g_IgnoredFiles
local g_ShouldIgnoreFile = {}

-- Initialize the g_ShouldIgnoreFile map:
for _, fnam in ipairs(g_IgnoredFiles) do
	g_ShouldIgnoreFile[fnam] = true
end

--- Keeps track of the number of violations for this folder
local g_NumViolations = 0





--- Reports one violation
-- Pretty-prints the message
-- Also increments g_NumViolations
local function ReportViolation(a_FileName, a_LineNumber, a_PatStart, a_PatEnd, a_Message)
	print(a_FileName .. "(" .. a_LineNumber .. "): " .. a_PatStart .. " .. " .. a_PatEnd .. ": " .. a_Message)
	g_NumViolations = g_NumViolations + 1
end





--- Searches for the specified pattern, if found, reports it as a violation with the given message
local function ReportViolationIfFound(a_Line, a_FileName, a_LineNum, a_Pattern, a_Message)
	local patStart, patEnd = a_Line:find(a_Pattern)
	if not(patStart) then
		return
	end
	ReportViolation(a_FileName, a_LineNum, patStart, patEnd, a_Message)
end





local g_ViolationPatterns =
{
	-- Parenthesis around comparisons:
	{"==[^)]+&&",     "Add parenthesis around comparison"},
	{"&&[^(]+==",     "Add parenthesis around comparison"},
	{"==[^)]+||",     "Add parenthesis around comparison"},
	{"||[^(]+==",     "Add parenthesis around comparison"},
	{"!=[^)]+&&",     "Add parenthesis around comparison"},
	{"&&[^(]+!=",     "Add parenthesis around comparison"},
	{"!=[^)]+||",     "Add parenthesis around comparison"},
	{"||[^(]+!=",     "Add parenthesis around comparison"},
	{"<[^)>]*&&",     "Add parenthesis around comparison"},  -- Must take special care of templates: "template <T> fn(Args && ...)"
	-- Cannot check a < following a && due to functions of the form x fn(y&& a, z<b> c)
	{"<[^)>]*||",     "Add parenthesis around comparison"},  -- Must take special care of templates: "template <T> fn(Args && ...)"
	{"||[^(]+<",      "Add parenthesis around comparison"},
	-- Cannot check ">" because of "obj->m_Flag &&". Check at least ">=":
	{">=[^)]+&&",     "Add parenthesis around comparison"},
	{"&&[^(]+>=",     "Add parenthesis around comparison"},
	{">=[^)]+||",     "Add parenthesis around comparison"},
	{"||[^(]+>=",     "Add parenthesis around comparison"},

	-- Check against indenting using spaces:
	{"^\t* +", "Indenting with a space"},

	-- Check against alignment using tabs:
	{"[^%s]\t+[^%s]", "Aligning with a tab"},

	-- Check against trailing whitespace:
	{"%s+\n", "Trailing whitespace or whitespace-only line"},

	-- Check that all "//"-style comments have at least two spaces in front (unless alone on line):
	{"[^%s] //", "Needs at least two spaces in front of a \"//\"-style comment"},

	-- Check that all "//"-style comments have at least one spaces after:
	{"%s//[^%s/*<]", "Needs a space after a \"//\"-style comment"},

	-- Check that doxy-comments are used only in the double-asterisk form:
	{"/// ", "Use doxycomments in the form /** Comment */"},

	-- Check that /* */ comments have whitespace around the insides:
	{"%*%*/",        "Wrong comment termination, use */"},
	{"/%*[^%s*/\"]", "Needs a space after /*"},  -- Need to take care of the special "//*/" comment ends
	{"/%*%*[^%s*<]", "Needs a space after /**"},
	{"[^%s/*]%*/",   "Needs a space before */"},

	-- Check against MS XML doxycomments:
	{"/%*%* <", "Remove the MS XML markers from comment"},

	-- Check that all commas have spaces after them and not in front of them:
	{" ,", "Extra space before a \",\""},
	{",[^%s\"%%\']", "Needs a space after a \",\""},  -- Report all except >> "," << needed for splitting and >>,%s<< needed for formatting

	-- Check that opening braces are not at the end of a code line:
	{"[^%s].-{\n?$", "Brace should be on a separate line"},

	-- Space after keywords:
	{"[^_]if%(", "Needs a space after \"if\""},
	{"%sfor%(", "Needs a space after \"for\""},
	{"%swhile%(", "Needs a space after \"while\""},
	{"%sswitch%(", "Needs a space after \"switch\""},
	{"%scatch%(", "Needs a space after \"catch\""},
	{"%stemplate<", "Needs a space after \"template\""},

	-- No space after keyword's parenthesis:
	{"[^%a#]if %( ", "Remove the space after \"(\""},
	{"for %( ", "Remove the space after \"(\""},
	{"while %( ", "Remove the space after \"(\""},
	{"catch %( ", "Remove the space after \"(\""},

	-- No space before a closing parenthesis:
	{" %)", "Remove the space before \")\""},

	-- Check spaces around "+":
	{"^[a-zA-Z0-9]+%+[a-zA-Z0-9]+",                      "Add space around +"},
	{"[!@#$%%%^&*() %[%]\t][a-zA-Z0-9]+%+[a-zA-Z0-9]+",  "Add space around +"},
	--[[
	-- Cannot check these because of text such as "X+" and "+2" appearing in some comments.
	{"^[a-zA-Z0-9]+ %+[a-zA-Z0-9]+",                     "Add space after +"},
	{"[!@#$%%%^&*() %[%]\t][a-zA-Z0-9]+ %+[a-zA-Z0-9]+", "Add space after +"},
	{"^[a-zA-Z0-9]+%+ [a-zA-Z0-9]+",                     "Add space before +"},
	{"[!@#$%%%^&*() %[%]\t][a-zA-Z0-9]+%+ [a-zA-Z0-9]+", "Add space before +"},
	--]]

	-- Cannot check spaces around "-", because the minus is sometimes used as a hyphen between-words

	-- Check spaces around "*":
	{"^[a-zA-Z0-9]+%*[a-zA-Z0-9]+",                             "Add space around *"},
	{"^[^\"]*[!@#$%%%^&*() %[%]\t][a-zA-Z0-9]+%*[a-zA-Z0-9]+",  "Add space around *"},
	{"^[a-zB-Z0-9]+%* [a-zA-Z0-9]+",                            "Add space before *"},
	{"^[^\"]*[!@#$%%%^&*() %[%]\t][a-zB-Z0-9]+%* [a-zA-Z0-9]+", "Add space before *"},

	-- Check spaces around "/":
	{"^[a-zA-Z0-9]+%/[a-zA-Z0-9]+",                            "Add space around /"},
	{"^[^\"]*[!@#$%%%^&*() %[%]\t][a-zA-Z0-9]+%/[a-zA-Z0-9]+", "Add space around /"},

	-- Check spaces around "&":
	{"^[a-zA-Z0-9]+%&[a-zA-Z0-9]+",                             "Add space around &"},
	{"^[^\"]*[!@#$%%%^&*() %[%]\t][a-zA-Z0-9]+%&[a-zA-Z0-9]+",  "Add space around &"},
	{"^[a-zA-Z0-9]+%& [a-zA-Z0-9]+",                            "Add space before &"},
	{"^[^\"]*[!@#$%%%^&*() %[%]\t][a-zA-Z0-9]+%& [a-zA-Z0-9]+", "Add space before &"},

	-- Check spaces around "==", "<=" and ">=":
	{"==[a-zA-Z0-9]+",                             "Add space after =="},
	{"[a-zA-Z0-9]+==[^\\]",                        "Add space before =="},
	{"<=[a-zA-Z0-9]+",                             "Add space after <="},
	{"[a-zA-Z0-9]+<=",                             "Add space before <="},
	{">=[a-zA-Z0-9]+",                             "Add space after >="},
	{"[a-zA-Z0-9]+>=",                             "Add space before >="},

	-- We don't like "Type const *" and "Type const &". Use "const Type *" and "const Type &" instead:
	{"const %&", "Use 'const Type &' instead of 'Type const &'"},
	{"const %*", "Use 'const Type *' instead of 'Type const *'"},

	-- Check if "else" is on the same line as a brace.
	{"}%s*else", "else has to be on a separate line"},
	{"else%s*{", "else has to be on a separate line"},

	-- Don't allow characters other than ASCII 0 - 127:
	{"[" .. string.char(128) .. "-" .. string.char(255) .. "]", "Character in the extended ASCII range (128 - 255) not allowed"},
}





--- Processes one file
local function ProcessFile(a_FileName)
	assert(type(a_FileName) == "string")

	-- Read the whole file:
	local f, err = io.open(a_FileName, "r")
	if (f == nil) then
		print("Cannot open file \"" .. a_FileName .. "\": " .. err)
		print("Aborting")
		os.exit(1)
	end
	local all = f:read("*all")
	f:close()

	-- Check that the last line is empty - otherwise processing won't work properly:
	local lastChar = string.byte(all, string.len(all))
	if ((lastChar ~= 13) and (lastChar ~= 10)) then
		local numLines = 1
		string.gsub(all, "\n", function() numLines = numLines + 1 end)  -- Count the number of line-ends
		ReportViolation(a_FileName, numLines, 1, 1, "Missing empty line at file end")
		return
	end

	-- Process each line separately:
	-- Ref.: https://stackoverflow.com/questions/10416869/iterate-over-possibly-empty-lines-in-a-way-that-matches-the-expectations-of-exis
	local lineCounter = 1
	local lastIndentLevel = 0
	local isLastLineControl = false
	local lastNonEmptyLine = 0
	local isAfterFunction = false
	local isSourceFile = a_FileName:match("%.cpp")
	all = all:gsub("\r\n", "\n")  -- normalize CRLF into LF-only
	string.gsub(all .. "\n", "[^\n]*\n",  -- Iterate over each line, while preserving empty lines
		function(a_Line)
			-- Check against each violation pattern:
			for _, pat in ipairs(g_ViolationPatterns) do
				ReportViolationIfFound(a_Line, a_FileName, lineCounter, pat[1], pat[2])
			end

			-- Check that divider comments are well formed - 80 slashes plus optional indent:
			local dividerStart, dividerEnd = a_Line:find("/////*")
			if (dividerStart) then
				if (dividerEnd ~= dividerStart + 79) then
					ReportViolation(a_FileName, lineCounter, 1, 80, "Divider comment should have exactly 80 slashes")
				end
				if (dividerStart > 1) then
					if (
						(a_Line:sub(1, dividerStart - 1) ~= string.rep("\t", dividerStart - 1)) or  -- The divider should have only indent in front of it
						(a_Line:len() > dividerEnd + 1)                                             -- The divider should have no other text following it
					) then
						ReportViolation(a_FileName, lineCounter, 1, 80, "Divider comment shouldn't have any extra text around it")
					end
				end
			end

			-- Check the indent level change from the last line, if it's too much, report:
			local indentStart, indentEnd = a_Line:find("\t+")
			local indentLevel = 0
			if (indentStart) then
				indentLevel = indentEnd - indentStart
			end
			if (indentLevel > 0) then
				if ((lastIndentLevel - indentLevel >= 2) or (lastIndentLevel - indentLevel <= -2)) then
					ReportViolation(a_FileName, lineCounter, 1, indentStart or 1, "Indent changed more than a single level between the previous line and this one: from " .. lastIndentLevel .. " to " .. indentLevel)
				end
				lastIndentLevel = indentLevel
			end

			-- Check that control statements have braces on separate lines after them:
			-- Note that if statements can be broken into multiple lines, in which case this test is not taken
			if (isLastLineControl) then
				if not(a_Line:find("^%s*{") or a_Line:find("^%s*#")) then
					-- Not followed by a brace, not followed by a preprocessor
					ReportViolation(a_FileName, lineCounter - 1, 1, 1, "Control statement needs a brace on separate line")
				end
			end
			local lineWithSpace = " " .. a_Line
			isLastLineControl =
				lineWithSpace:find("^%s+if %b()") or
				lineWithSpace:find("^%s+else if %b()") or
				lineWithSpace:find("^%s+for %b()") or
				lineWithSpace:find("^%s+switch %b()") or
				lineWithSpace:find("^%s+else\n") or
				lineWithSpace:find("^%s+else  //") or
				lineWithSpace:find("^%s+do %b()")


			-- Check that exactly 5 empty lines are left beteen functions and no more than 5 elsewhere
			if not(a_Line:find("^\n")) then
				local numEmptyLines = (lineCounter - lastNonEmptyLine) - 1

				local isStartOfFunction = (
					isAfterFunction and
					a_Line:find("^[%s%w]")
				)

				if (isSourceFile and isStartOfFunction and (numEmptyLines ~= 5)) then
					ReportViolation(a_FileName, lineCounter - 1, 1, 1, "Leave exactly 5 empty lines between functions (found " .. numEmptyLines ..")")
				elseif (numEmptyLines > 5) then
					ReportViolation(a_FileName, lineCounter - 1, 1, 1, "Leave at most 5 consecutive empty lines (found " .. numEmptyLines .. ")")
				end

				lastNonEmptyLine = lineCounter
				isAfterFunction = (a_Line == "}\n")
			end

			lineCounter = lineCounter + 1
		end
	)
end





--- Processes one item - a file or a folder
local function ProcessItem(a_ItemName)
	assert(type(a_ItemName) == "string")

	-- Skip files / folders that should be ignored
	if (g_ShouldIgnoreFile[a_ItemName]) then
		return
	end

	local ext = a_ItemName:match("%.([^/%.]-)$")
	if (g_ShouldProcessExt[ext]) then
		ProcessFile(a_ItemName)
	end
end





--- Array of files to process. Filled from cmdline arguments
local ToProcess = {}





--- Handlers for the command-line arguments
-- Maps flag => function
local CmdLineHandlers =
{
	-- "-f file" checks the specified file
	["-f"] = function (a_Args, a_Idx)
		local fnam = a_Args[a_Idx + 1]
		if not(fnam) then
			error("Invalid flag: '-f' needs a filename following it.")
		end
		table.insert(ToProcess, fnam)
		return a_Idx + 2  -- skip the filename in param parsing
	end,

	-- "-g" checks files reported by git as being committed.
	["-g"] = function (a_Args, a_Idx)
		local f = io.popen("git diff --cached --name-only --diff-filter=ACMR")
		for fnam in f:lines() do
			table.insert(ToProcess, fnam)
		end
	end,

	-- "-h" prints help and exits
	["-h"] = function (a_Args, a_Idx)
		print([[
Usage:")
"CheckBasicStyle [<options>]

Available options:
-f <filename> - checks the specified filename
-g            - checks files reported by Git as being committed
-h            - prints this help and exits
-l <listfile> - checks all files listed in the specified listfile
--            - reads the list of files to check from stdin

When no options are given, the script checks all files listed in the AllFiles.lst file.

Only .cpp and .h files are ever checked.
]])
		os.exit(0)
	end,

	-- "-l listfile" loads the list of files to check from the specified listfile
	["-l"] = function (a_Args, a_Idx)
		local listFile = a_Args[a_Idx + 1]
		if not(listFile) then
			error("Invalid flag: '-l' needs a filename following it.")
		end
		for fnam in io.lines(listFile) do
			table.insert(ToProcess, fnam)
		end
		return a_Idx + 2  -- Skip the listfile in param parsing
	end,

	-- "--" reads the list of files from stdin
	["--"] = function (a_Args, a_Idx)
		for fnam in io.lines() do
			table.insert(ToProcess, fnam)
		end
	end,
}





-- Remove buffering from stdout, so that the output appears immediately in IDEs:
io.stdout:setvbuf("no")

-- Parse the cmdline arguments to see what files to check:
local idx = 1
while (arg[idx]) do
	local handler = CmdLineHandlers[arg[idx]]
	if not(handler) then
		error("Unknown command-line argument #" .. idx .. ": " .. arg[idx])
	end
	idx = handler(arg, idx) or (idx + 1)  -- Call the handler, let it change the next index if it wants
end


-- By default process all files in the AllFiles.lst file (generated by cmake):
if not(arg[1]) then
	for fnam in io.lines("AllFiles.lst") do
		table.insert(ToProcess, fnam)
	end
end





-- Process the files in the list:
for _, fnam in ipairs(ToProcess) do

	-- Remove the optional "./" prefix:
	if (fnam:sub(1, 2) == "./") then
		fnam = fnam:sub(3)
	end

	ProcessItem(fnam)
end





-- Report final verdict:
print("Number of violations found: " .. g_NumViolations)
if (g_NumViolations > 0) then
	os.exit(2)
else
	os.exit(0)
end