Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Code health] Perform cppcheck cleanup #3150

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

chusitoo
Copy link
Contributor

@chusitoo chusitoo commented Nov 18, 2024

Fixes #2297

Changes

  • Cleaned up code based on current settings for CI to pass
  • Some warnings are either false positives and/or fail in v2.13 but no longer reported in v2.14+, although this would require building cppcheck from source
  • Also suppressed exporters/etw/include/opentelemetry/exporters/etw/TraceLoggingDynamic.h as it has dependencies on Windows macros (partially covered by microsoft_sal.cfg but not entirely). An alternative to suppressing it, maybe as a future improvement, could be to supply a custom cfg file with rules for cppcheck to ingest.

For significant contributions please make sure you have completed the following items:

  • CHANGELOG.md updated for non-trivial changes
  • Unit tests have been added
  • Changes in public API reviewed

Copy link

netlify bot commented Nov 18, 2024

Deploy Preview for opentelemetry-cpp-api-docs canceled.

Name Link
🔨 Latest commit f22a295
🔍 Latest deploy log https://app.netlify.com/sites/opentelemetry-cpp-api-docs/deploys/674125333ef8960008ff847c

Copy link

codecov bot commented Nov 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.86%. Comparing base (6a77bcd) to head (f22a295).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3150      +/-   ##
==========================================
+ Coverage   87.86%   87.86%   +0.01%     
==========================================
  Files         195      195              
  Lines        6151     6153       +2     
==========================================
+ Hits         5404     5406       +2     
  Misses        747      747              
Files with missing lines Coverage Δ
...pi/include/opentelemetry/baggage/baggage_context.h 100.00% <ø> (ø)
api/include/opentelemetry/context/context.h 100.00% <100.00%> (ø)
...ntelemetry/context/propagation/global_propagator.h 100.00% <100.00%> (ø)
...pi/include/opentelemetry/context/runtime_context.h 97.60% <ø> (ø)
api/include/opentelemetry/logs/event_id.h 83.34% <100.00%> (ø)
api/include/opentelemetry/logs/provider.h 100.00% <100.00%> (ø)
api/include/opentelemetry/metrics/provider.h 100.00% <100.00%> (ø)
...metry/nostd/internal/absl/types/internal/variant.h 74.57% <ø> (ø)
api/include/opentelemetry/nostd/shared_ptr.h 100.00% <ø> (ø)
api/include/opentelemetry/trace/context.h 91.67% <100.00%> (ø)
... and 9 more
---- 🚨 Try these New Features:

@chusitoo chusitoo marked this pull request as ready for review November 18, 2024 07:25
@chusitoo chusitoo requested a review from a team as a code owner November 18, 2024 07:25
Copy link
Member

@marcalff marcalff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for the contribution.

Please add a separate PR for the CI alone, which can be merged right away in the main branch. Comment out the last step, to not fail the build on errors, this will be enforced when cleanup is completed.

This will allow us reviewers to actually see the current cppcheck errors in the log, which will facilitate review, as we won't have to ask "why ?" or "what was the error ?" on various lines fixed.

After CI is merged, this PR will contain only the cleanup fixes then.

Comments on actual code cleanup to follow.

@chusitoo chusitoo changed the title [CI] Add cppcheck in the build [CI] Add cppcheck in the build - part 2 Nov 18, 2024
@@ -27,7 +27,7 @@ inline nostd::shared_ptr<Baggage> GetBaggage(const context::Context &context) no
}

inline context::Context SetBaggage(context::Context &context,
nostd::shared_ptr<Baggage> baggage) noexcept
const nostd::shared_ptr<Baggage> &baggage) noexcept
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Providing details because this is not trivial, as justification, for all reviewers:

  • ABI change (the prototype is different)
  • on a helper API
  • which is inlined anyway, expanded in the caller compile unit

Hence:

  • there is no ABI change overall, as no API implemented by the SDK is touched

Approved, this is safe IMO.

Comment on lines +32 to +33
: head_{nostd::shared_ptr<DataList>{new DataList(keys_and_values)}}
{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved as ABI safe, implementation change only.

Comment on lines +38 to +39
: head_{nostd::shared_ptr<DataList>{new DataList(key, value)}}
{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved as ABI safe, implementation change only.

@@ -32,7 +32,7 @@ class OPENTELEMETRY_EXPORT GlobalTextMapPropagator
return nostd::shared_ptr<TextMapPropagator>(GetPropagator());
}

static void SetGlobalPropagator(nostd::shared_ptr<TextMapPropagator> prop) noexcept
static void SetGlobalPropagator(const nostd::shared_ptr<TextMapPropagator> &prop) noexcept
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helper inlined.
Approved as ABI safe.

Comment on lines +155 to +156
static void SetRuntimeContextStorage(
const nostd::shared_ptr<RuntimeContextStorage> &storage) noexcept
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helper inlined.
Approved as ABI safe.

@@ -19,9 +19,9 @@ namespace logs
class EventId
{
public:
EventId(int64_t id, nostd::string_view name) noexcept : id_{id}
EventId(int64_t id, nostd::string_view name) noexcept
: id_{id}, name_{nostd::unique_ptr<char[]>{new char[name.length() + 1]}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ABI safe, internal implementation change.

@@ -39,7 +39,7 @@ class OPENTELEMETRY_EXPORT Provider
/**
* Changes the singleton LoggerProvider.
*/
static void SetLoggerProvider(nostd::shared_ptr<LoggerProvider> tp) noexcept
static void SetLoggerProvider(const nostd::shared_ptr<LoggerProvider> &tp) noexcept
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helper inlined.
Approved as ABI safe.

@@ -60,7 +60,7 @@ class OPENTELEMETRY_EXPORT Provider
/**
* Changes the singleton EventLoggerProvider.
*/
static void SetEventLoggerProvider(nostd::shared_ptr<EventLoggerProvider> tp) noexcept
static void SetEventLoggerProvider(const nostd::shared_ptr<EventLoggerProvider> &tp) noexcept
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helper inlined.
Approved as ABI safe.

@@ -38,7 +38,7 @@ class Provider
/**
* Changes the singleton MeterProvider.
*/
static void SetMeterProvider(nostd::shared_ptr<MeterProvider> tp) noexcept
static void SetMeterProvider(const nostd::shared_ptr<MeterProvider> &tp) noexcept
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Helper inlined.
Approved as ABI safe.

@@ -20,7 +20,7 @@ class DynamicLibraryHandle;
class Span final : public trace::Span
{
public:
Span(std::shared_ptr<trace::Tracer> &&tracer, nostd::shared_ptr<trace::Span> span) noexcept
Span(std::shared_ptr<trace::Tracer> &&tracer, const nostd::shared_ptr<trace::Span> &span) noexcept
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here changing the span constructor does not change the object layout or virtual table, should be ok.

This plugin class in particular is not inherited from in the SDK, changing the constructor is ok.

Approved as ABI safe.

Beside, plugin/tracer.h is not used as far as I know.

@marcalff
Copy link
Member

@chusitoo The CI is now merged to main, please merge main into this PR to resolve the conflicts.

@chusitoo chusitoo changed the title [CI] Add cppcheck in the build - part 2 [Code health] Perform cppcheck cleanup Nov 19, 2024
@@ -309,6 +307,7 @@ class HttpClient : public opentelemetry::ext::http::client::HttpClient
std::shared_ptr<opentelemetry::ext::http::client::Session> CreateSession(
nostd::string_view url) noexcept override;

// cppcheck-suppress [virtualCallInConstructor]
Copy link
Member

@marcalff marcalff Nov 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the suppression.

Instead, if this still raises a warning in cppcheck, we need to find where from, and fix the caller code.

Copy link
Contributor Author

@chusitoo chusitoo Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note that this and all other suppressions of [virtualCallInConstructor] are false positives no longer reported in v2.14+, as alluded to in the changelog summary.

I will revert, as requested. As a future improvement, using a more updated version of cppcheck (built from source) should mitigate these warnings.

Copy link
Contributor Author

@chusitoo chusitoo Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcalff I am noticing that the changes I had put in circular_buffer.h to suppress both [arithOperationsOnVoidPointer] are no longer reported in v2.16, so I am wondering if all these false positives waiting to be addressed should be documented somewhere. I think this would be beneficial to avoid repeat investigation when the fix is updating the scanner version.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes and the details.

@marcalff I am noticing that the changes I had put in circular_buffer.h to suppress both [arithOperationsOnVoidPointer] are no longer reported in v2.16, so I am wondering if all these false positives waiting to be addressed should be documented somewhere. I think this would be beneficial to avoid repeat investigation when the fix is updating the scanner version.

A possible solution is to list these in this PR comments

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion, thanks!

To summarize the 9 outstanding false positives currently being reported by cppcheck v2.13, can be mitigated by using a newer release, more precisely:

v2.14 fixes

  • ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h:310:8: style: Virtual function 'CancelAllSessions' is called from destructor '~HttpClient()' at line 206. Dynamic binding is not used. [virtualCallInConstructor]
  • sdk/include/opentelemetry/sdk/logs/batch_log_record_processor.h:88:8: style: Virtual function 'Shutdown' is called from destructor '~BatchLogRecordProcessor()' at line 325. Dynamic binding is not used. [virtualCallInConstructor]
  • sdk/include/opentelemetry/sdk/logs/multi_log_record_processor.h:46:8: style: Virtual function 'ForceFlush' is called from destructor '~MultiLogRecordProcessor()' at line 33. Dynamic binding is not used. [virtualCallInConstructor]
  • sdk/include/opentelemetry/sdk/logs/multi_log_record_processor.h:56:8: style: Virtual function 'Shutdown' is called from destructor '~MultiLogRecordProcessor()' at line 34. Dynamic binding is not used. [virtualCallInConstructor]
  • sdk/include/opentelemetry/sdk/trace/batch_span_processor.h:86:8: style: Virtual function 'Shutdown' is called from destructor '~BatchSpanProcessor()' at line 322. Dynamic binding is not used. [virtualCallInConstructor]
  • sdk/include/opentelemetry/sdk/trace/multi_span_processor.h:124:8: style: Virtual function 'Shutdown' is called from destructor '~MultiSpanProcessor()' at line 140. Dynamic binding is not used. [virtualCallInConstructor]
  • sdk/include/opentelemetry/sdk/trace/simple_processor.h:77:8: style: Virtual function 'Shutdown' is called from destructor '~SimpleSpanProcessor()' at line 88. Dynamic binding is not used. [virtualCallInConstructor]

v2.16 fixes

  • sdk/include/opentelemetry/sdk/common/circular_buffer.h:188:16: portability: 'data' is of type 'void *'. When using void pointers in calculations, the behaviour is undefined. [arithOperationsOnVoidPointer]
  • sdk/include/opentelemetry/sdk/common/circular_buffer.h:190:50: portability: 'data' is of type 'void *'. When using void pointers in calculations, the behaviour is undefined. [arithOperationsOnVoidPointer]

Copy link
Member

@marcalff marcalff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the cleanup.

All the code under api/ looks good to me, but I would like to have a second opinion (@lalitb , @ThomsonTan , @esigo) due to the risk of breaking the ABI.

See new comments for non api code.

Copy link
Member

@marcalff marcalff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, excellent cleanup.

@lalitb , @ThomsonTan , @esigo,

Please double check the code under api/ in particular, due to the risk of breaking the ABI.

I left comments to indicate why I think each change is safe.

@marcalff marcalff added the pr:please-review This PR is ready for review label Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr:please-review This PR is ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[CI] Add a C++ static code analyser in the build
3 participants