]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Extract MSVC debug option logic to a function
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 19 Apr 2023 14:24:14 +0000 (16:24 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 19 Apr 2023 16:41:47 +0000 (18:41 +0200)
src/argprocessing.cpp

index 54507724c9174463bb0e552eb4431eceb5126619..6e38b4b873aa48b8f83ebb957a7e3c18ac3ed155 100644 (file)
@@ -71,9 +71,9 @@ struct ArgumentProcessingState
   bool found_md_or_mmd_opt = false;
   bool found_Wa_a_opt = false;
 
-  std::string explicit_language;       // As specified with -x.
-  std::string input_charset_option;    // -finput-charset=...
-  std::string last_seen_msvc_z_option; // /Z7, /Zi or /ZI
+  std::string explicit_language;             // As specified with -x.
+  std::string input_charset_option;          // -finput-charset=...
+  std::string last_seen_msvc_z_debug_option; // /Z7, /Zi or /ZI
 
   // Is the dependency file set via -Wp,-M[M]D,target or -MFtarget?
   OutputDepOrigin output_dep_origin = OutputDepOrigin::none;
@@ -277,6 +277,14 @@ make_dash_option(const Config& config, const std::string& arg)
   return new_arg;
 }
 
+bool
+is_msvc_z_debug_option(std::string_view arg)
+{
+  static const char* debug_options[] = {"-Z7", "-ZI", "-Zi"};
+  return std::find(std::begin(debug_options), std::end(debug_options), arg)
+         != std::end(debug_options);
+}
+
 // Returns std::nullopt if the option wasn't recognized, otherwise the error
 // code (with Statistic::none for "no error").
 std::optional<Statistic>
@@ -609,14 +617,10 @@ process_option_arg(const Context& ctx,
   }
 
   if (config.is_compiler_group_msvc() && !config.is_compiler_group_clang()
-      && util::starts_with(arg, "-Z")) {
-    // Exclude other options starting with /Z (/Zc), which are not debug flags
-    const char debug_mode = arg[2];
-    if (debug_mode == 'i' || debug_mode == '7' || debug_mode == 'I') {
-      state.last_seen_msvc_z_option = args[i];
-      state.common_args.push_back(args[i]);
-      return Statistic::none;
-    }
+      && is_msvc_z_debug_option(arg)) {
+    state.last_seen_msvc_z_debug_option = args[i];
+    state.common_args.push_back(args[i]);
+    return Statistic::none;
   }
 
   // These options require special handling, because they behave differently
@@ -1172,10 +1176,11 @@ process_args(Context& ctx)
     return Statistic::unsupported_compiler_option;
   }
 
-  if (!state.last_seen_msvc_z_option.empty()
-      && state.last_seen_msvc_z_option.substr(2) != "7") {
+  if (!state.last_seen_msvc_z_debug_option.empty()
+      && state.last_seen_msvc_z_debug_option.substr(2) != "7") {
     // /Zi and /ZI are unsupported, but /Z7 is fine.
-    LOG("Compiler option {} is unsupported", state.last_seen_msvc_z_option);
+    LOG("Compiler option {} is unsupported",
+        state.last_seen_msvc_z_debug_option);
     return Statistic::unsupported_compiler_option;
   }