From 38a004d1b954e71a6b3de23cf1dfaf7339f02e4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Wed, 27 Oct 2021 18:09:12 +0200 Subject: [PATCH] XMake: Add inclusion fix --- src/Nazara/Core/Debug/NewRedefinition.cpp | 2 + src/Nazara/Core/MemoryManager.cpp | 1 + xmake/actions/checkfiles.lua | 227 ++++++++++++++++++---- 3 files changed, 190 insertions(+), 40 deletions(-) diff --git a/src/Nazara/Core/Debug/NewRedefinition.cpp b/src/Nazara/Core/Debug/NewRedefinition.cpp index 536ad9a0d..1a4281345 100644 --- a/src/Nazara/Core/Debug/NewRedefinition.cpp +++ b/src/Nazara/Core/Debug/NewRedefinition.cpp @@ -2,6 +2,8 @@ // This file is part of the "Nazara Engine - Core module" // For conditions of distribution and use, see copyright notice in Config.hpp +// no include fix + #include #if NAZARA_CORE_MANAGE_MEMORY diff --git a/src/Nazara/Core/MemoryManager.cpp b/src/Nazara/Core/MemoryManager.cpp index b5ebb9c06..4d0723083 100644 --- a/src/Nazara/Core/MemoryManager.cpp +++ b/src/Nazara/Core/MemoryManager.cpp @@ -15,6 +15,7 @@ #include #endif +// no include fix // The only file that does not need to include Debug.hpp namespace Nz diff --git a/xmake/actions/checkfiles.lua b/xmake/actions/checkfiles.lua index 7bf52ffc3..c574ef532 100644 --- a/xmake/actions/checkfiles.lua +++ b/xmake/actions/checkfiles.lua @@ -21,8 +21,6 @@ local function CompareLines(referenceLines, lines, firstLine, lineCount) for i = 1, lineCount do if lines[firstLine + i - 1] ~= referenceLines[i] then - print(lines[firstLine + i]) - print(referenceLines[i]) return false end end @@ -65,6 +63,7 @@ on_run(function () local checks = {} + -- Remove empty lines at the beginning of files table.insert(checks, { Name = "empty lines", Check = function (moduleName) @@ -107,6 +106,7 @@ on_run(function () end }) + -- Check header guards and #pragma once table.insert(checks, { Name = "header guards", Check = function (moduleName) @@ -212,52 +212,52 @@ on_run(function () print(filePath .. ": failed to identify header guard macro (endif)") canFix = false end - end - if canFix then - if macroName ~= pathHeaderGuard then - print(filePath .. ": header guard mismatch (got " .. macroName .. ", expected " .. pathHeaderGuard .. ")") + if canFix then + if macroName ~= pathHeaderGuard then + print(filePath .. ": header guard mismatch (got " .. macroName .. ", expected " .. pathHeaderGuard .. ")") - shouldFixEndif = false + shouldFixEndif = false - table.insert(fixes, { - File = filePath, - Func = function (lines) - lines[ifndefLine] = "#ifndef " .. pathHeaderGuard - lines[defineLine] = "#define " .. pathHeaderGuard - lines[endifLine] = "#endif // " .. pathHeaderGuard + table.insert(fixes, { + File = filePath, + Func = function (lines) + lines[ifndefLine] = "#ifndef " .. pathHeaderGuard + lines[defineLine] = "#define " .. pathHeaderGuard + lines[endifLine] = "#endif // " .. pathHeaderGuard - return lines - end - }) - end + return lines + end + }) + end - if shouldFixEndif then - print(filePath .. ": #endif was missing comment") + if shouldFixEndif then + print(filePath .. ": #endif was missing comment") - table.insert(fixes, { - File = filePath, - Func = function (lines) - lines[endifLine] = "#endif // " .. pathHeaderGuard + table.insert(fixes, { + File = filePath, + Func = function (lines) + lines[endifLine] = "#endif // " .. pathHeaderGuard - return lines - end - }) - end + return lines + end + }) + end - if not pragmaLine then - print(filePath .. ": no #pragma once found") - table.insert(fixes, { - File = filePath, - Func = function (lines) - table.insert(lines, ifndefLine - 1, "#pragma once") - table.insert(lines, ifndefLine - 1, "") + if not pragmaLine then + print(filePath .. ": no #pragma once found") + table.insert(fixes, { + File = filePath, + Func = function (lines) + table.insert(lines, ifndefLine - 1, "#pragma once") + table.insert(lines, ifndefLine - 1, "") - return lines - end - }) - elseif pragmaLine > ifndefLine then - print(filePath .. ": #pragma once is after header guard (should be before)") + return lines + end + }) + elseif pragmaLine > ifndefLine then + print(filePath .. ": #pragma once is after header guard (should be before)") + end end end end @@ -267,6 +267,153 @@ on_run(function () end }) + -- Every source file should include its header first + table.insert(checks, { + Name = "inclusion", + Check = function (moduleName) + local files = table.join( + os.files("include/Nazara/" .. moduleName .. "/**.inl"), + os.files("src/Nazara/" .. moduleName .. "/**.inl"), + os.files("src/Nazara/" .. moduleName .. "/**.cpp") + ) + + local fixes = {} + for _, filePath in pairs(files) do + local lines = GetFile(filePath) + + local headerPath = filePath:gsub("[\\/]", "/") + headerPath = headerPath:sub(headerPath:find("Nazara/"), -1) + headerPath = headerPath:sub(1, headerPath:find("%..+$") - 1) .. ".hpp" + + if os.isfile("include/" .. headerPath) or os.isfile("src/" .. headerPath) then + local inclusions = {} + + -- Retrieve all inclusions + for i = 1, #lines do + if lines[i] == "// no include fix" then + -- ignore file + inclusions = {} + break + end + + local includeMode, includePath = lines[i]:match("^%s*#%s*include%s*([<\"])(.+)[>\"]") + if includeMode then + table.insert(inclusions, { + line = i, + mode = includeMode, + path = includePath + }) + end + end + + if #inclusions > 0 then + local headerInclude + for i = 1, #inclusions do + if inclusions[i].path == headerPath then + headerInclude = i + if i ~= 1 then + print(filePath .. " doesn't include corresponding header first") + + local currentInclusion = inclusions[i] + table.insert(fixes, { + File = filePath, + Func = function (lines) + table.remove(lines, currentInclusion.line) + local firstHeaderLine = inclusions[1].line + table.insert(lines, firstHeaderLine, "#include <" .. headerPath .. ">") + + return lines + end + }) + end + + break + end + end + + local debugIncludeModule = moduleName ~= "Math" and moduleName or "Core" + + local debugInclude + local debugIncludeOff + for i = 1, #inclusions do + local module, off = inclusions[i].path:match("Nazara/(.+)/Debug(O?f?f?).hpp") + if module then + if off == "Off" then + debugIncludeOff = i + elseif off == "" then + debugInclude = i + else + print(filePath .. ": unrecognized debug include at line " .. inclusions[i].line) + end + + if module ~= debugIncludeModule then + print(filePath .. ": has wrong Debug" .. off .. " include") + + local currentInclusion = inclusions[i] + table.insert(fixes, { + File = filePath, + Func = function (lines) + lines[currentInclusion.line] = "#include " + return lines + end + }) + end + end + end + + local increment = 0 + + if not headerInclude then + print(filePath .. " is missing corresponding header inclusion") + + table.insert(fixes, { + File = filePath, + Func = function (lines) + local firstHeaderLine = inclusions[1].line + table.insert(lines, firstHeaderLine, "#include <" .. headerPath .. ">") + + increment = increment + 1 + + return lines + end + }) + end + + if not debugInclude then + print(filePath .. ": has missing Debug include") + local lastIncludeLine = inclusions[debugIncludeOff and #inclusions - 1 or #inclusions].line + table.insert(fixes, { + File = filePath, + Func = function (lines) + table.insert(lines, lastIncludeLine + increment + 1, "#include ") + return lines + end + }) + end + + if path.extension(filePath) == ".inl" then + if not debugInclude then + print(filePath .. ": has missing DebugOff include") + table.insert(fixes, { + File = filePath, + Func = function (lines) + table.insert(lines, "") + table.insert(lines, "#include ") + table.insert(lines, "") + return lines + end + }) + end + end + end + end + end + + return fixes + end + }) + + -- Check copyright date and format table.insert(checks, { Name = "copyright", Check = function (moduleName) @@ -440,7 +587,7 @@ on_run(function () local year, authors = lines[1]:match("^// Copyright %(C%) (Y?E?A?R?%d*) (.+)$") hasCopyright = year ~= nil - if authors == "AUTHORS" then + if not authors or authors == "AUTHORS" then authors = engineAuthor else local fixedAuthors = authors:gsub(prevAuthor, engineAuthor)