diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000000..2878339187 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,9 @@ +Checks: 'clang-diagnostic-*,clang-analyzer-*,*, + ,-cppcoreguidelines-pro-type-vararg + ,-cppcoreguidelines-pro-bounds-array-to-pointer-decay + ,-cppcoreguidelines-pro-bounds-constant-array-index + ,-cppcoreguidelines-pro-bounds-pointer-arithmetic + ' +WarningsAsErrors: '*' +HeaderFilterRegex: '*.h, *.hpp, *.hh, *.hxx' + diff --git a/CMakeLists.txt b/CMakeLists.txt index 1dc32ced5d..788988e6c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,9 @@ set(VERSION_PATCH "2") project(matrix CXX) +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + if (NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Build type" FORCE) message(STATUS "set build type to ${CMAKE_BUILD_TYPE}") @@ -65,7 +68,6 @@ add_compile_options( -Wsign-conversion -Wsign-promo -Wstrict-null-sentinel - -Wstrict-overflow=5 -Wswitch-default -Wundef -Wuninitialized @@ -74,7 +76,14 @@ add_compile_options( # clang tolerate unknown gcc options if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") - add_compile_options(-Wno-error=unused-command-line-argument-hard-error-in-future -Wno-unknown-warning-option) + add_compile_options( + -Wno-error=unused-command-line-argument-hard-error-in-future + -Wno-unknown-warning-option + ) +else() + add_compile_options( + -Wstrict-overflow=5 + ) endif() add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure) @@ -83,6 +92,9 @@ if(TESTING) enable_testing() add_subdirectory(test) add_dependencies(check test_build) + + add_custom_target(clang-tidy COMMAND clang-tidy -p . ${CMAKE_SOURCE_DIR}/test/*.cpp) + add_dependencies(clang-tidy test_build) endif() if(FORMAT) diff --git a/matrix/AxisAngle.hpp b/matrix/AxisAngle.hpp index ab945379d6..5a6cc4af9f 100644 --- a/matrix/AxisAngle.hpp +++ b/matrix/AxisAngle.hpp @@ -31,7 +31,7 @@ template class AxisAngle : public Vector { public: - virtual ~AxisAngle() {}; + ~AxisAngle() override = default; typedef Matrix Matrix31; diff --git a/matrix/Dcm.hpp b/matrix/Dcm.hpp index ba61d66a54..c418a8a30b 100644 --- a/matrix/Dcm.hpp +++ b/matrix/Dcm.hpp @@ -17,7 +17,6 @@ #include "math.hpp" - namespace matrix { @@ -30,7 +29,6 @@ class Euler; template class AxisAngle; - /** * Direction cosine matrix class * diff --git a/matrix/Matrix.hpp b/matrix/Matrix.hpp index dab1b65d30..0d3fd48e38 100644 --- a/matrix/Matrix.hpp +++ b/matrix/Matrix.hpp @@ -8,11 +8,10 @@ #pragma once -#include -#include -#include -#include -#include +#include +#include +#include + #if defined(SUPPORT_STDIOSTREAM) #include #include @@ -36,25 +35,20 @@ public: virtual ~Matrix() {}; - Matrix() : - _data() - { - } + // Constructors + Matrix() : _data() {} - Matrix(const Type data_[][N]) : - _data() + Matrix(const Type data_[][N]) : _data() { memcpy(_data, data_, sizeof(_data)); } - Matrix(const Type *data_) : - _data() + Matrix(const Type *data_) : _data() { memcpy(_data, data_, sizeof(_data)); } - Matrix(const Matrix &other) : - _data() + Matrix(const Matrix &other) : _data() { memcpy(_data, other._data, sizeof(_data)); } @@ -519,7 +513,7 @@ bool isEqualF(Type x, bool equal = true; - if (fabsf(x - y) > eps) { + if (fabs(x - y) > eps) { equal = false; } diff --git a/matrix/Scalar.hpp b/matrix/Scalar.hpp index 86025dfc0f..ca3743d328 100644 --- a/matrix/Scalar.hpp +++ b/matrix/Scalar.hpp @@ -8,12 +8,6 @@ #pragma once -#include -#include -#include -#include -#include - #include "math.hpp" namespace matrix diff --git a/matrix/SquareMatrix.hpp b/matrix/SquareMatrix.hpp index 32fb43ca9b..f1a5a9b134 100644 --- a/matrix/SquareMatrix.hpp +++ b/matrix/SquareMatrix.hpp @@ -8,12 +8,6 @@ #pragma once -#include -#include -#include -#include -#include - #include "math.hpp" #include "helper_functions.hpp" diff --git a/matrix/Vector.hpp b/matrix/Vector.hpp index 61382d7548..9265b2a223 100644 --- a/matrix/Vector.hpp +++ b/matrix/Vector.hpp @@ -22,7 +22,7 @@ template class Vector : public Matrix { public: - virtual ~Vector() {}; + ~Vector() override = default; typedef Matrix MatrixM1; diff --git a/matrix/helper_functions.hpp b/matrix/helper_functions.hpp index 79d57504c7..f501598f9b 100644 --- a/matrix/helper_functions.hpp +++ b/matrix/helper_functions.hpp @@ -1,14 +1,7 @@ #pragma once #include "math.hpp" - -// grody hack - this should go once C++11 is supported -// on all platforms. -#if defined (__PX4_NUTTX) || defined (__PX4_QURT) -#include -#else #include -#endif namespace matrix { diff --git a/test/attitude.cpp b/test/attitude.cpp index 460a3c10b5..49b47b3807 100644 --- a/test/attitude.cpp +++ b/test/attitude.cpp @@ -1,21 +1,20 @@ -#include -#include - -#include #include "test_macros.hpp" +#include -using namespace matrix; - -// important to list all classes here for coverage -template class Quaternion; -template class Euler; -template class Dcm; -template class AxisAngle; -template class Scalar; -template class SquareMatrix; -template class Vector; -template class Vector2; -template class Vector3; +using matrix::AxisAnglef; +using matrix::Dcm; +using matrix::Dcmf; +using matrix::Euler; +using matrix::Eulerf; +using matrix::eye; +using matrix::isEqualF; +using matrix::Matrix; +using matrix::Quaternion; +using matrix::Quatf; +using matrix::SquareMatrix; +using matrix::Vector3f; +using matrix::Vector; +using matrix::zeros; int main() { @@ -51,10 +50,10 @@ int main() // quaternion ctor Quatf q0(1, 2, 3, 4); Quatf q(q0); - TEST(fabs(q(0) - 1) < eps); - TEST(fabs(q(1) - 2) < eps); - TEST(fabs(q(2) - 3) < eps); - TEST(fabs(q(3) - 4) < eps); + TEST(fabsf(q(0) - 1) < eps); + TEST(fabsf(q(1) - 2) < eps); + TEST(fabsf(q(2) - 3) < eps); + TEST(fabsf(q(3) - 4) < eps); // quat normalization q.normalize(); @@ -102,10 +101,12 @@ int main() for (size_t i = 0; i < 1000; i++) { A = R * A; } + A.renormalize(); float err = 0.0f; - for (size_t row = 0; row < 3; row++) { - matrix::Vector3f rvec(A._data[row]); + + for (auto & row : A._data) { + Vector3f rvec(row); err += fabsf(1.0f - rvec.length()); } TEST(err < eps); diff --git a/test/filter.cpp b/test/filter.cpp index 368b788f7b..c5e6d2ca77 100644 --- a/test/filter.cpp +++ b/test/filter.cpp @@ -1,9 +1,11 @@ -#include - -#include #include "test_macros.hpp" +#include -using namespace matrix; +using matrix::Matrix; +using matrix::kalman_correct; +using matrix::eye; +using matrix::SquareMatrix; +using matrix::Vector; int main() { diff --git a/test/hatvee.cpp b/test/hatvee.cpp index c79a2b4184..6250621a24 100644 --- a/test/hatvee.cpp +++ b/test/hatvee.cpp @@ -1,11 +1,10 @@ -#include #include "test_macros.hpp" - #include -using namespace matrix; - -template class SquareMatrix; +using matrix::Dcm; +using matrix::Euler; +using matrix::isEqual; +using matrix::Vector3; int main() { diff --git a/test/helper.cpp b/test/helper.cpp index e4787fc3a2..e280f5238f 100644 --- a/test/helper.cpp +++ b/test/helper.cpp @@ -1,10 +1,10 @@ -#include - -#include #include "test_macros.hpp" +#include -using namespace matrix; - +using matrix::isEqual; +using matrix::isEqualF; +using matrix::Vector3f; +using matrix::wrap_pi; int main() { diff --git a/test/integration.cpp b/test/integration.cpp index abbfe885b3..ec4cf80230 100644 --- a/test/integration.cpp +++ b/test/integration.cpp @@ -1,13 +1,13 @@ -#include - -#include #include "test_macros.hpp" +#include -using namespace matrix; +using matrix::Matrix; +using matrix::ones; +using matrix::Vector; -Vector f(float t, const Matrix & y, const Matrix & u); +Vector f(float t, const Matrix & /*y*/, const Matrix & /*u*/); -Vector f(float t, const Matrix & y, const Matrix & u) { +Vector f(float t, const Matrix & /*y*/, const Matrix & /*u*/) { float v = -sinf(t); return v*ones(); } diff --git a/test/inverse.cpp b/test/inverse.cpp index 69808b4af1..92aaa70a03 100644 --- a/test/inverse.cpp +++ b/test/inverse.cpp @@ -1,15 +1,11 @@ -#include - -#include #include "test_macros.hpp" +#include -using namespace matrix; +using matrix::SquareMatrix; +using matrix::zeros; static const size_t n_large = 50; -template class SquareMatrix; -template class SquareMatrix; - int main() { float data[9] = {0, 2, 3, diff --git a/test/matrixAssignment.cpp b/test/matrixAssignment.cpp index 1cba50f097..6f18d36ee7 100644 --- a/test/matrixAssignment.cpp +++ b/test/matrixAssignment.cpp @@ -1,9 +1,11 @@ -#include #include "test_macros.hpp" +#include -using namespace matrix; - -template class Matrix; +using matrix::Matrix; +using matrix::Matrix3f; +using matrix::Scalar; +using matrix::Vector; +using matrix::Vector2f; int main() { @@ -24,7 +26,7 @@ int main() Matrix3f m2(data); for(int i=0; i<9; i++) { - TEST(fabs(data[i] - m2.data()[i]) < 1e-6f); + TEST(fabsf(data[i] - m2.data()[i]) < 1e-6f); } float data2d[3][3] = { @@ -34,7 +36,7 @@ int main() }; m2 = Matrix3f(data2d); for(int i=0; i<9; i++) { - TEST(fabs(data[i] - m2.data()[i]) < 1e-6f); + TEST(fabsf(data[i] - m2.data()[i]) < 1e-6f); } float data_times_2[9] = {2, 4, 6, 8, 10, 12, 14, 16, 18}; @@ -96,17 +98,17 @@ int main() m4.swapCols(2, 2); TEST(isEqual(m4, Matrix3f(data))); - TEST(fabs(m4.min() - 1) < 1e-5); - TEST(fabs((-m4).min() + 9) < 1e-5); + TEST(fabsf(m4.min() - 1) < 1e-5); + TEST(fabsf((-m4).min() + 9) < 1e-5); Scalar s; s = 1; const Vector & s_vect = s; - TEST(fabs(s - 1) < 1e-5); - TEST(fabs(s_vect(0) - 1.0f) < 1e-5); + TEST(fabsf(s - 1) < 1e-5); + TEST(fabsf(s_vect(0) - 1.0f) < 1e-5); Matrix m5 = s; - TEST(fabs(m5(0,0) - s) < 1e-5); + TEST(fabsf(m5(0,0) - s) < 1e-5); Matrix m6; m6.setRow(0, Vector2f(1, 2)); diff --git a/test/matrixMult.cpp b/test/matrixMult.cpp index ecf9d9b3af..97caa1532d 100644 --- a/test/matrixMult.cpp +++ b/test/matrixMult.cpp @@ -1,9 +1,8 @@ -#include - -#include #include "test_macros.hpp" +#include -using namespace matrix; +using matrix::Matrix3f; +using matrix::eye; int main() { diff --git a/test/matrixScalarMult.cpp b/test/matrixScalarMult.cpp index 147459d42b..925dbd95a6 100644 --- a/test/matrixScalarMult.cpp +++ b/test/matrixScalarMult.cpp @@ -1,9 +1,8 @@ -#include +#include "test_macros.hpp" #include -#include "test_macros.hpp" -using namespace matrix; +using matrix::Matrix3f; int main() { diff --git a/test/setIdentity.cpp b/test/setIdentity.cpp index f266d62732..fc48ed41a2 100644 --- a/test/setIdentity.cpp +++ b/test/setIdentity.cpp @@ -1,9 +1,7 @@ -#include #include "test_macros.hpp" +#include -using namespace matrix; - -template class Matrix; +using matrix::Matrix3f; int main() { @@ -13,10 +11,10 @@ int main() for (size_t i = 0; i < 3; i++) { for (size_t j = 0; j < 3; j++) { if (i == j) { - TEST( fabs(A(i, j) - 1) < 1e-7); + TEST(fabsf(A(i, j) - 1) < 1e-7); } else { - TEST( fabs(A(i, j) - 0) < 1e-7); + TEST(fabsf(A(i, j) - 0) < 1e-7); } } } @@ -27,13 +25,14 @@ int main() for (size_t i = 0; i < 3; i++) { for (size_t j = 0; j < 3; j++) { if (i == j) { - TEST( fabs(B(i, j) - 1) < 1e-7); + TEST(fabsf(B(i, j) - 1) < 1e-7); } else { - TEST( fabs(B(i, j) - 0) < 1e-7); + TEST(fabsf(B(i, j) - 0) < 1e-7); } } } + return 0; } diff --git a/test/slice.cpp b/test/slice.cpp index 36f13f504b..ba44980f1c 100644 --- a/test/slice.cpp +++ b/test/slice.cpp @@ -1,9 +1,8 @@ -#include - -#include #include "test_macros.hpp" +#include -using namespace matrix; +using matrix::Matrix; +using matrix::SquareMatrix; int main() { diff --git a/test/squareMatrix.cpp b/test/squareMatrix.cpp index ff87a3b8be..0ecb26874b 100644 --- a/test/squareMatrix.cpp +++ b/test/squareMatrix.cpp @@ -1,11 +1,9 @@ -#include #include "test_macros.hpp" #include -using namespace matrix; - -template class SquareMatrix; +using matrix::SquareMatrix; +using matrix::Vector3; int main() { diff --git a/test/transpose.cpp b/test/transpose.cpp index 9da5e32d98..350c626070 100644 --- a/test/transpose.cpp +++ b/test/transpose.cpp @@ -1,12 +1,8 @@ -#include - -#include #include "test_macros.hpp" -using namespace matrix; +#include -template class Matrix; -template class Matrix; +using matrix::Matrix; int main() { @@ -16,6 +12,7 @@ int main() float data_check[6] = {1, 4, 2, 5, 3, 6}; Matrix A_T_check(data_check); TEST(isEqual(A_T, A_T_check)); + return 0; } diff --git a/test/vector.cpp b/test/vector.cpp index 66aa109e2e..8bb71c997e 100644 --- a/test/vector.cpp +++ b/test/vector.cpp @@ -1,11 +1,9 @@ -#include - -#include #include "test_macros.hpp" -using namespace matrix; +#include -template class Vector; +using matrix::Vector; +using matrix::isEqualF; int main() { diff --git a/test/vector2.cpp b/test/vector2.cpp index a0625a7ee7..399b301456 100644 --- a/test/vector2.cpp +++ b/test/vector2.cpp @@ -1,35 +1,34 @@ -#include + #include #include "test_macros.hpp" -using namespace matrix; - -template class Vector; +using matrix::Vector2f; +using matrix::Matrix; int main() { Vector2f a(1, 0); Vector2f b(0, 1); - TEST(fabs(a % b - 1.0f) < 1e-5); + TEST(fabsf(a % b - 1.0f) < 1e-5); Vector2f c; - TEST(fabs(c(0) - 0) < 1e-5); - TEST(fabs(c(1) - 0) < 1e-5); + TEST(fabsf(c(0) - 0) < 1e-5); + TEST(fabsf(c(1) - 0) < 1e-5); Matrix d(a); - TEST(fabs(d(0,0) - 1) < 1e-5); - TEST(fabs(d(1,0) - 0) < 1e-5); + TEST(fabsf(d(0,0) - 1) < 1e-5); + TEST(fabsf(d(1,0) - 0) < 1e-5); Vector2f e(d); - TEST(fabs(e(0) - 1) < 1e-5); - TEST(fabs(e(1) - 0) < 1e-5); + TEST(fabsf(e(0) - 1) < 1e-5); + TEST(fabsf(e(1) - 0) < 1e-5); float data[] = {4,5}; Vector2f f(data); - TEST(fabs(f(0) - 4) < 1e-5); - TEST(fabs(f(1) - 5) < 1e-5); + TEST(fabsf(f(0) - 4) < 1e-5); + TEST(fabsf(f(1) - 5) < 1e-5); return 0; } diff --git a/test/vector3.cpp b/test/vector3.cpp index 611eef9887..8bb76a978d 100644 --- a/test/vector3.cpp +++ b/test/vector3.cpp @@ -1,34 +1,33 @@ -#include - -#include #include "test_macros.hpp" -using namespace matrix; +#include -template class Vector; +using matrix::Vector3f; +using matrix::Matrix; int main() { Vector3f a(1, 0, 0); Vector3f b(0, 1, 0); Vector3f c = a.cross(b); - TEST(isEqual(c, Vector3f(0,0,1))); + TEST(matrix::isEqual(c, Vector3f(0,0,1))); c = a % b; - TEST (isEqual(c, Vector3f(0,0,1))); + TEST(matrix::isEqual(c, Vector3f(0,0,1))); Matrix d(c); Vector3f e(d); - TEST (isEqual(e, d)); + TEST (matrix::isEqual(e, d)); float data[] = {4, 5, 6}; Vector3f f(data); - TEST(isEqual(f, Vector3f(4, 5, 6))); + TEST(matrix::isEqual(f, Vector3f(4, 5, 6))); + + TEST(matrix::isEqual(a + b, Vector3f(1, 1, 0))); + TEST(matrix::isEqual(a - b, Vector3f(1, -1, 0))); + TEST(matrix::isEqualF(a * b, 0.0f)); + TEST(matrix::isEqual(-a, Vector3f(-1, 0, 0))); + TEST(matrix::isEqual(a.unit(), a)); + TEST(matrix::isEqual(a.unit(), a.normalized())); + TEST(matrix::isEqual(a*2.0, Vector3f(2, 0, 0))); - TEST(isEqual(a + b, Vector3f(1, 1, 0))); - TEST(isEqual(a - b, Vector3f(1, -1, 0))); - TEST(isEqualF(a * b, 0.0f)); - TEST(isEqual(-a, Vector3f(-1, 0, 0))); - TEST(isEqual(a.unit(), a)); - TEST(isEqual(a.unit(), a.normalized())); - TEST(isEqual(a*2.0, Vector3f(2, 0, 0))); return 0; } diff --git a/test/vectorAssignment.cpp b/test/vectorAssignment.cpp index 215ba0a62b..1f78211965 100644 --- a/test/vectorAssignment.cpp +++ b/test/vectorAssignment.cpp @@ -2,9 +2,8 @@ #include "test_macros.hpp" -using namespace matrix; - -template class Vector; +using matrix::SquareMatrix; +using matrix::Vector3f; int main() { @@ -15,20 +14,20 @@ int main() static const float eps = 1e-7f; - TEST(fabs(v(0) - 1) < eps); - TEST(fabs(v(1) - 2) < eps); - TEST(fabs(v(2) - 3) < eps); + TEST(fabsf(v(0) - 1) < eps); + TEST(fabsf(v(1) - 2) < eps); + TEST(fabsf(v(2) - 3) < eps); Vector3f v2(4, 5, 6); - TEST(fabs(v2(0) - 4) < eps); - TEST(fabs(v2(1) - 5) < eps); - TEST(fabs(v2(2) - 6) < eps); + TEST(fabsf(v2(0) - 4) < eps); + TEST(fabsf(v2(1) - 5) < eps); + TEST(fabsf(v2(2) - 6) < eps); SquareMatrix m = diag(Vector3f(1,2,3)); - TEST(fabs(m(0, 0) - 1) < eps); - TEST(fabs(m(1, 1) - 2) < eps); - TEST(fabs(m(2, 2) - 3) < eps); + TEST(fabsf(m(0, 0) - 1) < eps); + TEST(fabsf(m(1, 1) - 2) < eps); + TEST(fabsf(m(2, 2) - 3) < eps); return 0; }