From: Alexander Lanin Date: Tue, 25 Feb 2020 20:14:01 +0000 (+0100) Subject: Low hanging fruits of some auto fixable improvements via clang-tidy (#545) X-Git-Tag: v4.0~585 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9b11996d0fceb4e5c13c9c39fbfba65c96fa2694;p=thirdparty%2Fccache.git Low hanging fruits of some auto fixable improvements via clang-tidy (#545) --- diff --git a/.clang-tidy b/.clang-tidy index f6e7172e7..89196e661 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -2,5 +2,7 @@ Checks: 'clang-diagnostic-*,clang-analyzer-*,-clang-analyzer-alpha*,-clang-analyzer-optin.performance.Padding' HeaderFilterRegex: 'src/.*|unittest/.*' CheckOptions: + - key: readability-braces-around-statements.ShortStatementLines + value: 1 ... diff --git a/src/ArgsInfo.hpp b/src/ArgsInfo.hpp index 33c6f7049..39aea7e39 100644 --- a/src/ArgsInfo.hpp +++ b/src/ArgsInfo.hpp @@ -95,7 +95,7 @@ struct ArgsInfo // Array for storing -arch options. static constexpr int max_arch_args = 10; size_t arch_args_size = 0; - char* arch_args[max_arch_args] = {NULL}; + char* arch_args[max_arch_args] = {nullptr}; // Relocating debuginfo in the format old=new. char** debug_prefix_maps = nullptr; diff --git a/src/Config.cpp b/src/Config.cpp index 0dfb2d071..30a5f11df 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -654,6 +654,8 @@ void Config::visit_items(const ItemVisitor& item_visitor) const { std::vector keys; + keys.reserve(k_config_key_table.size()); + for (const auto& item : k_config_key_table) { keys.emplace_back(item.first); } diff --git a/src/FormatNonstdStringView.hpp b/src/FormatNonstdStringView.hpp index ded6de1d5..0770a9012 100644 --- a/src/FormatNonstdStringView.hpp +++ b/src/FormatNonstdStringView.hpp @@ -30,7 +30,7 @@ template<> struct formatter { template constexpr auto - parse(ParseContext& ctx) -> decltype(ctx.begin()) + parse(ParseContext& ctx) const -> decltype(ctx.begin()) { return ctx.begin(); } diff --git a/src/args.cpp b/src/args.cpp index c2b5bf09f..85fcfac95 100644 --- a/src/args.cpp +++ b/src/args.cpp @@ -27,7 +27,7 @@ args_init(int init_argc, const char* const* init_args) struct args* args = (struct args*)x_malloc(sizeof(struct args)); args->argc = 0; args->argv = (char**)x_malloc(sizeof(char*)); - args->argv[0] = NULL; + args->argv[0] = nullptr; for (int i = 0; i < init_argc; i++) { args_add(args, init_args[i]); } @@ -39,11 +39,11 @@ args_init_from_string(const char* command) { char* p = x_strdup(command); char* q = p; - char *word, *saveptr = NULL; - struct args* args = args_init(0, NULL); + char *word, *saveptr = nullptr; + struct args* args = args_init(0, nullptr); while ((word = strtok_r(q, " \t\r\n", &saveptr))) { args_add(args, word); - q = NULL; + q = nullptr; } free(p); @@ -55,10 +55,10 @@ args_init_from_gcc_atfile(const char* filename) { char* argtext; if (!(argtext = read_text_file(filename, 0))) { - return NULL; + return nullptr; } - struct args* args = args_init(0, NULL); + struct args* args = args_init(0, nullptr); char* pos = argtext; char* argbuf = static_cast(x_malloc(strlen(argtext) + 1)); char* argpos = argbuf; @@ -210,7 +210,7 @@ args_add(struct args* args, const char* s) args->argv = (char**)x_realloc(args->argv, (args->argc + 2) * sizeof(char*)); args->argv[args->argc] = x_strdup(s); args->argc++; - args->argv[args->argc] = NULL; + args->argv[args->argc] = nullptr; } // Add all arguments in to_append to args. @@ -229,7 +229,7 @@ args_pop(struct args* args, int n) while (n--) { args->argc--; free(args->argv[args->argc]); - args->argv[args->argc] = NULL; + args->argv[args->argc] = nullptr; } } diff --git a/src/ccache.cpp b/src/ccache.cpp index 9f44a6557..97c7c1e62 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -137,7 +137,7 @@ struct pending_tmp_file }; // Temporary files to remove at program exit. -static struct pending_tmp_file* pending_tmp_files = NULL; +static struct pending_tmp_file* pending_tmp_files = nullptr; // How often (in seconds) to scan $CCACHE_DIR/tmp for left-over temporary // files. @@ -165,11 +165,11 @@ add_prefix(const Context& ctx, struct args* args, const char* prefix_command) return; } - struct args* prefix = args_init(0, NULL); + struct args* prefix = args_init(0, nullptr); char* e = x_strdup(prefix_command); - char* saveptr = NULL; + char* saveptr = nullptr; for (char* tok = strtok_r(e, " ", &saveptr); tok; - tok = strtok_r(NULL, " ", &saveptr)) { + tok = strtok_r(nullptr, " ", &saveptr)) { char* p; p = find_executable(ctx, tok, MYNAME); @@ -204,7 +204,7 @@ failed(enum stats stat, optional exit_code) static const char* temp_dir(const Context& ctx) { - static const char* path = NULL; + static const char* path = nullptr; if (path) { return path; // Memoize } @@ -216,20 +216,20 @@ temp_dir(const Context& ctx) } void -block_signals(void) +block_signals() { #ifndef _WIN32 - sigprocmask(SIG_BLOCK, &fatal_signal_set, NULL); + sigprocmask(SIG_BLOCK, &fatal_signal_set, nullptr); #endif } void -unblock_signals(void) +unblock_signals() { #ifndef _WIN32 sigset_t empty; sigemptyset(&empty); - sigprocmask(SIG_SETMASK, &empty, NULL); + sigprocmask(SIG_SETMASK, &empty, nullptr); #endif } @@ -245,7 +245,7 @@ add_pending_tmp_file(const char* path) } static void -do_clean_up_pending_tmp_files(void) +do_clean_up_pending_tmp_files() { struct pending_tmp_file* p = pending_tmp_files; while (p) { @@ -258,7 +258,7 @@ do_clean_up_pending_tmp_files(void) } static void -clean_up_pending_tmp_files(void) +clean_up_pending_tmp_files() { block_signals(); do_clean_up_pending_tmp_files(); @@ -276,7 +276,7 @@ signal_handler(int signum) // If ccache was killed explicitly, then bring the compiler subprocess (if // any) with us as well. if (signum == SIGTERM && compiler_pid != 0 - && waitpid(compiler_pid, NULL, WNOHANG) == 0) { + && waitpid(compiler_pid, nullptr, WNOHANG) == 0) { kill(compiler_pid, signum); } @@ -284,7 +284,7 @@ signal_handler(int signum) if (compiler_pid != 0) { // Wait for compiler subprocess to exit before we snuff it. - waitpid(compiler_pid, NULL, 0); + waitpid(compiler_pid, nullptr, 0); } // Resend signal to ourselves to exit properly after returning from the @@ -302,11 +302,11 @@ register_signal_handler(int signum) # ifdef SA_RESTART act.sa_flags = SA_RESTART; # endif - sigaction(signum, &act, NULL); + sigaction(signum, &act, nullptr); } static void -set_up_signal_handlers(void) +set_up_signal_handlers() { sigemptyset(&fatal_signal_set); sigaddset(&fatal_signal_set, SIGINT); @@ -332,7 +332,7 @@ set_up_signal_handlers(void) static void clean_up_internal_tempdir(const Context& ctx) { - time_t now = time(NULL); + time_t now = time(nullptr); auto st = Stat::stat(ctx.config.cache_dir(), Stat::OnError::log); if (!st || st.mtime() + k_tempdir_cleanup_interval >= now) { // No cleanup needed. @@ -549,7 +549,7 @@ do_remember_include_file(Context& ctx, if (ctx.config.direct_mode()) { if (!is_pch) { // else: the file has already been hashed. - char* source = NULL; + char* source = nullptr; size_t size; if (st.size() > 0) { if (!read_file(path.c_str(), st.size(), &source, &size)) { @@ -688,14 +688,14 @@ process_preprocessed_file(Context& ctx, ctx.ignore_headers = nullptr; ctx.ignore_headers_len = 0; if (!ctx.config.ignore_headers_in_manifest().empty()) { - char *header, *p, *q, *saveptr = NULL; + char *header, *p, *q, *saveptr = nullptr; p = x_strdup(ctx.config.ignore_headers_in_manifest().c_str()); q = p; while ((header = strtok_r(q, PATH_DELIM, &saveptr))) { ctx.ignore_headers = static_cast(x_realloc( ctx.ignore_headers, (ctx.ignore_headers_len + 1) * sizeof(char*))); ctx.ignore_headers[ctx.ignore_headers_len++] = x_strdup(header); - q = NULL; + q = nullptr; } free(p); } @@ -817,7 +817,7 @@ process_preprocessed_file(Context& ctx, hash_string_buffer(hash, inc_path, strlen(inc_path)); } - remember_include_file(ctx, inc_path, hash, system, NULL); + remember_include_file(ctx, inc_path, hash, system, nullptr); free(inc_path); p = q; // Everything of interest between p and q has been hashed now. } else if (q[0] == '.' && q[1] == 'i' && q[2] == 'n' && q[3] == 'c' @@ -857,7 +857,7 @@ process_preprocessed_file(Context& ctx, std::string pch_path = make_relative_path(ctx, ctx.included_pch_file.c_str()); hash_string(hash, pch_path); - remember_include_file(ctx, pch_path, hash, false, NULL); + remember_include_file(ctx, pch_path, hash, false, nullptr); } bool debug_included = getenv("CCACHE_DEBUG_INCLUDED"); @@ -916,7 +916,7 @@ use_relative_paths_in_depfile(const Context& ctx) if (relpath != token) { free(relpath); } - token = strtok_r(NULL, " \t", &saveptr); + token = strtok_r(nullptr, " \t", &saveptr); } } @@ -968,7 +968,7 @@ result_name_from_depfile(Context& ctx, struct hash* hash) cc_log("Cannot open dependency file %s: %s", ctx.args_info.output_dep.c_str(), strerror(errno)); - return NULL; + return nullptr; } char buf[10000]; @@ -976,7 +976,7 @@ result_name_from_depfile(Context& ctx, struct hash* hash) char* saveptr; char* token; for (token = strtok_r(buf, " \t\n", &saveptr); token; - token = strtok_r(NULL, " \t\n", &saveptr)) { + token = strtok_r(nullptr, " \t\n", &saveptr)) { if (str_endswith(token, ":") || str_eq(token, "\\")) { continue; } @@ -996,7 +996,7 @@ result_name_from_depfile(Context& ctx, struct hash* hash) std::string pch_path = make_relative_path(ctx, ctx.included_pch_file.c_str()); hash_string(hash, pch_path); - remember_include_file(ctx, pch_path, hash, false, NULL); + remember_include_file(ctx, pch_path, hash, false, nullptr); } bool debug_included = getenv("CCACHE_DEBUG_INCLUDED"); @@ -1094,11 +1094,7 @@ create_cachedir_tag(nonstd::string_view dir) return false; } f << cachedir_tag; - if (!f) { - return false; - } - - return true; + return static_cast(f); } // Run the real compiler and put the result in cache. @@ -1180,7 +1176,7 @@ to_cache(Context& ctx, } add_prefix(ctx, depend_mode_args, ctx.config.prefix_command().c_str()); - ctx.time_of_compilation = time(NULL); + ctx.time_of_compilation = time(nullptr); status = execute( depend_mode_args->argv, tmp_stdout_fd, tmp_stderr_fd, &compiler_pid); args_free(depend_mode_args); @@ -1367,9 +1363,9 @@ to_cache(Context& ctx, static struct digest* get_result_name_from_cpp(Context& ctx, struct args* args, struct hash* hash) { - ctx.time_of_compilation = time(NULL); + ctx.time_of_compilation = time(nullptr); - char* path_stderr = NULL; + char* path_stderr = nullptr; char* path_stdout = nullptr; int status; if (ctx.args_info.direct_i_file) { @@ -1517,16 +1513,16 @@ hash_nvcc_host_compiler(const Context& ctx, #else const char* compilers[] = {"gcc", "g++"}; #endif - for (size_t i = 0; i < ARRAY_SIZE(compilers); i++) { + for (const char* compiler : compilers) { if (ccbin) { - char* path = format("%s/%s", ccbin, compilers[i]); + char* path = format("%s/%s", ccbin, compiler); auto st = Stat::stat(path); if (st) { hash_compiler(ctx, hash, st, path, false); } free(path); } else { - char* path = find_executable(ctx, compilers[i], MYNAME); + char* path = find_executable(ctx, compiler, MYNAME); if (path) { auto st = Stat::stat(path, Stat::OnError::log); hash_compiler(ctx, hash, st, ccbin, false); @@ -1580,7 +1576,8 @@ hash_common_info(const Context& ctx, if (!(ctx.config.sloppiness() & SLOPPY_LOCALE)) { // Hash environment variables that may affect localization of compiler // warning messages. - const char* envvars[] = {"LANG", "LC_ALL", "LC_CTYPE", "LC_MESSAGES", NULL}; + const char* envvars[] = { + "LANG", "LC_ALL", "LC_CTYPE", "LC_MESSAGES", nullptr}; for (const char** p = envvars; *p; ++p) { char* v = getenv(*p); if (v) { @@ -1658,14 +1655,14 @@ hash_common_info(const Context& ctx, char* p = x_strdup(ctx.config.extra_files_to_hash().c_str()); char* q = p; char* path; - char* saveptr = NULL; + char* saveptr = nullptr; while ((path = strtok_r(q, PATH_DELIM, &saveptr))) { cc_log("Hashing extra file %s", path); hash_delimiter(hash, "extrafile"); if (!hash_file(hash, path)) { failed(STATS_BADEXTRAFILE); } - q = NULL; + q = nullptr; } free(p); } @@ -1787,7 +1784,7 @@ calculate_result_name(Context& ctx, } } - char* p = NULL; + char* p = nullptr; if (str_startswith(args->argv[i], "-specs=")) { p = args->argv[i] + 7; } else if (str_startswith(args->argv[i], "--specs=")) { @@ -1857,7 +1854,7 @@ calculate_result_name(Context& ctx, } if (!found_ccbin && ctx.args_info.actual_language == "cu") { - hash_nvcc_host_compiler(ctx, hash, NULL, NULL); + hash_nvcc_host_compiler(ctx, hash, nullptr, nullptr); } // For profile generation (-fprofile-arcs, -fprofile-generate): @@ -1895,7 +1892,7 @@ calculate_result_name(Context& ctx, hash_string(hash, ctx.args_info.arch_args[i]); } - struct digest* result_name = NULL; + struct digest* result_name = nullptr; if (direct_mode) { // Hash environment variables that affect the preprocessor output. const char* envvars[] = {"CPATH", @@ -1903,7 +1900,7 @@ calculate_result_name(Context& ctx, "CPLUS_INCLUDE_PATH", "OBJC_INCLUDE_PATH", "OBJCPLUS_INCLUDE_PATH", // clang - NULL}; + nullptr}; for (const char** p = envvars; *p; ++p) { char* v = getenv(*p); if (v) { @@ -1935,7 +1932,7 @@ calculate_result_name(Context& ctx, if (result & HASH_SOURCE_CODE_FOUND_TIME) { cc_log("Disabling direct mode"); ctx.config.set_direct_mode(false); - return NULL; + return nullptr; } char manifest_name_string[DIGEST_STRING_BUFFER_SIZE]; @@ -1970,7 +1967,7 @@ calculate_result_name(Context& ctx, ctx.args_info.arch_args[i]); if (i != ctx.args_info.arch_args_size - 1) { free(result_name); - result_name = NULL; + result_name = nullptr; } args_pop(preprocessor_args, 1); } @@ -2112,7 +2109,7 @@ is_precompiled_header(const char* path) } static bool -color_output_possible(void) +color_output_possible() { const char* term_env = getenv("TERM"); return isatty(STDERR_FILENO) && term_env && strcasecmp(term_env, "DUMB") != 0; @@ -2122,7 +2119,7 @@ static bool detect_pch(Context& ctx, const char* option, const char* arg, bool* found_pch) { // Try to be smart about detecting precompiled headers. - char* pch_file = NULL; + char* pch_file = nullptr; if (str_eq(option, "-include-pch") || str_eq(option, "-include-pth")) { if (Stat::stat(arg)) { cc_log("Detected use of precompiled header: %s", arg); @@ -2215,23 +2212,24 @@ process_args(Context& ctx, // * those that only should be passed to the preprocessor (if run_second_cpp // is false), and // * dependency options (like -MD and friends). - struct args* common_args = args_init(0, NULL); + struct args* common_args = args_init(0, nullptr); ArgsScopeGuard common_args_guard(common_args); // cpp_args contains arguments that were not added to common_args, i.e. those // that should only be passed to the preprocessor if run_second_cpp is false. // If run_second_cpp is true, they will be passed to the compiler as well. - struct args* cpp_args = args_init(0, NULL); + struct args* cpp_args = args_init(0, nullptr); ArgsScopeGuard cpp_args_guard(cpp_args); // dep_args contains dependency options like -MD. They are only passed to the // preprocessor, never to the compiler. - struct args* dep_args = args_init(0, NULL); + struct args* dep_args = args_init(0, nullptr); ArgsScopeGuard dep_args_guard(dep_args); // compiler_only_args contains arguments that should only be passed to the // compiler, not the preprocessor. - struct args* compiler_only_args = args_init(0, NULL); // will leak on failure + struct args* compiler_only_args = + args_init(0, nullptr); // will leak on failure bool found_color_diagnostics = false; bool found_directives_only = false; @@ -3615,7 +3613,7 @@ do_cache_compilation(Context& ctx, char* argv[]) // Need to dump log buffer as the last exit function to not lose any logs. exitfn_add_last(dump_debug_log_buffer_exitfn, &ctx); - FILE* debug_text_file = NULL; + FILE* debug_text_file = nullptr; if (ctx.config.debug()) { std::string path = fmt::format("{}.ccache-input-text", ctx.args_info.output_obj); @@ -3652,8 +3650,8 @@ do_cache_compilation(Context& ctx, char* argv[]) args_extend(args_to_hash, extra_args_to_hash); bool put_result_in_manifest = false; - struct digest* result_name = NULL; - struct digest* result_name_from_manifest = NULL; + struct digest* result_name = nullptr; + struct digest* result_name_from_manifest = nullptr; if (ctx.config.direct_mode()) { cc_log("Trying direct lookup"); MTR_BEGIN("hash", "direct_hash"); @@ -3742,7 +3740,8 @@ do_cache_compilation(Context& ctx, char* argv[]) add_prefix(ctx, compiler_args, ctx.config.prefix_command().c_str()); // In depend_mode, extend the direct hash. - struct hash* depend_mode_hash = ctx.config.depend_mode() ? direct_hash : NULL; + struct hash* depend_mode_hash = + ctx.config.depend_mode() ? direct_hash : nullptr; // Run real compiler, sending output to cache. MTR_BEGIN("cache", "to_cache"); @@ -3764,30 +3763,30 @@ handle_main_options(int argc, char* argv[]) PRINT_STATS, }; static const struct option options[] = { - {"cleanup", no_argument, 0, 'c'}, - {"clear", no_argument, 0, 'C'}, - {"dump-manifest", required_argument, 0, DUMP_MANIFEST}, - {"dump-result", required_argument, 0, DUMP_RESULT}, - {"get-config", required_argument, 0, 'k'}, - {"hash-file", required_argument, 0, HASH_FILE}, - {"help", no_argument, 0, 'h'}, - {"max-files", required_argument, 0, 'F'}, - {"max-size", required_argument, 0, 'M'}, - {"print-stats", no_argument, 0, PRINT_STATS}, - {"recompress", required_argument, 0, 'X'}, - {"set-config", required_argument, 0, 'o'}, - {"show-compression", no_argument, 0, 'x'}, - {"show-config", no_argument, 0, 'p'}, - {"show-stats", no_argument, 0, 's'}, - {"version", no_argument, 0, 'V'}, - {"zero-stats", no_argument, 0, 'z'}, - {0, 0, 0, 0}}; + {"cleanup", no_argument, nullptr, 'c'}, + {"clear", no_argument, nullptr, 'C'}, + {"dump-manifest", required_argument, nullptr, DUMP_MANIFEST}, + {"dump-result", required_argument, nullptr, DUMP_RESULT}, + {"get-config", required_argument, nullptr, 'k'}, + {"hash-file", required_argument, nullptr, HASH_FILE}, + {"help", no_argument, nullptr, 'h'}, + {"max-files", required_argument, nullptr, 'F'}, + {"max-size", required_argument, nullptr, 'M'}, + {"print-stats", no_argument, nullptr, PRINT_STATS}, + {"recompress", required_argument, nullptr, 'X'}, + {"set-config", required_argument, nullptr, 'o'}, + {"show-compression", no_argument, nullptr, 'x'}, + {"show-config", no_argument, nullptr, 'p'}, + {"show-stats", no_argument, nullptr, 's'}, + {"version", no_argument, nullptr, 'V'}, + {"zero-stats", no_argument, nullptr, 'z'}, + {nullptr, 0, nullptr, 0}}; Context& ctx = initialize(argc, argv); (void)ctx; int c; - while ((c = getopt_long(argc, argv, "cCk:hF:M:po:sVxX:z", options, NULL)) + while ((c = getopt_long(argc, argv, "cCk:hF:M:po:sVxX:z", options, nullptr)) != -1) { switch (c) { case DUMP_MANIFEST: @@ -3845,7 +3844,7 @@ handle_main_options(int argc, char* argv[]) break; case 'F': { // --max-files - ctx.config.set_value_in_file( + Config::set_value_in_file( ctx.config.primary_config_path(), "max_files", optarg); unsigned files = atoi(optarg); if (files == 0) { @@ -3861,7 +3860,7 @@ handle_main_options(int argc, char* argv[]) if (!parse_size_with_suffix(optarg, &size)) { fatal("invalid size: %s", optarg); } - ctx.config.set_value_in_file( + Config::set_value_in_file( ctx.config.primary_config_path(), "max_size", optarg); if (size == 0) { printf("Unset cache size limit\n"); @@ -3880,8 +3879,7 @@ handle_main_options(int argc, char* argv[]) } char* key = x_strndup(optarg, p - optarg); char* value = p + 1; - ctx.config.set_value_in_file( - ctx.config.primary_config_path(), key, value); + Config::set_value_in_file(ctx.config.primary_config_path(), key, value); free(key); break; } diff --git a/src/cleanup.cpp b/src/cleanup.cpp index 65589b6de..0001db28b 100644 --- a/src/cleanup.cpp +++ b/src/cleanup.cpp @@ -61,7 +61,7 @@ clean_up_dir(const std::string& subdir, uint64_t cache_size = 0; uint32_t files_in_cache = 0; - time_t current_time = time(NULL); + time_t current_time = time(nullptr); for (size_t i = 0; i < files.size(); ++i, progress_receiver(1.0 / 3 + 1.0 * i / files.size() / 3)) { diff --git a/src/compopt.cpp b/src/compopt.cpp index 885cfa023..3d70b89e2 100644 --- a/src/compopt.cpp +++ b/src/compopt.cpp @@ -183,11 +183,11 @@ compopt_short(bool (*fn)(const char*), const char* option) } // Used by unittest/test_compopt.c. -bool compopt_verify_sortedness_and_flags(void); +bool compopt_verify_sortedness_and_flags(); // For test purposes. bool -compopt_verify_sortedness_and_flags(void) +compopt_verify_sortedness_and_flags() { for (size_t i = 0; i < ARRAY_SIZE(compopts); i++) { if (compopts[i].type & TOO_HARD && compopts[i].type & TAKES_CONCAT_ARG) { diff --git a/src/counters.cpp b/src/counters.cpp index 2de240814..6eda43a44 100644 --- a/src/counters.cpp +++ b/src/counters.cpp @@ -28,7 +28,7 @@ struct counters* counters_init(size_t initial_size) { auto c = static_cast(x_malloc(sizeof(counters))); - c->data = NULL; + c->data = nullptr; c->size = 0; c->allocated = 0; counters_resize(c, initial_size); diff --git a/src/execute.cpp b/src/execute.cpp index b47741950..67e1d9504 100644 --- a/src/execute.cpp +++ b/src/execute.cpp @@ -314,7 +314,7 @@ find_executable(const Context& ctx, const char* name, const char* exclude_name) } if (!path) { cc_log("No PATH variable"); - return NULL; + return nullptr; } return find_executable_in_path(name, exclude_name, path); @@ -333,9 +333,9 @@ find_executable_in_path(const char* name, // Search the path looking for the first compiler of the right name that // isn't us. - char* saveptr = NULL; + char* saveptr = nullptr; for (char* tok = strtok_r(path_buf, PATH_DELIM, &saveptr); tok; - tok = strtok_r(NULL, PATH_DELIM, &saveptr)) { + tok = strtok_r(nullptr, PATH_DELIM, &saveptr)) { #ifdef _WIN32 char namebuf[MAX_PATH]; int ret = SearchPath(tok, name, NULL, sizeof(namebuf), namebuf, NULL); @@ -373,7 +373,7 @@ find_executable_in_path(const char* name, } free(path_buf); - return NULL; + return nullptr; } void diff --git a/src/exitfn.cpp b/src/exitfn.cpp index 612a84ac0..5b1b153c6 100644 --- a/src/exitfn.cpp +++ b/src/exitfn.cpp @@ -31,7 +31,7 @@ struct exit_function struct nullary_exit_function { - void (*function)(void); + void (*function)(); }; static struct exit_function* exit_functions; @@ -47,7 +47,7 @@ call_nullary_exit_function(void* context) // Initialize exit functions. Must be called once before exitfn_add* are used. void -exitfn_init(void) +exitfn_init() { if (atexit(exitfn_call) != 0) { fatal("atexit failed: %s", strerror(errno)); @@ -57,7 +57,7 @@ exitfn_init(void) // Add a nullary function to be called when ccache exits. Functions are called // in reverse order. void -exitfn_add_nullary(void (*function)(void)) +exitfn_add_nullary(void (*function)()) { auto p = static_cast(x_malloc(sizeof(exit_function))); p->function = reinterpret_cast(function); @@ -85,7 +85,7 @@ exitfn_add_last(void (*function)(void*), void* context) auto p = static_cast(x_malloc(sizeof(exit_function))); p->function = function; p->context = context; - p->next = NULL; + p->next = nullptr; struct exit_function** q = &exit_functions; while (*q) { @@ -105,17 +105,16 @@ exitfn_delete_context(Context* ctx) // Call added functions. void -exitfn_call(void) +exitfn_call() { struct exit_function* p = exit_functions; - exit_functions = NULL; + exit_functions = nullptr; while (p) { p->function(p->context); struct exit_function* q = p; p = p->next; free(q); } - if (context_to_clean_up) { - delete context_to_clean_up; - } + + delete context_to_clean_up; } diff --git a/src/hash.cpp b/src/hash.cpp index da0a813b5..edde65aeb 100644 --- a/src/hash.cpp +++ b/src/hash.cpp @@ -65,12 +65,12 @@ do_debug_text(struct hash* hash, const void* s, size_t len) } struct hash* -hash_init(void) +hash_init() { auto hash = static_cast(malloc(sizeof(struct hash))); blake2b_init(&hash->state, DIGEST_SIZE); - hash->debug_binary = NULL; - hash->debug_text = NULL; + hash->debug_binary = nullptr; + hash->debug_text = nullptr; return hash; } @@ -79,8 +79,8 @@ hash_copy(struct hash* hash) { auto result = static_cast(malloc(sizeof(struct hash))); result->state = hash->state; - result->debug_binary = NULL; - result->debug_text = NULL; + result->debug_binary = nullptr; + result->debug_text = nullptr; return result; } diff --git a/src/hash.hpp b/src/hash.hpp index cfc092a09..9b88a4130 100644 --- a/src/hash.hpp +++ b/src/hash.hpp @@ -43,7 +43,7 @@ bool digests_equal(const struct digest* d1, const struct digest* d2); struct hash; // Create a new hash state. -struct hash* hash_init(void); +struct hash* hash_init(); // Create a new hash state from an existing hash state. struct hash* hash_copy(struct hash* hash); diff --git a/src/hashutil.cpp b/src/hashutil.cpp index 58ebcb9c5..f8ac48535 100644 --- a/src/hashutil.cpp +++ b/src/hashutil.cpp @@ -210,7 +210,7 @@ hash_source_code_string(const Config& config, // Make sure that the hash sum changes if the (potential) expansion of // __DATE__ changes. - time_t t = time(NULL); + time_t t = time(nullptr); struct tm now; hash_delimiter(hash, "date"); if (!localtime_r(&t, &now)) { @@ -430,13 +430,13 @@ hash_multicommand_output(struct hash* hash, char* command_string = x_strdup(commands); char* p = command_string; char* command; - char* saveptr = NULL; + char* saveptr = nullptr; bool ok = true; while ((command = strtok_r(p, ";", &saveptr))) { if (!hash_command_output(hash, command, compiler)) { ok = false; } - p = NULL; + p = nullptr; } free(command_string); return ok; diff --git a/src/language.cpp b/src/language.cpp index df8b9d4eb..315312d51 100644 --- a/src/language.cpp +++ b/src/language.cpp @@ -66,7 +66,7 @@ static const struct {".tcc", "c++-header"}, {".TCC", "c++-header"}, {".cu", "cu"}, - {NULL, NULL}, + {nullptr, nullptr}, }; // Supported languages and corresponding preprocessed languages. @@ -92,7 +92,7 @@ static const struct {"objective-c++-cpp-output", "objective-c++-cpp-output"}, {"assembler-with-cpp", "assembler"}, {"assembler", "assembler"}, - {NULL, NULL}, + {nullptr, nullptr}, }; // Guess the language of a file based on its extension. Returns NULL if the @@ -106,7 +106,7 @@ language_for_file(const char* fname) return extensions[i].language; } } - return NULL; + return nullptr; } // Return the preprocessed language for a given language, or NULL if unknown. @@ -114,14 +114,14 @@ const char* p_language_for_language(const char* language) { if (!language) { - return NULL; + return nullptr; } for (int i = 0; languages[i].language; ++i) { if (str_eq(language, languages[i].language)) { return languages[i].p_language; } } - return NULL; + return nullptr; } // Return the default file extension (including dot) for a language, or NULL if @@ -130,20 +130,20 @@ const char* extension_for_language(const char* language) { if (!language) { - return NULL; + return nullptr; } for (int i = 0; extensions[i].extension; i++) { if (str_eq(language, extensions[i].language)) { return extensions[i].extension; } } - return NULL; + return nullptr; } bool language_is_supported(const char* language) { - return p_language_for_language(language) != NULL; + return p_language_for_language(language) != nullptr; } bool diff --git a/src/legacy_util.cpp b/src/legacy_util.cpp index ef46e5415..e43043645 100644 --- a/src/legacy_util.cpp +++ b/src/legacy_util.cpp @@ -109,7 +109,7 @@ mkstemp(char* name_template) #ifndef _WIN32 static mode_t -get_umask(void) +get_umask() { static bool mask_retrieved = false; static mode_t mask; @@ -138,7 +138,7 @@ clone_file(const char* src, const char* dest, bool via_tmp_file) } int dest_fd; - char* tmp_file = NULL; + char* tmp_file = nullptr; if (via_tmp_file) { tmp_file = x_strdup(dest); dest_fd = create_tmp_fd(&tmp_file); @@ -202,7 +202,7 @@ copy_file(const char* src, const char* dest, bool via_tmp_file) } int dest_fd; - char* tmp_file = NULL; + char* tmp_file = nullptr; if (via_tmp_file) { tmp_file = x_strdup(dest); dest_fd = create_tmp_fd(&tmp_file); @@ -246,7 +246,7 @@ move_file(const char* src, const char* dest) // Return a static string with the current hostname. const char* -get_hostname(void) +get_hostname() { static char hostname[260] = ""; @@ -320,7 +320,7 @@ get_hostname(void) // Return a string to be passed to mkstemp to create a temporary file. Also // tries to cope with NFS by adding the local hostname. const char* -tmp_string(void) +tmp_string() { static char* ret; if (!ret) { @@ -336,7 +336,7 @@ format(const char* format, ...) va_list ap; va_start(ap, format); - char* ptr = NULL; + char* ptr = nullptr; if (vasprintf(&ptr, format, ap) == -1) { fatal("Out of memory in format"); } @@ -403,7 +403,7 @@ x_malloc(size_t size) if (size == 0) { // malloc() may return NULL if size is zero, so always do this to make sure // that the code handles it regardless of platform. - return NULL; + return nullptr; } void* ret = malloc(size); if (!ret) { @@ -454,7 +454,7 @@ void reformat(char** ptr, const char* format, ...) { char* saved = *ptr; - *ptr = NULL; + *ptr = nullptr; va_list ap; va_start(ap, format); @@ -668,7 +668,7 @@ create_tmp_file(char** fname, const char* mode) // Return current user's home directory, or NULL if it can't be determined. const char* -get_home_directory(void) +get_home_directory() { const char* p = getenv("HOME"); if (p) { @@ -688,7 +688,7 @@ get_home_directory(void) } } #endif - return NULL; + return nullptr; } // Check whether s1 and s2 have the same executable name. @@ -729,7 +729,7 @@ void update_mtime(const char* path) { #ifdef HAVE_UTIMES - utimes(path, NULL); + utimes(path, nullptr); #else utime(path, NULL); #endif @@ -893,7 +893,7 @@ read_file(const char* path, size_t size_hint, char** data, size_t* size) if (ret == -1) { cc_log("Failed reading %s", path); free(*data); - *data = NULL; + *data = nullptr; return false; } @@ -913,7 +913,7 @@ read_text_file(const char* path, size_t size_hint) data[size] = '\0'; return data; } else { - return NULL; + return nullptr; } } @@ -972,7 +972,7 @@ char* subst_env_in_string(const char* str, char** errmsg) { assert(errmsg); - *errmsg = NULL; + *errmsg = nullptr; char* result = x_strdup(""); const char* p = str; // Interval start. @@ -982,7 +982,7 @@ subst_env_in_string(const char* str, char** errmsg) reformat(&result, "%s%.*s", result, (int)(q - p), p); if (!expand_variable(&q, &result, errmsg)) { free(result); - return NULL; + return nullptr; } p = q + 1; } @@ -1005,11 +1005,11 @@ set_cloexec_flag(int fd) } double -time_seconds(void) +time_seconds() { #ifdef HAVE_GETTIMEOFDAY struct timeval tv; - gettimeofday(&tv, NULL); + gettimeofday(&tv, nullptr); return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0; #else return (double)time(NULL); diff --git a/src/lockfile.cpp b/src/lockfile.cpp index 6092e6ad4..a58a4907b 100644 --- a/src/lockfile.cpp +++ b/src/lockfile.cpp @@ -33,9 +33,9 @@ bool lockfile_acquire(const char* path, unsigned staleness_limit) { char* lockfile = format("%s.lock", path); - char* my_content = NULL; - char* content = NULL; - char* initial_content = NULL; + char* my_content = nullptr; + char* content = nullptr; + char* initial_content = nullptr; const char* hostname = get_hostname(); bool acquired = false; unsigned to_sleep = 1000; // Microseconds. @@ -43,7 +43,8 @@ lockfile_acquire(const char* path, unsigned staleness_limit) while (true) { free(my_content); - my_content = format("%s:%d:%d", hostname, (int)getpid(), (int)time(NULL)); + my_content = + format("%s:%d:%d", hostname, (int)getpid(), (int)time(nullptr)); #if defined(_WIN32) || defined(__CYGWIN__) int fd = open(lockfile, O_WRONLY | O_CREAT | O_EXCL | O_BINARY, 0666); diff --git a/src/logging.cpp b/src/logging.cpp index f59877ad5..3ef5c785b 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -120,7 +120,7 @@ log_prefix(bool log_updated_time) char timestamp[100]; struct tm tm; struct timeval tv; - gettimeofday(&tv, NULL); + gettimeofday(&tv, nullptr); # ifdef __MINGW64_VERSION_MAJOR localtime_r((time_t*)&tv.tv_sec, &tm); # else @@ -150,11 +150,11 @@ log_prefix(bool log_updated_time) } } -static void warn_log_fail(void) ATTR_NORETURN; +static void warn_log_fail() ATTR_NORETURN; // Warn about failure writing to the log file and then exit. static void -warn_log_fail(void) +warn_log_fail() { // Note: Can't call fatal() since that would lead to recursion. fprintf(stderr, diff --git a/src/manifest.cpp b/src/manifest.cpp index 764a94871..961c62681 100644 --- a/src/manifest.cpp +++ b/src/manifest.cpp @@ -191,6 +191,8 @@ struct ManifestData } std::vector file_info_indexes; + file_info_indexes.reserve(included_files.size()); + for (const auto& item : included_files) { file_info_indexes.push_back(get_file_info_index(item.first, item.second, @@ -337,15 +339,15 @@ write_manifest(const Config& config, { uint64_t payload_size = 0; payload_size += 4; // n_files - for (size_t i = 0; i < mf.files.size(); ++i) { - payload_size += 2 + mf.files[i].length(); + for (const auto& file : mf.files) { + payload_size += 2 + file.length(); } payload_size += 4; // n_file_infos payload_size += mf.file_infos.size() * (4 + DIGEST_SIZE + 8 + 8 + 8); payload_size += 4; // n_results - for (size_t i = 0; i < mf.results.size(); ++i) { + for (const auto& result : mf.results) { payload_size += 4; // n_file_info_indexes - payload_size += mf.results[i].file_info_indexes.size() * 4; + payload_size += result.file_info_indexes.size() * 4; payload_size += DIGEST_SIZE; } @@ -357,27 +359,27 @@ write_manifest(const Config& config, Compression::level_from_config(config), payload_size); writer.write(mf.files.size()); - for (uint32_t i = 0; i < mf.files.size(); ++i) { - writer.write(mf.files[i].length()); - writer.write(mf.files[i].data(), mf.files[i].length()); + for (const auto& file : mf.files) { + writer.write(file.length()); + writer.write(file.data(), file.length()); } writer.write(mf.file_infos.size()); - for (uint32_t i = 0; i < mf.file_infos.size(); ++i) { - writer.write(mf.file_infos[i].index); - writer.write(mf.file_infos[i].digest.bytes, DIGEST_SIZE); - writer.write(mf.file_infos[i].fsize); - writer.write(mf.file_infos[i].mtime); - writer.write(mf.file_infos[i].ctime); + for (const auto& file_info : mf.file_infos) { + writer.write(file_info.index); + writer.write(file_info.digest.bytes, DIGEST_SIZE); + writer.write(file_info.fsize); + writer.write(file_info.mtime); + writer.write(file_info.ctime); } writer.write(mf.results.size()); - for (uint32_t i = 0; i < mf.results.size(); ++i) { - writer.write(mf.results[i].file_info_indexes.size()); - for (uint32_t j = 0; j < mf.results[i].file_info_indexes.size(); ++j) { - writer.write(mf.results[i].file_info_indexes[j]); + for (const auto& result : mf.results) { + writer.write(result.file_info_indexes.size()); + for (uint32_t j = 0; j < result.file_info_indexes.size(); ++j) { + writer.write(result.file_info_indexes[j]); } - writer.write(mf.results[i].name.bytes, DIGEST_SIZE); + writer.write(result.name.bytes, DIGEST_SIZE); } writer.finalize(); @@ -392,8 +394,8 @@ verify_result(const Context& ctx, std::unordered_map& stated_files, std::unordered_map& hashed_files) { - for (uint32_t i = 0; i < result.file_info_indexes.size(); ++i) { - const auto& fi = mf.file_infos[result.file_info_indexes[i]]; + for (uint32_t file_info_index : result.file_info_indexes) { + const auto& fi = mf.file_infos[file_info_index]; const auto& path = mf.files[fi.index]; auto stated_files_iter = stated_files.find(path); @@ -494,7 +496,7 @@ manifest_get(const Context& ctx, const std::string& path) std::unordered_map hashed_files; // Check newest result first since it's a bit more likely to match. - struct digest* name = NULL; + struct digest* name = nullptr; for (uint32_t i = mf->results.size(); i > 0; i--) { if (verify_result( ctx, *mf, mf->results[i - 1], stated_files, hashed_files)) { @@ -606,8 +608,8 @@ manifest_dump(const std::string& path, FILE* stream) char name[DIGEST_STRING_BUFFER_SIZE]; fmt::print(stream, " {}:\n", i); fmt::print(stream, " File info indexes:"); - for (unsigned j = 0; j < mf->results[i].file_info_indexes.size(); ++j) { - fmt::print(stream, " {}", mf->results[i].file_info_indexes[j]); + for (uint32_t file_info_index : mf->results[i].file_info_indexes) { + fmt::print(stream, " {}", file_info_index); } fmt::print(stream, "\n"); digest_as_string(&mf->results[i].name, name); diff --git a/src/stats.cpp b/src/stats.cpp index ebfccbeda..992bd6f41 100644 --- a/src/stats.cpp +++ b/src/stats.cpp @@ -75,90 +75,102 @@ static struct {STATS_CACHEHIT_DIR, "direct_cache_hit", "cache hit (direct)", - NULL, + nullptr, FLAG_ALWAYS}, {STATS_CACHEHIT_CPP, "preprocessed_cache_hit", "cache hit (preprocessed)", - NULL, + nullptr, FLAG_ALWAYS}, - {STATS_CACHEMISS, "cache_miss", "cache miss", NULL, FLAG_ALWAYS}, - {STATS_LINK, "called_for_link", "called for link", NULL, 0}, + {STATS_CACHEMISS, "cache_miss", "cache miss", nullptr, FLAG_ALWAYS}, + {STATS_LINK, "called_for_link", "called for link", nullptr, 0}, {STATS_PREPROCESSING, "called_for_preprocessing", "called for preprocessing", - NULL, + nullptr, + 0}, + {STATS_MULTIPLE, + "multiple_source_files", + "multiple source files", + nullptr, 0}, - {STATS_MULTIPLE, "multiple_source_files", "multiple source files", NULL, 0}, {STATS_STDOUT, "compiler_produced_stdout", "compiler produced stdout", - NULL, + nullptr, 0}, {STATS_NOOUTPUT, "compiler_produced_no_output", "compiler produced no output", - NULL, + nullptr, 0}, {STATS_EMPTYOUTPUT, "compiler_produced_empty_output", "compiler produced empty output", - NULL, + nullptr, 0}, - {STATS_STATUS, "compile_failed", "compile failed", NULL, 0}, - {STATS_ERROR, "internal_error", "ccache internal error", NULL, 0}, - {STATS_PREPROCESSOR, "preprocessor_error", "preprocessor error", NULL, 0}, + {STATS_STATUS, "compile_failed", "compile failed", nullptr, 0}, + {STATS_ERROR, "internal_error", "ccache internal error", nullptr, 0}, + {STATS_PREPROCESSOR, "preprocessor_error", "preprocessor error", nullptr, 0}, {STATS_CANTUSEPCH, "could_not_use_precompiled_header", "can't use precompiled header", - NULL, + nullptr, + 0}, + {STATS_CANTUSEMODULES, + "could_not_use_modules", + "can't use modules", + nullptr, 0}, - {STATS_CANTUSEMODULES, "could_not_use_modules", "can't use modules", NULL, 0}, {STATS_COMPILER, "could_not_find_compiler", "couldn't find the compiler", - NULL, + nullptr, 0}, - {STATS_MISSING, "missing_cache_file", "cache file missing", NULL, 0}, - {STATS_ARGS, "bad_compiler_arguments", "bad compiler arguments", NULL, 0}, + {STATS_MISSING, "missing_cache_file", "cache file missing", nullptr, 0}, + {STATS_ARGS, "bad_compiler_arguments", "bad compiler arguments", nullptr, 0}, {STATS_SOURCELANG, "unsupported_source_language", "unsupported source language", - NULL, + nullptr, + 0}, + {STATS_COMPCHECK, + "compiler_check_failed", + "compiler check failed", + nullptr, 0}, - {STATS_COMPCHECK, "compiler_check_failed", "compiler check failed", NULL, 0}, - {STATS_CONFTEST, "autoconf_test", "autoconf compile/link", NULL, 0}, + {STATS_CONFTEST, "autoconf_test", "autoconf compile/link", nullptr, 0}, {STATS_UNSUPPORTED_OPTION, "unsupported_compiler_option", "unsupported compiler option", - NULL, + nullptr, 0}, {STATS_UNSUPPORTED_DIRECTIVE, "unsupported_code_directive", "unsupported code directive", - NULL, + nullptr, 0}, - {STATS_OUTSTDOUT, "output_to_stdout", "output to stdout", NULL, 0}, + {STATS_OUTSTDOUT, "output_to_stdout", "output to stdout", nullptr, 0}, {STATS_BADOUTPUTFILE, "bad_output_file", "could not write to output file", - NULL, + nullptr, 0}, - {STATS_NOINPUT, "no_input_file", "no input file", NULL, 0}, + {STATS_NOINPUT, "no_input_file", "no input file", nullptr, 0}, {STATS_BADEXTRAFILE, "error_hashing_extra_file", "error hashing extra file", - NULL, + nullptr, 0}, {STATS_NUMCLEANUPS, "cleanups_performed", "cleanups performed", - NULL, + nullptr, FLAG_ALWAYS}, {STATS_NUMFILES, "files_in_cache", "files in cache", - NULL, + nullptr, FLAG_NOZERO | FLAG_ALWAYS}, {STATS_TOTALSIZE, "cache_size_kibibyte", @@ -168,14 +180,14 @@ static struct {STATS_OBSOLETE_MAXFILES, "OBSOLETE", "OBSOLETE", - NULL, + nullptr, FLAG_NOZERO | FLAG_NEVER}, {STATS_OBSOLETE_MAXSIZE, "OBSOLETE", "OBSOLETE", - NULL, + nullptr, FLAG_NOZERO | FLAG_NEVER}, - {STATS_NONE, NULL, NULL, NULL, 0}}; + {STATS_NONE, nullptr, nullptr, nullptr, 0}}; static char* format_size(uint64_t size) @@ -201,7 +213,7 @@ format_timestamp(uint64_t timestamp) strftime(buffer, sizeof(buffer), "%c", &tm); return format(" %s", buffer); } else { - return NULL; + return nullptr; } } @@ -350,10 +362,9 @@ stats_flush_to_file(const Config& config, } if (!config.log_file().empty() || config.debug()) { - for (int i = 0; i < STATS_END; ++i) { - if (updates->data[stats_info[i].stat] != 0 - && !(stats_info[i].flags & FLAG_NOZERO)) { - cc_log("Result: %s", stats_info[i].message); + for (auto& info : stats_info) { + if (updates->data[info.stat] != 0 && !(info.flags & FLAG_NOZERO)) { + cc_log("Result: %s", info.message); } } } @@ -527,7 +538,7 @@ stats_zero(const Config& config) x_unlink(fname); free(fname); - time_t timestamp = time(NULL); + time_t timestamp = time(nullptr); for (int dir = 0; dir <= 0xF; dir++) { struct counters* counters = counters_init(STATS_END); diff --git a/unittest/framework.hpp b/unittest/framework.hpp index cc6c67087..b3626d321 100644 --- a/unittest/framework.hpp +++ b/unittest/framework.hpp @@ -144,9 +144,9 @@ typedef unsigned (*suite_fn)(unsigned); int cct_run(const suite_fn* suites, int verbose); void cct_suite_begin(const char* name); -void cct_suite_end(void); +void cct_suite_end(); void cct_test_begin(const char* name); -void cct_test_end(void); +void cct_test_end(); void cct_check_passed(const char* file, int line, const char* assertion); void cct_check_failed(const char* file, int line, diff --git a/unittest/main.cpp b/unittest/main.cpp index 3d07a6fb5..7db9d7255 100644 --- a/unittest/main.cpp +++ b/unittest/main.cpp @@ -42,7 +42,7 @@ const suite_fn k_legacy_suites[] = { &suite_legacy_util, &suite_lockfile, &suite_stats, - NULL, + nullptr, }; int diff --git a/unittest/test_ZstdCompression.cpp b/unittest/test_ZstdCompression.cpp index 4eab5466f..c6fe00ca1 100644 --- a/unittest/test_ZstdCompression.cpp +++ b/unittest/test_ZstdCompression.cpp @@ -90,8 +90,8 @@ TEST_CASE("Large compressible Compression::Type::zstd roundtrip") TEST_CASE("Large uncompressible Compression::Type::zstd roundtrip") { char data[100000]; - for (size_t i = 0; i < sizeof(data); i++) { - data[i] = rand() % 256; + for (char& c : data) { + c = rand() % 256; } File f("data.zstd", "wb"); diff --git a/unittest/test_args.cpp b/unittest/test_args.cpp index 2d441021d..0324a0911 100644 --- a/unittest/test_args.cpp +++ b/unittest/test_args.cpp @@ -26,7 +26,7 @@ TEST_SUITE(args) TEST(args_init_empty) { - struct args* args = args_init(0, NULL); + struct args* args = args_init(0, nullptr); CHECK(args); CHECK_INT_EQ(0, args->argc); CHECK(!args->argv[0]); diff --git a/unittest/test_argument_processing.cpp b/unittest/test_argument_processing.cpp index 31e90022e..86d5ce12f 100644 --- a/unittest/test_argument_processing.cpp +++ b/unittest/test_argument_processing.cpp @@ -75,7 +75,7 @@ TEST(dash_E_should_result_in_called_for_preprocessing) struct args *preprocessed, *compiler; create_file("foo.c", ""); - CHECK(process_args(ctx, orig, &preprocessed, NULL, &compiler) + CHECK(process_args(ctx, orig, &preprocessed, nullptr, &compiler) == STATS_PREPROCESSING); args_free(orig); @@ -89,7 +89,7 @@ TEST(dash_M_should_be_unsupported) struct args *preprocessed, *compiler; create_file("foo.c", ""); - CHECK(process_args(ctx, orig, &preprocessed, NULL, &compiler) + CHECK(process_args(ctx, orig, &preprocessed, nullptr, &compiler) == STATS_UNSUPPORTED_OPTION); args_free(orig); @@ -105,12 +105,12 @@ TEST(dependency_args_to_preprocessor_if_run_second_cpp_is_false) struct args* orig = args_init_from_string("cc " DEP_ARGS " -c foo.c -o foo.o"); struct args* exp_cpp = args_init_from_string("cc " DEP_ARGS); - struct args* exp_extra = args_init(0, NULL); + struct args* exp_extra = args_init(0, nullptr); struct args* exp_cc = args_init_from_string("cc -c"); #undef DEP_ARGS - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); ctx.config.set_run_second_cpp(false); @@ -135,9 +135,9 @@ TEST(dependency_args_to_compiler_if_run_second_cpp_is_true) struct args* exp_extra = args_init_from_string(DEP_ARGS); struct args* exp_cc = args_init_from_string("cc -c " DEP_ARGS); #undef DEP_ARGS - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); CHECK(!process_args(ctx, orig, &act_cpp, &act_extra, &act_cc)); @@ -163,13 +163,13 @@ TEST(cpp_only_args_to_preprocessor_if_run_second_cpp_is_false) struct args* orig = args_init_from_string("cc " CPP_ARGS " " DEP_ARGS " -c foo.c -o foo.o"); struct args* exp_cpp = args_init_from_string("cc " CPP_ARGS " " DEP_ARGS); - struct args* exp_extra = args_init(0, NULL); + struct args* exp_extra = args_init(0, nullptr); struct args* exp_cc = args_init_from_string("cc -c"); #undef DEP_ARGS #undef CPP_ARGS - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); ctx.config.set_run_second_cpp(false); @@ -200,9 +200,9 @@ TEST(cpp_only_args_to_preprocessor_and_compiler_if_run_second_cpp_is_true) struct args* exp_cc = args_init_from_string("cc " CPP_ARGS " -c " DEP_ARGS); #undef DEP_ARGS #undef CPP_ARGS - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); CHECK(!process_args(ctx, orig, &act_cpp, &act_extra, &act_cc)); @@ -224,9 +224,9 @@ TEST(dependency_args_that_take_an_argument_should_not_require_space_delimiter) struct args* exp_extra = args_init_from_string(DEP_ARGS); struct args* exp_cc = args_init_from_string("cc -c " DEP_ARGS); #undef DEP_ARGS - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); CHECK(!process_args(ctx, orig, &act_cpp, &act_extra, &act_cc)); @@ -246,9 +246,9 @@ TEST(MQ_flag_should_not_be_added_if_run_second_cpp_is_true) struct args* exp_cpp = args_init_from_string("cc"); struct args* exp_extra = args_init_from_string("-MD -MF foo.d"); struct args* exp_cc = args_init_from_string("cc -c -MD -MF foo.d"); - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); CHECK(!process_args(ctx, orig, &act_cpp, &act_extra, &act_cc)); @@ -266,11 +266,11 @@ TEST(MQ_flag_should_be_added_if_run_second_cpp_is_false) struct args* orig = args_init_from_string("cc -c -MD foo.c -MF foo.d -o foo.o"); struct args* exp_cpp = args_init_from_string("cc -MD -MF foo.d -MQ foo.o"); - struct args* exp_extra = args_init(0, NULL); + struct args* exp_extra = args_init(0, nullptr); struct args* exp_cc = args_init_from_string("cc -c"); - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); ctx.config.set_run_second_cpp(false); @@ -288,11 +288,11 @@ TEST(MF_should_be_added_if_run_second_cpp_is_false) struct args* orig = args_init_from_string("cc -c -MD foo.c -o foo.o"); struct args* exp_cpp = args_init_from_string("cc -MD -MF foo.d -MQ foo.o"); - struct args* exp_extra = args_init(0, NULL); + struct args* exp_extra = args_init(0, nullptr); struct args* exp_cc = args_init_from_string("cc -c"); - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); @@ -313,9 +313,9 @@ TEST(MF_should_not_be_added_if_run_second_cpp_is_true) struct args* exp_cpp = args_init_from_string("cc"); struct args* exp_extra = args_init_from_string("-MD"); struct args* exp_cc = args_init_from_string("cc -c -MD"); - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); @@ -335,9 +335,9 @@ TEST(equal_sign_after_MF_should_be_removed) struct args* exp_cpp = args_init_from_string("cc"); struct args* exp_extra = args_init_from_string("-MFpath"); struct args* exp_cc = args_init_from_string("cc -c -MFpath"); - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); @@ -355,9 +355,9 @@ TEST(sysroot_should_be_rewritten_if_basedir_is_used) char* arg_string; struct args* orig; - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); ctx.config.set_base_dir(get_root()); @@ -380,9 +380,9 @@ TEST(sysroot_with_separate_argument_should_be_rewritten_if_basedir_is_used) char* arg_string; struct args* orig; - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); ctx.config.set_base_dir(get_root()); @@ -408,9 +408,9 @@ TEST(MF_flag_with_immediate_argument_should_work_as_last_argument) struct args* exp_cpp = args_init_from_string("cc"); struct args* exp_extra = args_init_from_string("-MMD -MT bar -MFfoo.d"); struct args* exp_cc = args_init_from_string("cc -c -MMD -MT bar -MFfoo.d"); - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); @@ -433,9 +433,9 @@ TEST(MT_flag_with_immediate_argument_should_work_as_last_argument) args_init_from_string("-MMD -MFfoo.d -MT foo -MTbar"); struct args* exp_cc = args_init_from_string("cc -c -MMD -MFfoo.d -MT foo -MTbar"); - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); @@ -458,9 +458,9 @@ TEST(MQ_flag_with_immediate_argument_should_work_as_last_argument) args_init_from_string("-MMD -MFfoo.d -MQ foo -MQbar"); struct args* exp_cc = args_init_from_string("cc -c -MMD -MFfoo.d -MQ foo -MQbar"); - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); @@ -482,9 +482,9 @@ TEST(MQ_flag_without_immediate_argument_should_not_add_MQobj) struct args* exp_extra = args_init_from_string("-MD -MP -MFfoo.d -MQ foo.d"); struct args* exp_cc = args_init_from_string("gcc -c -MD -MP -MFfoo.d -MQ foo.d"); - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); @@ -506,9 +506,9 @@ TEST(MT_flag_without_immediate_argument_should_not_add_MTobj) struct args* exp_extra = args_init_from_string("-MD -MP -MFfoo.d -MT foo.d"); struct args* exp_cc = args_init_from_string("gcc -c -MD -MP -MFfoo.d -MT foo.d"); - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); @@ -530,9 +530,9 @@ TEST(MQ_flag_with_immediate_argument_should_not_add_MQobj) struct args* exp_extra = args_init_from_string("-MD -MP -MFfoo.d -MQfoo.d"); struct args* exp_cc = args_init_from_string("gcc -c -MD -MP -MFfoo.d -MQfoo.d"); - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); @@ -554,9 +554,9 @@ TEST(MT_flag_with_immediate_argument_should_not_add_MQobj) struct args* exp_extra = args_init_from_string("-MD -MP -MFfoo.d -MTfoo.d"); struct args* exp_cc = args_init_from_string("gcc -c -MD -MP -MFfoo.d -MTfoo.d"); - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); @@ -575,11 +575,11 @@ TEST(fprofile_flag_with_existing_dir_should_be_rewritten_to_real_path) struct args* orig = args_init_from_string("gcc -c -fprofile-generate=some/dir foo.c"); struct args* exp_cpp = args_init_from_string("gcc"); - struct args* exp_extra = args_init(0, NULL); + struct args* exp_extra = args_init(0, nullptr); struct args* exp_cc = args_init_from_string("gcc"); - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; char* s; @@ -609,12 +609,12 @@ TEST(fprofile_flag_with_nonexistent_dir_should_not_be_rewritten) args_init_from_string("gcc -c -fprofile-generate=some/dir foo.c"); struct args* exp_cpp = args_init_from_string("gcc -fprofile-generate=some/dir"); - struct args* exp_extra = args_init(0, NULL); + struct args* exp_extra = args_init(0, nullptr); struct args* exp_cc = args_init_from_string("gcc -fprofile-generate=some/dir -c"); - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); @@ -632,9 +632,9 @@ TEST(isystem_flag_with_separate_arg_should_be_rewritten_if_basedir_is_used) char* arg_string; struct args* orig; - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); ctx.config.set_base_dir(get_root()); @@ -657,9 +657,9 @@ TEST(isystem_flag_with_concat_arg_should_be_rewritten_if_basedir_is_used) char* cwd; char* arg_string; struct args* orig; - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); ctx.config.set_base_dir("/"); // posix @@ -685,9 +685,9 @@ TEST(I_flag_with_concat_arg_should_be_rewritten_if_basedir_is_used) char* cwd; char* arg_string; struct args* orig; - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); ctx.config.set_base_dir(x_strdup("/")); // posix @@ -712,11 +712,11 @@ TEST(debug_flag_order_with_known_option_first) struct args* orig = args_init_from_string("cc -g1 -gsplit-dwarf foo.c -c"); struct args* exp_cpp = args_init_from_string("cc -g1 -gsplit-dwarf"); - struct args* exp_extra = args_init(0, NULL); + struct args* exp_extra = args_init(0, nullptr); struct args* exp_cc = args_init_from_string("cc -g1 -gsplit-dwarf -c"); - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); CHECK(!process_args(ctx, orig, &act_cpp, &act_extra, &act_cc)); @@ -733,11 +733,11 @@ TEST(debug_flag_order_with_known_option_last) struct args* orig = args_init_from_string("cc -gsplit-dwarf -g1 foo.c -c"); struct args* exp_cpp = args_init_from_string("cc -gsplit-dwarf -g1"); - struct args* exp_extra = args_init(0, NULL); + struct args* exp_extra = args_init(0, nullptr); struct args* exp_cc = args_init_from_string("cc -gsplit-dwarf -g1 -c"); - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); CHECK(!process_args(ctx, orig, &act_cpp, &act_extra, &act_cc)); @@ -759,9 +759,9 @@ TEST(options_not_to_be_passed_to_the_preprocesor) " -Wa,foo -Werror -Xlinker fie -Xlinker,fum -Wno-error"); struct args* exp_cc = args_init_from_string( "cc -g -Wa,foo -Werror -Xlinker fie -Xlinker,fum -Wno-error -DX -c"); - struct args* act_cpp = NULL; - struct args* act_extra = NULL; - struct args* act_cc = NULL; + struct args* act_cpp = nullptr; + struct args* act_extra = nullptr; + struct args* act_cc = nullptr; create_file("foo.c", ""); CHECK(!process_args(ctx, orig, &act_cpp, &act_extra, &act_cc)); diff --git a/unittest/test_compopt.cpp b/unittest/test_compopt.cpp index 6a331bc9c..71ebfd476 100644 --- a/unittest/test_compopt.cpp +++ b/unittest/test_compopt.cpp @@ -21,11 +21,12 @@ #include "../src/compopt.hpp" #include "framework.hpp" +bool compopt_verify_sortedness_and_flags(); + TEST_SUITE(compopt) TEST(option_table_should_be_sorted) { - bool compopt_verify_sortedness_and_flags(void); CHECK(compopt_verify_sortedness_and_flags()); } diff --git a/unittest/test_legacy_util.cpp b/unittest/test_legacy_util.cpp index fb88e99a8..3ddde80f1 100644 --- a/unittest/test_legacy_util.cpp +++ b/unittest/test_legacy_util.cpp @@ -127,7 +127,7 @@ TEST(parse_size_with_suffix) TEST(format_command) { - const char* argv[] = {"foo", "bar", NULL}; + const char* argv[] = {"foo", "bar", nullptr}; CHECK_STR_EQ_FREE2("foo bar\n", format_command(argv)); }