check_defines.lua 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #!/usr/bin/lua5.2
  2. usedlines = {c={}, n={}}
  3. useddefs = {c={}, n={}}
  4. function AddElem(tab, q)
  5. if (tab.c[q]) then
  6. tab.c[q] = tab.c[q] + 1
  7. else
  8. tab.c[q] = 1
  9. tab.n[#tab.n+1]=q
  10. end
  11. end
  12. function PrintTab(tab)
  13. table.sort(tab.n)
  14. for _,n in ipairs(tab.n) do
  15. --print(tab.c[n], n)
  16. print(n)
  17. end
  18. end
  19. function noifdef(f)
  20. local out = {}
  21. local changed = false
  22. for l in io.lines(f) do
  23. local n = l:gsub("^#ifdef ([%w_]+)", "#if defined(%1)")
  24. n = n:gsub("^#ifndef ([%w_]+)", "#if !defined(%1)")
  25. out[#out+1] = (n)
  26. if l ~= n then
  27. --print(l , "-->", n)
  28. changed = true
  29. end
  30. if n:match("^#if") then
  31. local q = n:gsub("%/%*.+%*%/", "")
  32. q = q:gsub("%s+$", "")
  33. q = q:gsub("^%s+", "")
  34. q = q:gsub("%s+", " ")
  35. AddElem(usedlines, q)
  36. for w in q:gmatch("%(%s*([%w_]+)%s*%)") do
  37. AddElem(useddefs, w)
  38. end
  39. end
  40. end
  41. if changed then
  42. local fi = io.open(f, "w")
  43. for _,l in pairs(out) do
  44. fi:write(l .. "\n")
  45. end
  46. fi:close()
  47. print(f .. " rewritten")
  48. end
  49. -- print(#out .. " lines processed")
  50. end
  51. path = path or ""
  52. noifdef(path .. "src/civetweb.c")
  53. noifdef(path .. "src/civetweb_private_lua.h")
  54. noifdef(path .. "src/main.c")
  55. noifdef(path .. "src/md5.inl")
  56. noifdef(path .. "src/mod_duktape.inl")
  57. noifdef(path .. "src/mod_http2.inl")
  58. noifdef(path .. "src/mod_lua.inl")
  59. noifdef(path .. "src/mod_lua_shared.inl")
  60. noifdef(path .. "src/mod_zlib.inl")
  61. noifdef(path .. "src/sha1.inl")
  62. noifdef(path .. "src/timer.inl")
  63. noifdef(path .. "src/wolfssl_extras.inl")
  64. noifdef(path .. "src/response.inl")
  65. noifdef(path .. "src/handle_form.inl")
  66. --PrintTab(usedlines)
  67. --print("Defines used")
  68. PrintTab(useddefs)