From b85f6b68ac1664f8a6a6b17eeef6140e676b184b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Tue, 26 Oct 2021 15:52:31 +0200 Subject: [PATCH] XMake: Add header guard check --- xmake/actions/checkfiles.lua | 188 +++++++++++++++++++++++++++--- xmake/actions/generateheaders.lua | 8 +- xmake/actions/spirv.lua | 12 +- xmake/actions/unicode.lua | 1 + 4 files changed, 185 insertions(+), 24 deletions(-) diff --git a/xmake/actions/checkfiles.lua b/xmake/actions/checkfiles.lua index 5e3ef4327..7bf52ffc3 100644 --- a/xmake/actions/checkfiles.lua +++ b/xmake/actions/checkfiles.lua @@ -66,7 +66,7 @@ on_run(function () local checks = {} table.insert(checks, { - Name = "remove empty lines at the beginning", + Name = "empty lines", Check = function (moduleName) local files = table.join( os.files("include/Nazara/" .. moduleName .. "/**.hpp"), @@ -107,6 +107,166 @@ on_run(function () end }) + table.insert(checks, { + Name = "header guards", + Check = function (moduleName) + local files = table.join( + os.files("include/Nazara/" .. moduleName .. "/**.hpp"), + os.files("src/Nazara/" .. moduleName .. "/**.hpp") + ) + + local fixes = {} + for _, filePath in pairs(files) do + local lines = GetFile(filePath) + + local pragmaLine + local ifndefLine + local defineLine + local endifLine + local macroName + + local pathMacro = filePath:gsub("[/\\]", "_") + do + pathMacro = pathMacro:sub(pathMacro:lastof(moduleName .. "_", true) + #moduleName + 1) + local i = pathMacro:lastof(".", true) + if i then + pathMacro = pathMacro:sub(1, i - 1) + end + end + + local pathHeaderGuard = (moduleName ~= pathMacro) and "NAZARA_" .. moduleName:upper() .. "_" .. pathMacro:upper() .. "_HPP" or "NAZARA_" .. moduleName:upper() .. "_HPP" + + local canFix = true + local ignored = false + + -- Fetch pragma once, ifdef and define lines + for i = 1, #lines do + if lines[i] == "// no header guards" then + canFix = false + ignored = true + break + end + + if lines[i] == "#pragma once" then + if pragmaLine then + print(filePath .. ": multiple #pragma once found") + canFix = false + break + end + + pragmaLine = i + elseif not ifndefLine and lines[i]:startswith("#ifndef") then + ifndefLine = i + + macroName = lines[i]:match("^#ifndef%s+(.+)$") + if not macroName then + print(filePath .. ": failed to identify header guard macro (ifndef)") + canFix = false + break + end + elseif ifndefLine and not defineLine and lines[i]:startswith("#define") then + defineLine = i + + local defineMacroName = lines[i]:match("^#define%s+(.+)$") + if not defineMacroName then + print(filePath .. ": failed to identify header guard macro (define)") + canFix = false + break + end + + if defineMacroName ~= macroName then + print(filePath .. ": failed to identify header guard macro (define macro doesn't match ifdef)") + canFix = false + break + end + end + + if ifndefLine and defineLine then + break + end + end + + if not ignored then + if not ifndefLine or not defineLine or not macroName then + print(filePath .. ": failed to identify header guard macro") + canFix = false + end + + -- Fetch endif line + if canFix then + local shouldFixEndif = false + + for i = #lines, 1, -1 do + if lines[i]:startswith("#endif") then + local macro = lines[i]:match("#endif // (.+)") + if macro ~= macroName then + shouldFixEndif = true + end + + endifLine = i + break + end + end + + if not endifLine then + 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 .. ")") + + shouldFixEndif = false + + table.insert(fixes, { + File = filePath, + Func = function (lines) + lines[ifndefLine] = "#ifndef " .. pathHeaderGuard + lines[defineLine] = "#define " .. pathHeaderGuard + lines[endifLine] = "#endif // " .. pathHeaderGuard + + return lines + end + }) + end + + if shouldFixEndif then + print(filePath .. ": #endif was missing comment") + + table.insert(fixes, { + File = filePath, + Func = function (lines) + lines[endifLine] = "#endif // " .. pathHeaderGuard + + 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, "") + + return lines + end + }) + elseif pragmaLine > ifndefLine then + print(filePath .. ": #pragma once is after header guard (should be before)") + end + end + end + end + + return fixes + end + }) + table.insert(checks, { Name = "copyright", Check = function (moduleName) @@ -255,7 +415,7 @@ on_run(function () table.insert(newLines, lines[i]) end - UpdateFile(configFilePath, newLines) + return newLines end }) end @@ -332,7 +492,7 @@ on_run(function () table.insert(lines, #copyrightLines + 1, "") end - UpdateFile(filePath, lines) + return lines end }) end @@ -359,18 +519,18 @@ on_run(function () if shouldFix then for _, fix in pairs(fixes) do print("Fixing " .. fix.File) - fix.Func(assert(fileLines[fix.File])) - end - - for filePath, _ in pairs(updatedFiles) do - local lines = assert(fileLines[filePath]) - if lines[#lines] ~= "" then - table.insert(lines, "") - end - - print("Saving changes to " .. filePath) - io.writefile(filePath, table.concat(lines, "\n")) + UpdateFile(fix.File, fix.Func(assert(fileLines[fix.File]))) end end end + + for filePath, _ in pairs(updatedFiles) do + local lines = assert(fileLines[filePath]) + if lines[#lines] ~= "" then + table.insert(lines, "") + end + + print("Saving changes to " .. filePath) + io.writefile(filePath, table.concat(lines, "\n")) + end end) diff --git a/xmake/actions/generateheaders.lua b/xmake/actions/generateheaders.lua index b3eaa16df..bd6d1c9b7 100644 --- a/xmake/actions/generateheaders.lua +++ b/xmake/actions/generateheaders.lua @@ -53,7 +53,7 @@ on_run(function () paths[moduleName .. "_Components"] = { Excludes = table.copy(excludedFiles), Header = head, - HeaderGuard = "NAZARA_GLOBAL_" .. moduleName:upper() .. "_COMPONENTS_HPP", + HeaderGuard = "NAZARA_" .. moduleName:upper() .. "_COMPONENTS_HPP", Name = "Nazara" .. moduleName .. " components", SearchDir = modulePath .. "/Components", Target = modulePath .. "/Components.hpp" @@ -64,7 +64,7 @@ on_run(function () paths[moduleName .. "_Systems"] = { Excludes = table.copy(excludedFiles), Header = head, - HeaderGuard = "NAZARA_GLOBAL_" .. moduleName:upper() .. "_SYSTEMS_HPP", + HeaderGuard = "NAZARA_" .. moduleName:upper() .. "_SYSTEMS_HPP", Name = "Nazara" .. moduleName .. " systems", SearchDir = modulePath .. "/Systems", Target = modulePath .. "/Systems.hpp" @@ -85,7 +85,7 @@ on_run(function () ["InstanceFunctions.hpp"] = true, }, Header = paths["OpenGLRenderer"].Header, - HeaderGuard = "NAZARA_GLOBAL_OPENGLRENDERER_WRAPPER_HPP", + HeaderGuard = "NAZARA_OPENGLRENDERER_WRAPPER_HPP", Name = "OpenGL wrapper", SearchDir = "include/Nazara/OpenGLRenderer/Wrapper", Target = "include/Nazara/OpenGLRenderer/Wrapper.hpp" @@ -99,7 +99,7 @@ on_run(function () ["InstanceFunctions.hpp"] = true, }, Header = paths["VulkanRenderer"].Header, - HeaderGuard = "NAZARA_GLOBAL_VULKANRENDERER_WRAPPER_HPP", + HeaderGuard = "NAZARA_VULKANRENDERER_WRAPPER_HPP", Name = "Vulkan wrapper", SearchDir = "include/Nazara/VulkanRenderer/Wrapper", Target = "include/Nazara/VulkanRenderer/Wrapper.hpp" diff --git a/xmake/actions/spirv.lua b/xmake/actions/spirv.lua index 8c1e1f93e..53cd50480 100644 --- a/xmake/actions/spirv.lua +++ b/xmake/actions/spirv.lua @@ -72,11 +72,11 @@ on_run(function() assert(headerFile, "failed to open Spir-V header") headerFile:write([[ -// Copyright (C) ]] .. os.date("%Y") .. [[ Jérôme Leclercq +// Copyright (C) ]] .. os.date("%Y") .. [[ Jérôme "Lynix" Leclercq (lynix680@gmail.com) // This file is part of the "Nazara Engine - Shader module" -// For conditions of distribution and use, see copyright notice in Config.hpp" +// For conditions of distribution and use, see copyright notice in Config.hpp -// This file was generated automatically, please do not edit +// this file was automatically generated and should not be edited #pragma once @@ -211,12 +211,12 @@ headerFile:write([[ assert(sourceFile, "failed to open Spir-V source") sourceFile:write([[ -// this file was automatically generated and should not be edited - -// Copyright (C) ]] .. os.date("%Y") .. [[ Jérôme Leclercq +// Copyright (C) ]] .. os.date("%Y") .. [[ Jérôme "Lynix" Leclercq (lynix680@gmail.com) // This file is part of the "Nazara Engine - Shader module" // For conditions of distribution and use, see copyright notice in Config.hpp +// this file was automatically generated and should not be edited + #include #include #include diff --git a/xmake/actions/unicode.lua b/xmake/actions/unicode.lua index 9fa9b2ca0..fe2299c87 100644 --- a/xmake/actions/unicode.lua +++ b/xmake/actions/unicode.lua @@ -179,6 +179,7 @@ local DirectionToString = {} file:write([[ // this file was automatically generated and should not be edited +// no header guards // Copyright (C) ]] .. os.date("%Y") .. [[ Jérôme Leclercq // This file is part of the "Nazara Engine - Core module"