feat: more functions fixed

This commit is contained in:
Krzysztof Rudnicki 2025-09-08 15:32:55 +02:00
parent da16e9d33d
commit 1d7313690b
5 changed files with 1537 additions and 270 deletions

6
.vscode/tasks.json vendored
View File

@ -42,6 +42,12 @@
"command": "python -m pip install -r requirements.txt && pytest -q", "command": "python -m pip install -r requirements.txt && pytest -q",
"group": "build" "group": "build"
}, },
{
"label": "pytest quick",
"type": "shell",
"command": "python -m pip install -r requirements.txt && pytest -q",
"group": "build"
},
{ {
"label": "pytest quick", "label": "pytest quick",
"type": "shell", "type": "shell",

16
articles/.clang-format Normal file
View File

@ -0,0 +1,16 @@
BasedOnStyle: LLVM
Language: Cpp
ColumnLimit: 100
ReflowComments: true
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: AllIfsAndElse
AllowShortLoopsOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: true
BinPackArguments: true
BinPackParameters: true
BreakBeforeBraces: Attach
PointerAlignment: Left
IndentWidth: 2
TabWidth: 2
UseTab: Never
SortIncludes: false

View File

@ -2,11 +2,39 @@ CC ?= cc
CFLAGS ?= -O2 -Wall -Wextra -pedantic CFLAGS ?= -O2 -Wall -Wextra -pedantic
LDFLAGS ?= LDFLAGS ?=
.ONESHELL:
all: server_c all: server_c
server_c: server_c.c server_c: server_c.c
$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
format:
@command -v clang-format >/dev/null 2>&1 || { echo "clang-format not found"; exit 1; }
@echo "Running clang-format (ColumnLimit=100)..."
@clang-format -i -style=file server_c.c
format-check:
@command -v clang-format >/dev/null 2>&1 || { echo "clang-format not found"; exit 1; }
@echo "Checking formatting with clang-format..."
@# Use clang-format dry-run so this works without git and doesn't depend on commit state
@if clang-format --dry-run -Werror -style=file server_c.c >/dev/null 2>&1; then \
echo "Format check passed"; \
else \
echo "Formatting changes needed (run 'make format')"; \
exit 1; \
fi
# fail if any function exceeds 20 non-empty lines between '{' and matching '}'
funcsize-check:
@echo "Checking function sizes (<= 20 lines)..."
@command -v clang-tidy >/dev/null 2>&1 || { echo "clang-tidy not found"; exit 1; }
@clang-tidy \
-checks=readability-function-size \
-warnings-as-errors=readability-function-size \
-config='{ "CheckOptions": [ { "key": "readability-function-size.LineThreshold", "value": "20" } ] }' \
server_c.c -- -std=gnu11 $(CFLAGS) $(LDFLAGS)
lint: lint:
@echo "Running clang-tidy..." @echo "Running clang-tidy..."
@clang-tidy \ @clang-tidy \
@ -15,6 +43,8 @@ lint:
server_c.c -- -std=gnu11 $(CFLAGS) $(LDFLAGS) || true server_c.c -- -std=gnu11 $(CFLAGS) $(LDFLAGS) || true
@echo "Running cppcheck..." @echo "Running cppcheck..."
@cppcheck --enable=all --inconclusive --std=c11 --language=c --quiet --inline-suppr --check-level=exhaustive --suppress=missingIncludeSystem server_c.c || true @cppcheck --enable=all --inconclusive --std=c11 --language=c --quiet --inline-suppr --check-level=exhaustive --suppress=missingIncludeSystem server_c.c || true
@echo "Checking line length (<= 100 chars) in server_c.c..."
@awk 'length($$0)>100{print "server_c.c:" NR ": line too long (" length($$0) " > 100)"; err=1} END{if(err){print "Line length check FAILED"; exit 1} else {print "Line length check passed"}}' server_c.c
build: minify build: minify

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,31 @@
BEGIN{ in_func=0; depth=0; start=0; err=0; prev="" }
{
line=$0
# track function start when we see an opening brace at top-level and previous non-empty
# line looks like a function signature (ends with ')' and not ';', and not a typedef/struct/enum/union)
for(i=1;i<=length(line);i++){
c=substr(line,i,1)
if(c=="{"){
if(depth==0 && !in_func){
# Heuristic check on previous non-empty trimmed line
t=prev
sub(/^\s+/, "", t); sub(/\s+$/, "", t)
if(t ~ /\)$/ && t !~ /;\s*$/ && t !~ /^(typedef|struct|enum|union)\b/){
in_func=1; start=NR
}
}
depth++
} else if(c=="}"){
depth--
if(in_func && depth==0){
lines=NR-start+1
if(lines>20){ print FILENAME ":" start " function too long: " lines " lines"; err=1 }
in_func=0
}
}
}
# update previous non-empty line
tmp=line; sub(/^\s+/, "", tmp); sub(/\s+$/, "", tmp)
if(length(tmp)>0){ prev=tmp }
}
END{ if(err) exit 1 }