1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
#!/usr/bin/env lua
--[[
Copyright (c) 2006-2011 Gordon Gremme <gordon@gremme.org>
Copyright (c) 2006-2008 Center for Bioinformatics, University of Hamburg
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
]]
function usage()
io.stderr:write(string.format("Usage: %s file [...]\n", arg[0]))
io.stderr:write("Check C sources for accordance with coding standards.\n")
os.exit(1)
end
if #arg == 0 then usage() end
error_code = 0
function err(filename, line_number, msg)
io.stderr:write(string.format("%s: %d: %s\n", filename, line_number, msg))
error_code = 1
end
function errf(filename, msg)
io.stderr:write(string.format("%s: %s\n", filename, msg))
error_code = 1
end
function check_header_file(fn, filecontent)
local ifndef = filecontent:match("^.-(#ifndef.-)\n")
local define = filecontent:match("^.-#ifndef.-\n(#define.-)\n")
local endif = filecontent:match("\n(#endif)$")
local define_name
local had_err = false
if fn:find("/") then
define_name = fn:match("^.*/(.+)$")
else
define_name = fn;
end
define_name = define_name:upper()
define_name = define_name:gsub("%.", "_")
define_name = define_name:gsub("%-", "_")
if (ifndef) then
local given_name = ifndef:match("#ifndef (.+)")
if not (given_name == define_name) then
errf(fn, "first #ifndef should be named '" .. define_name .. "' (is '" ..
given_name .. "')")
end
else
errf(fn, "does not have #ifndef " .. define_name .. " line")
end
if (define) then
local given_name = define:match("#define (.+)")
if not (given_name == define_name) then
errf(fn, "first #define should be named '" .. define_name .. "' (is '" ..
given_name .. "')")
end
else
errf(fn, "does not have #define " .. define_name ..
" line after first #ifndef line")
end
if not endif then
errf(fn, "does not end with plain #endif line")
end
end
for _, fn in ipairs(arg) do -- iterate over all files
-- reset per file variables
local empty_line_found = false
local ln = 1 -- the line number
local header_file = false
local filelines = {}
local first_line = nil
local last_line = nil
if string.find(fn, "%.h$") then
header_file = true
end
for l in io.lines(fn) do -- iterate over all lines
-- remember first line
if not first_line then first_line = l end
-- check for unnecessary blanks
if string.find(l, " $") then
err(fn, ln, "unnecessary blank(s) at the end of line")
end
-- check for missing blanks
if string.find(l, " if%(") then err(fn, ln, "missing blank") end
if string.find(l, " sizeof%(") then err(fn, ln, "missing blank") end
if string.find(l, " switch%(") then err(fn, ln, "missing blank") end
if string.find(l, " while%(") then err(fn, ln, "missing blank") end
if string.find(l, " for%(") then err(fn, ln, "missing blank") end
if string.find(l, " do%(") then err(fn, ln, "missing blank") end
if string.find(l, "%){") then err(fn, ln, "missing blank") end
if not string.match(fn, "types_api.h$") then
-- check for unsigned long long and long long
if string.find(l, "unsigned%s+long%s+long[%s,%*]") or
string.find(l, "%(unsigned%s+long%s+long%)") then
err(fn, ln, "do not use 'unsigned long long', use 'GtUint64' instead")
elseif string.find(l, "long%s+long[%s,%*]") or
string.find(l, "%(long%s+long%)") then
err(fn, ln, "do not use 'long long', use 'GtInt64' instead")
end
-- check for unsigned long and long
if string.find(l, "unsigned%s+long[%s,%*]") or
string.find(l, "%(unsigned%s+long%)") then
err(fn, ln, "do not use 'unsigned long', use 'GtUword' instead")
elseif string.find(l, "%slong[,%*]") or
string.find(l, "%(long%)") then
err(fn, ln, "do not use 'long', use 'GtWord' instead")
end
-- check flag characters
if string.find(l, "%%lld") then
err(fn, ln, "do not use '%lld', use 'GT_LLD' instead")
end
if string.find(l, "%%llu") then
err(fn, ln, "do not use '%llu', use 'GT_LLU' instead")
end
if string.find(l, "%%ld") then
err(fn, ln, "do not use '%ld', use 'GT_WD' instead")
end
if string.find(l, "%%lu") then
err(fn, ln, "do not use '%lu', use 'GT_WU' instead")
end
if string.find(l, "%%zu") then
err(fn, ln, "do not use '%zu', use 'GT_ZU' instead")
end
end
-- check for long double
if string.find(l, "long double") then
err (fn, ln, "do not use 'long double', use 'double' instead")
end
-- check for superfluous blanks
if string.find(l, "return ") then err(fn, ln, "superfluous blank") end
-- check line length
if #l > 80 then err(fn, ln, "line longer than 80 characters") end
-- check for C++ style comments
if string.find(l, "[^:/\"a-z]//") then err(fn, ln, "C++ style comment") end
-- check for tabulators
if string.find(l, "\t") then
err(fn, ln, "tabulator found, use blanks instead")
end
-- check for commata at the beginning of a line
if string.find(l, "^ *,") then
err(fn, ln, "illegal ',' at beginning of line")
end
-- check for old has_err usage
if string.find(l, " has_err") then
err(fn, ln, "illegal variable name 'has_err', use 'had_err' instead")
end
-- check for gt library includes with <
if string.find(l, "^#include <libgt") then
err(fn, ln, "wrong library include (<...>), use \"...\" instead")
end
-- check for tool includes with <
if string.find(l, "^#include <tools/") then
err(fn, ln, "wrong tool include (<...>), use \"...\" instead")
end
-- check for stdlib assert.h includes and assert(3) usage
if not string.match(fn, "assert_api.h$") then
if string.find(l, "^#include <assert.h>") or
string.find(l, "^#include \"assert.h\"") then
err(fn, ln,
"do not include <assert.h>, use \"core/assert_api.h\" instead")
end
if string.find(l, " assert%(") then
err(fn, ln, "do not use assert(), use gt_assert() instead")
end
end
-- check for carriage return
if string.find(l, "\r") then
err(fn, ln, "contains carriage return (Windows newline?)")
end
-- check for two empty lines in a row
if string.find(l, "^$") then
if (empty_line_found) then
err(fn, ln, "two empty lines in a row")
end
empty_line_found = true
else
empty_line_found = false
end
if (header_file) then
filelines[ln] = l
end
-- remember last line
last_line = l
-- increate line number
ln = ln + 1
end
-- make sure first line is not empty
if first_line and string.find(first_line, "^$") then
err(fn, 1, "first line is empty")
end
-- make sure last line is not empty
if last_line and string.find(last_line, "^$") then
err(fn, ln-1, "trailing line is empty")
end
if (header_file) then
local filecontent = table.concat(filelines, "\n")
check_header_file(fn, filecontent)
end
end
os.exit(error_code)
|