Convert some const to constexpr (#2342)

This guarantees that the function or value will resolve at compile-time.
This commit is contained in:
Andrew Gaul 2023-11-14 22:15:17 +09:00 committed by GitHub
parent 919575f312
commit 4845831f93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 93 additions and 104 deletions

View File

@ -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'

View File

@ -34,7 +34,7 @@
//-------------------------------------------------------------------
// Symbols
//-------------------------------------------------------------------
#define ADD_HEAD_REGEX "reg:"
static constexpr char ADD_HEAD_REGEX[] = "reg:";
//-------------------------------------------------------------------
// Class AdditionalHeader

View File

@ -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;

View File

@ -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
@ -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
@ -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;

View File

@ -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

View File

@ -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)) {

View File

@ -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" \
"---------------------------------------------------------------------------"
@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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;
}
@ -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;
}

View File

@ -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

View File

@ -44,7 +44,7 @@
//-------------------------------------------------------------------
const char* s3fs_crypt_lib_name()
{
static const char version[] = "NSS";
static constexpr char version[] = "NSS";
return version;
}

View File

@ -44,7 +44,7 @@
//-------------------------------------------------------------------
const char* s3fs_crypt_lib_name()
{
static const char version[] = "OpenSSL";
static constexpr char version[] = "OpenSSL";
return version;
}

View File

@ -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'},

View File

@ -38,7 +38,7 @@
//-------------------------------------------------------------------
// Symbols
//-------------------------------------------------------------------
#define DEFAULT_AWS_PROFILE_NAME "default"
static constexpr char DEFAULT_AWS_PROFILE_NAME[] = "default";
//-------------------------------------------------------------------
// External Credential dummy function
@ -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";
@ -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";
constexpr char S3fsCred::ALLBUCKET_FIELDS_TYPE[];
constexpr char S3fsCred::KEYVAL_FIELDS_TYPE[];
constexpr char S3fsCred::AWS_ACCESSKEYID[];
constexpr char S3fsCred::AWS_SECRETKEY[];
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";
constexpr char S3fsCred::ECS_IAM_ENV_VAR[];
constexpr char S3fsCred::IAMCRED_ACCESSKEYID[];
constexpr char S3fsCred::IAMCRED_SECRETACCESSKEY[];
constexpr char S3fsCred::IAMCRED_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::IAMv2_token_url[];
constexpr char S3fsCred::IAMv2_token_ttl_hdr[];
constexpr char S3fsCred::IAMv2_token_hdr[];
std::string S3fsCred::bucket_name;
@ -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;

View File

@ -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;
@ -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);

View File

@ -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"
@ -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;
}

View File

@ -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;

View File

@ -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;
@ -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 :
@ -86,7 +86,7 @@ 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] " :
@ -94,13 +94,9 @@ class S3fsLog
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()

View File

@ -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

View File

@ -66,7 +66,11 @@ enum class stat_time_type{
MTIME,
CTIME
};
extern const struct timespec S3FS_OMIT_TS;
//-------------------------------------------------------------------
// Utility for nanosecond time(timespec)
//-------------------------------------------------------------------
static constexpr struct timespec S3FS_OMIT_TS = {0, UTIME_OMIT};
int compare_timespec(const struct timespec& ts1, const struct timespec& ts2);
int compare_timespec(const struct stat& st, stat_time_type type, const struct timespec& ts);

View File

@ -34,7 +34,7 @@
//-------------------------------------------------------------------
// Variables
//-------------------------------------------------------------------
static const char c_strErrorObjectName[] = "FILE or SUBDIR in DIR";
static constexpr char c_strErrorObjectName[] = "FILE or SUBDIR in DIR";
// [NOTE]
// mutex for static variables in GetXmlNsUrl

View File

@ -32,7 +32,6 @@
//-------------------------------------------------------------------
// Global variables
//-------------------------------------------------------------------
const char SPACES[] = " \t\r\n";
//-------------------------------------------------------------------
// Functions
@ -147,9 +146,9 @@ std::string peeloff(std::string s)
// Therefore, it is a function to use as URL encoding
// for use in query strings.
//
static const char* encode_general_except_chars = ".-_~"; // For general URL encode
static const char* encode_path_except_chars = ".-_~/"; // For fuse(included path) URL encode
static const char* encode_query_except_chars = ".-_~=&%"; // For query params(and encoded string)
static constexpr char encode_general_except_chars[] = ".-_~"; // For general URL encode
static constexpr char encode_path_except_chars[] = ".-_~/"; // For fuse(included path) URL encode
static constexpr char encode_query_except_chars[] = ".-_~=&%"; // For query params(and encoded string)
static std::string rawUrlEncode(const std::string &s, const char* except_chars)
{
@ -381,7 +380,7 @@ std::string s3fs_hex_upper(const unsigned char* input, size_t length)
std::string s3fs_base64(const unsigned char* input, size_t length)
{
static const char base[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
static constexpr char base[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
std::string result;
result.reserve(((length + 3 - 1) / 3) * 4 + 1);
@ -460,7 +459,7 @@ std::string s3fs_decode64(const char* input, size_t input_len)
// Base location for transform. The range 0xE000 - 0xF8ff
// is a private range, se use the start of this range.
static const unsigned int escape_base = 0xe000;
static constexpr unsigned int escape_base = 0xe000;
// encode bytes into wobbly utf8.
// 'result' can be null. returns true if transform was needed.

View File

@ -30,7 +30,7 @@
//-------------------------------------------------------------------
// Global variables
//-------------------------------------------------------------------
extern const char SPACES[];
static constexpr char SPACES[] = " \t\r\n";
//-------------------------------------------------------------------
// Inline functions

View File

@ -34,27 +34,27 @@
//---------------------------------------------------------
// Const
//---------------------------------------------------------
const char usage_string[] = "Usage : \"mknod_test <base file path>\"";
static constexpr char usage_string[] = "Usage : \"mknod_test <base file path>\"";
const char str_mode_reg[] = "REGULAR";
const char str_mode_chr[] = "CHARACTER";
const char str_mode_blk[] = "BLOCK";
const char str_mode_fifo[] = "FIFO";
const char str_mode_sock[] = "SOCK";
static constexpr char str_mode_reg[] = "REGULAR";
static constexpr char str_mode_chr[] = "CHARACTER";
static constexpr char str_mode_blk[] = "BLOCK";
static constexpr char str_mode_fifo[] = "FIFO";
static constexpr char str_mode_sock[] = "SOCK";
const char str_ext_reg[] = "reg";
const char str_ext_chr[] = "chr";
const char str_ext_blk[] = "blk";
const char str_ext_fifo[] = "fifo";
const char str_ext_sock[] = "sock";
static constexpr char str_ext_reg[] = "reg";
static constexpr char str_ext_chr[] = "chr";
static constexpr char str_ext_blk[] = "blk";
static constexpr char str_ext_fifo[] = "fifo";
static constexpr char str_ext_sock[] = "sock";
// [NOTE]
// It would be nice if PATH_MAX could be used as is, but since there are
// issues using on Linux and we also must support for macos, this simple
// test program defines a fixed value for simplicity.
//
static const size_t S3FS_TEST_PATH_MAX = 255;
static const size_t MAX_BASE_PATH_LENGTH = S3FS_TEST_PATH_MAX - 5;
static constexpr size_t S3FS_TEST_PATH_MAX = 255;
static constexpr size_t MAX_BASE_PATH_LENGTH = S3FS_TEST_PATH_MAX - 5;
//---------------------------------------------------------
// Test function

View File

@ -49,7 +49,7 @@ typedef std::list<std::string> strlist_t;
//---------------------------------------------------------
// Const
//---------------------------------------------------------
const char usage_string[] = "Usage : \"write_multiblock -f <file path> -p <start offset:size>\" (allows -f and -p multiple times.)";
static constexpr char usage_string[] = "Usage : \"write_multiblock -f <file path> -p <start offset:size>\" (allows -f and -p multiple times.)";
//---------------------------------------------------------
// Utility functions