Skip to content

Extend clang-tidy with Google C++ style checks; add cpplint - #2965

Draft
kshaji3 wants to merge 4 commits into
borglab:developfrom
kshaji3:cpp-style-lint-ci
Draft

kshaji3 wants to merge 4 commits into
borglab:developfrom
kshaji3:cpp-style-lint-ci

Conversation

@kshaji3

@kshaji3 kshaji3 commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor
  • This PR adds a linter for clang-tidy checks that is based on the requirements outlined in copilot-instructions.md, which follows Google's C++ standards, with some additions.
  • This PR will only fail code that is freshly written, meaning that if you edit a part of the codebase that previously would have failed CI, you will have to edit it so that it now passes CI (which should be pretty light work).
  • Should run through GitHub Workflows.

You can run the CI checker locally from the command line with:

# Clang-Tidy (needs a compile_commands.json; see DEVELOP.md for the cmake invocation)
git diff --merge-base origin/develop --no-ext-diff --no-color --unified=0 \
  --diff-filter=AMR -- ':(glob)**/*.cpp' \
  ':(exclude)gtsam/3rdparty' ':(exclude)wrap' ':(exclude)CppUnitLite' |
  clang-tidy-diff.py -p1 -path build-tidy -j 2 -quiet \
    -regex '.*\.cpp' -only-check-in-db -warnings-as-errors '*'

# cpplint (no compile database needed)
git diff --merge-base origin/develop --no-ext-diff --no-color --unified=0 \
  --diff-filter=AMR -- ':(glob)**/*.cpp' ':(glob)**/*.h' ':(glob)**/*.hpp' \
  ':(exclude)gtsam/3rdparty' ':(exclude)wrap' ':(exclude)CppUnitLite' |
  python3 scripts/cpplint_diff.py

This is intended to be the first in a set of 3 PRs, which includes linting for .h and .py files

Shaji and others added 3 commits December 2, 2026 10:35
Enforces the parts of the Google C++ Style Guide
(https://google.github.io/styleguide/cppguide.html) that GTSAM's own
stated convention builds on (.github/copilot-instructions.md: "Classes
and types are UpperCamelCase; methods and functions are lowerCamelCase.
[...] Apart from those naming conventions, follow Google C++ style."),
on top of the existing clang-tidy-diff.py-based PR gating.

.clang-tidy: adds the google-* check family and
readability-identifier-naming, scoped to what was empirically validated
against a broad sample of real GTSAM files (Pose2, NoiseModel,
HessianFactor, LevenbergMarquardtParams/Optimizer, Values, FactorGraph,
Key) before committing to it -- several naming categories that looked
safe from a quick grep turned out to have pervasive, deliberate real
exceptions once checked more broadly, and are intentionally left
unenforced rather than fought with suppressions:
  - function/method/static-method casing: a genuine mix (named-
    constructor factories are deliberately UpperCamelCase; other
    methods are a real mix of UpperCamelCase and lowerCamelCase with no
    mechanical way to tell exception from debt)
  - parameter/variable casing: short mathematical notation (H, A, R, M,
    G11-G33) is load-bearing, confirmed in 40+ headers
  - typedefs/aliases: the nested "typedef shared_ptr<X> shared_ptr;"
    idiom appears in 140+ headers
  - template parameters: predominantly UPPER_CASE, not CamelCase
  - members: private/protected are usually lowerCamelCase_, but real
    snake_case counter-examples exist in core files
  - constants: the kCamelCase convention is real, but constant
    detection also false-positived on non-constant lambda captures
Kept: class/struct/enum/union type names (CamelCase), namespaces
(lower_case), enum constants (CamelCase, matching current code), and
macros (UPPER_CASE) -- the categories with no counter-examples found.
google-runtime-references and google-default-arguments are disabled for
the same reason (non-const Jacobian out-params and print()'s default
argument are pervasive, intentional GTSAM idioms, not oversights).

CPPLINT.cfg + scripts/cpplint_diff.py: adds cpplint (Google's own
reference linter for the style guide) as a second check, since it
catches things clang-tidy's config above doesn't attempt (explicit
single-arg constructors, redundant virtual+override, include-what-you-
use, namespace-closing comments). cpplint_diff.py mirrors the existing
clang-tidy-diff.py-based gating: it filters cpplint's output down to
diagnostics on lines actually added in the diff, so this doesn't
surface GTSAM's pre-existing style backlog on every PR that happens to
touch an old file. Covered by scripts/test_cpplint_diff.py.

.github/workflows/code-quality.yml: wires cpplint_diff.py in as a new
job alongside the existing clang-tidy one, and widens the existing
"Test quality checker" step's discovery pattern from the single
hardcoded test_code_quality.py to test_*.py so new test files (like
test_cpplint_diff.py) are picked up automatically. The cpplint job's
diff-identification step mirrors clang-tidy's push/PR/nightly-aware
base resolution (rebased on top of "Gate PR and nightly CI on both
code-quality checks", which turned this workflow into a workflow_call
callee and replaced clang-tidy's old "HEAD^1 HEAD" PR-only diffing with
logic that also handles develop pushes and nightly runs correctly);
cpplint now gets the same treatment instead of keeping the old
PR-only assumption.

All of the above was validated against this repo before committing:
ran the real pinned clang-tidy (23.1.4) and cpplint (2.0.2) versions
against real compile_commands.json output and real files, including an
end-to-end smoke test of the diff-filtering pipeline with a synthetic
violation, and the full existing + new Python test suite.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
PR CI's new cpplint job (code-quality.yml) had no local-terminal
equivalent, unlike the existing "Opt-in local Clang-Tidy" section --
a developer had to either wait for CI or manually re-derive the
cpplint_diff.py invocation. Adds a matching "Opt-in local cpplint"
section with the same structure: install the pinned tool version,
then run scripts/cpplint_diff.py against the merge-base diff with
origin/develop, exactly like CI does. Unlike Clang-Tidy this needs no
compilation database, so setup is just a pip install.

Verified end-to-end with a synthetic violation (single-arg constructor
not marked explicit): the documented command found exactly that one
finding, confirming the diff-scoping stays correct outside of CI too.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Per explicit direction: enforce naming uniformly going forward rather
than carving out permanent exceptions for every category that doesn't
fully conform yet. This WILL surface real failures once a future PR
touches a non-conforming line (expected -- this check is diff-scoped,
so nothing about the existing codebase fails until it's touched again,
and the backlog gets fixed incrementally rather than needing one big
retroactive rename pass).

Re-enables, with GTSAM's stated conventions as the target: Function/
Method/ClassMethodCase (camelBack), Parameter/VariableCase (camelBack),
Typedef/TypeAliasCase (CamelCase), TemplateParameterCase (CamelCase),
Private/Protected/PublicMemberCase (camelBack, trailing underscore for
private/protected), and Constant/GlobalConstant/StaticConstantCase
(kCamelCase). The known, confirmed conflicts documented in the previous
commit (named-constructor factories, math-notation parameters, the
nested shared_ptr typedef idiom, UPPER_CASE template parameters,
snake_case member counter-examples, and constant-detection false
positives on lambda captures) are kept as inline documentation -- now
framed as an expected follow-up punch list instead of a justification
for leaving them off.

Verified this PR's own diff (config/docs/scripts only, no C++ source)
stays clean under clang-tidy-diff.py and cpplint_diff.py, and confirmed
end to end that touching a real pre-existing non-conforming line (e.g.
NoiseModel::sqrt_information_) does get flagged as expected through the
actual CI mechanism, not just a bare clang-tidy invocation.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@dellaert

dellaert commented Dec 2, 2026

Copy link
Copy Markdown
Member

Please add a section in pr description written entirely by you

@kshaji3

kshaji3 commented Dec 2, 2026

Copy link
Copy Markdown
Contributor Author

Done

Three more copilot-instructions.md rules, implemented to the letter
regardless of current conformance -- existing violations are either
grandfathered nowhere (the full-repo check) or will surface the next
time a touched line triggers them (the diff-scoped checks), per
explicit direction rather than watered down to match current practice.

.clang-tidy: adds readability-function-cognitive-complexity at its
default threshold (25), implementing "flag overly complex or long
functions and recommend breaking them into smaller functions."
Validated against real files before enabling (e.g. flagged
LevenbergMarquardtOptimizer::tryLambda at complexity 77) without being
so sensitive it flags ordinary functions.

scripts/check_code_quality.py (+ its test): adds detection of bare
[[deprecated]] attributes, implementing "Use GTSAM's versioned
deprecation mechanism rather than adding a standalone [[deprecated]]
attribute." This check runs unconditionally over the whole repo (like
the rest of this script, unlike the diff-scoped clang-tidy/cpplint
jobs), and there currently are 3 real violations across
gtsam/base/MatrixLieGroup.h and gtsam/navigation/AttitudeFactor.h --
left as real, unlisted failures (no exception list, unlike this
script's existing grandfathered violations) for a follow-up to fix,
since this one wasn't carved out.

scripts/cpplint_diff.py (+ its test): adds two more GTSAM-specific
diff-scoped checks neither clang-tidy nor cpplint cover, implementing
"Use /// for single-line documentation comments rather than /** */"
(477 existing files don't conform) and "Put at least one short //
comment immediately above every TEST() explaining the behavior being
verified" (1885 of 3977 existing TEST()s don't). Both are diff-scoped
rather than added to check_code_quality.py given that volume -- the
full-repo mechanism is reserved for rules with few enough pre-existing
violations that failing immediately and unconditionally is reasonable
(like the deprecated-attribute rule above); these follow the same
incremental-enforcement model as the identifier-naming checks already
in .clang-tidy.

Also simplified cpplint_diff.py's diff parsing while extending it:
instead of hand-walking +/-/context markers to track exact added-line
numbers, it now extracts each hunk's (start, end) range straight from
the "@@ ... +start,count @@" header, the same way clang-tidy-diff.py
(already a dependency) does it. Since this project's documented
commands always use "--unified=0" (zero context lines), a hunk's range
*is* exactly its added lines here, so this loses no precision while
removing the need to classify each line in the diff body.

No new CI job, no new local command: everything above runs through
the exact same two jobs (clang-tidy, cpplint) and the exact same three
documented command lines (clang-tidy-diff.py, cpplint_diff.py,
check_code_quality.py) as before.

Verified before committing: the full 21-test suite passes; this PR's
own diff (still config/scripts only) stays clean under all three
mechanisms; deliberately touching real pre-existing violations (a
missing-explicit constructor, a single-line Doxygen comment, a TEST()
without a comment, in one synthetic file) surfaces findings from all
three new checks correctly through the diff-scoped pipeline; and
running check_code_quality.py against the real repo surfaces exactly
the 3 known [[deprecated]] violations, nothing else.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants