Skip to content

Commit 337fe38

Browse files
committed
add lint related script; add
1 parent 81e475a commit 337fe38

File tree

4 files changed

+485
-0
lines changed

4 files changed

+485
-0
lines changed

.clang-tidy

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
# Configure clang-tidy for this project
3+
Checks: >
4+
-*,
5+
bugprone-*,
6+
clang-analyzer-*,
7+
performance-*,
8+
portability-*,
9+
readability-*,
10+
-readability-magic-numbers,
11+
-readability-braces-around-statements,
12+
-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling
13+
14+
# Turn all warnings into errors
15+
WarningsAsErrors: ''
16+
17+
# Configure clang-tidy to ignore certain headers
18+
HeaderFilterRegex: '.*'
19+
20+
# Configure checks that allow for exceptions
21+
CheckOptions:
22+
- { key: readability-function-cognitive-complexity.Threshold, value: 50 }
23+
- { key: readability-magic-numbers.IgnoredIntegerValues, value: '1;2;3;4;8;16;32;64;128;256;512;1024;2048;4096;8192' }
24+
- { key: bugprone-argument-comment.StrictMode, value: 0 }
25+
- { key: bugprone-easily-swappable-parameters.MinimumLength, value: 4 }
26+
- { key: bugprone-unhandled-self-assignment.WarnOnlyIfThisHasSuspiciousField, value: 1 }
27+
- { key: readability-function-cognitive-complexity.Threshold, value: 50 }
28+
- { key: readability-function-size.LineThreshold, value: 150 }
29+
- { key: readability-function-size.StatementThreshold, value: 100 }
30+
- { key: readability-function-size.ParameterThreshold, value: 10 }
31+
- { key: bugprone-assert-side-effect.AssertMacros, value: 'assert' }
32+
- { key: bugprone-assert-side-effect.CheckFunctionCalls, value: 1 }
33+
- { key: cppcoreguidelines-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic, value: 1 }
34+
- { key: misc-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic, value: 1 }

.github/workflows/code-quality.yml

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
name: Code Quality
2+
3+
on:
4+
push:
5+
branches: [ main, master, develop ]
6+
pull_request:
7+
branches: [ main, master, develop ]
8+
9+
jobs:
10+
code-quality:
11+
name: Code Quality Checks
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
with:
16+
fetch-depth: 0 # Fetch all history for proper git diff
17+
18+
- name: Install dependencies
19+
run: |
20+
sudo apt-get update
21+
sudo apt-get install -y clang-format clang-tidy cmake g++ make
22+
bash scripts/install_dependency.sh
23+
24+
- name: Get changed C/C++ files
25+
id: changed-files
26+
uses: tj-actions/changed-files@v41
27+
with:
28+
files: |
29+
**/*.c
30+
**/*.h
31+
**/*.cpp
32+
**/*.cc
33+
**/*.hpp
34+
35+
- name: Create log directory
36+
run: mkdir -p logs
37+
38+
- name: Clang-format check
39+
if: steps.changed-files.outputs.any_changed == 'true'
40+
run: |
41+
echo "Running clang-format checks..."
42+
FORMAT_ERRORS=0
43+
44+
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
45+
echo "Checking formatting for $file"
46+
if ! clang-format --dry-run --Werror "$file" >> logs/format.log 2>&1; then
47+
echo "::error file=$file::Formatting issues in $file"
48+
FORMAT_ERRORS=$((FORMAT_ERRORS + 1))
49+
fi
50+
done
51+
52+
if [ $FORMAT_ERRORS -gt 0 ]; then
53+
echo "Found formatting issues in $FORMAT_ERRORS files."
54+
echo "You can fix these issues with: clang-format -i <file>"
55+
cat logs/format.log
56+
exit 1
57+
else
58+
echo "Formatting check passed."
59+
fi
60+
61+
- name: Configure CMake
62+
if: steps.changed-files.outputs.any_changed == 'true'
63+
run: |
64+
cmake -B build -DCMAKE_BUILD_TYPE=Debug \
65+
-DCMAKE_C_FLAGS="-Wall -Wextra -Werror -Wno-unused-variable -Wno-unused-function -Wno-unused-parameter -Wno-unused-but-set-variable -Wpedantic -Wformat=2 -Wformat-security -Wshadow -Wwrite-strings -Wstrict-prototypes -Wold-style-definition -Wredundant-decls -Wnested-externs -Wmissing-include-dirs" \
66+
-DCMAKE_CXX_FLAGS="-Wall -Wextra -Werror -Wno-unused-variable -Wno-unused-function -Wno-unused-parameter -Wno-unused-but-set-variable -Wno-pedantic -Wformat=2 -Wformat-security -Wshadow -Wwrite-strings -Wmissing-include-dirs" \
67+
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON
68+
69+
- name: Clang-tidy check
70+
if: steps.changed-files.outputs.any_changed == 'true'
71+
run: |
72+
echo "Running clang-tidy checks..."
73+
FILES_WITH_ISSUES=0
74+
75+
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
76+
echo "Checking $file with clang-tidy"
77+
LOG_FILE="logs/tidy_$(basename "$file").log"
78+
79+
# Run clang-tidy with selected checks (same as pre-commit hook)
80+
if ! clang-tidy -p=build \
81+
-checks='-*,bugprone-*,cert-*,clang-analyzer-*,cppcoreguidelines-*,performance-*,portability-*,readability-*,-readability-magic-numbers,-readability-braces-around-statements,-cppcoreguidelines-avoid-magic-numbers,-readability-identifier-length,-clang-diagnostic-unused-command-line-argument' \
82+
"$file" > "$LOG_FILE" 2>&1; then
83+
echo "::error file=$file::clang-tidy found issues in $file"
84+
cat "$LOG_FILE"
85+
FILES_WITH_ISSUES=$((FILES_WITH_ISSUES + 1))
86+
fi
87+
done
88+
89+
if [ $FILES_WITH_ISSUES -gt 0 ]; then
90+
echo "clang-tidy found issues in $FILES_WITH_ISSUES files."
91+
exit 1
92+
else
93+
echo "clang-tidy check passed successfully."
94+
fi
95+
96+
- name: Compilation check
97+
if: steps.changed-files.outputs.any_changed == 'true'
98+
run: |
99+
echo "Checking for compilation warnings..."
100+
if cmake --build build -j$(nproc) > logs/compile.log 2>&1; then
101+
echo "Compilation successful!"
102+
else
103+
echo "Compilation failed. Please fix the warnings."
104+
cat logs/compile.log
105+
exit 1
106+
fi

scripts/lint.sh

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6+
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
7+
8+
# Colors for output
9+
RED='\033[0;31m'
10+
GREEN='\033[0;32m'
11+
YELLOW='\033[1;33m'
12+
NC='\033[0m' # No Color
13+
14+
# Check if clang-tidy is installed
15+
if ! command -v clang-tidy &> /dev/null; then
16+
echo -e "${RED}Error: clang-tidy is not installed${NC}"
17+
echo "Please install clang-tidy:"
18+
echo " Ubuntu/Debian: sudo apt install clang-tidy"
19+
echo " macOS: brew install llvm"
20+
exit 1
21+
fi
22+
23+
# Function to print usage
24+
usage() {
25+
echo "Usage: $0 [options]"
26+
echo "Options:"
27+
echo " -h, --help Show this help message"
28+
echo " -f, --fix Apply suggested fixes"
29+
echo " -p, --path PATH Path to compile_commands.json"
30+
echo " --all Check all files (default: only changed files)"
31+
exit 1
32+
}
33+
34+
# Parse arguments
35+
FIX=0
36+
CHECK_ALL=0
37+
COMPILE_COMMANDS_DIR="_build"
38+
39+
while [[ $# -gt 0 ]]; do
40+
case $1 in
41+
-h|--help)
42+
usage
43+
;;
44+
-f|--fix)
45+
FIX=1
46+
shift
47+
;;
48+
-p|--path)
49+
COMPILE_COMMANDS_DIR="$2"
50+
shift 2
51+
;;
52+
--all)
53+
CHECK_ALL=1
54+
shift
55+
;;
56+
*)
57+
echo -e "${RED}Unknown option: $1${NC}"
58+
usage
59+
;;
60+
esac
61+
done
62+
63+
# Ensure we're in the project root
64+
cd "${PROJECT_ROOT}"
65+
66+
# Check if compile_commands.json exists
67+
if [ ! -f "${COMPILE_COMMANDS_DIR}/compile_commands.json" ]; then
68+
echo -e "${YELLOW}Warning: compile_commands.json not found in ${COMPILE_COMMANDS_DIR}${NC}"
69+
echo "Building project to generate compile_commands.json..."
70+
71+
# Create build directory if it doesn't exist
72+
mkdir -p "${COMPILE_COMMANDS_DIR}"
73+
74+
# Generate compile_commands.json
75+
pushd "${COMPILE_COMMANDS_DIR}" > /dev/null
76+
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON ..
77+
popd > /dev/null
78+
fi
79+
80+
# Get list of files to check
81+
if [ $CHECK_ALL -eq 1 ]; then
82+
# Find all C/C++ source files
83+
FILES=$(find libCacheSim -type f \( -name "*.c" -o -name "*.cpp" -o -name "*.cc" -o -name "*.h" -o -name "*.hpp" \))
84+
else
85+
# Get only changed files
86+
FILES=$(git diff --name-only HEAD | grep -E "\.(c|cpp|cc|h|hpp)$" || true)
87+
if [ -z "$FILES" ]; then
88+
echo -e "${YELLOW}No C/C++ files changed${NC}"
89+
exit 0
90+
fi
91+
fi
92+
93+
# Run clang-tidy
94+
echo -e "${GREEN}Running clang-tidy...${NC}"
95+
ERRORS=0
96+
for file in $FILES; do
97+
echo "Checking $file..."
98+
if [ $FIX -eq 1 ]; then
99+
# Run with fix and create backup files
100+
clang-tidy -p "${COMPILE_COMMANDS_DIR}" --fix "$file" || ERRORS=$?
101+
else
102+
clang-tidy -p "${COMPILE_COMMANDS_DIR}" "$file" || ERRORS=$?
103+
fi
104+
done
105+
106+
if [ $ERRORS -eq 0 ]; then
107+
echo -e "${GREEN}No linting errors found${NC}"
108+
else
109+
echo -e "${RED}Linting errors found${NC}"
110+
exit 1
111+
fi

0 commit comments

Comments
 (0)