mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-07-04 14:43:01 +02:00
chore: added linting to image veiwer
This commit is contained in:
parent
630dbeb40f
commit
be301762ec
61
C/imageViewer/.clang-format
Normal file
61
C/imageViewer/.clang-format
Normal file
@ -0,0 +1,61 @@
|
||||
# Clang-format configuration for imageViewer project
|
||||
---
|
||||
Language: C
|
||||
# Base style
|
||||
BasedOnStyle: LLVM
|
||||
|
||||
# Indentation
|
||||
IndentWidth: 4
|
||||
TabWidth: 4
|
||||
UseTab: Never
|
||||
IndentCaseLabels: true
|
||||
|
||||
# Braces
|
||||
BreakBeforeBraces: Attach
|
||||
Cpp11BracedListStyle: true
|
||||
|
||||
# Line length and breaking
|
||||
ColumnLimit: 100
|
||||
BreakBeforeBinaryOperators: None
|
||||
BreakBeforeTernaryOperators: true
|
||||
AlwaysBreakAfterReturnType: None
|
||||
|
||||
# Spacing
|
||||
SpaceBeforeParens: ControlStatements
|
||||
SpaceInEmptyParentheses: false
|
||||
SpacesInParentheses: false
|
||||
SpacesInSquareBrackets: false
|
||||
SpaceAfterCStyleCast: false
|
||||
SpaceBeforeAssignmentOperators: true
|
||||
|
||||
# Alignment
|
||||
AlignOperands: true
|
||||
AlignConsecutiveAssignments: false
|
||||
AlignConsecutiveDeclarations: false
|
||||
AlignTrailingComments: true
|
||||
|
||||
# Function parameters
|
||||
AllowAllParametersOfDeclarationOnNextLine: false
|
||||
BinPackParameters: false
|
||||
BinPackArguments: false
|
||||
|
||||
# Includes
|
||||
SortIncludes: true
|
||||
IncludeBlocks: Preserve
|
||||
|
||||
# Comments
|
||||
ReflowComments: true
|
||||
|
||||
# Pointers and references
|
||||
PointerAlignment: Right
|
||||
DerivePointerAlignment: false
|
||||
|
||||
# Control statements
|
||||
AllowShortIfStatementsOnASingleLine: false
|
||||
AllowShortLoopsOnASingleLine: false
|
||||
AllowShortFunctionsOnASingleLine: None
|
||||
AllowShortCaseLabelsOnASingleLine: false
|
||||
|
||||
# Misc
|
||||
KeepEmptyLinesAtTheStartOfBlocks: false
|
||||
MaxEmptyLinesToKeep: 1
|
||||
38
C/imageViewer/.clang-tidy
Normal file
38
C/imageViewer/.clang-tidy
Normal file
@ -0,0 +1,38 @@
|
||||
# Clang-tidy configuration for imageViewer project
|
||||
Checks: >
|
||||
clang-diagnostic-*,
|
||||
clang-analyzer-*,
|
||||
bugprone-*,
|
||||
cert-*,
|
||||
misc-*,
|
||||
modernize-*,
|
||||
performance-*,
|
||||
portability-*,
|
||||
readability-*,
|
||||
-readability-magic-numbers,
|
||||
-modernize-use-trailing-return-type,
|
||||
-cert-err33-c,
|
||||
-misc-unused-parameters,
|
||||
-readability-isolate-declaration
|
||||
|
||||
WarningsAsErrors: ''
|
||||
HeaderFilterRegex: '.*\.h$'
|
||||
AnalyzeTemporaryDtors: false
|
||||
FormatStyle: file
|
||||
CheckOptions:
|
||||
- key: readability-identifier-naming.VariableCase
|
||||
value: snake_case
|
||||
- key: readability-identifier-naming.FunctionCase
|
||||
value: snake_case
|
||||
- key: readability-identifier-naming.MacroCase
|
||||
value: UPPER_CASE
|
||||
- key: readability-identifier-naming.TypedefCase
|
||||
value: CamelCase
|
||||
- key: readability-identifier-naming.StructCase
|
||||
value: CamelCase
|
||||
- key: readability-function-size.LineThreshold
|
||||
value: 100
|
||||
- key: readability-function-size.StatementThreshold
|
||||
value: 50
|
||||
- key: misc-non-private-member-variables-in-classes.IgnoreClassesWithAllMemberVariablesBeingPublic
|
||||
value: true
|
||||
43
C/imageViewer/.vscode/settings.json
vendored
Normal file
43
C/imageViewer/.vscode/settings.json
vendored
Normal file
@ -0,0 +1,43 @@
|
||||
{
|
||||
"C_Cpp.default.cStandard": "c99",
|
||||
"C_Cpp.default.intelliSenseMode": "linux-gcc-x64",
|
||||
"C_Cpp.default.includePath": [
|
||||
"${workspaceFolder}/**",
|
||||
"/usr/include/SDL2"
|
||||
],
|
||||
"C_Cpp.default.defines": [
|
||||
"_REENTRANT"
|
||||
],
|
||||
"C_Cpp.default.compilerPath": "/usr/bin/gcc",
|
||||
"C_Cpp.clang_format_style": "file",
|
||||
"C_Cpp.clang_format_fallbackStyle": "LLVM",
|
||||
|
||||
"files.associations": {
|
||||
"*.h": "c",
|
||||
"*.c": "c"
|
||||
},
|
||||
|
||||
"editor.formatOnSave": true,
|
||||
"editor.tabSize": 4,
|
||||
"editor.insertSpaces": true,
|
||||
"editor.rulers": [100],
|
||||
|
||||
"clang-tidy.executable": "clang-tidy",
|
||||
"clang-tidy.checks": [
|
||||
"clang-diagnostic-*",
|
||||
"clang-analyzer-*",
|
||||
"bugprone-*",
|
||||
"cert-*",
|
||||
"misc-*",
|
||||
"performance-*",
|
||||
"portability-*",
|
||||
"readability-*"
|
||||
],
|
||||
|
||||
"cppcheck.enable": true,
|
||||
"cppcheck.standard": ["c99"],
|
||||
"cppcheck.suppress": [
|
||||
"missingIncludeSystem",
|
||||
"unusedFunction"
|
||||
]
|
||||
}
|
||||
124
C/imageViewer/LINTING.md
Normal file
124
C/imageViewer/LINTING.md
Normal file
@ -0,0 +1,124 @@
|
||||
# C Language Linter Setup for imageViewer Project
|
||||
|
||||
This directory contains a comprehensive linting setup for the imageViewer C project.
|
||||
|
||||
## Tools Included
|
||||
|
||||
### 1. **clang-tidy** - Static Analysis
|
||||
- Configuration: `.clang-tidy`
|
||||
- Checks for bugs, performance issues, and style violations
|
||||
- Enforces modern C coding standards
|
||||
|
||||
### 2. **clang-format** - Code Formatting
|
||||
- Configuration: `.clang-format`
|
||||
- Automatically formats code to consistent style
|
||||
- 100-character line limit, 4-space indentation
|
||||
|
||||
### 3. **cppcheck** - Additional Static Analysis
|
||||
- Detects memory leaks, null pointer dereferences
|
||||
- Checks for undefined behavior
|
||||
|
||||
### 4. **gcc with warnings** - Compiler Analysis
|
||||
- Comprehensive warning flags
|
||||
- Standards compliance checking
|
||||
|
||||
## Usage
|
||||
|
||||
### Quick Start
|
||||
```bash
|
||||
# Install dependencies (Arch Linux)
|
||||
make deps-arch
|
||||
|
||||
# Run all lint checks
|
||||
make lint
|
||||
|
||||
# Format code
|
||||
make format
|
||||
|
||||
# Run all checks
|
||||
make check
|
||||
|
||||
# Check for memory leaks
|
||||
make memcheck
|
||||
```
|
||||
|
||||
### Individual Commands
|
||||
```bash
|
||||
# Manual linting
|
||||
./lint.sh
|
||||
|
||||
# Format specific file
|
||||
clang-format -i main.c
|
||||
|
||||
# Run clang-tidy
|
||||
clang-tidy main.c -- -I/usr/include/SDL2 -D_REENTRANT
|
||||
|
||||
# Run cppcheck
|
||||
cppcheck --enable=all main.c
|
||||
```
|
||||
|
||||
## VS Code Integration
|
||||
|
||||
The `.vscode/settings.json` file provides:
|
||||
- Automatic formatting on save
|
||||
- C99 standard compliance
|
||||
- IntelliSense configuration for SDL2
|
||||
- Integrated linting with clang-tidy and cppcheck
|
||||
|
||||
## Recommended Extensions for VS Code
|
||||
- C/C++ (Microsoft)
|
||||
- clang-tidy (mine-cetinkaya-fianso)
|
||||
- cppcheck (unixwrapped)
|
||||
|
||||
## Linting Rules
|
||||
|
||||
### Enabled Checks
|
||||
- **clang-diagnostic-***: Compiler diagnostics
|
||||
- **clang-analyzer-***: Static analysis
|
||||
- **bugprone-***: Bug-prone patterns
|
||||
- **cert-***: CERT secure coding standards
|
||||
- **misc-***: Miscellaneous checks
|
||||
- **performance-***: Performance improvements
|
||||
- **portability-***: Cross-platform issues
|
||||
- **readability-***: Code readability
|
||||
|
||||
### Disabled Checks
|
||||
- `readability-magic-numbers`: Allows constants like window dimensions
|
||||
- `cert-err33-c`: Allows ignoring some function return values
|
||||
- `misc-unused-parameters`: Common in callback functions
|
||||
|
||||
## Code Quality Workflow
|
||||
|
||||
1. **Write Code**: Develop features in `main.c`
|
||||
2. **Lint**: Run `make lint` to check for issues
|
||||
3. **Format**: Run `make format` to fix formatting
|
||||
4. **Build**: Run `make` to compile
|
||||
5. **Test**: Run `make test` with sample images
|
||||
6. **Memory Check**: Run `make memcheck` for leak detection
|
||||
|
||||
## Configuration Files
|
||||
|
||||
- `.clang-tidy`: Static analysis rules
|
||||
- `.clang-format`: Code formatting style
|
||||
- `.vscode/settings.json`: VS Code integration
|
||||
- `lint.sh`: Comprehensive linting script
|
||||
- `Makefile`: Build and quality targets
|
||||
|
||||
## Installation on Different Systems
|
||||
|
||||
### Arch Linux
|
||||
```bash
|
||||
sudo pacman -S clang cppcheck valgrind
|
||||
```
|
||||
|
||||
### Ubuntu/Debian
|
||||
```bash
|
||||
sudo apt install clang-tidy cppcheck clang-format valgrind
|
||||
```
|
||||
|
||||
### Fedora/RHEL
|
||||
```bash
|
||||
sudo dnf install clang-tools-extra cppcheck clang valgrind
|
||||
```
|
||||
|
||||
This setup ensures high code quality, consistency, and helps catch potential issues early in development.
|
||||
@ -14,7 +14,7 @@ ifneq ($(SDL2_CFLAGS),)
|
||||
LIBS = $(SDL2_LIBS) -lSDL2_image -lm
|
||||
endif
|
||||
|
||||
.PHONY: all clean install deps help
|
||||
.PHONY: all clean install deps help lint format check memcheck
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
@ -24,18 +24,44 @@ $(TARGET): $(SOURCE)
|
||||
clean:
|
||||
rm -f $(TARGET)
|
||||
|
||||
# Linting and code quality targets
|
||||
lint:
|
||||
@echo "Running comprehensive lint checks..."
|
||||
@./lint.sh
|
||||
|
||||
format:
|
||||
@echo "Formatting code..."
|
||||
@if command -v clang-format >/dev/null 2>&1; then \
|
||||
clang-format -i $(SOURCE); \
|
||||
echo "Code formatted successfully"; \
|
||||
else \
|
||||
echo "clang-format not found. Install it with: sudo pacman -S clang"; \
|
||||
fi
|
||||
|
||||
check: lint
|
||||
@echo "Running extended checks..."
|
||||
@if command -v valgrind >/dev/null 2>&1; then \
|
||||
echo "Memory leak check available. Run: make memcheck"; \
|
||||
else \
|
||||
echo "Install valgrind for memory leak detection: sudo pacman -S valgrind"; \
|
||||
fi
|
||||
|
||||
memcheck: $(TARGET)
|
||||
@echo "Running memory leak check..."
|
||||
@valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./$(TARGET) 2>&1 | head -50
|
||||
|
||||
# Install dependencies on Ubuntu/Debian
|
||||
deps-debian:
|
||||
sudo apt-get update
|
||||
sudo apt-get install libsdl2-dev libsdl2-image-dev
|
||||
sudo apt-get install libsdl2-dev libsdl2-image-dev clang-tidy cppcheck clang-format valgrind
|
||||
|
||||
# Install dependencies on Fedora/RHEL/CentOS
|
||||
deps-fedora:
|
||||
sudo dnf install SDL2-devel SDL2_image-devel
|
||||
sudo dnf install SDL2-devel SDL2_image-devel clang-tools-extra cppcheck clang valgrind
|
||||
|
||||
# Install dependencies on Arch Linux
|
||||
deps-arch:
|
||||
sudo pacman -S sdl2 sdl2_image
|
||||
sudo pacman -S sdl2 sdl2_image clang cppcheck valgrind
|
||||
|
||||
# Run with a test image
|
||||
test:
|
||||
@ -52,21 +78,24 @@ test:
|
||||
fi
|
||||
|
||||
help:
|
||||
@echo "JPG Image Viewer - Makefile targets:"
|
||||
@echo "imageViewer - Makefile targets:"
|
||||
@echo " all - Build the image viewer"
|
||||
@echo " clean - Remove built files"
|
||||
@echo " test - Run with a test image (if available)"
|
||||
@echo " lint - Run comprehensive code linting"
|
||||
@echo " format - Format code with clang-format"
|
||||
@echo " check - Run all code quality checks"
|
||||
@echo " memcheck - Run memory leak detection"
|
||||
@echo " deps-debian - Install dependencies on Ubuntu/Debian"
|
||||
@echo " deps-fedora - Install dependencies on Fedora/RHEL/CentOS"
|
||||
@echo " deps-arch - Install dependencies on Arch Linux"
|
||||
@echo " help - Show this help"
|
||||
@echo ""
|
||||
@echo "Usage: ./$(TARGET) <image_file.jpg>"
|
||||
@echo "Code Quality Workflow:"
|
||||
@echo " 1. make lint - Check for issues"
|
||||
@echo " 2. make format - Fix formatting"
|
||||
@echo " 3. make check - Run all checks"
|
||||
@echo " 4. make - Build project"
|
||||
@echo " 5. make memcheck - Check for memory leaks"
|
||||
@echo ""
|
||||
@echo "Controls:"
|
||||
@echo " Mouse wheel / +/- : Zoom in/out"
|
||||
@echo " Mouse drag : Pan image"
|
||||
@echo " R : Reset zoom and position"
|
||||
@echo " F : Fit image to window"
|
||||
@echo " H : Show help"
|
||||
@echo " ESC/Q : Quit"
|
||||
@echo "Usage: ./$(TARGET) <image_file.jpg>"
|
||||
|
||||
200
C/imageViewer/lint.sh
Executable file
200
C/imageViewer/lint.sh
Executable file
@ -0,0 +1,200 @@
|
||||
# Lint script for imageViewer project
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
print_step() {
|
||||
echo -e "${BLUE}==>${NC} ${1}"
|
||||
}
|
||||
|
||||
print_success() {
|
||||
echo -e "${GREEN}✓${NC} ${1}"
|
||||
}
|
||||
|
||||
print_warning() {
|
||||
echo -e "${YELLOW}⚠${NC} ${1}"
|
||||
}
|
||||
|
||||
print_error() {
|
||||
echo -e "${RED}✗${NC} ${1}"
|
||||
}
|
||||
|
||||
# Check if required tools are installed
|
||||
check_tools() {
|
||||
print_step "Checking required tools..."
|
||||
|
||||
local missing_tools=()
|
||||
|
||||
if ! command -v clang-tidy &> /dev/null; then
|
||||
missing_tools+=("clang-tidy")
|
||||
fi
|
||||
|
||||
if ! command -v cppcheck &> /dev/null; then
|
||||
missing_tools+=("cppcheck")
|
||||
fi
|
||||
|
||||
if ! command -v clang-format &> /dev/null; then
|
||||
missing_tools+=("clang-format")
|
||||
fi
|
||||
|
||||
if [ ${#missing_tools[@]} -ne 0 ]; then
|
||||
print_error "Missing required tools: ${missing_tools[*]}"
|
||||
print_step "Installing missing tools..."
|
||||
|
||||
# Check if we're on Arch Linux
|
||||
if command -v pacman &> /dev/null; then
|
||||
sudo pacman -S --needed clang cppcheck
|
||||
elif command -v apt &> /dev/null; then
|
||||
sudo apt update && sudo apt install -y clang-tidy cppcheck clang-format
|
||||
elif command -v dnf &> /dev/null; then
|
||||
sudo dnf install -y clang-tools-extra cppcheck clang
|
||||
else
|
||||
print_error "Please install the following tools manually: ${missing_tools[*]}"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
print_success "All required tools are available"
|
||||
}
|
||||
|
||||
# Run clang-tidy
|
||||
run_clang_tidy() {
|
||||
print_step "Running clang-tidy analysis..."
|
||||
|
||||
if [ -f ".clang-tidy" ]; then
|
||||
clang-tidy main.c -- -I/usr/include/SDL2 -D_REENTRANT 2>/dev/null || {
|
||||
print_warning "clang-tidy found issues (see output above)"
|
||||
}
|
||||
else
|
||||
print_warning ".clang-tidy config not found, using default settings"
|
||||
clang-tidy main.c -- -I/usr/include/SDL2 -D_REENTRANT 2>/dev/null || {
|
||||
print_warning "clang-tidy found issues (see output above)"
|
||||
}
|
||||
fi
|
||||
|
||||
print_success "clang-tidy analysis completed"
|
||||
}
|
||||
|
||||
# Run cppcheck
|
||||
run_cppcheck() {
|
||||
print_step "Running cppcheck analysis..."
|
||||
|
||||
cppcheck --enable=all --suppress=missingIncludeSystem --suppress=unusedFunction \
|
||||
--suppress=constParameter --quiet --std=c99 main.c || {
|
||||
print_warning "cppcheck found issues (see output above)"
|
||||
}
|
||||
|
||||
print_success "cppcheck analysis completed"
|
||||
}
|
||||
|
||||
# Check code formatting
|
||||
check_formatting() {
|
||||
print_step "Checking code formatting..."
|
||||
|
||||
if [ -f ".clang-format" ]; then
|
||||
if clang-format --dry-run --Werror main.c 2>/dev/null; then
|
||||
print_success "Code formatting is correct"
|
||||
else
|
||||
print_warning "Code formatting issues found"
|
||||
echo "Run 'clang-format -i main.c' to fix formatting"
|
||||
fi
|
||||
else
|
||||
print_warning ".clang-format config not found, skipping format check"
|
||||
fi
|
||||
}
|
||||
|
||||
# Run basic compile check
|
||||
compile_check() {
|
||||
print_step "Running compile check..."
|
||||
|
||||
# Try to compile with extra warnings
|
||||
if gcc -Wall -Wextra -Wpedantic -std=c99 -O2 \
|
||||
$(pkg-config --cflags sdl2 2>/dev/null || echo "-I/usr/include/SDL2") \
|
||||
-c main.c -o /tmp/main.o 2>/dev/null; then
|
||||
print_success "Compile check passed"
|
||||
rm -f /tmp/main.o
|
||||
else
|
||||
print_error "Compile check failed"
|
||||
print_step "Trying compile with detailed errors..."
|
||||
gcc -Wall -Wextra -Wpedantic -std=c99 -O2 \
|
||||
$(pkg-config --cflags sdl2 2>/dev/null || echo "-I/usr/include/SDL2") \
|
||||
-c main.c -o /tmp/main.o
|
||||
fi
|
||||
}
|
||||
|
||||
# Check for common C issues
|
||||
check_common_issues() {
|
||||
print_step "Checking for common C issues..."
|
||||
|
||||
local issues=0
|
||||
|
||||
# Check for TODO/FIXME comments
|
||||
if grep -n "TODO\|FIXME\|XXX\|HACK" main.c 2>/dev/null; then
|
||||
print_warning "Found TODO/FIXME comments"
|
||||
issues=$((issues + 1))
|
||||
fi
|
||||
|
||||
# Check for potential buffer overflows
|
||||
if grep -n "strcpy\|strcat\|sprintf\|gets" main.c 2>/dev/null; then
|
||||
print_warning "Found potentially unsafe string functions"
|
||||
issues=$((issues + 1))
|
||||
fi
|
||||
|
||||
# Check for magic numbers (basic check)
|
||||
if grep -E "\b[0-9]{3,}\b" main.c | grep -v "printf\|#define" 2>/dev/null; then
|
||||
print_warning "Found potential magic numbers"
|
||||
issues=$((issues + 1))
|
||||
fi
|
||||
|
||||
if [ $issues -eq 0 ]; then
|
||||
print_success "No common issues found"
|
||||
fi
|
||||
}
|
||||
|
||||
# Main execution
|
||||
main() {
|
||||
echo -e "${BLUE}C Language Linter for imageViewer Project${NC}"
|
||||
echo "=========================================="
|
||||
echo
|
||||
|
||||
# Check if we're in the right directory
|
||||
if [ ! -f "main.c" ]; then
|
||||
print_error "main.c not found. Please run this script from the imageViewer directory."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
check_tools
|
||||
echo
|
||||
|
||||
compile_check
|
||||
echo
|
||||
|
||||
run_clang_tidy
|
||||
echo
|
||||
|
||||
run_cppcheck
|
||||
echo
|
||||
|
||||
check_formatting
|
||||
echo
|
||||
|
||||
check_common_issues
|
||||
echo
|
||||
|
||||
print_success "Linting completed!"
|
||||
echo
|
||||
echo -e "${BLUE}Available commands:${NC}"
|
||||
echo " ./lint.sh - Run all checks"
|
||||
echo " clang-format -i main.c - Fix formatting"
|
||||
echo " clang-tidy main.c --fix - Apply clang-tidy fixes"
|
||||
}
|
||||
|
||||
# Run main function
|
||||
main "$@"
|
||||
Loading…
Reference in New Issue
Block a user