Skip to content

Commit ee2f6c7

Browse files
chore: fix sonar findings (#106)
* fix sonar findings * improvements * fix sonar findings * fix windows build
1 parent 2c5ba19 commit ee2f6c7

28 files changed

Lines changed: 452 additions & 390 deletions

‎.github/copilot-instructions.md‎

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,11 @@ Both the `extern template` declarations in headers and the matching `.cpp` files
200200
}
201201
```
202202
203-
3. **Add the `.cpp` file to `CMakeLists.txt`** conditionally:
203+
3. **Add the `.cpp` file to `CMakeLists.txt`** using the coverage helper:
204204
```cmake
205-
if (EMIL_ENABLE_COVERAGE)
206-
target_sources(numerical.domain PRIVATE
207-
Algorithm.cpp
208-
)
209-
endif()
205+
numerical_add_coverage_sources(numerical.domain
206+
Algorithm.cpp
207+
)
210208
```
211209

212210
4. **Instantiate all specializations used by tests** — each `extern template` in the header must have a matching `template class` in the `.cpp` file
@@ -287,6 +285,47 @@ class Algorithm
287285
- Optional compiler optimizations via `NUMERICAL_TOOLBOX_ENABLE_OPTIMIZATIONS`
288286
- Coverage builds via the `coverage` CMake preset (`EMIL_ENABLE_COVERAGE=On`)
289287

288+
### Header-Only Library CMake Pattern
289+
290+
All header-only library targets under `numerical/` use the reusable CMake functions
291+
defined in `cmake/NumericalHeaderLibrary.cmake`:
292+
293+
- **`numerical_add_header_library(<target> [STATIC])`** — Creates the library target.
294+
In coverage builds (`EMIL_ENABLE_COVERAGE=On`), creates a regular (or `STATIC`) library
295+
so that explicit template instantiation `.cpp` files are compiled with `--coverage` flags.
296+
In non-coverage builds, creates an `INTERFACE` library. Sets `NUMERICAL_VISIBILITY` in
297+
the calling scope to either `PUBLIC` or `INTERFACE` for use with `target_include_directories`
298+
and `target_link_libraries`.
299+
300+
- **`numerical_add_coverage_sources(<target> <sources...>)`** — Conditionally adds
301+
explicit template instantiation `.cpp` files only when `EMIL_ENABLE_COVERAGE` is set.
302+
303+
#### Example CMakeLists.txt
304+
305+
```cmake
306+
numerical_add_header_library(numerical.domain)
307+
308+
target_include_directories(numerical.domain ${NUMERICAL_VISIBILITY}
309+
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/../../>"
310+
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
311+
)
312+
313+
target_link_libraries(numerical.domain ${NUMERICAL_VISIBILITY}
314+
infra.util
315+
numerical.math
316+
)
317+
318+
target_sources(numerical.domain PRIVATE
319+
Algorithm.hpp
320+
)
321+
322+
numerical_add_coverage_sources(numerical.domain
323+
Algorithm.cpp
324+
)
325+
326+
add_subdirectory(test)
327+
```
328+
290329
## Version Control
291330

292331
- Keep commits atomic and focused

‎CMakeLists.txt‎

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ if (EMIL_ENABLE_COVERAGE)
2525
add_definitions(-DNUMERICAL_TOOLBOX_COVERAGE_BUILD=1)
2626
endif()
2727

28+
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
29+
include(NumericalHeaderLibrary)
30+
2831
add_definitions(-DEMIL_ENABLE_TRACING=1)
2932

3033
if (NUMERICAL_TOOLBOX_STANDALONE)
@@ -34,19 +37,16 @@ if (NUMERICAL_TOOLBOX_STANDALONE)
3437
emil
3538
GIT_REPOSITORY https://github.com/embedded-pro/embedded-infra-lib.git
3639
GIT_TAG 74165ff75d8734b9968bd5f2772e542968bfe2a0 # Unreleased
40+
PATCH_COMMAND ${CMAKE_COMMAND} -Demil_SOURCE_DIR=<SOURCE_DIR> -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/patch-emil.cmake
3741
)
3842

39-
FetchContent_MakeAvailable(emil)
40-
41-
if (EMIL_HOST_BUILD)
42-
FetchContent_Declare(
43-
cucumber-cpp-runner
44-
GIT_REPOSITORY https://github.com/philips-software/amp-cucumber-cpp-runner.git
45-
GIT_TAG d8b8c030c0bea97f1c41cc5229a927b2d06f5290 # v3.0.0
46-
)
43+
set(EMIL_INCLUDE_MBEDTLS Off CACHE BOOL "" FORCE)
44+
set(EMIL_INCLUDE_ECHO Off CACHE BOOL "" FORCE)
45+
set(EMIL_FETCH_ECHO_COMPILERS Off CACHE BOOL "" FORCE)
46+
set(EMIL_BUILD_ECHO_COMPILERS Off CACHE BOOL "" FORCE)
47+
set(EMIL_ENABLE_DOCKER_TOOLS Off CACHE BOOL "" FORCE)
4748

48-
FetchContent_MakeAvailable(cucumber-cpp-runner)
49-
endif()
49+
FetchContent_MakeAvailable(emil)
5050
endif()
5151

5252
project(numerical-toolbox LANGUAGES C CXX ASM VERSION 1.0.0) # x-release-please-version
@@ -89,18 +89,6 @@ if (NUMERICAL_TOOLBOX_BUILD_SIMULATOR)
8989
add_subdirectory(simulator)
9090
endif()
9191

92-
if(TARGET yaml-cpp)
93-
emil_exclude_from_clang_format(yaml-cpp)
94-
95-
get_target_property(yaml_cpp_compile_options yaml-cpp COMPILE_OPTIONS)
96-
if(yaml_cpp_compile_options)
97-
list(REMOVE_ITEM yaml_cpp_compile_options /WX -Werror)
98-
set_target_properties(yaml-cpp PROPERTIES COMPILE_OPTIONS "${yaml_cpp_compile_options}")
99-
endif()
100-
101-
set_target_properties(yaml-cpp PROPERTIES COMPILE_WARNING_AS_ERROR OFF)
102-
endif()
103-
10492
emil_clangformat_directories(numerical DIRECTORIES .)
10593

10694
if (NUMERICAL_TOOLBOX_BUILD_SIMULATOR)

‎cmake/NumericalHeaderLibrary.cmake‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function(numerical_add_header_library target)
2+
cmake_parse_arguments(ARG "STATIC" "" "" ${ARGN})
3+
4+
if (EMIL_ENABLE_COVERAGE)
5+
if (ARG_STATIC)
6+
add_library(${target} ${EMIL_EXCLUDE_FROM_ALL} STATIC)
7+
else()
8+
add_library(${target} ${EMIL_EXCLUDE_FROM_ALL})
9+
endif()
10+
set(NUMERICAL_VISIBILITY PUBLIC PARENT_SCOPE)
11+
else()
12+
add_library(${target} ${EMIL_EXCLUDE_FROM_ALL} INTERFACE)
13+
set(NUMERICAL_VISIBILITY INTERFACE PARENT_SCOPE)
14+
endif()
15+
endfunction()
16+
17+
function(numerical_add_coverage_sources target)
18+
if (EMIL_ENABLE_COVERAGE)
19+
target_sources(${target} PRIVATE ${ARGN})
20+
endif()
21+
endfunction()

‎cmake/patch-emil.cmake‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
set(EMIL_CMAKELISTS "${emil_SOURCE_DIR}/CMakeLists.txt")
2+
set(EMIL_EXTERNAL_CMAKELISTS "${emil_SOURCE_DIR}/external/CMakeLists.txt")
3+
4+
file(READ "${EMIL_CMAKELISTS}" content)
5+
6+
if (NOT content MATCHES "EMIL_INCLUDE_ECHO")
7+
string(REPLACE
8+
"add_subdirectory(protobuf)\nadd_subdirectory(services)\nadd_subdirectory(upgrade)"
9+
"if (EMIL_INCLUDE_ECHO)\n add_subdirectory(protobuf)\n add_subdirectory(services)\n add_subdirectory(upgrade)\nendif()"
10+
content "${content}"
11+
)
12+
file(WRITE "${EMIL_CMAKELISTS}" "${content}")
13+
endif()
14+
15+
file(READ "${EMIL_EXTERNAL_CMAKELISTS}" ext_content)
16+
17+
if (NOT ext_content MATCHES "EMIL_INCLUDE_ECHO")
18+
string(REPLACE
19+
"add_subdirectory(protobuf)\nadd_subdirectory(protoc)"
20+
"if (EMIL_INCLUDE_ECHO)\n add_subdirectory(protobuf)\n add_subdirectory(protoc)\nendif()"
21+
ext_content "${ext_content}"
22+
)
23+
file(WRITE "${EMIL_EXTERNAL_CMAKELISTS}" "${ext_content}")
24+
endif()

‎numerical/analysis/CMakeLists.txt‎

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
if (EMIL_ENABLE_COVERAGE)
2-
add_library(numerical.analysis ${EMIL_EXCLUDE_FROM_ALL})
3-
set(_analysis_visibility PUBLIC)
4-
else()
5-
add_library(numerical.analysis ${EMIL_EXCLUDE_FROM_ALL} INTERFACE)
6-
set(_analysis_visibility INTERFACE)
7-
endif()
1+
numerical_add_header_library(numerical.analysis)
82

9-
target_include_directories(numerical.analysis ${_analysis_visibility}
3+
target_include_directories(numerical.analysis ${NUMERICAL_VISIBILITY}
104
"$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/../../>"
115
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
126
)
137

14-
target_link_libraries(numerical.analysis ${_analysis_visibility}
8+
target_link_libraries(numerical.analysis ${NUMERICAL_VISIBILITY}
159
infra.util
1610
numerical.math
1711
numerical.solver
@@ -26,11 +20,10 @@ target_sources(numerical.analysis PRIVATE
2620
RootLocus.hpp
2721
)
2822

29-
if (EMIL_ENABLE_COVERAGE)
30-
target_sources(numerical.analysis PRIVATE
31-
FastFourierTransformRadix2Impl.cpp
32-
RootLocus.cpp
33-
)
34-
endif()
23+
numerical_add_coverage_sources(numerical.analysis
24+
FastFourierTransformRadix2Impl.cpp
25+
FrequencyResponse.cpp
26+
RootLocus.cpp
27+
)
3528

3629
add_subdirectory(test)

‎numerical/analysis/FastFourierTransform.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace analysis
1818
class FastFourierTransform
1919
{
2020
static_assert(math::is_qnumber<QNumberType>::value ||
21-
std::is_floating_point<QNumberType>::value,
21+
std::is_floating_point_v<QNumberType>,
2222
"FastFourierTransform can only be instantiated with math::QNumber types.");
2323

2424
public:

‎numerical/analysis/FastFourierTransformRadix2Impl.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ namespace analysis
8686
ResetFrequencyDomain();
8787

8888
for (std::size_t i = 0; i < input.size(); i++)
89-
frequencyDomain[i] = (math::Complex<QNumberType>{ input[i], 0.0f });
89+
frequencyDomain[i] = (math::Complex<QNumberType>{ input[i], QNumberType(0.0f) });
9090

9191
BitReversePermutation();
9292
Calculate();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "numerical/analysis/FrequencyResponse.hpp"
2+
3+
namespace analysis
4+
{
5+
template class FrequencyResponse<float, 64>;
6+
template class FrequencyResponse<float, 128>;
7+
}

‎numerical/analysis/FrequencyResponse.hpp‎

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "infra/util/BoundedVector.hpp"
55
#include "infra/util/MemoryRange.hpp"
66
#include "numerical/math/QNumber.hpp"
7+
#include <cmath>
78
#include <complex>
89
#include <tuple>
910

@@ -12,8 +13,8 @@ namespace analysis
1213
template<typename QNumberType, std::size_t NumberOfPoints>
1314
class FrequencyResponse
1415
{
15-
static_assert(math::is_qnumber<QNumberType>::value ||
16-
std::is_floating_point<QNumberType>::value,
16+
static_assert(math::is_qnumber_v<QNumberType> ||
17+
std::is_floating_point_v<QNumberType>,
1718
"FrequencyResponse can only be instantiated with math::QNumber types.");
1819

1920
public:
@@ -44,22 +45,24 @@ namespace analysis
4445
template<typename QNumberType, std::size_t NumberOfPoints>
4546
std::tuple<typename FrequencyResponse<QNumberType, NumberOfPoints>::Vector, typename FrequencyResponse<QNumberType, NumberOfPoints>::Vector, typename FrequencyResponse<QNumberType, NumberOfPoints>::Vector> FrequencyResponse<QNumberType, NumberOfPoints>::Calculate()
4647
{
47-
const auto fstart = sampleFrequency / response.max_size();
48+
const auto maxSize = static_cast<double>(response.max_size());
49+
const auto fstart = static_cast<float>(sampleFrequency / maxSize);
4850
const auto fend = sampleFrequency / 2.0f;
49-
const auto multiplier = std::pow(fend / fstart, 1.0f / (response.max_size() - 1));
51+
const auto multiplier = static_cast<float>(std::pow(fend / fstart, 1.0 / (maxSize - 1.0)));
5052

51-
for (auto f = fstart; f <= fend; f *= multiplier)
53+
for (std::size_t n = 0; n < response.max_size(); ++n)
5254
{
53-
auto omega = 2.0f * M_PI * f / sampleFrequency;
55+
auto f = fstart * std::pow(multiplier, static_cast<float>(n));
56+
auto omega = 2.0f * static_cast<float>(math::pi) * f / sampleFrequency;
5457

55-
std::complex<float> numerator(0.0, 0.0);
56-
std::complex<float> denominator(0.0, 0.0);
58+
std::complex<float> numerator(0.0f, 0.0f);
59+
std::complex<float> denominator(0.0f, 0.0f);
5760

58-
for (size_t i = 0; i < b.size(); ++i)
59-
numerator += math::ToFloat(b[i]) * std::polar<float>(1.0f, -omega * i); // z = e^(jω)
61+
for (std::size_t i = 0; i < b.size(); ++i)
62+
numerator += math::ToFloat(b[i]) * std::polar(1.0f, -omega * static_cast<float>(i));
6063

61-
for (size_t i = 0; i < a.size(); ++i)
62-
denominator += math::ToFloat(a[i]) * std::polar<float>(1.0f, -omega * i); // z = e^(jω)
64+
for (std::size_t i = 0; i < a.size(); ++i)
65+
denominator += math::ToFloat(a[i]) * std::polar(1.0f, -omega * static_cast<float>(i));
6366

6467
if (denominator == 0.0f)
6568
denominator.real(1.0f);
@@ -68,11 +71,16 @@ namespace analysis
6871

6972
frequencies.emplace_back(f);
7073
response.emplace_back(20.0f * std::log10(std::abs(h)));
71-
phase.emplace_back(std::arg(h) * 180.0f / M_PI);
74+
phase.emplace_back(std::arg(h) * 180.0f / static_cast<float>(math::pi));
7275
}
7376

7477
return std::make_tuple(frequencies, response, phase);
7578
}
79+
80+
#ifdef NUMERICAL_TOOLBOX_COVERAGE_BUILD
81+
extern template class FrequencyResponse<float, 64>;
82+
extern template class FrequencyResponse<float, 128>;
83+
#endif
7684
}
7785

7886
#endif

‎numerical/analysis/test/CMakeLists.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ target_link_libraries(numerical.analysis_test PUBLIC
1111
target_sources(numerical.analysis_test PRIVATE
1212
TestDiscreteCosineTransform.cpp
1313
TestFastFourierTransformRadix2Impl.cpp
14+
TestFrequencyResponse.cpp
1415
TestPowerDensitySpectrum.cpp
1516
TestRootLocus.cpp
1617
)

0 commit comments

Comments
 (0)