Skip to content

Commit

Permalink
Add missing const and nodiscard keywords and update to c++23 and c17
Browse files Browse the repository at this point in the history
  • Loading branch information
MTRNord committed Mar 28, 2024
1 parent 09e0c21 commit 13ec20b
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 83 deletions.
4 changes: 2 additions & 2 deletions .kdev4/persephone.kdev4
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ checkUnusedFunction=true
[CustomDefinesAndIncludes][ProjectPath0]
Path=.
parseAmbiguousAsCPP=true
parserArguments=-ferror-limit=100 -fspell-checking -Wdocumentation -Wunused-parameter -Wunreachable-code -Wall -std=c++2a
parserArgumentsC=-ferror-limit=100 -fspell-checking -Wdocumentation -Wunused-parameter -Wunreachable-code -Wall -std=c11
parserArguments=-ferror-limit=100 -fspell-checking -Wdocumentation -Wunused-parameter -Wunreachable-code -Wall -std=c++23
parserArgumentsC=-ferror-limit=100 -fspell-checking -Wdocumentation -Wunused-parameter -Wunreachable-code -Wall -std=c17
parserArgumentsCuda=-ferror-limit=100 -fspell-checking -Wdocumentation -Wunused-parameter -Wunreachable-code -Wall -std=c++11
parserArgumentsOpenCL=-ferror-limit=100 -fspell-checking -Wdocumentation -Wunused-parameter -Wunreachable-code -Wall -cl-std=CL1.1

Expand Down
29 changes: 14 additions & 15 deletions meson.build
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
project(
'persephone',
'cpp',
version: '0.1.0',
default_options: [
'cpp_std=c++20',
'c_std=c11',
'warning_level=3',
# yaml_cpp break werror. Fixes appreciated
#'werror=true',
'b_lto=true',
'b_thinlto_cache=true',
#'b_sanitize=address,undefined',
# Clang fails to link with sanatizers without this: https://github.com/mesonbuild/meson/issues/764
#'b_lundef=false',
],
'persephone',
'cpp',
version : '0.1.0',
default_options : [
'cpp_std=c++23',
'c_std=c17',
'warning_level=3',
# yaml_cpp break werror. Fixes appreciated
#'werror=true',
'b_lto=true',
'b_thinlto_cache=true',
# Clang fails to link with sanatizers without this: https://github.com/mesonbuild/meson/issues/764
#'b_lundef=false',
]
)
#
add_project_arguments(
Expand Down
11 changes: 6 additions & 5 deletions src/database/database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
#include <stdexcept>
#include <zlib.h>

void Database::migrate() {
void Database::migrate() const {
Migrator migrator;
migrator.migrate();
}

drogon::Task<Database::UserCreationResp>
[[nodiscard]] drogon::Task<Database::UserCreationResp>
Database::create_user(Database::UserCreationData const &data) const {
auto sql = drogon::app().getDbClient();

Expand Down Expand Up @@ -63,7 +63,8 @@ Database::create_user(Database::UserCreationData const &data) const {
co_return resp_data;
}

drogon::Task<bool> Database::user_exists(std::string matrix_id) const {
[[nodiscard]] drogon::Task<bool>
Database::user_exists(std::string matrix_id) const {
auto sql = drogon::app().getDbClient();
try {
auto f = co_await sql->execSqlCoro(
Expand All @@ -79,7 +80,7 @@ drogon::Task<bool> Database::user_exists(std::string matrix_id) const {
}
}

drogon::Task<std::optional<Database::UserInfo>>
[[nodiscard]] drogon::Task<std::optional<Database::UserInfo>>
Database::get_user_info(std::string auth_token) const {
auto sql = drogon::app().getDbClient();
try {
Expand Down Expand Up @@ -112,7 +113,7 @@ Database::get_user_info(std::string auth_token) const {
}
}

drogon::Task<bool>
[[nodiscard]] drogon::Task<bool>
Database::validate_access_token(std::string auth_token) const {
auto sql = drogon::app().getDbClient();
try {
Expand Down
2 changes: 1 addition & 1 deletion src/database/database.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void prepare_statements();
*/
class Database {
public:
void migrate();
void migrate() const;

struct [[nodiscard]] UserCreationData {
std::string matrix_id;
Expand Down
10 changes: 5 additions & 5 deletions src/database/migrations/migrator.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "migrator.hpp"

void Migrator::migrate() {
void Migrator::migrate() const {
LOG_INFO << "Starting database migration";
this->migration_v0();
this->migration_v1();
Expand All @@ -10,7 +10,7 @@ void Migrator::migrate() {
LOG_INFO << "Finished database migration";
}

void Migrator::migration_v0() {
void Migrator::migration_v0() const {
auto sql = drogon::app().getDbClient("default");
assert(sql);
try {
Expand All @@ -25,7 +25,7 @@ void Migrator::migration_v0() {
}
}

void Migrator::migration_v1() {
void Migrator::migration_v1() const {
LOG_INFO << "Starting database migration v0->v1";
auto sql = drogon::app().getDbClient("default");
assert(sql);
Expand Down Expand Up @@ -159,7 +159,7 @@ void Migrator::migration_v1() {
}
}

void Migrator::migration_v2() {
void Migrator::migration_v2() const {
LOG_INFO << "Starting database migration v1->v2";
auto sql = drogon::app().getDbClient();
assert(sql);
Expand Down Expand Up @@ -203,7 +203,7 @@ void Migrator::migration_v2() {
}
}

void Migrator::migration_v3() {
void Migrator::migration_v3() const {
LOG_INFO << "Starting database migration v2->v3";
auto sql = drogon::app().getDbClient();
assert(sql);
Expand Down
10 changes: 5 additions & 5 deletions src/database/migrations/migrator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

class Migrator {
public:
void migrate();
void migrate() const;

private:
void migration_v0();
void migration_v1();
void migration_v2();
void migration_v3();
void migration_v0() const;
void migration_v1() const;
void migration_v2() const;
void migration_v3() const;
};
2 changes: 1 addition & 1 deletion src/utils/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct [[nodiscard]] Config {
MatrixConfig matrix_config;
WebserverConfig webserver_config;

explicit Config() {
[[nodiscard]] explicit Config() {
LOG_INFO << "Loading config file";
YAML::Node config = YAML::LoadFile("config.yaml");
this->load_db(config);
Expand Down
15 changes: 9 additions & 6 deletions src/utils/json_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
#include <stdexcept>

namespace json_utils {
std::vector<unsigned char> unbase64_key(const std::string &input) {
[[nodiscard]] std::vector<unsigned char>
unbase64_key(const std::string &input) {
size_t b64_str_len = input.size();
size_t bin_len = b64_str_len * (static_cast<size_t>(4) * 3);
std::vector<unsigned char> bin_str(bin_len);
Expand All @@ -29,7 +30,7 @@ std::vector<unsigned char> unbase64_key(const std::string &input) {
return bin_str;
}

std::string base64_key(const std::vector<unsigned char> &input) {
[[nodiscard]] std::string base64_key(const std::vector<unsigned char> &input) {
unsigned long long private_key_len = input.size();
const size_t base64_max_len = sodium_base64_encoded_len(
private_key_len, sodium_base64_VARIANT_URLSAFE_NO_PADDING);
Expand All @@ -45,8 +46,10 @@ std::string base64_key(const std::vector<unsigned char> &input) {
return base64_str;
}

json sign_json(const std::string &server_name, const std::string &key_id,
const std::vector<unsigned char> &secret_key, json json_data) {
[[nodiscard]] json sign_json(const std::string &server_name,
const std::string &key_id,
const std::vector<unsigned char> &secret_key,
json json_data) {
// Get existing (or not yet existing) signatures and unsigned fields
auto signatures = json_data.value("signatures", json(json::value_t::object));
auto unsigned_value = json_data.value("unsigned", json{});
Expand Down Expand Up @@ -81,8 +84,8 @@ json sign_json(const std::string &server_name, const std::string &key_id,
return json_data;
}

std::tuple<std::array<unsigned char, crypto_sign_PUBLICKEYBYTES>,
std::array<unsigned char, crypto_sign_SECRETKEYBYTES>>
[[nodiscard]] std::tuple<std::array<unsigned char, crypto_sign_PUBLICKEYBYTES>,
std::array<unsigned char, crypto_sign_SECRETKEYBYTES>>
generate_server_key() {
std::array<unsigned char, crypto_sign_PUBLICKEYBYTES> pk;
std::array<unsigned char, crypto_sign_SECRETKEYBYTES> sk;
Expand Down
31 changes: 17 additions & 14 deletions src/utils/state_res.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* "m.room.join_rules", "m.room.power_levels", "m.room.history_visibility",
* "m.room.redaction", and others.
*/
json v11_redact(const json &event) {
[[nodiscard]] json v11_redact(const json &event) {
// We copy here to (if needed) have the original still intact
json event_copy(event);

Expand Down Expand Up @@ -94,14 +94,14 @@ json v11_redact(const json &event) {
return event_copy;
}

json redact(const json &event, const std::string &room_version) {
[[nodiscard]] json redact(const json &event, const std::string &room_version) {
if (room_version == "11") {
return v11_redact(event);
}
throw MatrixRoomVersionError(room_version);
}

std::string reference_hash_v11(const json &event) {
[[nodiscard]] std::string reference_hash_v11(const json &event) {
// We copy here to (if needed) have the original still intact
json event_copy(event);

Expand All @@ -119,15 +119,17 @@ std::string reference_hash_v11(const json &event) {
return sha256_hash_string;
}

std::string reference_hash(const json &event, const std::string &room_version) {
[[nodiscard]] std::string reference_hash(const json &event,
const std::string &room_version) {
if (room_version == "11") {
return reference_hash_v11(event);
}

throw MatrixRoomVersionError(room_version);
}

std::string event_id(const json &event, const std::string &room_version) {
[[nodiscard]] std::string event_id(const json &event,
const std::string &room_version) {
auto hash = reference_hash(event, room_version);

unsigned long long hash_len = hash.size();
Expand All @@ -148,7 +150,7 @@ std::string event_id(const json &event, const std::string &room_version) {

// Function to create the partial state map from unconflicted events while
// preserving ordering
std::map<EventType, std::map<StateKey, StateEvent>>
[[nodiscard]] std::map<EventType, std::map<StateKey, StateEvent>>
createPartialState(const std::vector<StateEvent> &unconflictedEvents) {
std::map<EventType, std::map<StateKey, StateEvent>> partialState;

Expand All @@ -163,7 +165,8 @@ createPartialState(const std::vector<StateEvent> &unconflictedEvents) {
return partialState;
}

StateEventSets splitEvents(const std::vector<std::vector<StateEvent>> &forks) {
[[nodiscard]] StateEventSets
splitEvents(const std::vector<std::vector<StateEvent>> &forks) {
StateEventSets result;
std::vector<std::map<std::pair<EventType, StateKey>, int>> stateTuples(
forks.size());
Expand Down Expand Up @@ -213,7 +216,7 @@ StateEventSets splitEvents(const std::vector<std::vector<StateEvent>> &forks) {
return result;
}

std::map<EventID, int>
[[nodiscard]] std::map<EventID, int>
sorted_incoming_edges(const std::map<EventID, int> &incoming_edges,
const std::map<EventID, StateEvent> &event_map) {
auto comparator = [&](const EventID &x, const EventID &y) {
Expand Down Expand Up @@ -250,7 +253,7 @@ sorted_incoming_edges(const std::map<EventID, int> &incoming_edges,
return sorted_edges;
}

std::vector<StateEvent>
[[nodiscard]] std::vector<StateEvent>
kahns_algorithm(const std::vector<StateEvent> &full_conflicted_set) {
std::vector<StateEvent> output_events;
std::map<EventID, StateEvent> event_map;
Expand Down Expand Up @@ -286,7 +289,7 @@ kahns_algorithm(const std::vector<StateEvent> &full_conflicted_set) {
return output_events;
}

bool auth_against_partial_state_version_11(
[[nodiscard]] bool auth_against_partial_state_version_11(
const std::map<EventType, std::map<StateKey, StateEvent>>
&current_partial_state,
StateEvent &e) {
Expand Down Expand Up @@ -366,7 +369,7 @@ bool auth_against_partial_state_version_11(
// This checks if the event is allowed by the auth checks
// These are defined in
// https://spec.matrix.org/v1.9/rooms/v11/#authorization-rules
bool auth_against_partial_state(
[[nodiscard]] bool auth_against_partial_state(
std::map<EventType, std::map<StateKey, StateEvent>> &current_partial_state,
StateEvent &e) {
if (e["type"].get<std::string>() == "m.room.create") {
Expand Down Expand Up @@ -400,7 +403,7 @@ void mainline_iterate(std::vector<StateEvent> &power_level_mainline,
}
}

StateEvent
[[nodiscard]] StateEvent
get_closest_mainline_event(std::vector<StateEvent> &power_level_mainline,
StateEvent &event) {
StateEvent closest_mainline_event;
Expand Down Expand Up @@ -431,7 +434,7 @@ get_closest_mainline_event(std::vector<StateEvent> &power_level_mainline,
return closest_mainline_event;
}

std::vector<StateEvent>
[[nodiscard]] std::vector<StateEvent>
sorted_normal_state_events(std::vector<StateEvent> normal_events) {
auto compare_events = [](StateEvent &x, StateEvent &y) {
if (x["position_on_mainline"].get<std::string>() !=
Expand All @@ -452,7 +455,7 @@ sorted_normal_state_events(std::vector<StateEvent> normal_events) {
return normal_events;
}

std::map<EventType, std::map<StateKey, StateEvent>>
[[nodiscard]] std::map<EventType, std::map<StateKey, StateEvent>>
stateres_v2(const std::vector<std::vector<StateEvent>> &forks) {
auto state_event_sets = splitEvents(forks);
auto partial_state = createPartialState(state_event_sets.unconflictedEvents);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/state_res.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using EventType = std::string;
using StateKey = std::string;

// Custom struct to hold conflicted and unconflicted state sets
struct StateEventSets {
struct [[nodiscard]] StateEventSets {
std::vector<StateEvent> conflictedEvents;
std::vector<StateEvent> unconflictedEvents;
};
Expand Down
8 changes: 4 additions & 4 deletions src/utils/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ using namespace drogon;

constexpr auto UserAgent = "persephone/0.1.0";

struct SRVRecord {
struct [[nodiscard]] SRVRecord {
std::string host;
unsigned int port;
unsigned short int priority;
unsigned short int weight;
};

struct ResolvedServer {
struct [[nodiscard]] ResolvedServer {
std::string address;
// Unsigned long since conversion from string is a little easier here
unsigned long port;
std::string server_name;
};

struct HTTPRequest {
struct [[nodiscard]] HTTPRequest {
drogon::HttpClientPtr client;
drogon::HttpMethod method;
std::string path;
Expand All @@ -40,7 +40,7 @@ struct HTTPRequest {
int timeout;
};

struct VerifyKeyData {
struct [[nodiscard]] VerifyKeyData {
std::vector<unsigned char> private_key;
std::string public_key_base64;
std::string key_id;
Expand Down

0 comments on commit 13ec20b

Please sign in to comment.