Setting Up C++ Quality Gates in Cursor: 3 Automated Checks

C++ development demands rigorous quality control. This guide shows you how to set up three automated checks in Cursor that run before every commit: target builds, sanitizer builds, and static analysis with cppcheck. Combined with a Git pre-push hook, these checks ensure your code is always production-ready.
The Three Checks
Check 1: Target Build
Compiles your code with the same settings as your deployment environment.
Check 2: Sanitizer Build
Runs AddressSanitizer (ASan) and UndefinedBehaviorSanitizer (UBSan) to catch memory errors and undefined behavior.
Check 3: Static Analysis
Uses cppcheck to find bugs, style violations, and potential issues without running the code.
Prerequisites
Install required tools:
# Ubuntu/Debian
sudo apt-get update
sudo apt-get install -y build-essential cmake cppcheck
# macOS
brew install cmake cppcheck
# Windows (via MSYS2 or WSL)
pacman -S mingw-w64-x86_64-cmake mingw-w64-x86_64-cppcheck
Project Structure
my-cpp-project/
├── src/
│ ├── main.cpp
│ └── utils.cpp
├── include/
│ └── utils.h
├── tests/
│ └── test_main.cpp
├── CMakeLists.txt
├── .cursor/
│ └── quality-gate.sh
└── .git/hooks/
└── pre-push
Step 1: CMake Configuration
Create CMakeLists.txt with build profiles:
cmake_minimum_required(VERSION 3.14)
project(MyProject CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Source files
add_executable(myapp
src/main.cpp
src/utils.cpp
)
target_include_directories(myapp PRIVATE include)
# Target build profile (production-like)
add_library(target_profile INTERFACE)
target_compile_options(target_profile INTERFACE
-O2
-DNDEBUG
-Wall
-Wextra
-Werror
)
# Sanitizer build profile
add_library(sanitizer_profile INTERFACE)
target_compile_options(sanitizer_profile INTERFACE
-O1
-g
-fsanitize=address,undefined
-fno-omit-frame-pointer
)
target_link_options(sanitizer_profile INTERFACE
-fsanitize=address,undefined
)
# Apply profiles
option(USE_SANITIZER "Build with sanitizers" OFF)
if(USE_SANITIZER)
target_link_libraries(myapp sanitizer_profile)
else()
target_link_libraries(myapp target_profile)
endif()
# Testing
enable_testing()
add_executable(myapp_tests tests/test_main.cpp)
target_link_libraries(myapp_tests target_profile)
add_test(NAME unit_tests COMMAND myapp_tests)
Step 2: Quality Gate Script
Create .cursor/quality-gate.sh:
#!/bin/bash
set -e
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
BUILD_DIR="$PROJECT_ROOT/build"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo "=========================================="
echo " Cursor C++ Quality Gate"
echo "=========================================="
# Clean build directory
rm -rf "$BUILD_DIR"
mkdir -p "$BUILD_DIR"
# Check 1: Target Build
echo -e "\n${YELLOW}[1/3] Target Build${NC}"
echo "Building with production settings..."
mkdir -p "$BUILD_DIR/target"
cd "$BUILD_DIR/target"
cmake ../.. -DCMAKE_BUILD_TYPE=Release
make -j$(nproc)
echo -e "${GREEN}Target build: PASSED${NC}"
# Run tests
echo "Running tests..."
ctest --output-on-failure
echo -e "${GREEN}Tests: PASSED${NC}"
# Check 2: Sanitizer Build
echo -e "\n${YELLOW}[2/3] Sanitizer Build${NC}"
echo "Building with AddressSanitizer and UBSan..."
mkdir -p "$BUILD_DIR/sanitizer"
cd "$BUILD_DIR/sanitizer"
cmake ../.. -DCMAKE_BUILD_TYPE=Debug -DUSE_SANITIZER=ON
make -j$(nproc)
echo "Running with sanitizers..."
./myapp || {
echo -e "${RED}Sanitizer detected errors!${NC}"
exit 1
}
echo -e "${GREEN}Sanitizer build: PASSED${NC}"
# Check 3: Static Analysis
echo -e "\n${YELLOW}[3/3] Static Analysis (cppcheck)${NC}"
cd "$PROJECT_ROOT"
cppcheck \
--enable=all \
--suppress=missingIncludeSystem \
--error-exitcode=1 \
--inline-suppr \
--check-config \
-I include \
src/ \
tests/ \
2>&1 | tee "$BUILD_DIR/cppcheck-report.txt"
echo -e "${GREEN}Static analysis: PASSED${NC}"
# Summary
echo -e "\n=========================================="
echo -e "${GREEN}All quality gates passed!${NC}"
echo "=========================================="
echo "Build artifacts: $BUILD_DIR/"
echo "Report: $BUILD_DIR/cppcheck-report.txt"
Make it executable:
chmod +x .cursor/quality-gate.sh
Step 3: Git Pre-Push Hook
Create .git/hooks/pre-push:
#!/bin/bash
echo "Running quality gate before push..."
if [ -f ".cursor/quality-gate.sh" ]; then
bash .cursor/quality-gate.sh
exit $?
else
echo "Warning: Quality gate script not found"
exit 0
fi
Make it executable:
chmod +x .git/hooks/pre-push
Step 4: Cursor Integration
Add to Cursor Tasks
Create .vscode/tasks.json (Cursor uses VS Code task system):
{
"version": "2.0.0",
"tasks": [
{
"label": "C++ Quality Gate",
"type": "shell",
"command": "bash",
"args": [".cursor/quality-gate.sh"],
"group": {
"kind": "test",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
},
"problemMatcher": ["$gcc"]
},
{
"label": "Quick Build",
"type": "shell",
"command": "cmake",
"args": ["--build", "build/target", "--parallel"],
"group": "build"
},
{
"label": "Run Sanitizer Build",
"type": "shell",
"command": "bash",
"args": ["-c", "mkdir -p build/sanitizer && cd build/sanitizer && cmake ../.. -DUSE_SANITIZER=ON && make -j$(nproc) && ./myapp"],
"group": "test"
}
]
}
Keyboard Shortcuts
Add to .vscode/keybindings.json:
[
{
"key": "ctrl+shift+q",
"command": "workbench.action.tasks.runTask",
"args": "C++ Quality Gate"
}
]
Understanding Sanitizer Output
AddressSanitizer (ASan)
Detects:
- Use after free
- Heap buffer overflow
- Stack buffer overflow
- Global buffer overflow
- Use after return
- Use after scope
- Double-free
- Memory leaks
Example output:
==12345==ERROR: AddressSanitizer: heap-buffer-overflow
READ of size 4 at 0x602000000014 thread T0
#0 0x5555555551a9 in main src/main.cpp:15
UndefinedBehaviorSanitizer (UBSan)
Detects:
- Signed integer overflow
- Division by zero
- Null pointer dereference
- Misaligned pointer access
- Invalid shift operations
Example output:
src/utils.cpp:42:15: runtime error: signed integer overflow
Customizing cppcheck
Suppression Comments
In your code:
// cppcheck-suppress unusedFunction
void debugHelper() {
// This is used in debug builds only
}
// cppcheck-suppress knownConditionTrueFalse
if (x == x) { // Intentional NaN check
// handle NaN
}
Configuration File
Create cppcheck-suppressions.txt:
unusedFunction:*test*
missingIncludeSystem
unmatchedSuppression
Update the script:
cppcheck \
--enable=all \
--suppressions-list=cppcheck-suppressions.txt \
--error-exitcode=1 \
src/ tests/
CI/CD Integration
GitHub Actions
name: C++ Quality Gate
on: [push, pull_request]
jobs:
quality-gate:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y build-essential cmake cppcheck
- name: Run Quality Gate
run: bash .cursor/quality-gate.sh
Troubleshooting
| Issue | Solution |
|---|---|
| Sanitizer build too slow | Use -O1 instead of -O0 for faster builds |
| cppcheck false positives | Add suppression comments or config file |
| Hook not running | Ensure .git/hooks/pre-push is executable |
| Out of memory during build | Reduce parallel jobs: make -j2 instead of -j$(nproc) |
Quick Reference
# Run full quality gate
bash .cursor/quality-gate.sh
# Quick target build only
mkdir -p build && cd build && cmake .. && make -j$(nproc)
# Run with sanitizers
mkdir -p build-san && cd build-san && cmake .. -DUSE_SANITIZER=ON && make -j$(nproc) && ./myapp
# Run cppcheck only
cppcheck --enable=all --error-exitcode=1 src/