1
0
cuberite-2a/src/CheckBasicStyle.lua

468 lines
16 KiB
Lua
Raw Normal View History

2014-07-21 11:57:50 -04:00
#!/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.h",
"Bindings/Bindings.cpp",
"Bindings/LuaState_Implementation.cpp",
}
--- 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"},
2015-03-21 15:35:25 -04:00
{"<[^)>]*&&", "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)
2015-03-21 15:35:25 -04:00
{"<[^)>]*||", "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"},
2015-07-31 10:49:10 -04:00
-- Check that doxy-comments are used only in the double-asterisk form:
{"/// ", "Use doxycomments in the form /** Comment */"},
2015-07-31 10:49:10 -04:00
-- 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 */"},
2015-07-31 10:49:10 -04:00
-- 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 \"(\""},
2014-07-21 09:21:54 -04:00
{"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")
1.9 / 1.9.2 / 1.9.3 / 1.9.4 protocol support (#3135) * Semistable update to 15w31a I'm going through snapshots in a sequential order since it should make things easier, and since protocol version history is written. * Update to 15w34b protocol Also, fix an issue with the Entity Equipment packet from the past version. Clients are able to connect and do stuff! * Partially update to 15w35e Chunk data doesn't work, but the client joins. I'm waiting to do chunk data because chunk data has an incomplete format until 15w36d. * Add '/blk' debug command This command lets one see what block they are looking at, and makes figuring out what's supposed to be where in a highly broken chunk possible. * Fix CRLF normalization in CheckBasicStyle.lua Normally, this doesn't cause an issue, but when running from cygwin, it detects the CR as whitespace and creates thousands of violations for every single line. Lua, when run on windows, will normalize automatically, but when run via cygwin, it won't. The bug was simply that gsub was returning a replaced version, but not changing the parameter, so the replaced version was ignored. * Update to 15w40b This includes chunk serialization. Fully functional chunk serialization for 1.9. I'm not completely happy with the chunk serialization as-is (correct use of palettes would be great), but cuberite also doesn't skip sending empty chunks so this performance optimization should probably come later. The creation of a full buffer is suboptimal, but it's the easiest way to implement this code. * Write long-by-long rather than creating a buffer This is a bit faster and should be equivalent. However, the code still doesn't look too good. * Update to 15w41a protocol This includes the new set passengers packet, which works off of the ridden entity, not the rider. That means, among other things, that information about the previously ridden vehicle is needed when detaching. So a new method with that info was added. * Update to 15w45a * 15w51b protocol * Update to 1.9.0 protocol Closes #3067. There are still a few things that need to be worked out (picking up items, effects, particles, and most importantly inventory), but in general this should work. I'll make a few more changes tomorrow to get the rest of the protocol set up, along with 1.9.1/1.9.2 (which did make a few changes). Chunks, however, _are_ working, along with most other parts of the game (placing/breaking blocks). * Fix item pickup packet not working That was a silly mistake, but at least it was an easy one. * 1.9.2 protocol support * Fix version info found in server list ping Thus, the client reports that it can connect rather than saying that the server is out of date. This required creating separate classes for 1.9.1 and 1.9.2, unfortunately. * Fix build errors generated by clang These didn't happen in MSVC. * Add protocol19x.cpp and protocol19x.h to CMakeLists * Ignore warnings in protocol19x that are ignored in protocol18x * Document BLOCK_FACE and DIG_STATUS constants * Fix BLOCK_FACE links and add separate section for DIG_STATUS * Fix bat animation and object spawning The causes of both of these are explained in #3135, but the gist is that both were typos. * Implement Use Item packet This means that buckets, bows, fishing rods, and several other similar items now work when not looking at a block. * Handle DIG_STATUS_SWAP_ITEM_IN_HAND * Add support for spawn eggs and potions The items are transformed from the 1.9 version to the 1.8 version when reading and transformed back when sending. * Remove spammy potion debug logging * Fix wolf collar color metadata The wrong type was being used, causing several clientside issues (including the screen going black). * Fix 1.9 chunk sending in the nether The nether and the end don't send skylight. * Fix clang build errors * Fix water bottles becoming mundane potions This happened because the can become splash potion bit got set incorrectly. Water bottles and mundane potions are only differentiated by the fact that water bottles have a metadata of 0, so setting that bit made it a mundane potion. Also add missing break statements to the read item NBT switch, which would otherwise break items with custom names and also cause incorrect "Unimplemented NBT data when parsing!" logging. * Copy Protocol18x as Protocol19x Aditionally, method and class names have been swapped to clean up other diffs. This commit is only added to make the following diffs more readable; it doesn't make any other changes (beyond class names). * Make thrown potions use the correct appearence This was caused by potions now using metadata. * Add missing api doc for cSplashPotionEntity::GetItem * Fix compile error in SplashPotionEntity.cpp * Fix fix of cSplashPotionEntity API doc * Temporarilly disable fall damage particles These were causing issues in 1.9 due to the changed effect ID. * Properly send a kick packet when connecting with an invalid version This means that the client no longer waits on the server screen with no indication whatsoever. However, right now the server list ping isn't implemented for unknown versions, so it'll only load "Old" on the ping. I also added a GetVarIntSize method to cByteBuffer. This helps clean up part of the code here (and I think it could clean up other parts), but it may make sense for it to be moved elsewhere (or declared in a different way). * Handle server list pings from unrecognized versions This isn't the cleanest way of writing it (it feels odd to use ProtocolRecognizer to send packets, and the addition of m_InPingForUnrecognizedVersion feels like the wrong technique), but it works and I can't think of a better way (apart from creating a full separate protocol class to handle only the ping... which would be worse). * Use cPacketizer for the disconnect packet This also should fix clang build errors. * Add 1.9.3 / 1.9.4 support * Fix incorrect indentation in APIDesc
2016-05-14 15:12:42 -04:00
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