diff options
author | EuAndreh <eu@euandre.org> | 2021-08-27 22:44:19 -0300 |
---|---|---|
committer | EuAndreh <eu@euandre.org> | 2021-08-27 22:44:19 -0300 |
commit | ede12d99fc46ca3fd638d8b47f498497ef28b3df (patch) | |
tree | 0cd627ace07e3917924ad675cd19946ff4c1f5b3 /tests/c-lint.sh | |
parent | TODOs.md: Add comments to the body of #task-a6bdaeb0-7099-c728-2b7c-b080aa2fba33 (diff) | |
download | gistatic-ede12d99fc46ca3fd638d8b47f498497ef28b3df.tar.gz gistatic-ede12d99fc46ca3fd638d8b47f498497ef28b3df.tar.xz |
src/*.c: Add newline between function return type and its name
The purpose of this change is two-fold:
- make function and variable declarations grep-friendly (one can use a
pattern /^fn_name/ to find the definition);
- make the lines shorter, so less functions need to have their arguments
span over many lines.
This is more grep-friendly both for finding the function definition, but
also for matching on the return type of the function or the variable.
Update tests/c-lint.sh to enforce this.
Diffstat (limited to '')
-rwxr-xr-x | tests/c-lint.sh | 68 |
1 files changed, 55 insertions, 13 deletions
diff --git a/tests/c-lint.sh b/tests/c-lint.sh index 42d185c..0fa0b01 100755 --- a/tests/c-lint.sh +++ b/tests/c-lint.sh @@ -1,23 +1,65 @@ #!/bin/sh +set -eu + +awk ' +BEGIN { + ret = 0 + msg = "function not on the start of the line:" +} + +/^[a-zA-Z0-9_]+ .+\(/ { + if (ret == 0) { + print msg + } + printf "%s:%s:%s\n", FILENAME, FNR, $0 + ret = 1 +} + +END { + exit ret +} +' "$@" -RE='[a-z]+\(\) {' -if grep -Eq "$RE" "$@"; then - echo 'Functions with no argument without explicit "void" parameter:' >&2 - grep -En "$RE" "$@" - exit 1 -fi awk ' -/^[a-zA-Z0-9_]+.+{$/ && !/^static / { - match($0, /[a-zA-Z0-9_]+\(/) - fn_name = substr($0, RSTART, RLENGTH - 1) +BEGIN { + ret = 0 + static = 1 + msg = "non-static function is not declared in a header:" +} + +/^[a-zA-Z0-9_]+\(.*$/ && static == 0 { + split($0, line, /\(/) + fn_name = line[1] if (fn_name != "main") { - header = substr(FILENAME, 0, length(FILENAME) - 1) "h" - if (system("grep -qF \"" fn_name "(\" " header)) { - print "non-static function is not declared in a header:" + header = substr(FILENAME, 0, length(FILENAME) - 2) ".h" + if (system("grep -q ^\"" fn_name "\" \"" header "\"")) { + if (ret == 0) { + print msg + } printf "%s:%s:%s\n", FILENAME, FNR, $0 - exit 1 + ret = 1 } } } + +/^static / { + static = 1 +} + +!/^static / { + static = 0 +} + +END { + exit ret +} ' "$@" + + +RE='[a-z]+\(\) {' +if grep -Eq "$RE" "$@"; then + echo 'Functions with no argument without explicit "void" parameter:' >&2 + grep -En "$RE" "$@" + exit 1 +fi |