Skip to content

Commit

Permalink
Update demangle to fix sonar issues and see how it changes coverage (#85
Browse files Browse the repository at this point in the history
)
  • Loading branch information
TrentHouliston authored Sep 22, 2023
1 parent d5f54e2 commit 2b2a540
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Checks: >
cppcoreguidelines-*,
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-non-private-member-variables-in-classes,
-cppcoreguidelines-owning-memory,
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
-cppcoreguidelines-pro-bounds-constant-array-index,
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sonarcloud.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
for f in build/tests/individual/*; do echo "Testing $f"; ./$f; done
- name: Collect coverage into one XML report
run: gcovr --sonarqube > coverage.xml
run: gcovr --exclude-unreachable-branches --exclude-noncode-lines --sonarqube > coverage.xml

- name: Run sonar-scanner
env:
Expand Down
15 changes: 12 additions & 3 deletions src/util/demangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,19 @@ namespace util {
*/
std::string demangle(const char* symbol) {

if (symbol == nullptr) {
return {};
}

int status = -1;
const std::unique_ptr<char, void (*)(void*)> res{abi::__cxa_demangle(symbol, nullptr, nullptr, &status),
std::free};
if (status == 0) {
const std::unique_ptr<char, void (*)(char*)> res{abi::__cxa_demangle(symbol, nullptr, nullptr, &status),
[](char* ptr) {
// The API for __cxa_demangle REQUIRES us to call free
// No choice here so suppress the linter
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
std::free(ptr); // NOSONAR
}};
if (res != nullptr) {
std::string demangled = res.get();
demangled = std::regex_replace(demangled, std::regex(R"(\s+)"), "");
return demangled;
Expand Down
12 changes: 12 additions & 0 deletions tests/util/demangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ SCENARIO("Test the demangle function correctly demangles symbols", "[util][deman
}
}

GIVEN("A nullptr as the symbol") {
const char* symbol = nullptr;

WHEN("Demangle is called") {
std::string result = NUClear::util::demangle(symbol);

THEN("It should return an empty string") {
REQUIRE(result.empty());
}
}
}

GIVEN("A symbol from a struct") {
const char* symbol = typeid(TestSymbol).name();
const std::string expected = "TestSymbol";
Expand Down

0 comments on commit 2b2a540

Please sign in to comment.