Skip to content

Commit

Permalink
Convert some const to constexpr (s3fs-fuse#2342)
Browse files Browse the repository at this point in the history
This guarantees that the function or value will resolve at compile-time.
  • Loading branch information
gaul authored Nov 14, 2023
1 parent 919575f commit 4845831
Show file tree
Hide file tree
Showing 27 changed files with 95 additions and 106 deletions.
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@ Checks: '
-readability-isolate-declaration,
-readability-magic-numbers,
-readability-named-parameter,
-readability-redundant-declaration,
-readability-simplify-boolean-expr,
-readability-suspicious-call-argument'
2 changes: 1 addition & 1 deletion src/addhead.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
//-------------------------------------------------------------------
// Symbols
//-------------------------------------------------------------------
#define ADD_HEAD_REGEX "reg:"
static constexpr char ADD_HEAD_REGEX[] = "reg:";

//-------------------------------------------------------------------
// Class AdditionalHeader
Expand Down
4 changes: 2 additions & 2 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
// Global variables
//-------------------------------------------------------------------
// TODO: namespace these
static const int64_t FIVE_GB = 5LL * 1024LL * 1024LL * 1024LL;
static const off_t MIN_MULTIPART_SIZE = 5 * 1024 * 1024;
static constexpr int64_t FIVE_GB = 5LL * 1024LL * 1024LL * 1024LL;
static constexpr off_t MIN_MULTIPART_SIZE = 5 * 1024 * 1024;

extern bool foreground;
extern bool nomultipart;
Expand Down
15 changes: 6 additions & 9 deletions src/curl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@
//-------------------------------------------------------------------
// Symbols
//-------------------------------------------------------------------
static const char EMPTY_PAYLOAD_HASH[] = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
static const char EMPTY_MD5_BASE64_HASH[] = "1B2M2Y8AsgTpgAmY7PhCfg==";
static constexpr char EMPTY_PAYLOAD_HASH[] = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
static constexpr char EMPTY_MD5_BASE64_HASH[] = "1B2M2Y8AsgTpgAmY7PhCfg==";

//-------------------------------------------------------------------
// Class S3fsCurl
//-------------------------------------------------------------------
static const int MULTIPART_SIZE = 10 * 1024 * 1024;
static const int GET_OBJECT_RESPONSE_LIMIT = 1024;
static constexpr int MULTIPART_SIZE = 10 * 1024 * 1024;
static constexpr int GET_OBJECT_RESPONSE_LIMIT = 1024;

// [NOTE] about default mime.types file
// If no mime.types file is specified in the mime option, s3fs
Expand All @@ -65,8 +65,8 @@ static const int GET_OBJECT_RESPONSE_LIMIT = 1024;
// If the mime.types file is not found, s3fs will exit with an
// error.
//
static const char DEFAULT_MIME_FILE[] = "/etc/mime.types";
static const char SPECIAL_DARWIN_MIME_FILE[] = "/etc/apache2/mime.types";
static constexpr char DEFAULT_MIME_FILE[] = "/etc/mime.types";
static constexpr char SPECIAL_DARWIN_MIME_FILE[] = "/etc/apache2/mime.types";

// [NOTICE]
// This symbol is for libcurl under 7.23.0
Expand All @@ -83,9 +83,6 @@ static const char SPECIAL_DARWIN_MIME_FILE[] = "/etc/apache2/mime.types";
//-------------------------------------------------------------------
// Class S3fsCurl
//-------------------------------------------------------------------
const long S3fsCurl::S3FSCURL_RESPONSECODE_NOTSET;
const long S3fsCurl::S3FSCURL_RESPONSECODE_FATAL_ERROR;
const int S3fsCurl::S3FSCURL_PERFORM_RESULT_NOTSET;
pthread_mutex_t S3fsCurl::curl_warnings_lock;
pthread_mutex_t S3fsCurl::curl_handles_lock;
S3fsCurl::callback_locks_t S3fsCurl::callback_locks;
Expand Down
6 changes: 3 additions & 3 deletions src/curl.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,9 @@ class S3fsCurl
CURLcode curlCode; // handle curl return

public:
static const long S3FSCURL_RESPONSECODE_NOTSET = -1;
static const long S3FSCURL_RESPONSECODE_FATAL_ERROR = -2;
static const int S3FSCURL_PERFORM_RESULT_NOTSET = 1;
static constexpr long S3FSCURL_RESPONSECODE_NOTSET = -1;
static constexpr long S3FSCURL_RESPONSECODE_FATAL_ERROR = -2;
static constexpr int S3FSCURL_PERFORM_RESULT_NOTSET = 1;

public:
// constructor/destructor
Expand Down
4 changes: 2 additions & 2 deletions src/curl_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@ std::string url_to_host(const std::string &url)
{
S3FS_PRN_INFO3("url is %s", url.c_str());

static const char HTTP[] = "http://";
static const char HTTPS[] = "https://";
static constexpr char HTTP[] = "http://";
static constexpr char HTTPS[] = "https://";
std::string hostname;

if (is_prefix(url.c_str(), HTTP)) {
Expand Down
4 changes: 2 additions & 2 deletions src/fdcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
//
// The following symbols are used by FdManager::RawCheckAllCache().
//
#define CACHEDBG_FMT_DIR_PROB "Directory: %s"
// These must be #defines due to string literal concatenation.
#define CACHEDBG_FMT_HEAD "---------------------------------------------------------------------------\n" \
"Check cache file and its stats file consistency at %s\n" \
"---------------------------------------------------------------------------"
Expand Down Expand Up @@ -70,7 +70,7 @@
// This process may not be complete, but it is easy way can
// be realized.
//
#define NOCACHE_PATH_PREFIX_FORM " __S3FS_UNEXISTED_PATH_%lx__ / " // important space words for simply
static constexpr char NOCACHE_PATH_PREFIX_FORM[] = " __S3FS_UNEXISTED_PATH_%lx__ / "; // important space words for simply

//------------------------------------------------
// FdManager class variable
Expand Down
2 changes: 1 addition & 1 deletion src/fdcache_entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
//------------------------------------------------
// Symbols
//------------------------------------------------
static const int MAX_MULTIPART_CNT = 10 * 1000; // S3 multipart max count
static constexpr int MAX_MULTIPART_CNT = 10 * 1000; // S3 multipart max count

//------------------------------------------------
// FdEntity class variables
Expand Down
2 changes: 1 addition & 1 deletion src/fdcache_page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
//------------------------------------------------
// Symbols
//------------------------------------------------
static const int CHECK_CACHEFILE_PART_SIZE = 1024 * 16; // Buffer size in PageList::CheckZeroAreaInFile()
static constexpr int CHECK_CACHEFILE_PART_SIZE = 1024 * 16; // Buffer size in PageList::CheckZeroAreaInFile()

//------------------------------------------------
// fdpage_list_t utility
Expand Down
2 changes: 1 addition & 1 deletion src/fdcache_pseudofd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
// The minimum pseudo fd value starts 2.
// This is to avoid mistakes for 0(stdout) and 1(stderr), which are usually used.
//
static const int MIN_PSEUDOFD_NUMBER = 2;
static constexpr int MIN_PSEUDOFD_NUMBER = 2;

//------------------------------------------------
// PseudoFdManager class methods
Expand Down
4 changes: 2 additions & 2 deletions src/gnutls_auth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

const char* s3fs_crypt_lib_name(void)
{
static const char version[] = "GnuTLS(nettle)";
static constexpr char version[] = "GnuTLS(nettle)";

return version;
}
Expand All @@ -59,7 +59,7 @@ const char* s3fs_crypt_lib_name(void)

const char* s3fs_crypt_lib_name()
{
static const char version[] = "GnuTLS(gcrypt)";
static constexpr char version[] = "GnuTLS(gcrypt)";

return version;
}
Expand Down
2 changes: 1 addition & 1 deletion src/metaheader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include "metaheader.h"
#include "string_util.h"

static const struct timespec DEFAULT_TIMESPEC = {-1, 0};
static constexpr struct timespec DEFAULT_TIMESPEC = {-1, 0};

//-------------------------------------------------------------------
// Utility functions for convert
Expand Down
2 changes: 1 addition & 1 deletion src/nss_auth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
//-------------------------------------------------------------------
const char* s3fs_crypt_lib_name()
{
static const char version[] = "NSS";
static constexpr char version[] = "NSS";

return version;
}
Expand Down
2 changes: 1 addition & 1 deletion src/openssl_auth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
//-------------------------------------------------------------------
const char* s3fs_crypt_lib_name()
{
static const char version[] = "OpenSSL";
static constexpr char version[] = "OpenSSL";

return version;
}
Expand Down
2 changes: 1 addition & 1 deletion src/s3fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5540,7 +5540,7 @@ int main(int argc, char* argv[])
time_t incomp_abort_time = (24 * 60 * 60);
S3fsLog singletonLog;

static const struct option long_opts[] = {
static constexpr struct option long_opts[] = {
{"help", no_argument, nullptr, 'h'},
{"version", no_argument, nullptr, 0},
{"debug", no_argument, nullptr, 'd'},
Expand Down
36 changes: 17 additions & 19 deletions src/s3fs_cred.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
//-------------------------------------------------------------------
// Symbols
//-------------------------------------------------------------------
#define DEFAULT_AWS_PROFILE_NAME "default"
static constexpr char DEFAULT_AWS_PROFILE_NAME[] = "default";

//-------------------------------------------------------------------
// External Credential dummy function
Expand All @@ -53,8 +53,8 @@
//
const char* VersionS3fsCredential(bool detail)
{
static const char version[] = "built-in";
static const char detail_version[] =
static constexpr char version[] = "built-in";
static constexpr char detail_version[] =
"s3fs-fuse built-in Credential I/F Function\n"
"Copyright(C) 2007 s3fs-fuse\n";

Expand Down Expand Up @@ -113,21 +113,19 @@ bool UpdateS3fsCredential(char** ppaccess_key_id, char** ppserect_access_key, ch
//-------------------------------------------------------------------
// Class Variables
//-------------------------------------------------------------------
const char* S3fsCred::ALLBUCKET_FIELDS_TYPE = "";
const char* S3fsCred::KEYVAL_FIELDS_TYPE = "\t";
const char* S3fsCred::AWS_ACCESSKEYID = "AWSAccessKeyId";
const char* S3fsCred::AWS_SECRETKEY = "AWSSecretKey";

const int S3fsCred::IAM_EXPIRE_MERGIN = 20 * 60; // update timing
const char* S3fsCred::ECS_IAM_ENV_VAR = "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI";
const char* S3fsCred::IAMCRED_ACCESSKEYID = "AccessKeyId";
const char* S3fsCred::IAMCRED_SECRETACCESSKEY = "SecretAccessKey";
const char* S3fsCred::IAMCRED_ROLEARN = "RoleArn";

const char* S3fsCred::IAMv2_token_url = "http://169.254.169.254/latest/api/token";
int S3fsCred::IAMv2_token_ttl = 21600;
const char* S3fsCred::IAMv2_token_ttl_hdr = "X-aws-ec2-metadata-token-ttl-seconds";
const char* S3fsCred::IAMv2_token_hdr = "X-aws-ec2-metadata-token";
constexpr char S3fsCred::ALLBUCKET_FIELDS_TYPE[];
constexpr char S3fsCred::KEYVAL_FIELDS_TYPE[];
constexpr char S3fsCred::AWS_ACCESSKEYID[];
constexpr char S3fsCred::AWS_SECRETKEY[];

constexpr char S3fsCred::ECS_IAM_ENV_VAR[];
constexpr char S3fsCred::IAMCRED_ACCESSKEYID[];
constexpr char S3fsCred::IAMCRED_SECRETACCESSKEY[];
constexpr char S3fsCred::IAMCRED_ROLEARN[];

constexpr char S3fsCred::IAMv2_token_url[];
constexpr char S3fsCred::IAMv2_token_ttl_hdr[];
constexpr char S3fsCred::IAMv2_token_hdr[];

std::string S3fsCred::bucket_name;

Expand Down Expand Up @@ -1186,7 +1184,7 @@ bool S3fsCred::CheckIAMCredentialUpdate(std::string* access_key_id, std::string*

const char* S3fsCred::GetCredFuncVersion(bool detail) const
{
static const char errVersion[] = "unknown";
static constexpr char errVersion[] = "unknown";

if(!pFuncCredVersion){
return errVersion;
Expand Down
26 changes: 13 additions & 13 deletions src/s3fs_cred.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ typedef std::map<std::string, std::string> iamcredmap_t;
class S3fsCred
{
private:
static const char* ALLBUCKET_FIELDS_TYPE; // special key for mapping(This name is absolutely not used as a bucket name)
static const char* KEYVAL_FIELDS_TYPE; // special key for mapping(This name is absolutely not used as a bucket name)
static const char* AWS_ACCESSKEYID;
static const char* AWS_SECRETKEY;
static constexpr char ALLBUCKET_FIELDS_TYPE[] = ""; // special key for mapping(This name is absolutely not used as a bucket name)
static constexpr char KEYVAL_FIELDS_TYPE[] = "\t"; // special key for mapping(This name is absolutely not used as a bucket name)
static constexpr char AWS_ACCESSKEYID[] = "AWSAccessKeyId";
static constexpr char AWS_SECRETKEY[] = "AWSSecretKey";

static const int IAM_EXPIRE_MERGIN;
static const char* ECS_IAM_ENV_VAR;
static const char* IAMCRED_ACCESSKEYID;
static const char* IAMCRED_SECRETACCESSKEY;
static const char* IAMCRED_ROLEARN;
static constexpr int IAM_EXPIRE_MERGIN = 20 * 60; // update timing
static constexpr char ECS_IAM_ENV_VAR[] = "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI";
static constexpr char IAMCRED_ACCESSKEYID[] = "AccessKeyId";
static constexpr char IAMCRED_SECRETACCESSKEY[] = "SecretAccessKey";
static constexpr char IAMCRED_ROLEARN[] = "RoleArn";

static std::string bucket_name;

Expand Down Expand Up @@ -89,10 +89,10 @@ class S3fsCred
fp_UpdateS3fsCredential pFuncCredUpdate;

public:
static const char* IAMv2_token_url;
static int IAMv2_token_ttl;
static const char* IAMv2_token_ttl_hdr;
static const char* IAMv2_token_hdr;
static constexpr char IAMv2_token_url[] = "http://169.254.169.254/latest/api/token";
static constexpr int IAMv2_token_ttl = 21600;
static constexpr char IAMv2_token_ttl_hdr[] = "X-aws-ec2-metadata-token-ttl-seconds";
static constexpr char IAMv2_token_hdr[] = "X-aws-ec2-metadata-token";

private:
static bool ParseIAMRoleFromMetaDataResponse(const char* response, std::string& rolename);
Expand Down
4 changes: 2 additions & 2 deletions src/s3fs_help.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
//-------------------------------------------------------------------
// Contents
//-------------------------------------------------------------------
static const char help_string[] =
static constexpr char help_string[] =
"\n"
"Mount an Amazon S3 bucket as a file system.\n"
"\n"
Expand Down Expand Up @@ -638,7 +638,7 @@ void show_version()

const char* short_version()
{
static const char short_ver[] = "s3fs version " VERSION "(" COMMIT_HASH_VAL ")";
static constexpr char short_ver[] = "s3fs version " VERSION "(" COMMIT_HASH_VAL ")";
return short_ver;
}

Expand Down
7 changes: 3 additions & 4 deletions src/s3fs_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@
//-------------------------------------------------------------------
// S3fsLog class : variables
//-------------------------------------------------------------------
const int S3fsLog::NEST_MAX;
const char* const S3fsLog::nest_spaces[S3fsLog::NEST_MAX] = {"", " ", " ", " "};
const char S3fsLog::LOGFILEENV[] = "S3FS_LOGFILE";
const char S3fsLog::MSGTIMESTAMP[] = "S3FS_MSGTIMESTAMP";
constexpr char S3fsLog::LOGFILEENV[];
constexpr const char* S3fsLog::nest_spaces[];
constexpr char S3fsLog::MSGTIMESTAMP[];
S3fsLog* S3fsLog::pSingleton = nullptr;
S3fsLog::s3fs_log_level S3fsLog::debug_level = S3fsLog::LEVEL_CRIT;
FILE* S3fsLog::logfp = nullptr;
Expand Down
20 changes: 8 additions & 12 deletions src/s3fs_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ class S3fsLog
};

protected:
static const int NEST_MAX = 4;
static const char* const nest_spaces[NEST_MAX];
static const char LOGFILEENV[];
static const char MSGTIMESTAMP[];
static constexpr int NEST_MAX = 4;
static constexpr const char* nest_spaces[NEST_MAX] = {"", " ", " ", " "};
static constexpr char LOGFILEENV[] = "S3FS_LOGFILE";
static constexpr char MSGTIMESTAMP[] = "S3FS_MSGTIMESTAMP";

static S3fsLog* pSingleton;
static s3fs_log_level debug_level;
Expand All @@ -76,7 +76,7 @@ class S3fsLog
static bool IsS3fsLogInfo() { return IsS3fsLogLevel(LEVEL_INFO); }
static bool IsS3fsLogDbg() { return IsS3fsLogLevel(LEVEL_DBG); }

static int GetSyslogLevel(s3fs_log_level level)
static constexpr int GetSyslogLevel(s3fs_log_level level)
{
return ( LEVEL_DBG == (level & LEVEL_DBG) ? LOG_DEBUG :
LEVEL_INFO == (level & LEVEL_DBG) ? LOG_INFO :
Expand All @@ -86,21 +86,17 @@ class S3fsLog

static std::string GetCurrentTime();

static const char* GetLevelString(s3fs_log_level level)
static constexpr const char* GetLevelString(s3fs_log_level level)
{
return ( LEVEL_DBG == (level & LEVEL_DBG) ? "[DBG] " :
LEVEL_INFO == (level & LEVEL_DBG) ? "[INF] " :
LEVEL_WARN == (level & LEVEL_DBG) ? "[WAN] " :
LEVEL_ERR == (level & LEVEL_DBG) ? "[ERR] " : "[CRT] " );
}

static const char* GetS3fsLogNest(int nest)
static constexpr const char* GetS3fsLogNest(int nest)
{
if(nest < NEST_MAX){
return nest_spaces[nest];
}else{
return nest_spaces[NEST_MAX - 1];
}
return nest_spaces[nest < NEST_MAX ? nest : NEST_MAX - 1];
}

static bool IsSetLogFile()
Expand Down
5 changes: 0 additions & 5 deletions src/s3fs_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,11 +444,6 @@ void print_launch_message(int argc, char** argv)
S3FS_PRN_LAUNCH_INFO("%s", message.c_str());
}

//-------------------------------------------------------------------
// Utility for nanosecond time(timespec)
//-------------------------------------------------------------------
const struct timespec S3FS_OMIT_TS = {0, UTIME_OMIT};

//
// result: -1 ts1 < ts2
// 0 ts1 == ts2
Expand Down
Loading

0 comments on commit 4845831

Please sign in to comment.