]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
C++-ify -optf/--options-file processing in process_arg
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 5 May 2020 12:59:06 +0000 (14:59 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 5 May 2020 18:25:58 +0000 (20:25 +0200)
This also makes the implementation acually work with multiple option
files delimited by commas.

src/argprocessing.cpp
unittest/test_argument_processing.cpp

index 29ca36cf86afb554a838d74a22e24c9ed0bc1945..b36222c251f805db8a6fb649464d4f7dba787235 100644 (file)
@@ -243,35 +243,23 @@ process_arg(Context& ctx,
 
   // Handle cuda "-optf" and "--options-file" argument.
   if (ctx.guessed_compiler == GuessedCompiler::nvcc
-      && (str_eq(argv[i], "-optf") || str_eq(argv[i], "--options-file"))) {
-    if (i == argc - 1) {
-      cc_log("Expected argument after %s", argv[i]);
+      && (args[i] == "-optf" || args[i] == "--options-file")) {
+    if (i == args.size() - 1) {
+      cc_log("Expected argument after %s", args[i].c_str());
       return STATS_ARGS;
     }
     ++i;
 
     // Argument is a comma-separated list of files.
-    const char* str_start = argv[i];
-    const char* str_end = strchr(str_start, ',');
-    size_t index = i + 1;
-
-    if (!str_end) {
-      str_end = str_start + strlen(str_start);
-    }
-
-    while (str_end) {
-      std::string path(str_start, str_end - str_start);
-      auto file_args = args_init_from_gcc_atfile(path);
+    auto paths = Util::split_into_strings(args[i], ",");
+    for (auto it = paths.rbegin(); it != paths.rend(); ++it) {
+      auto file_args = args_init_from_gcc_atfile(*it);
       if (!file_args) {
-        cc_log("Couldn't read cuda options file %s", path.c_str());
+        cc_log("Couldn't read CUDA options file %s", it->c_str());
         return STATS_ARGS;
       }
 
-      size_t new_index = file_args->size() + index;
-      args_insert(args, index, *file_args, false);
-      index = new_index;
-      str_start = str_end;
-      str_end = strchr(str_start, ',');
+      args.insert(i + 1, *file_args);
     }
 
     return nullopt;
index 6780294586378327809ff266e47e9b6f8b4b0fd7..7e4f862c8ab738bac6c76cce139ae87b497306e3 100644 (file)
@@ -639,4 +639,26 @@ TEST(options_not_to_be_passed_to_the_preprocesor)
   CHECK_ARGS_EQ_FREE12(exp_cc, act_cc);
 }
 
+TEST(cuda_option_file)
+{
+  Context ctx;
+  ctx.guessed_compiler = GuessedCompiler::nvcc;
+
+  ctx.orig_args = Args::from_string("nvcc -optf foo.optf,bar.optf");
+  Args exp_cpp = args_init_from_string("nvcc -g -Wall -DX");
+  Args exp_extra = args_init_from_string("");
+  Args exp_cc = args_init_from_string("nvcc -g -Wall -DX -c");
+  Args act_cpp;
+  Args act_extra;
+  Args act_cc;
+
+  create_file("foo.c", "");
+  create_file("foo.optf", "-c foo.c -g -Wall -o");
+  create_file("bar.optf", "out -DX");
+  CHECK(!process_args(ctx, act_cpp, act_extra, act_cc));
+  CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp);
+  CHECK_ARGS_EQ_FREE12(exp_extra, act_extra);
+  CHECK_ARGS_EQ_FREE12(exp_cc, act_cc);
+}
+
 TEST_SUITE_END