8000 [Code health] Perform cppcheck cleanup (#3150) · open-telemetry/opentelemetry-cpp@2d80c18 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2d80c18

Browse files
authored
[Code health] Perform cppcheck cleanup (#3150)
1 parent 23562e6 commit 2d80c18

File tree

37 files changed

+172
-174
lines changed

37 files changed

+172
-174
lines changed

.github/workflows/cppcheck.yml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,14 @@ jobs:
5656
- name: Count warnings
5757
run: |
5858
set +e
59-
COUNT=`grep -c -E "\[.+\]" cppcheck.log`
60-
echo "cppcheck reported ${COUNT} warning(s)"
61-
# TODO: uncomment to enforce failing the build
62-
# if [ $COUNT -ne 0 ] ; then exit 1 ; fi
59+
readonly WARNING_COUNT=`grep -c -E "\[.+\]" cppcheck.log`
60+
echo "cppcheck reported ${WARNING_COUNT} warning(s)"
61+
# Acceptable limit, to decrease over time down to 0
62+
readonly WARNING_LIMIT=10
63+
# FAIL the build if WARNING_COUNT > WARNING_LIMIT
64+
if [ $WARNING_COUNT -gt $WARNING_LIMIT ] ; then
65+
exit 1
66+
# WARN in annotations if WARNING_COUNT > 0
67+
elif [ $WARNING_COUNT -gt 0 ] ; then
68+
echo "::warning::cppcheck reported ${WARNING_COUNT} warning(s)"
69+
fi

api/include/opentelemetry/baggage/baggage_context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ inline nostd::shared_ptr<Baggage> GetBaggage(const context::Context &context) no
2727
}
2828

2929
inline context::Context SetBaggage(context::Context &context,
30-
nostd::shared_ptr<Baggage> baggage) noexcept
30+
const nostd::shared_ptr<Baggage> &baggage) noexcept
3131
{
3232
return context.SetValue(kBaggageHeader, baggage);
3333
}

api/include/opentelemetry/context/context.h

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,14 @@ class Context
2929
// hold a shared_ptr to the head of the DataList linked list
3030
template <class T>
3131
Context(const T &keys_and_values) noexcept
32-
{
33-
head_ = nostd::shared_ptr<DataList>{new DataList(keys_and_values)};
34-
}
32+
: head_{nostd::shared_ptr<DataList>{new DataList(keys_and_values)}}
33+
{}
3534

3635
// Creates a context object from a key and value, this will
3736
// hold a shared_ptr to the head of the DataList linked list
3837
Context(nostd::string_view key, ContextValue value) noexcept
39-
{
40-
head_ = nostd::shared_ptr<DataList>{new DataList(key, value)};
41-
}
38+
: head_{nostd::shared_ptr<DataList>{new DataList(key, value)}}
39+
{}
4240

4341
// Accepts a new iterable and then returns a new context that
4442
// 29C2 contains the new key and value data. It attaches the
@@ -92,22 +90,21 @@ class Context
9290

9391
private:
9492
// A linked list to contain the keys and values of this context node
95-
class DataList
93+
struct DataList
9694
{
97-
public:
98-
char *key_;
95+
char *key_ = nullptr;
9996

100-
nostd::shared_ptr<DataList> next_;
97+
nostd::shared_ptr<DataList> next_{nullptr};
10198

102-
size_t key_length_;
99+
size_t key_length_ = 0UL;
103100

104101
ContextValue value_;
105102

106-
DataList() { next_ = nullptr; }
103+
DataList() = default;
107104

108105
// Builds a data list off of a key and value iterable and returns the head
109106
template <class T>
110-
DataList(const T &keys_and_vals) : key_{nullptr}, next_(nostd::shared_ptr<DataList>{nullptr})
107+
DataList(const T &keys_and_vals)
111108
{
112109
bool first = true;
113110
auto *node = this;
@@ -132,9 +129,18 @@ class Context
132129
{
133130
key_ = new char[key.size()];
134131
key_length_ = key.size();
135-
memcpy(key_, key.data(), key.size() * sizeof(char));
136-
value_ = value;
132+
std::memcpy(key_, key.data(), key.size() * sizeof(char));
137133
next_ = nostd::shared_ptr<DataList>{nullptr};
134+
value_ = value;
135+
}
136+
137+
DataList(const DataList &other)
138+
: key_(new char[other.key_length_]),
139+
next_(other.next_),
140+
key_length_(other.key_length_),
141+
value_(other.value_)
142+
{
143+
std::memcpy(key_, other.key_, other.key_length_ * sizeof(char));
138144
}
139145

140146
DataList &operator=(DataList &&other) noexcept

api/include/opentelemetry/context/propagation/global_propagator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class OPENTELEMETRY_EXPORT GlobalTextMapPropagator
3232
return nostd::shared_ptr<TextMapPropagator>(GetPropagator());
3333
}
3434

35-
static void SetGlobalPropagator(nostd::shared_ptr<TextMapPropagator> prop) noexcept
35+
static void SetGlobalPropagator(const nostd::shared_ptr<TextMapPropagator> &prop) noexcept
3636
{
3737
std::lock_guard<common::SpinLockMutex> guard(GetLock());
3838
GetPropagator() = prop;

api/include/opentelemetry/context/runtime_context.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ class OPENTELEMETRY_EXPORT RuntimeContext
152152
*
153153
* @param storage a custom runtime context storage
154154
*/
155-
static void SetRuntimeContextStorage(nostd::shared_ptr<RuntimeContextStorage> storage) noexcept
155+
static void SetRuntimeContextStorage(
156+
const nostd::shared_ptr<RuntimeContextStorage> &storage) noexcept
156157
{
157158
GetStorage() = storage;
158159
}

api/include/opentelemetry/logs/event_id.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ namespace logs
1919
class EventId
2020
{
2121
public:
22-
EventId(int64_t id, nostd::string_view name) noexcept : id_{id}
22+
EventId(int64_t id, nostd::string_view name) noexcept
23+
: id_{id}, name_{nostd::unique_ptr<char[]>{new char[name.length() + 1]}}
2324
{
24-
name_ = nostd::unique_ptr<char[]>{new char[name.length() + 1]};
2525
std::copy(name.begin(), name.end(), name_.get());
2626
name_.get()[name.length()] = 0;
2727
}

api/include/opentelemetry/logs/provider.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class OPENTELEMETRY_EXPORT Provider
3939
/**
4040
* Changes the singleton LoggerProvider.
4141
*/
42-
static void SetLoggerProvider(nostd::shared_ptr<LoggerProvider> tp) noexcept
42+
static void SetLoggerProvider(const nostd::shared_ptr<LoggerProvider> &tp) noexcept
4343
{
4444
std::lock_guard<common::SpinLockMutex> guard(GetLock());
4545
GetProvider() = tp;
@@ -60,7 +60,7 @@ class OPENTELEMETRY_EXPORT Provider
6060
/**
6161
* Changes the singleton EventLoggerProvider.
6262
*/
63-
static void SetEventLoggerProvider(nostd::shared_ptr<EventLoggerProvider> tp) noexcept
63+
static void SetEventLoggerProvider(const nostd::shared_ptr<EventLoggerProvider> &tp) noexcept
6464
{
6565
std::lock_guard<common::SpinLockMutex> guard(GetLock());
6666
GetEventProvider() = tp;

api/include/opentelemetry/metrics/provider.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Provider
3838
/**
3939
* Changes the singleton MeterProvider.
4040
*/
41-
static void SetMeterProvider(nostd::shared_ptr<MeterProvider> tp) noexcept
41+
static void SetMeterProvider(const nostd::shared_ptr<MeterProvider> &tp) noexcept
4242
{
4343
std::lock_guard<common::SpinLockMutex> guard(GetLock());
4444
GetProvider() = tp;

api/include/opentelemetry/nostd/internal/absl/types/internal/variant.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ template <class ReturnType, class FunctionObject, std::size_t... BoundIndices>
221221
struct MakeVisitationMatrix<ReturnType, FunctionObject, index_sequence<>,
222222
index_sequence<BoundIndices...>> {
223223
using ResultType = ReturnType (*)(FunctionObject&&);
224+
// cppcheck-suppress [duplInheritedMember]
224225
static constexpr ResultType Run() {
225226
return &call_with_indices<ReturnType, FunctionObject,
226227
(BoundIndices - 1)...>;
@@ -722,6 +723,7 @@ struct VariantCoreAccess {
722723
Self* self, Args&&... args) {
723724
Destroy(*self);
724725
using New = typename absl::OTABSL_OPTION_NAMESPACE_NAME::variant_alternative<NewIndex, Self>::type;
726+
// cppcheck-suppress [legacyUninitvar]
725727
New* const result = ::new (static_cast<void*>(&self->state_))
726728
New(absl::OTABSL_OPTION_NAMESPACE_NAME::forward<Args>(args)...);
727729
self->index_ = NewIndex;
@@ -1310,6 +1312,7 @@ class VariantStateBaseDestructorNontrivial : protected VariantStateBase<T...> {
13101312
VariantStateBaseDestructorNontrivial* self;
13111313
};
13121314

1315+
// cppcheck-suppress [duplInheritedMember]
13131316
void destroy() { VisitIndices<sizeof...(T)>::Run(Destroyer{this}, index_); }
13141317

13151318
~VariantStateBaseDestructorNontrivial() { destroy(); }

api/include/opentelemetry/nostd/shared_ptr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class shared_ptr
3737

3838
struct alignas(kAlignment) PlacementBuffer
3939
{
40-
char data[kMaxSize];
40+
char data[kMaxSize]{};
4141
};
4242

4343
class shared_ptr_wrapper

0 commit comments

Comments
 (0)
0