From 950aeffff8d641a0855fbd9a9b2993d9dfd7d2c3 Mon Sep 17 00:00:00 2001 From: peterbell10 Date: Thu, 26 Jul 2018 22:24:36 +0100 Subject: CheckBasicStyle: Check number of empty lines between functions (#4267) Add check for number of empty lines between functions and fix the corresponding failures --- src/CheckBasicStyle.lua | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'src/CheckBasicStyle.lua') diff --git a/src/CheckBasicStyle.lua b/src/CheckBasicStyle.lua index 6cd499fd1..4376d460a 100755 --- a/src/CheckBasicStyle.lua +++ b/src/CheckBasicStyle.lua @@ -44,11 +44,6 @@ local g_IgnoredFiles = "Bindings/Bindings.h", "Bindings/Bindings.cpp", "Bindings/LuaState_Implementation.cpp", - "LeakFinder.cpp", - "LeakFinder.h", - "MersenneTwister.h", - "StackWalker.cpp", - "StackWalker.h", } --- The list of files not to be processed, as a dictionary (filename => true), built from g_IgnoredFiles @@ -241,6 +236,9 @@ local function ProcessFile(a_FileName) 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) @@ -296,6 +294,26 @@ local function ProcessFile(a_FileName) 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 ) -- cgit v1.2.3