summaryrefslogtreecommitdiffstats
path: root/src/CheckBasicStyle.lua
diff options
context:
space:
mode:
authorp-mcgowan <mickeymcgowan@shaw.ca>2014-12-05 20:24:09 +0100
committerp-mcgowan <mickeymcgowan@shaw.ca>2014-12-05 20:24:09 +0100
commita8bbd5efe422dd20a6bdcfc18cd0befbfeb93f9d (patch)
tree8c1fa6af38d8997f0cc935b62362245373b5db86 /src/CheckBasicStyle.lua
parentreformat (diff)
parentCheckBasicStyle: Check missing braces for control statements. (diff)
downloadcuberite-a8bbd5efe422dd20a6bdcfc18cd0befbfeb93f9d.tar
cuberite-a8bbd5efe422dd20a6bdcfc18cd0befbfeb93f9d.tar.gz
cuberite-a8bbd5efe422dd20a6bdcfc18cd0befbfeb93f9d.tar.bz2
cuberite-a8bbd5efe422dd20a6bdcfc18cd0befbfeb93f9d.tar.lz
cuberite-a8bbd5efe422dd20a6bdcfc18cd0befbfeb93f9d.tar.xz
cuberite-a8bbd5efe422dd20a6bdcfc18cd0befbfeb93f9d.tar.zst
cuberite-a8bbd5efe422dd20a6bdcfc18cd0befbfeb93f9d.zip
Diffstat (limited to '')
-rw-r--r--src/CheckBasicStyle.lua41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/CheckBasicStyle.lua b/src/CheckBasicStyle.lua
index 76ae8c325..7914aedfd 100644
--- a/src/CheckBasicStyle.lua
+++ b/src/CheckBasicStyle.lua
@@ -92,6 +92,25 @@ 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"},
+ {"<[^)T][^)]*&&", "Add parenthesis around comparison"}, -- Must take special care of templates: "template <T> fn(Args && ...)"
+ {"&&[^(]+<", "Add parenthesis around comparison"},
+ {"<[^)T][^)]*||", "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"},
@@ -162,6 +181,7 @@ local function ProcessFile(a_FileName)
-- Ref.: http://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
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)
@@ -198,6 +218,24 @@ local function ProcessFile(a_FileName)
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()")
lineCounter = lineCounter + 1
end
@@ -227,6 +265,9 @@ end
+-- Remove buffering from stdout, so that the output appears immediately in IDEs:
+io.stdout:setvbuf("no")
+
-- Process all files in the AllFiles.lst file (generated by cmake):
for fnam in io.lines("AllFiles.lst") do
ProcessItem(fnam)