Skip to content

Commit 7f4f168

Browse files
authored
fix: treat warnings as errors in CI (#253)
Many users might compile the SDK using a "warnings as errors" flag. To support this use-case, update CI to compile the sdk with CMAKE_COMPILE_WARNING_AS_ERROR=TRUE. This commit also resolves existing warnings, with the exception of OpenSSL deprecation warnings which have been supressed using CMake generator expressions.
1 parent fa6662d commit 7f4f168

File tree

16 files changed

+121
-68
lines changed

16 files changed

+121
-68
lines changed

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
BasedOnStyle: Chromium
33
IndentWidth: 4
44
QualifierAlignment: Right
5+
NamespaceIndentation: None
56
...

cmake/expected.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.24")
77
cmake_policy(SET CMP0135 NEW)
88
endif ()
99

10+
set(EXPECTED_BUILD_TESTS OFF)
1011
FetchContent_Declare(tl-expected
1112
GIT_REPOSITORY https://github.com/TartanLlama/expected.git
1213
GIT_TAG 292eff8bd8ee230a7df1d6a1c00c4ea0eb2f0362
13-
)
14+
)
1415

1516
FetchContent_MakeAvailable(tl-expected)

libs/client-sdk/src/bindings/c/builder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ LDClientConfigBuilder_Build(LDClientConfigBuilder b,
9393
LD_ASSERT_NOT_NULL(b);
9494
LD_ASSERT_NOT_NULL(out_config);
9595

96-
return launchdarkly::ConsumeBuilder<ConfigBuilder>(b, out_config);
96+
return launchdarkly::detail::ConsumeBuilder<ConfigBuilder>(b, out_config);
9797
}
9898

9999
LD_EXPORT(void)

libs/client-sdk/src/data_sources/streaming_data_source.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
#include "streaming_data_source.hpp"
2+
3+
#include <launchdarkly/context_builder.hpp>
4+
#include <launchdarkly/detail/unreachable.hpp>
5+
#include <launchdarkly/encoding/base_64.hpp>
6+
#include <launchdarkly/network/http_requester.hpp>
7+
#include <launchdarkly/serialization/json_context.hpp>
8+
19
#include <boost/asio/any_io_executor.hpp>
210
#include <boost/asio/io_context.hpp>
311
#include <boost/asio/post.hpp>
@@ -6,13 +14,6 @@
614

715
#include <utility>
816

9-
#include "streaming_data_source.hpp"
10-
11-
#include <launchdarkly/context_builder.hpp>
12-
#include <launchdarkly/encoding/base_64.hpp>
13-
#include <launchdarkly/network/http_requester.hpp>
14-
#include <launchdarkly/serialization/json_context.hpp>
15-
1617
namespace launchdarkly::client_side::data_sources {
1718

1819
static char const* const kCouldNotParseEndpoint =
@@ -30,6 +31,7 @@ static char const* DataSourceErrorToString(launchdarkly::sse::Error error) {
3031
case sse::Error::ReadTimeout:
3132
return "read timeout reached";
3233
}
34+
launchdarkly::detail::unreachable();
3335
}
3436

3537
StreamingDataSource::StreamingDataSource(

libs/common/include/launchdarkly/attributes.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ class Attributes final {
118118
: key_(std::move(key)),
119119
name_(std::move(name)),
120120
anonymous_(anonymous),
121-
custom_attributes_(std::move(attributes)),
122-
private_attributes_(std::move(private_attributes)) {}
121+
private_attributes_(std::move(private_attributes)),
122+
custom_attributes_(std::move(attributes)) {}
123123

124124
friend std::ostream& operator<<(std::ostream& out,
125125
Attributes const& attrs) {
@@ -142,9 +142,13 @@ class Attributes final {
142142
}
143143

144144
Attributes(Attributes const& context) = default;
145+
145146
Attributes(Attributes&& context) = default;
147+
146148
~Attributes() = default;
149+
147150
Attributes& operator=(Attributes const&) = default;
151+
148152
Attributes& operator=(Attributes&&) = default;
149153

150154
private:

libs/common/include/launchdarkly/detail/c_binding_helpers.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include <tl/expected.hpp>
88

9-
namespace launchdarkly {
9+
namespace launchdarkly::detail {
1010
template <typename T, typename = void>
1111
struct has_result_type : std::false_type {};
1212

@@ -105,5 +105,5 @@ bool OptReturnReinterpretCast(std::optional<OptType>& opt,
105105

106106
#define LD_ASSERT_NOT_NULL(param) LD_ASSERT(param != nullptr)
107107

108-
} // namespace launchdarkly
108+
} // namespace launchdarkly::detail
109109
// NOLINTEND cppcoreguidelines-pro-type-reinterpret-cast
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace launchdarkly::detail {
2+
3+
// This may be replaced with a standard routine when C++23 is available.
4+
[[noreturn]] inline void unreachable() {
5+
// Uses compiler specific extensions if possible.
6+
// Even if no extension is used, undefined behavior is still raised by
7+
// an empty function body and the noreturn attribute.
8+
#if defined(__GNUC__) // GCC, Clang, ICC
9+
__builtin_unreachable();
10+
#elif defined(_MSC_VER) // MSVC
11+
__assume(false);
12+
#endif
13+
}
14+
} // namespace launchdarkly::detail

libs/common/include/launchdarkly/value.hpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class Value final {
119119
using iterator_category = std::forward_iterator_tag;
120120
using difference_type = std::ptrdiff_t;
121121

122-
using value_type = std::pair<const std::string, Value>;
122+
using value_type = std::pair<std::string const, Value>;
123123
using pointer = value_type const*;
124124
using reference = value_type const&;
125125

@@ -374,7 +374,7 @@ class Value final {
374374
static Value const& Null();
375375

376376
friend std::ostream& operator<<(std::ostream& out, Value const& value) {
377-
switch (value.type_) {
377+
switch (value.Type()) {
378378
case Type::kNull:
379379
out << "null()";
380380
break;
@@ -409,16 +409,17 @@ class Value final {
409409
operator int() const { return AsInt(); }
410410

411411
private:
412-
std::variant<bool, double, std::string, Array, Object> storage_;
413-
enum Type type_;
412+
struct null_type {};
413+
414+
std::variant<null_type, bool, double, std::string, Array, Object> storage_;
414415

415416
// Empty constants used when accessing the wrong type.
416417
// These are not inline static const because of this bug:
417418
// https://developercommunity.visualstudio.com/t/inline-static-destructors-are-called-multiple-time/1157794
418-
static const std::string empty_string_;
419-
static const Array empty_vector_;
420-
static const Object empty_map_;
421-
static const Value null_value_;
419+
static std::string const empty_string_;
420+
static Array const empty_vector_;
421+
static Object const empty_map_;
422+
static Value const null_value_;
422423
};
423424

424425
bool operator==(Value const& lhs, Value const& rhs);

libs/common/src/attribute_reference.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,11 @@ AttributeReference::AttributeReference(char const* ref_str)
229229
std::string AttributeReference::PathToStringReference(
230230
std::vector<std::string_view> path) {
231231
// Approximate size to reduce resizes.
232-
auto size = std::accumulate(path.begin(), path.end(), 0,
233-
[](auto sum, auto const& component) {
234-
return sum + component.size() + 1;
235-
});
232+
std::size_t size =
233+
std::accumulate(path.begin(), path.end(), std::size_t{0},
234+
[](std::size_t sum, auto const& component) {
235+
return sum + component.size() + 1;
236+
});
236237

237238
std::string redaction_name;
238239
redaction_name.reserve(size);

libs/common/src/bindings/c/data/evaluation_detail.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define FROM_REASON(ptr) (reinterpret_cast<LDEvalReason>(ptr));
1212

1313
using namespace launchdarkly;
14+
using namespace launchdarkly::detail;
1415

1516
LD_EXPORT(void)
1617
LDEvalDetail_Free(LDEvalDetail detail) {

libs/common/src/bindings/c/value.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <launchdarkly/bindings/c/value.h>
55
#include <launchdarkly/bindings/c/iter.hpp>
66
#include <launchdarkly/detail/c_binding_helpers.hpp>
7+
#include <launchdarkly/detail/unreachable.hpp>
78
#include <launchdarkly/value.hpp>
89

910
using launchdarkly::Value;
@@ -59,7 +60,7 @@ LD_EXPORT(enum LDValueType) LDValue_Type(LDValue val) {
5960
case Value::Type::kArray:
6061
return LDValueType_Array;
6162
}
62-
LD_ASSERT(!"Unsupported value type.");
63+
launchdarkly::detail::unreachable();
6364
}
6465

6566
LD_EXPORT(bool) LDValue_GetBool(LDValue val) {

libs/common/src/value.cpp

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,80 @@
1-
#pragma clang diagnostic push
2-
31
#include <launchdarkly/value.hpp>
42

53
#include <cstring>
64
#include <iostream>
75

86
namespace launchdarkly {
97

10-
const std::string Value::empty_string_;
11-
const Value::Array Value::empty_vector_;
12-
const Value::Object Value::empty_map_;
13-
const Value Value::null_value_;
8+
std::string const Value::empty_string_;
9+
Value::Array const Value::empty_vector_;
10+
Value::Object const Value::empty_map_;
11+
Value const Value::null_value_;
12+
13+
Value::Value() : storage_{null_type{}} {}
1414

15-
Value::Value() : type_(Value::Type::kNull), storage_{0.0} {}
15+
Value::Value(bool boolean) : storage_{boolean} {}
1616

17-
Value::Value(bool boolean) : type_(Value::Type::kBool), storage_{boolean} {}
17+
Value::Value(double num) : storage_{num} {}
1818

19-
Value::Value(double num) : type_(Value::Type::kNumber), storage_{num} {}
19+
Value::Value(int num) : storage_{(double)num} {}
2020

21-
Value::Value(int num) : type_(Value::Type::kNumber), storage_{(double)num} {}
21+
Value::Value(std::string str) : storage_{std::move(str)} {}
2222

23-
Value::Value(std::string str)
24-
: type_(Value::Type::kString), storage_{std::move(str)} {}
23+
Value::Value(char const* str) : storage_{std::string(str)} {}
2524

26-
Value::Value(char const* str)
27-
: type_(Value::Type::kString), storage_{std::string(str)} {}
25+
Value::Value(std::vector<Value> arr) : storage_{std::move(arr)} {}
2826

29-
Value::Value(std::vector<Value> arr)
30-
: type_(Value::Type::kArray), storage_{std::move(arr)} {}
27+
Value::Value(std::map<std::string, Value> obj) : storage_{std::move(obj)} {}
3128

32-
Value::Value(std::map<std::string, Value> obj)
33-
: type_(Value::Type::kObject), storage_{std::move(obj)} {}
29+
template <class>
30+
inline constexpr bool always_false_v = false;
3431

3532
enum Value::Type Value::Type() const {
36-
return type_;
33+
return std::visit(
34+
[](auto const& arg) {
35+
using T = std::decay_t<decltype(arg)>;
36+
if constexpr (std::is_same_v<T, null_type>) {
37+
return Type::kNull;
38+
} else if constexpr (std::is_same_v<T, bool>) {
39+
return Type::kBool;
40+
} else if constexpr (std::is_same_v<T, double>) {
41+
return Type::kNumber;
42+
} else if constexpr (std::is_same_v<T, std::string>) {
43+
return Type::kString;
44+
} else if constexpr (std::is_same_v<T, Value::Array>) {
45+
return Type::kArray;
46+
} else if constexpr (std::is_same_v<T, Value::Object>) {
47+
return Type::kObject;
48+
} else {
49+
static_assert(always_false_v<T>,
50+
"all value types must be visited");
51+
}
52+
},
53+
storage_);
3754
}
3855

3956
bool Value::IsNull() const {
40-
return type_ == Type::kNull;
57+
return std::holds_alternative<null_type>(storage_);
4158
}
4259

4360
bool Value::IsBool() const {
44-
return type_ == Type::kBool;
61+
return std::holds_alternative<bool>(storage_);
4562
}
4663

4764
bool Value::IsNumber() const {
48-
return type_ == Type::kNumber;
65+
return std::holds_alternative<double>(storage_);
4966
}
5067

5168
bool Value::IsString() const {
52-
return type_ == Type::kString;
69+
return std::holds_alternative<std::string>(storage_);
5370
}
5471

5572
bool Value::IsArray() const {
56-
return type_ == Type::kArray;
73+
return std::holds_alternative<Value::Array>(storage_);
5774
}
5875

5976
bool Value::IsObject() const {
60-
return type_ == Type::kObject;
77+
return std::holds_alternative<Value::Object>(storage_);
6178
}
6279

6380
Value const& Value::Null() {
@@ -67,62 +84,60 @@ Value const& Value::Null() {
6784
}
6885

6986
bool Value::AsBool() const {
70-
if (type_ == Type::kBool) {
87+
if (IsBool()) {
7188
return std::get<bool>(storage_);
7289
}
7390
return false;
7491
}
7592

7693
int Value::AsInt() const {
77-
if (type_ == Type::kNumber) {
94+
if (IsNumber()) {
7895
return static_cast<int>(std::get<double>(storage_));
7996
}
8097
return 0;
8198
}
8299

83100
double Value::AsDouble() const {
84-
if (type_ == Type::kNumber) {
101+
if (IsNumber()) {
85102
return std::get<double>(storage_);
86103
}
87104
return 0.0;
88105
}
89106

90107
std::string const& Value::AsString() const {
91-
if (type_ == Type::kString) {
108+
if (IsString()) {
92109
return std::get<std::string>(storage_);
93110
}
94111
return empty_string_;
95112
}
96113

97114
Value::Array const& Value::AsArray() const {
98-
if (type_ == Type::kArray) {
115+
if (IsArray()) {
99116
return std::get<Array>(storage_);
100117
}
101118
return empty_vector_;
102119
}
103120

104121
Value::Object const& Value::AsObject() const {
105-
if (type_ == Type::kObject) {
122+
if (IsObject()) {
106123
return std::get<Object>(storage_);
107124
}
108125
return empty_map_;
109126
}
110127

111128
Value::Value(std::optional<std::string> opt_str) : storage_{0.0} {
112129
if (opt_str.has_value()) {
113-
type_ = Type::kString;
114130
storage_ = opt_str.value();
115131
} else {
116-
type_ = Type::kNull;
132+
storage_ = null_type{};
117133
}
118134
}
119135
Value::Value(std::initializer_list<Value> values)
120-
: type_(Type::kArray), storage_(std::vector<Value>(values)) {}
136+
: storage_(std::vector<Value>(values)) {}
137+
138+
Value::Value(Value::Array arr) : storage_(std::move(arr)) {}
121139

122-
Value::Value(Value::Array arr)
123-
: storage_(std::move(arr)), type_(Type::kArray) {}
124-
Value::Value(Value::Object obj)
125-
: storage_(std::move(obj)), type_(Type::kObject) {}
140+
Value::Value(Value::Object obj) : storage_(std::move(obj)) {}
126141

127142
Value::Array::Iterator::Iterator(std::vector<Value>::const_iterator iterator)
128143
: iterator_(iterator) {}

0 commit comments

Comments
 (0)