Skip to content

Commit bdcee72

Browse files
committed
Update .clang-tidy, Makefile, and documentation for improved code quality checks
- Simplified .clang-tidy configuration to enforce one variable per declaration for better readability. - Updated Makefile to automatically find and use clang-format and clang-tidy from Homebrew's LLVM installation. - Enhanced CONTRIBUTING.md to clarify installation instructions for clang tools and added guidelines for variable declarations. - Refactored multiple C files to declare one variable per line, improving code readability.
1 parent 86f32f7 commit bdcee72

File tree

36 files changed

+147
-63
lines changed

36 files changed

+147
-63
lines changed

.clang-tidy

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
11
---
2+
# Minimal checks for K&R C code style
3+
# Only enforce one variable per declaration for readability
24
Checks: >
35
-*,
4-
bugprone-*,
5-
cert-*,
6-
clang-analyzer-*,
7-
cppcoreguidelines-*,
8-
performance-*,
9-
portability-*,
10-
readability-*,
11-
-readability-magic-numbers,
12-
-readability-identifier-length,
13-
-cppcoreguidelines-avoid-magic-numbers,
14-
-bugprone-easily-swappable-parameters,
15-
-readability-function-cognitive-complexity
6+
readability-isolate-declaration
167
178
WarningsAsErrors: ''
189
HeaderFilterRegex: '.*'
19-
AnalyzeTemporaryDtors: false
2010
FormatStyle: 'file'
21-
User: ''

.github/workflows/c.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ jobs:
1212
steps:
1313
- uses: actions/checkout@v4
1414

15+
- name: Install clang-tidy
16+
run: sudo apt-get update && sudo apt-get install -y clang-tidy
17+
1518
- name: Check formatting
1619
run: make format-check
1720

CONTRIBUTING.md

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,20 @@ Thanks for your interest in contributing! This guide will help you get started.
1515

1616
You'll need:
1717
- A C compiler (GCC or Clang)
18-
- `clang-format` for code formatting
1918
- `make` for building
19+
- `clang-format` and `clang-tidy` for code quality checks
2020

21-
### Installing clang-format
21+
### Installing Dependencies
2222

2323
**macOS:**
2424
```shell
25-
brew install clang-format
25+
brew install llvm
2626
```
27+
> The Makefile automatically finds the tools from Homebrew's LLVM installation - no need to modify your PATH.
2728
2829
**Ubuntu / Debian:**
2930
```shell
30-
sudo apt-get install clang-format
31+
sudo apt-get install clang-format clang-tidy
3132
```
3233

3334
**Arch Linux:**
@@ -69,6 +70,25 @@ The CI will automatically check formatting and linting on your pull request.
6970

7071
We use `clang-format` with minimal configuration (LLVM style, 4-space indentation). Just run `make format` and your code will be properly formatted.
7172

73+
Additional rules enforced by `clang-tidy`:
74+
75+
### One variable per declaration
76+
77+
Declare each variable on its own line for better readability.
78+
79+
```c
80+
// ✗ Avoid
81+
int x, y, z;
82+
float fahr, celsius;
83+
84+
// ✓ Preferred
85+
int x;
86+
int y;
87+
int z;
88+
float fahr;
89+
float celsius;
90+
```
91+
7292
## What to Contribute
7393

7494
- **Bug fixes** - Found an error in a solution? Please fix it!

Makefile

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,39 @@ EXECS = $(SOURCES:%.c=%)
66
LDLIBS = -lm
77
CFLAGS = -Wall -Wextra -Wpedantic
88

9+
# Find clang tools: check PATH first, then Homebrew LLVM location
10+
LLVM_PREFIX := $(shell brew --prefix llvm 2>/dev/null)
11+
CLANG_FORMAT := $(or $(shell command -v clang-format 2>/dev/null),$(wildcard $(LLVM_PREFIX)/bin/clang-format))
12+
CLANG_TIDY := $(or $(shell command -v clang-tidy 2>/dev/null),$(wildcard $(LLVM_PREFIX)/bin/clang-tidy))
13+
914
all: install-hooks $(EXECS)
1015

1116
lint:
1217
@echo "Linting with compiler warnings..."
1318
@for f in $(filter %.c, $(SOURCES)); do \
1419
$(CC) $(CFLAGS) -fsyntax-only $$f 2>&1 || exit 1; \
1520
done
21+
ifneq ($(CLANG_TIDY),)
22+
@echo "Running clang-tidy..."
23+
@for f in $(filter %.c, $(SOURCES)); do \
24+
$(CLANG_TIDY) $$f -- $(CFLAGS) 2>&1 || exit 1; \
25+
done
26+
else
27+
@echo "Skipping clang-tidy (not installed). Install with: brew install llvm"
28+
endif
1629
@echo "Lint passed!"
1730

1831
format:
19-
find . \( -name '*.c' -o -name '*.h' \) -print0 | xargs -0 clang-format -i
32+
ifeq ($(CLANG_FORMAT),)
33+
$(error clang-format not found. Install with: brew install llvm)
34+
endif
35+
find . \( -name '*.c' -o -name '*.h' \) -print0 | xargs -0 $(CLANG_FORMAT) -i
2036

2137
format-check:
22-
find . \( -name '*.c' -o -name '*.h' \) -print0 | xargs -0 clang-format --dry-run --Werror
38+
ifeq ($(CLANG_FORMAT),)
39+
$(error clang-format not found. Install with: brew install llvm)
40+
endif
41+
find . \( -name '*.c' -o -name '*.h' \) -print0 | xargs -0 $(CLANG_FORMAT) --dry-run --Werror
2342

2443
check: format-check lint
2544

chapter_1/exercise_1_03/fahrenheit_celsius.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
// floating point version
66

77
int main(void) {
8-
float fahr, celsius;
9-
int lower, upper, step;
8+
float fahr;
9+
float celsius;
10+
int lower;
11+
int upper;
12+
int step;
1013

1114
lower = 0;
1215
upper = 300;

chapter_1/exercise_1_04/celsius_fahrenheit.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
// floating point version
66

77
int main(void) {
8-
float celsius, fahr;
9-
int lower, upper, step;
8+
float celsius;
9+
float fahr;
10+
int lower;
11+
int upper;
12+
int step;
1013

1114
lower = 0;
1215
upper = 300;

chapter_1/exercise_1_05/celsius_fahrenheit.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
// floating point version with for loop
66

77
int main(void) {
8-
float celsius, fahr;
9-
int lower, upper, step;
8+
float celsius;
9+
float fahr;
10+
int lower;
11+
int upper;
12+
int step;
1013

1114
lower = 0;
1215
upper = 300;

chapter_1/exercise_1_15/temperature_conversion.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
float celsius_to_fahrenheit(int celsius);
44

55
int main(void) {
6-
float celsius, fahr;
7-
int lower, upper, step;
6+
float celsius;
7+
float fahr;
8+
int lower;
9+
int upper;
10+
int step;
811

912
lower = 0;
1013
upper = 300;

chapter_1/exercise_1_16/longest_line.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ int main(void) {
3030
}
3131

3232
int get_line(char line[], int maxline) {
33-
int c, i;
33+
int c;
34+
int i;
3435

3536
for (i = 0; i < maxline - 1 && (c = getchar()) != EOF && c != '\n'; ++i) {
3637
line[i] = c;

chapter_1/exercise_1_17/line_80.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ int main(void) {
1919
}
2020

2121
int get_line(char line[], int max_line_len) {
22-
int c, i;
22+
int c;
23+
int i;
2324

2425
for (i = 0; i < max_line_len - 1 && (c = getchar()) != EOF && c != '\n';
2526
++i) {

0 commit comments

Comments
 (0)