From: Joel Rosdahl Date: Wed, 6 May 2020 19:32:22 +0000 (+0200) Subject: C++-ify language functions X-Git-Tag: v4.0~492 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9fac7eee7ba789fe06fad68f95c5402118765ed2;p=thirdparty%2Fccache.git C++-ify language functions --- diff --git a/src/argprocessing.cpp b/src/argprocessing.cpp index 7de5cfa2e..df7f924d2 100644 --- a/src/argprocessing.cpp +++ b/src/argprocessing.cpp @@ -778,7 +778,7 @@ process_arg(Context& ctx, } if (!args_info.input_file.empty()) { - if (language_for_file(argv[i])) { + if (!language_for_file(argv[i]).empty()) { cc_log("Multiple input files: %s and %s", args_info.input_file.c_str(), argv[i]); @@ -916,10 +916,9 @@ process_args(Context& ctx, if (!state.explicit_language.empty() && state.explicit_language == "none") { state.explicit_language.clear(); } - state.file_language = - from_cstr(language_for_file(args_info.input_file.c_str())); + state.file_language = language_for_file(args_info.input_file); if (!state.explicit_language.empty()) { - if (!language_is_supported(state.explicit_language.c_str())) { + if (!language_is_supported(state.explicit_language)) { cc_log("Unsupported language: %s", state.explicit_language.c_str()); return STATS_SOURCELANG; } @@ -964,8 +963,7 @@ process_args(Context& ctx, config.set_run_second_cpp(true); } - args_info.direct_i_file = - language_is_preprocessed(args_info.actual_language.c_str()); + args_info.direct_i_file = language_is_preprocessed(args_info.actual_language); if (args_info.output_is_precompiled_header && !config.run_second_cpp()) { // It doesn't work to create the .gch from preprocessed source. @@ -974,9 +972,8 @@ process_args(Context& ctx, } if (config.cpp_extension().empty()) { - const char* p_language = - p_language_for_language(args_info.actual_language.c_str()); - config.set_cpp_extension(extension_for_language(p_language) + 1); + std::string p_language = p_language_for_language(args_info.actual_language); + config.set_cpp_extension(extension_for_language(p_language).substr(1)); } // Don't try to second guess the compilers heuristics for stdout handling. @@ -1124,8 +1121,7 @@ process_args(Context& ctx, // reset the language specified with -x, so if -x is given, we have to // specify the preprocessed language explicitly. args_add(*compiler_args, "-x"); - args_add(*compiler_args, - p_language_for_language(state.explicit_language.c_str())); + args_add(*compiler_args, p_language_for_language(state.explicit_language)); } if (state.found_c_opt) { diff --git a/src/language.cpp b/src/language.cpp index 315312d51..1b4955dc0 100644 --- a/src/language.cpp +++ b/src/language.cpp @@ -18,15 +18,17 @@ #include "language.hpp" -#include "legacy_util.hpp" +#include "Util.hpp" + +namespace { // Supported file extensions and corresponding languages (as in parameter to // the -x option). -static const struct +const struct { const char* extension; const char* language; -} extensions[] = { +} k_ext_lang_table[] = { {".c", "c"}, {".C", "c++"}, {".cc", "c++"}, @@ -70,11 +72,11 @@ static const struct }; // Supported languages and corresponding preprocessed languages. -static const struct +const struct { const char* language; const char* p_language; -} languages[] = { +} k_lang_p_lang_table[] = { {"c", "cpp-output"}, {"cpp-output", "cpp-output"}, {"c-header", "cpp-output"}, @@ -95,61 +97,50 @@ static const struct {nullptr, nullptr}, }; -// Guess the language of a file based on its extension. Returns NULL if the -// extension is unknown. -const char* -language_for_file(const char* fname) +} // namespace + +std::string +language_for_file(const std::string& fname) { - const char* p = get_extension(fname); - for (int i = 0; extensions[i].extension; i++) { - if (str_eq(p, extensions[i].extension)) { - return extensions[i].language; + auto ext = Util::get_extension(fname); + for (size_t i = 0; k_ext_lang_table[i].extension; ++i) { + if (k_ext_lang_table[i].extension == ext) { + return k_ext_lang_table[i].language; } } - return nullptr; + return {}; } -// Return the preprocessed language for a given language, or NULL if unknown. -const char* -p_language_for_language(const char* language) +std::string +p_language_for_language(const std::string& language) { - if (!language) { - return nullptr; - } - for (int i = 0; languages[i].language; ++i) { - if (str_eq(language, languages[i].language)) { - return languages[i].p_language; + for (size_t i = 0; k_lang_p_lang_table[i].language; ++i) { + if (language == k_lang_p_lang_table[i].language) { + return k_lang_p_lang_table[i].p_language; } } - return nullptr; + return {}; } -// Return the default file extension (including dot) for a language, or NULL if -// unknown. -const char* -extension_for_language(const char* language) +std::string +extension_for_language(const std::string& language) { - if (!language) { - return nullptr; - } - for (int i = 0; extensions[i].extension; i++) { - if (str_eq(language, extensions[i].language)) { - return extensions[i].extension; + for (size_t i = 0; k_ext_lang_table[i].extension; i++) { + if (language == k_ext_lang_table[i].language) { + return k_ext_lang_table[i].extension; } } - return nullptr; + return {}; } bool -language_is_supported(const char* language) +language_is_supported(const std::string& language) { - return p_language_for_language(language) != nullptr; + return !p_language_for_language(language).empty(); } bool -language_is_preprocessed(const char* language) +language_is_preprocessed(const std::string& language) { - const char* p_language = p_language_for_language(language); - assert(p_language); - return str_eq(language, p_language); + return language == p_language_for_language(language); } diff --git a/src/language.hpp b/src/language.hpp index 83dd41802..69f7f269e 100644 --- a/src/language.hpp +++ b/src/language.hpp @@ -1,4 +1,4 @@ -// Copyright (C) 2010-2019 Joel Rosdahl and other contributors +// Copyright (C) 2010-2020 Joel Rosdahl and other contributors // // See doc/AUTHORS.adoc for a complete list of contributors. // @@ -20,8 +20,22 @@ #include "system.hpp" -const char* language_for_file(const char* fname); -const char* p_language_for_language(const char* language); -const char* extension_for_language(const char* language); -bool language_is_supported(const char* language); -bool language_is_preprocessed(const char* language); +#include + +// Guess the language of `fname` based on its extension. Returns the empty +// string if the extension is unknown. +std::string language_for_file(const std::string& fname); + +// Return the preprocessed language for `language`, or the empty string if +// unknown. +std::string p_language_for_language(const std::string& language); + +// Return the default file extension (including dot) for `language`, or the +// empty string if unknown. +std::string extension_for_language(const std::string& language); + +// Return whether `language` is a supported language. +bool language_is_supported(const std::string& language); + +// Return whether `language` is supported preprocessed language. +bool language_is_preprocessed(const std::string& language);