Skip to content

Commit

Permalink
[lint] Identify mismatched clang-format-{on,off} comments (RobotLocom…
Browse files Browse the repository at this point in the history
  • Loading branch information
jwnimmer-tri authored Oct 18, 2023
1 parent 4d895b5 commit 696f61e
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 18 deletions.
2 changes: 0 additions & 2 deletions common/symbolic/expression/test/substitution_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,8 @@ TEST_F(SymbolicSubstitutionTest, CheckHomomorphismFormulaVarExpr) {
fns.push_back([&](const Expression& x) { return !fns[15](x); });
fns.push_back([&](const Expression& x) {
Eigen::Matrix<Expression, 2, 2> m;
// clang-format off
m << (x * y_), 2.0,
2.0, (x * y_);
// clang-format on
return positive_semidefinite(m);
});
// clang-format on
Expand Down
6 changes: 3 additions & 3 deletions geometry/optimization/test/affine_subspace_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ GTEST_TEST(AffineSubspaceTest, EqualityTest) {
basis1 << 1, 0,
0, 1,
0, 0;
// clang- format on
// clang-format on
Eigen::VectorXd translation1(3);
translation1 << 0, 0, 1;

Expand Down Expand Up @@ -1112,13 +1112,13 @@ GTEST_TEST(AffineSubspaceTest, EqualityTest2) {
basis1 << -1, 0.5,
0.5, -1,
0.5, 0.5;
// clang- format on
// clang-format on
Eigen::Matrix<double, 3, 2> basis2;
// clang-format off
basis2 << 1, 0,
-1, 1,
0, -1;
// clang- format on
// clang-format on
Eigen::VectorXd translation1(3);
translation1 << 0, 0, 1;
Eigen::VectorXd translation2(3);
Expand Down
11 changes: 4 additions & 7 deletions geometry/optimization/test/hpolyhedron_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,6 @@ GTEST_TEST(HPolyhedronTest, IrredundantBallIntersectionContainsBothOriginal) {
HPolyhedron L1_ball = HPolyhedron::MakeL1Ball(3);
HPolyhedron Linfty_ball = HPolyhedron::MakeUnitBox(3);

// clang-format on
HPolyhedron IrredL1intoLinf = Linfty_ball.Intersection(L1_ball, true);
HPolyhedron IrredLinfintoL1 = L1_ball.Intersection(Linfty_ball, true);

Expand Down Expand Up @@ -905,7 +904,7 @@ GTEST_TEST(HPolyhedronTest, ReduceToInfeasibleSet) {
0, 1, -1,
0, 0, -1;
b << 1, -1, -1, -1, 0;
// clang-format off
// clang-format on

HPolyhedron H{A, b};
HPolyhedron H_reduced = H.ReduceInequalities();
Expand Down Expand Up @@ -938,9 +937,9 @@ GTEST_TEST(HPolyhedronTest, IsEmptyNonMinimalInequalitySet) {
0, 0, -1,
1, -1, 0,
-1, 0, 1,
0, 1, -1;;
b << 1, 0, -1, -1, -1;;
// clang-format off
0, 1, -1;
b << 1, 0, -1, -1, -1;
// clang-format on

HPolyhedron H{A, b};
EXPECT_TRUE(H.IsEmpty());
Expand All @@ -960,8 +959,6 @@ GTEST_TEST(HPolyhedronTest, IsEmptyBoundedHPolyhedron) {
EXPECT_FALSE(H.IsEmpty());
}



GTEST_TEST(HPolyhedronTest, IntersectionTest) {
HPolyhedron H_A = HPolyhedron::MakeUnitBox(2);
HPolyhedron H_B = HPolyhedron::MakeBox(Vector2d(0, 0), Vector2d(2, 2));
Expand Down
2 changes: 1 addition & 1 deletion geometry/optimization/test/intersection_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ GTEST_TEST(IntersectionTest, EmptyIntersectionTest1) {
0, 1, -1,
0, 0, -1;
b << 1, -1, -1, -1, 0;
// clang-format off
// clang-format on

HPolyhedron H1{A, b};
const Point P1(Vector3d{0.1, 1.2, 0.3});
Expand Down
2 changes: 1 addition & 1 deletion solvers/test/complementary_problem_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ GTEST_TEST(TestComplementaryProblem, flp2) {
0, 0, 0, 0,
8.0 / 3, 2, 2, 8.0/3,
2, 5.0/4, 5.0/4, 2;
// clang-format off;
// clang-format on
Eigen::Vector4d q(0, 0, -36, -25);
prog.AddLinearComplementarityConstraint(M, q, {x, y});
prog.AddBoundingBoxConstraint(0, 10, x);
Expand Down
2 changes: 0 additions & 2 deletions solvers/test/mathematical_program_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2666,9 +2666,7 @@ TEST_F(SymbolicLorentzConeTest, Test3) {
// [ x(2)]
// [ -x(1) ]
Matrix<Expression, 4, 1> e;
// clang-format on
e << 2 * x_(0) + 3 * x_(2), -x_(0) + 2 * x_(2), +x_(2), -x_(1);
// clang-format off;
CheckParsedSymbolicLorentzConeConstraint(&prog_, e);
}

Expand Down
2 changes: 0 additions & 2 deletions systems/controllers/test/linear_quadratic_regulator_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,7 @@ GTEST_TEST(TestLqr, ConstrainedLinearSystem) {
3, 10;
// clang-format on
Eigen::Matrix<double, 3, 2> N;
// clang-format on
N << 1, 0.5, 0.5, -1, 0, 1;
// clang-format off

TestLqrWithHjb(A, B, Q, R);
TestLqrWithHjb(A, B, Q, R, N);
Expand Down
41 changes: 41 additions & 0 deletions tools/lint/drakelint.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,46 @@ def _check_iostream(filename):
return 1


def _check_clang_format_toggles(filename):
"""Checks that clang-format-{off,on} are correctly paired up.
"""
# These are the needles we'll be looking for.
offs = [
"// clang-format off\n",
"// clang-format off ",
"/* clang-format off */",
"/* clang-format off to disable clang-format-includes */"
]
ons = [
"// clang-format on\n",
"// clang-format on ",
"/* clang-format on */",
]

with open(filename, mode='r', encoding='utf-8') as file:
lines = file.readlines()
enabled = True
num_errors = 0
for i, line in enumerate(lines):
line = line + "\n"
found_on = any([x in line for x in ons])
found_off = any([x in line for x in offs])
if found_on:
if enabled:
print(f"ERROR: {filename}:{i + 1}: "
"This line is redundant; clang-format is already on")
num_errors += 1
enabled = True
if found_off:
if not enabled:
print(f"ERROR: {filename}:{i + 1}: "
"This line is redundant; clang-format is already off")
num_errors += 1
enabled = False

return num_errors


def main():
"""Run Drake lint checks on each path specified as a command-line argument.
Exit 1 if any of the paths are invalid or any lint checks fail.
Expand All @@ -182,6 +222,7 @@ def main():
total_errors += _check_includes(filename)
total_errors += _check_unguarded_openmp_uses(filename)
total_errors += _check_iostream(filename)
total_errors += _check_clang_format_toggles(filename)

if total_errors == 0:
sys.exit(0)
Expand Down

0 comments on commit 696f61e

Please sign in to comment.