Skip to content

Commit ae35af4

Browse files
authored
Merge pull request #11 from freesurfer-rge/freesurfer.rge/compiler-warnings
Turn on compiler warnings
2 parents 374e165 + f2f2f5d commit ae35af4

15 files changed

+103
-35
lines changed

CMakeLists.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,23 @@ install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
1515

1616
add_library(coverage_config INTERFACE)
1717

18+
# Warning options for the compiler
19+
string(
20+
APPEND _warning_opts
21+
"$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>:-Wall;-Wextra;-Weffc++;-Werror;>"
22+
"$<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>>:-Wthread-safety;-Wpedantic;>"
23+
"$<$<CXX_COMPILER_ID:GNU>:-pedantic;-pedantic-errors;>"
24+
)
25+
26+
1827
if (COMPILE_TESTS)
1928
if (CODE_COVERAGE)
2029
message("Enabled coverage flags")
2130
target_compile_options(coverage_config INTERFACE -O0 -g --coverage)
2231
target_link_libraries(coverage_config INTERFACE --coverage)
2332
endif ()
2433
add_executable(jsonrpccpp-test test/main.cpp test/client.cpp test/typemapper.cpp test/dispatcher.cpp test/server.cpp test/batchclient.cpp test/testclientconnector.hpp examples/warehouse/warehouseapp.cpp test/warehouseapp.cpp)
34+
target_compile_options(jsonrpccpp-test PUBLIC "${_warning_opts}")
2535
target_include_directories(jsonrpccpp-test PRIVATE vendor examples)
2636
target_link_libraries(jsonrpccpp-test coverage_config json-rpc-cxx)
2737
enable_testing()
@@ -31,8 +41,10 @@ endif ()
3141
if (COMPILE_EXAMPLES)
3242
find_package(Threads)
3343
add_executable(example-warehouse examples/warehouse/main.cpp examples/warehouse/warehouseapp.cpp examples/warehouse/types.h examples/inmemoryconnector.hpp)
44+
target_compile_options(example-warehouse PUBLIC "${_warning_opts}")
3445
target_link_libraries(example-warehouse json-rpc-cxx Threads::Threads)
35-
target_include_directories(example-warehouse PRIVATE vendor examples)
46+
target_include_directories(example-warehouse SYSTEM PRIVATE vendor)
47+
target_include_directories(example-warehouse PRIVATE examples)
3648
add_test(NAME example COMMAND example-warehouse)
3749
endif ()
3850

examples/cpphttplibconnector.hpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@ class CppHttpLibClientConnector : public jsonrpccxx::IClientConnector {
2222

2323
class CppHttpLibServerConnector {
2424
public:
25-
explicit CppHttpLibServerConnector(jsonrpccxx::JsonRpcServer &server, int port) : server(server), port(port) {
26-
httpServer.Post("/jsonrpc", [&](const httplib::Request &req, httplib::Response &res) {
27-
res.status = 200;
28-
res.set_content(server.HandleRequest(req.body), "application/json");
29-
});
25+
explicit CppHttpLibServerConnector(jsonrpccxx::JsonRpcServer &server, int port) :
26+
thread(),
27+
server(server),
28+
httpServer(),
29+
port(port) {
30+
httpServer.Post("/jsonrpc",
31+
[this](const httplib::Request &req, httplib::Response &res) {
32+
this->PostAction(req, res);
33+
});
3034
}
35+
3136
virtual ~CppHttpLibServerConnector() { StopListening(); }
3237

3338
bool StartListening() {
@@ -49,4 +54,10 @@ class CppHttpLibServerConnector {
4954
jsonrpccxx::JsonRpcServer &server;
5055
httplib::Server httpServer;
5156
int port;
52-
};
57+
58+
void PostAction(const httplib::Request &req,
59+
httplib::Response &res) {
60+
res.status = 200;
61+
res.set_content(this->server.HandleRequest(req.body), "application/json");
62+
}
63+
};

examples/warehouse/main.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ class WareHouseClient {
2222
void doWarehouseStuff(IClientConnector &clientConnector) {
2323
JsonRpcClient client(clientConnector, version::v2);
2424
WareHouseClient appClient(client);
25-
Product p = {"0xff", 22.4, "Product 1", category::cash_carry};
25+
Product p;
26+
p.id = "0xff";
27+
p.price = 22.4;
28+
p.name = "Product 1";
29+
p.cat = category::cash_carry;
2630
cout << "Adding product: " << std::boolalpha << appClient.AddProduct(p) << "\n";
2731

2832
Product p2 = appClient.GetProduct("0xff");
@@ -54,4 +58,4 @@ int main() {
5458
doWarehouseStuff(httpClient);
5559

5660
return 0;
57-
}
61+
}

examples/warehouse/types.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,28 @@
22
#include <nlohmann/json.hpp>
33

44
enum class category { order, cash_carry };
5+
56
struct Product {
7+
public:
8+
Product() : id(), price(), name(), cat() {}
69
std::string id;
710
double price;
811
std::string name;
912
category cat;
1013
};
1114

12-
NLOHMANN_JSON_SERIALIZE_ENUM(category, {{category::order, "order"}, {category::cash_carry, "cc"}});
13-
inline void to_json(nlohmann::json &j, const Product &p) { j = nlohmann::json{{"id", p.id}, {"price", p.price}, {"name", p.name}, {"category", p.cat}}; }
15+
NLOHMANN_JSON_SERIALIZE_ENUM(category, {{category::order, "order"}, {category::cash_carry, "cc"}})
16+
17+
inline void to_json(nlohmann::json &j, const Product &p) {
18+
j = nlohmann::json{{"id", p.id},
19+
{"price", p.price},
20+
{"name", p.name},
21+
{"category", p.cat}};
22+
}
23+
1424
inline void from_json(const nlohmann::json &j, Product &p) {
1525
j.at("name").get_to(p.name);
1626
j.at("id").get_to(p.id);
1727
j.at("price").get_to(p.price);
1828
j.at("category").get_to(p.cat);
19-
}
29+
}

examples/warehouse/warehouseapp.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
class WarehouseServer {
77
public:
8+
WarehouseServer() :
9+
products() {}
10+
811
bool AddProduct(const Product &p);
912
const Product& GetProduct(const std::string& id);
1013

include/jsonrpccxx/batchclient.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace jsonrpccxx {
4646

4747
class BatchResponse {
4848
public:
49-
explicit BatchResponse(json &&response) : response(response) {
49+
explicit BatchResponse(json &&response) : response(response), results(), errors(), nullIds() {
5050
for (auto &[key, value] : response.items()) {
5151
if (value.is_object() && valid_id_not_null(value) && has_key(value, "result")) {
5252
results[value["id"]] = std::stoi(key);
@@ -98,4 +98,4 @@ namespace jsonrpccxx {
9898
}
9999
}
100100
};
101-
}
101+
}

include/jsonrpccxx/dispatcher.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ namespace jsonrpccxx {
1212

1313
class Dispatcher {
1414
public:
15+
Dispatcher() :
16+
methods(),
17+
notifications(),
18+
mapping() {}
19+
1520
bool Add(const std::string &name, MethodHandle callback, const NamedParamMapping &mapping = NAMED_PARAM_MAPPING) {
1621
if (contains(name))
1722
return false;
@@ -98,4 +103,4 @@ namespace jsonrpccxx {
98103
throw JsonRpcException(-32600, "invalid request: params field must be an array, object");
99104
}
100105
};
101-
} // namespace jsonrpccxx
106+
} // namespace jsonrpccxx

include/jsonrpccxx/server.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
namespace jsonrpccxx {
88
class JsonRpcServer {
99
public:
10+
JsonRpcServer() : dispatcher() {}
1011
virtual ~JsonRpcServer() = default;
1112
virtual std::string HandleRequest(const std::string &request) = 0;
1213

@@ -106,4 +107,4 @@ namespace jsonrpccxx {
106107
}
107108
}
108109
};
109-
}
110+
}

test/client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct F {
1313
TestClientConnector c;
1414
JsonRpcClient clientV1;
1515
JsonRpcClient clientV2;
16-
F() : clientV1(c, version::v1), clientV2(c, version::v2) {}
16+
F() : c(), clientV1(c, version::v1), clientV2(c, version::v2) {}
1717
};
1818

1919
TEST_CASE_METHOD(F, "v2_method_noparams", TEST_MODULE) {

test/integrationtest.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
using namespace jsonrpccxx;
99

1010
struct IntegrationTest {
11-
IntegrationTest() : connector(rpcServer), client(connector, version::v2) {}
11+
IntegrationTest() : rpcServer(), connector(rpcServer), client(connector, version::v2) {}
1212
JsonRpc2Server rpcServer;
1313
InMemoryConnector connector;
1414
JsonRpcClient client;
15-
};
15+
};

test/server.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct Server2 {
1313
JsonRpc2Server server;
1414
TestServerConnector connector;
1515

16-
Server2() : connector(server) {}
16+
Server2() : server(), connector(server) {}
1717
};
1818

1919
TEST_CASE_METHOD(Server2, "v2_method_not_found", TEST_MODULE) {
@@ -61,9 +61,10 @@ TEST_CASE_METHOD(Server2, "v2_malformed_requests", TEST_MODULE) {
6161

6262
enum class category { ord, cc };
6363

64-
NLOHMANN_JSON_SERIALIZE_ENUM(category, {{category::ord, "order"}, {category::cc, "cc"}});
64+
NLOHMANN_JSON_SERIALIZE_ENUM(category, {{category::ord, "order"}, {category::cc, "cc"}})
6565

6666
struct product {
67+
product() : id(), price(), name(), cat() {}
6768
int id;
6869
double price;
6970
string name;
@@ -75,6 +76,8 @@ void from_json(const json &j, product &p);
7576

7677
class TestServer {
7778
public:
79+
TestServer() : param_proc(), param_a(), param_b(), catalog() {}
80+
7881
unsigned int add_function(unsigned int a, unsigned int b) {
7982
this->param_a = a;
8083
this->param_b = b;
@@ -95,8 +98,8 @@ class TestServer {
9598
};
9699

97100
void dirty_notification() { throw std::exception(); }
98-
int dirty_method(int a, int b) { throw std::exception(); }
99-
int dirty_method2(int a, int b) { throw - 7; }
101+
int dirty_method(int a, int b) { to_string(a+b); throw std::exception(); }
102+
int dirty_method2(int a, int b) { throw (a+b); }
100103

101104
string param_proc;
102105
int param_a;

test/testclientconnector.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ using namespace std;
1111

1212
class TestClientConnector : public IClientConnector {
1313
public:
14+
TestClientConnector() : request(), raw_response() {}
15+
1416
json request;
1517
string raw_response;
1618

@@ -64,4 +66,4 @@ class TestClientConnector : public IClientConnector {
6466
CHECK(has_key(request, "params"));
6567
}
6668
}
67-
};
69+
};

test/testserverconnector.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ using namespace Catch::Matchers;
88

99
class TestServerConnector {
1010
public:
11-
explicit TestServerConnector(JsonRpcServer &handler) : handler(handler) {}
11+
explicit TestServerConnector(JsonRpcServer &handler) : handler(handler), raw_response() {}
1212

1313
void SendRawRequest(const string &request) { this->raw_response = handler.HandleRequest(request); }
1414
void SendRequest(const json &request) { SendRawRequest(request.dump()); }
@@ -60,4 +60,4 @@ class TestServerConnector {
6060
private:
6161
JsonRpcServer &handler;
6262
string raw_response;
63-
};
63+
};

test/typemapper.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,34 @@ TEST_CASE("test incorrect params", TEST_MODULE) {
8585
enum class category { order, cash_carry };
8686

8787
struct product {
88+
product() : id(), price(), name(), cat() {}
8889
int id;
8990
double price;
9091
string name;
9192
category cat;
9293
};
9394

94-
NLOHMANN_JSON_SERIALIZE_ENUM(category, {{category::order, "order"}, {category::cash_carry, "cc"}});
95+
NLOHMANN_JSON_SERIALIZE_ENUM(category, {{category::order, "order"}, {category::cash_carry, "cc"}})
9596

9697
void to_json(json &j, const product &p) { j = json{{"id", p.id}, {"price", p.price}, {"name", p.name}, {"category", p.cat}}; }
9798

9899
product get_product(int id) {
99-
if (id == 1)
100-
return product{1, 22.50, "some product", category::order};
101-
else if (id == 2)
102-
return product{2, 55.50, "some product 2", category::cash_carry};
100+
if (id == 1) {
101+
product p;
102+
p.id = 1;
103+
p.price = 22.50;
104+
p.name = "some product";
105+
p.cat = category::order;
106+
return p;
107+
}
108+
else if (id == 2) {
109+
product p;
110+
p.id = 2;
111+
p.price = 55.50;
112+
p.name = "some product 2";
113+
p.cat = category::cash_carry;
114+
return p;
115+
}
103116
throw JsonRpcException(-50000, "product not found");
104117
}
105118

@@ -131,7 +144,7 @@ static vector<product> catalog;
131144
bool add_products(const vector<product> &products) {
132145
std::copy(products.begin(), products.end(), std::back_inserter(catalog));
133146
return true;
134-
};
147+
}
135148

136149
TEST_CASE("test with custom params", TEST_MODULE) {
137150
MethodHandle mh = GetHandle(&add_products);
@@ -175,4 +188,4 @@ TEST_CASE("test number range checking", TEST_MODULE) {
175188
unsigned short max_ss = numeric_limits<short>::max();
176189
CHECK(mh2({max_su, max_ss}) == max_su + max_ss);
177190
REQUIRE_THROWS_WITH(mh2({max_su, max_su}), Contains("invalid parameter: exceeds value range of integer"));
178-
}
191+
}

test/warehouseapp.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ TEST_CASE_METHOD(IntegrationTest, "warehouse_test", TEST_MODULE) {
99
rpcServer.Add("GetProduct", GetHandle(&WarehouseServer::GetProduct, app));
1010
rpcServer.Add("AddProduct", GetHandle(&WarehouseServer::AddProduct, app));
1111

12-
Product p = {"0xff", 22.4, "Product 1", category::cash_carry};
12+
Product p;
13+
p.id = "0xff";
14+
p.price = 22.4;
15+
p.name = "Product 1";
16+
p.cat = category::cash_carry;
1317
CHECK(client.CallMethod<bool>(1, "AddProduct", {p}));
1418

1519
Product p2 = client.CallMethod<Product>(1, "GetProduct", {"0xff"});
1620
CHECK((p2.id == p.id && p2.name == p.name && p2.price == p.price && p2.cat == p.cat));
17-
}
21+
}

0 commit comments

Comments
 (0)