cpplint
Cpplint is an actively maintained, open-source command-line tool for static analysis of C/C++ source files, primarily used to enforce adherence to Google's C++ style guide. Version 2.0.2 is the latest stable release, with frequent updates incorporating modern C++ standards and Python environment compatibility.
Warnings
- breaking Python 2 and Python 3.7 are no longer supported. Users must upgrade to Python 3.8 or newer.
- breaking The `setup.py lint` subcommand was removed. Direct invocation of `cpplint` commands is now required.
- gotcha As of version 2.0.2, non-const references are no longer erred on by default. This changes previous behavior where they might have been flagged as style violations.
- gotcha cpplint strictly enforces Google's C++ Style Guide, which may not align with all project or team-specific style preferences.
- gotcha cpplint is rules-based and uses heuristics, which can lead to occasional false positives or negatives. It is not a substitute for a comprehensive code review.
Install
-
pip install cpplint
Quickstart
# Create a dummy C++ file for linting
with open("main.cpp", "w") as f:
f.write("""#include <iostream>\n\nint main() {\n std::cout << "Hello, World!" << std::endl; // Missing space before '!'\n return 0;\n}\n""")
# Run cpplint on the file
# This is primarily a command-line tool, so we use subprocess
import subprocess
import os
try:
# cpplint usually exits with non-zero on errors, so capture output
# Using --filter to ignore common line_length warnings for this example
result = subprocess.run(
["cpplint", "--filter=-whitespace/line_length", "main.cpp"],
capture_output=True,
text=True,
check=False # Don't raise CalledProcessError for non-zero exit codes
)
print("cpplint output (stdout):")
print(result.stdout)
print("\ncpplint output (stderr):")
print(result.stderr)
if result.returncode != 0:
print("\nLinting found issues. Return code:", result.returncode)
else:
print("\nNo linting issues found (or filtered out).")
except FileNotFoundError:
print("Error: cpplint command not found. Ensure it's installed and in your PATH.")
finally:
# Clean up the dummy file
if os.path.exists("main.cpp"):
os.remove("main.cpp")