]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
C++-ify language functions
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 6 May 2020 19:32:22 +0000 (21:32 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 6 May 2020 19:46:31 +0000 (21:46 +0200)
src/argprocessing.cpp
src/language.cpp
src/language.hpp

index 7de5cfa2ee4839876281fa42ea0adffb7090702b..df7f924d24686742f8c2554f9f4cf0c6c321f14c 100644 (file)
@@ -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) {
index 315312d5198e9c832808b55643f411e601021279..1b4955dc021e0a6f7fcc48a55f1a2c01397e5000 100644 (file)
 
 #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);
 }
index 83dd41802562bdaf27c5f4c70f39c0a92a20ff44..69f7f269e1c2fc024263af13d20adec239aeded3 100644 (file)
@@ -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.
 //
 
 #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 <string>
+
+// 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);