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 15 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 48099c2
🔍 Latest deploy log https://app.netlify.com/sites/opentelemetry-cpp-api-docs/deploys/673ffff50a2a120008257a79

Copy link

codecov bot commented Nov 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.85%. Comparing base (fe68d51) to head (48099c2).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3150      +/-   ##
==========================================
- Coverage   87.86%   87.85%   -0.01%     
==========================================
  Files         195      195              
  Lines        6151     6153       +2     
==========================================
+ Hits         5404     5405       +1     
- Misses        747      748       +1     
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

... and 1 file with indirect coverage changes

---- 🚨 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.

value_ = value;
}

DataList(const DataList &other)
Copy link
Member

Choose a reason for hiding this comment

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

Ok to have a copy constructor, this clears the cppcheck warning, and is safer in case of refactoring.

I suspect this code to be unreachable.

@@ -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.

Comment on lines +37 to +38
inline context::Context SetSpan(context::Context &context,
const nostd::shared_ptr<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.

Helper inlined.
Approved as ABI safe.

@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
@@ -73,4 +58,5 @@ jobs:
set +e
COUNT=`grep -c -E "\[.+\]" cppcheck.log`
echo "cppcheck reported ${COUNT} warning(s)"
if [ $COUNT -ne 0 ] ; then exit 1 ; fi
# TODO: uncomment to enforce failing the build
# if [ $COUNT -ne 0 ] ; then exit 1 ; fi
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should this check be re-enabled now?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, please do.

If there are unresolved warnings left, it is ok to test with some margin, say count > 5.

The point is to prevent new errors to come in with more merges, while giving us time to resolve possibly more complex issues in follow up PRs and ultimately have count > 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Made the WARNING_LIMIT = 10 to match your PR for iwyu part 5 and to accommodate the 9 false positives that are no longer suppressed. I also thought it would be a good idea to make it stand out a bit more when the count is below threshold but not zero, by emitting a warning annotation.

image


return opentelemetry::sdk::common::ExportResult::kFailure;
const auto result = opentelemetry::sdk::common::ExportResult::kFailure;
result_callback(result);
Copy link
Member

Choose a reason for hiding this comment

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

Here and in other places, the call to result_callback() has moved a bit.

Could you provide more details ?

Copy link
Contributor Author

@chusitoo chusitoo Nov 21, 2024

Choose a reason for hiding this comment

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

Absolutely. The scan reports an [accessMoved] warning here

This is due to the move that happens higher in OtlpHttpClient::Export() on L753 which seems as though it is likely undefined behavior calling it from that point on.

Since the moved from result_callback appears to be used only to consume the result from createSession(), my solution was to move the invocation closer to the call sites where it's been moved to, just before the function returns the actual result.

@@ -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.

@@ -85,6 +85,7 @@ class BatchLogRecordProcessor : public LogRecordProcessor
*
* NOTE: Timeout functionality not supported yet.
*/
// cppcheck-suppress [virtualCallInConstructor]
Copy link
Member

Choose a reason for hiding this comment

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

Remove the suppression, and identify/fix callers.

@@ -43,6 +43,7 @@ class MultiLogRecordProcessor : public LogRecordProcessor
* @param timeout that the forceflush is required to finish within.
* @return a result code indicating whether it succeeded, failed or timed out
*/
// cppcheck-suppress [virtualCallInConstructor]
Copy link
Member

Choose a reason for hiding this comment

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

Remove the suppression, and identify/fix callers.

@@ -53,6 +54,7 @@ class MultiLogRecordProcessor : public LogRecordProcessor
* shutdown before giving up and returning failure.
* @return true if the shutdown succeeded, false otherwise
*/
// cppcheck-suppress [virtualCallInConstructor]
Copy link
Member

Choose a reason for hiding this comment

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

Remove the suppression, and identify/fix callers.

@@ -83,6 +83,7 @@ class BatchSpanProcessor : public SpanProcessor
*
* NOTE: Timeout functionality not supported yet.
*/
// cppcheck-suppress [virtualCallInConstructor]
Copy link
Member

Choose a reason for hiding this comment

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

Remove the suppression, and identify/fix callers.

@@ -121,6 +121,7 @@ class MultiSpanProcessor : public SpanProcessor
return result;
}

// cppcheck-suppress [virtualCallInConstructor]
Copy link
Member

Choose a reason for hiding this comment

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

Remove the suppression, and identify/fix callers.

@@ -74,6 +74,7 @@ class SimpleSpanProcessor : public SpanProcessor
return true;
}

// cppcheck-suppress [virtualCallInConstructor]
Copy link
Member

Choose a reason for hiding this comment

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

Remove the suppression, and identify/fix callers.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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