Skip to content

Commit

Permalink
Mark functions as static and const
Browse files Browse the repository at this point in the history
Also use constexpr where we can
  • Loading branch information
AZero13 committed Oct 21, 2022
1 parent 7911e0a commit 8d79a52
Show file tree
Hide file tree
Showing 29 changed files with 70 additions and 68 deletions.
4 changes: 2 additions & 2 deletions include/vcpkg/base/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace fmt
template<>
struct formatter<vcpkg::LineInfo, char>
{
constexpr auto parse(format_parse_context& ctx) const -> decltype(ctx.begin())
static constexpr auto parse(format_parse_context& ctx) -> decltype(ctx.begin())
{
return vcpkg::basic_format_parse_impl(ctx);
}
Expand All @@ -49,7 +49,7 @@ namespace fmt
template<>
struct formatter<std::error_code, char> : formatter<std::string, char>
{
constexpr auto parse(format_parse_context& ctx) const -> decltype(ctx.begin())
static constexpr auto parse(format_parse_context& ctx) -> decltype(ctx.begin())
{
return vcpkg::basic_format_parse_impl(ctx);
}
Expand Down
2 changes: 1 addition & 1 deletion include/vcpkg/base/messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ namespace vcpkg
return lhs.data() >= rhs.data();
}

bool empty() { return m_data.empty(); }
bool empty() const { return m_data.empty(); }
void clear() { m_data.clear(); }

private:
Expand Down
2 changes: 1 addition & 1 deletion include/vcpkg/binarycaching.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace vcpkg
std::vector<std::string> headers_for_put;
std::vector<std::string> headers_for_get;

LocalizedString valid();
LocalizedString valid() const;
std::string instantiate_variables(const InstallPlanAction& action) const;
};

Expand Down
2 changes: 1 addition & 1 deletion include/vcpkg/build.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ namespace vcpkg
Cache<Path, TripletMapEntry> m_triplet_cache;
Cache<Path, std::string> m_toolchain_cache;

const TripletMapEntry& get_triplet_cache(const Filesystem& fs, const Path& p);
const TripletMapEntry& get_triplet_cache(const Filesystem& fs, const Path& p) const;

#if defined(_WIN32)
struct EnvMapEntry
Expand Down
2 changes: 1 addition & 1 deletion include/vcpkg/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace vcpkg
std::vector<std::string> overlay_triplets;

Json::Object serialize() const;
void validate_as_active();
void validate_as_active() const;

std::unique_ptr<RegistrySet> instantiate_registry_set(const VcpkgPaths& paths, const Path& config_dir) const;

Expand Down
18 changes: 9 additions & 9 deletions include/vcpkg/export.prefab.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ namespace vcpkg::Export::Prefab
struct NdkVersion
{
NdkVersion(int _major, int _minor, int _patch) : m_major{_major}, m_minor{_minor}, m_patch{_patch} { }
int major() { return this->m_major; }
int minor() { return this->m_minor; }
int patch() { return this->m_patch; }
std::string to_string();
void to_string(std::string& out);
int major() const { return this->m_major; }
int minor() const { return this->m_minor; }
int patch() const { return this->m_patch; }
std::string to_string() const;
void to_string(std::string& out) const;

friend bool operator==(const NdkVersion& lhs, const NdkVersion& rhs)
{
Expand All @@ -49,22 +49,22 @@ namespace vcpkg::Export::Prefab
int api;
int ndk;
std::string stl;
std::string to_string();
std::string to_string() const;
};

struct PlatformModuleMetadata
{
std::vector<std::string> export_libraries;
std::string library_name;
std::string to_json();
std::string to_json() const;
};

struct ModuleMetadata
{
std::vector<std::string> export_libraries;
std::string library_name;
PlatformModuleMetadata android;
std::string to_json();
std::string to_json() const;
};

struct PackageMetadata
Expand All @@ -73,7 +73,7 @@ namespace vcpkg::Export::Prefab
int schema;
std::vector<std::string> dependencies;
std::string version;
std::string to_json();
std::string to_json() const;
};

void do_export(const std::vector<ExportPlanAction>& export_plan,
Expand Down
20 changes: 10 additions & 10 deletions include/vcpkg/metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,24 +98,24 @@ namespace vcpkg
Metrics(const Metrics&) = delete;
Metrics& operator=(const Metrics&) = delete;

void set_send_metrics(bool should_send_metrics);
void set_print_metrics(bool should_print_metrics);
static void set_send_metrics(bool should_send_metrics);
static void set_print_metrics(bool should_print_metrics);

// This function is static and must be called outside the g_metrics lock.
static void enable();

void track_metric(const std::string& name, double value);
void track_buildtime(const std::string& name, double value);
static void track_metric(const std::string& name, double value);
static void track_buildtime(const std::string& name, double value);

void track_define_property(DefineMetric metric);
void track_string_property(StringMetric metric, StringView value);
void track_bool_property(BoolMetric metric, bool value);
static void track_define_property(DefineMetric metric);
static void track_string_property(StringMetric metric, StringView value);
static void track_bool_property(BoolMetric metric, bool value);

void track_feature(const std::string& feature, bool value);
static void track_feature(const std::string& feature, bool value);

bool metrics_enabled();
static bool metrics_enabled();

void upload(const std::string& payload);
static void upload(const std::string& payload);
void flush(Filesystem& fs);
};

Expand Down
2 changes: 1 addition & 1 deletion include/vcpkg/paragraphparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace vcpkg
void required_field(StringView fieldname, std::pair<std::string&, TextRowCol&> out);

std::string optional_field(StringView fieldname);
void optional_field(StringView fieldname, std::pair<std::string&, TextRowCol&> out);
void optional_field(StringView fieldname, std::pair<std::string&, TextRowCol&> out) const;

void add_type_error(const std::string& fieldname, const char* type) { expected_types[fieldname] = type; }

Expand Down
2 changes: 1 addition & 1 deletion include/vcpkg/xunitwriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace vcpkg
const std::string& abi_tag,
const std::vector<std::string>& features);

std::string build_xml(Triplet controlling_triplet);
std::string build_xml(Triplet controlling_triplet) const;

private:
std::map<std::string, std::vector<XunitTest>> m_tests;
Expand Down
4 changes: 2 additions & 2 deletions src/vcpkg-fuzz/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace

// returns {arg, ""} when there isn't an `=`
// skips preceding `-`s
std::pair<StringView, StringView> split_arg(StringView arg)
std::pair<StringView, StringView> split_arg(StringView arg) const
{
auto first = std::find_if(arg.begin(), arg.end(), [](char c) { return c != '-'; });
auto division = std::find(first, arg.end(), '=');
Expand All @@ -112,7 +112,7 @@ namespace
}
}

[[noreturn]] void print_help_and_exit(bool invalid = false)
[[noreturn]] void print_help_and_exit(bool invalid = false) const
{
auto color = invalid ? Color::error : Color::none;

Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg-test/dependencies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct MockVersionedPortfileProvider : IVersionedPortfileProvider

SourceControlFileAndLocation& emplace(std::string&& name,
Version&& version,
VersionScheme scheme = VersionScheme::String)
VersionScheme scheme = VersionScheme::String) const
{
#if defined(__cpp_lib_map_try_emplace) && __cpp_lib_map_try_emplace >= 201411
auto it = v.try_emplace(name).first;
Expand Down
5 changes: 3 additions & 2 deletions src/vcpkg-test/expected.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace
std::size_t moves = 0;
std::size_t move_assigns = 0;

void check_no_ops()
void check_no_ops() const
{
CHECK(copies == 0);
CHECK(copy_assigns == 0);
Expand Down Expand Up @@ -76,7 +76,8 @@ namespace
++cr->move_assigns;
return *this;
}
std::string to_string() const { return "a construct tracker"; }

static std::string to_string() { return "a construct tracker"; }
~ConstructTracker() { --cr->alive; }
};
}
Expand Down
8 changes: 4 additions & 4 deletions src/vcpkg-test/hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ using vcpkg::StringView;

TEST_CASE ("SHA256: basic tests", "[hash][sha256]")
{
const auto algorithm = Hash::Algorithm::Sha256;
constexpr auto algorithm = Hash::Algorithm::Sha256;

CHECK_HASH_STRING("", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
CHECK_HASH_STRING(";", "41b805ea7ac014e23556e98bb374702a08344268f92489a02f0880849394a1e4");
Expand All @@ -72,11 +72,11 @@ TEST_CASE ("SHA256: basic tests", "[hash][sha256]")

TEST_CASE ("SHA256: NIST test cases (small)", "[hash][sha256]")
{
const auto algorithm = Hash::Algorithm::Sha256;
constexpr auto algorithm = Hash::Algorithm::Sha256;

CHECK_HASH(1, 0xbd, "68325720aabd7c82f30f554b313d0570c95accbb7dc4b5aae11204c08ffe732b");
{
const unsigned char data[] = {0xc9, 0x8c, 0x8e, 0x55};
constexpr unsigned char data[] = {0xc9, 0x8c, 0x8e, 0x55};
CHECK_HASH_OF(data, "7abc22c0ae5af26ce93dbb94433a0e0b2e119d014f8e7f65bd56c61ccccd9504");
}
CHECK_HASH(55, 0, "02779466cdec163811d078815c633f21901413081449002f24aa3e80f0b88ef7");
Expand All @@ -90,7 +90,7 @@ TEST_CASE ("SHA256: NIST test cases (small)", "[hash][sha256]")

TEST_CASE ("SHA512: NIST test cases (small)", "[hash][sha512]")
{
const auto algorithm = Hash::Algorithm::Sha512;
constexpr auto algorithm = Hash::Algorithm::Sha512;

CHECK_HASH_STRING("",
"cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f"
Expand Down
4 changes: 2 additions & 2 deletions src/vcpkg-test/optional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ TEST_CASE ("optional.map", "[optional]")

struct MoveTest
{
int operator()(int&&) { return 1; }
int operator()(const int&) { return -1; }
int operator()(int&&) const { return 1; }
int operator()(const int&) const { return -1; }
} move_test;

Optional<int> dst = std::move(five).map(move_test);
Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg/base/downloads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace vcpkg
return ret;
}

ExpectedS<int> query_status()
ExpectedS<int> query_status() const
{
DWORD dwStatusCode = 0;
DWORD dwSize = sizeof(dwStatusCode);
Expand Down
4 changes: 2 additions & 2 deletions src/vcpkg/base/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ namespace vcpkg::Json
JsonStyle style;
std::string& buffer;

void append_indent(int indent)
void append_indent(int indent) const
{
if (style.use_tabs())
{
Expand All @@ -1162,7 +1162,7 @@ namespace vcpkg::Json
}
};

void append_unicode_escape(char16_t code_unit)
void append_unicode_escape(char16_t code_unit) const
{
buffer.append("\\u");

Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg/base/strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ vcpkg::ExpectedL<std::string> vcpkg::details::api_stable_format_impl(StringView
{
// Transforms similarly to std::format -- "{xyz}" -> f(xyz), "{{" -> "{", "}}" -> "}"

static const char s_brackets[] = "{}";
static constexpr char s_brackets[] = "{}";

std::string out;
auto prev = sv.begin();
Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg/base/system.process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ namespace vcpkg

friend void swap(ProcessInfo& lhs, ProcessInfo& rhs) noexcept { lhs.swap(rhs); }

unsigned int wait()
unsigned int wait() const
{
const DWORD result = WaitForSingleObject(proc_info.hProcess, INFINITE);
Checks::check_exit(VCPKG_LINE_INFO, result != WAIT_FAILED, "WaitForSingleObject failed");
Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg/binarycaching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ namespace

namespace vcpkg
{
LocalizedString UrlTemplate::valid()
LocalizedString UrlTemplate::valid() const
{
std::vector<std::string> invalid_keys;
auto result = api_stable_format(url_template, [&](std::string&, StringView key) {
Expand Down
10 changes: 5 additions & 5 deletions src/vcpkg/build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ namespace vcpkg
});
}

const EnvCache::TripletMapEntry& EnvCache::get_triplet_cache(const Filesystem& fs, const Path& p)
const EnvCache::TripletMapEntry& EnvCache::get_triplet_cache(const Filesystem& fs, const Path& p) const
{
return m_triplet_cache.get_lazy(p, [&]() -> TripletMapEntry {
return TripletMapEntry{Hash::get_file_hash(fs, p, Hash::Algorithm::Sha256).value_or_exit(VCPKG_LINE_INFO)};
Expand Down Expand Up @@ -657,17 +657,17 @@ namespace vcpkg
rc = cmd_execute_and_stream_lines(
command,
[&](StringView s) {
static const StringLiteral s_hash_marker = "#COMPILER_HASH#";
static constexpr StringLiteral s_hash_marker = "#COMPILER_HASH#";
if (Strings::starts_with(s, s_hash_marker))
{
compiler_info.hash = s.substr(s_hash_marker.size()).to_string();
}
static const StringLiteral s_version_marker = "#COMPILER_CXX_VERSION#";
static constexpr StringLiteral s_version_marker = "#COMPILER_CXX_VERSION#";
if (Strings::starts_with(s, s_version_marker))
{
compiler_info.version = s.substr(s_version_marker.size()).to_string();
}
static const StringLiteral s_id_marker = "#COMPILER_CXX_ID#";
static constexpr StringLiteral s_id_marker = "#COMPILER_CXX_ID#";
if (Strings::starts_with(s, s_id_marker))
{
compiler_info.id = s.substr(s_id_marker.size()).to_string();
Expand Down Expand Up @@ -1089,7 +1089,7 @@ namespace vcpkg
// If there is an unusually large number of files in the port then
// something suspicious is going on. Rather than hash all of them
// just mark the port as no-hash
const int max_port_file_count = 100;
constexpr int max_port_file_count = 100;

std::string portfile_cmake_contents;
std::vector<Path> files;
Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg/commands.version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace

namespace vcpkg::Commands::Version
{
const StringLiteral version = version_init;
constexpr StringLiteral version = version_init;
const CommandStructure COMMAND_STRUCTURE = {
create_example_string("version"),
0,
Expand Down
2 changes: 1 addition & 1 deletion src/vcpkg/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ namespace vcpkg
return known_fields;
}

void Configuration::validate_as_active()
void Configuration::validate_as_active() const
{
if (!ce_metadata.is_empty())
{
Expand Down
Loading

0 comments on commit 8d79a52

Please sign in to comment.