From 806b511643ec830f50d6d6975b2ecfd1876a6f17 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Mon, 15 Aug 2011 22:38:26 +0200 Subject: [PATCH] Fix -fprofile-*=dir handling when dir doesn't exist --- ccache.c | 12 ++++++--- test/test_argument_processing.c | 44 +++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/ccache.c b/ccache.c index 339ce5fe6..23c7758c9 100644 --- a/ccache.c +++ b/ccache.c @@ -1605,15 +1605,21 @@ cc_process_args(struct args *orig_args, struct args **preprocessor_args, if (arg_profile_dir) { char* option = x_strndup(argv[i], arg_profile_dir - argv[i]); + char *dir; /* Convert to absolute path. */ - arg_profile_dir = x_realpath(arg_profile_dir + 1); + dir = x_realpath(arg_profile_dir + 1); + if (!dir) { + /* Directory doesn't exist. */ + dir = x_strdup(arg_profile_dir + 1); + } /* We can get a better hit rate by using the real path here. */ free(arg); - arg = format("%s=%s", option, profile_dir); - cc_log("Rewriting arg to %s", arg); + arg = format("%s=%s", option, dir); + cc_log("Rewriting %s to %s", argv[i], arg); free(option); + free(dir); } if (str_startswith(argv[i], "-fprofile-generate") diff --git a/test/test_argument_processing.c b/test/test_argument_processing.c index a1fdb8168..18dcaea45 100644 --- a/test/test_argument_processing.c +++ b/test/test_argument_processing.c @@ -230,4 +230,48 @@ TEST(MT_flag_with_immediate_argument_should_add_MQobj) args_free(orig); } +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_cc = args_init_from_string("gcc"); + struct args *act_cpp = NULL, *act_cc = NULL; + char *s; + + create_file("foo.c", ""); + mkdir("some", 0777); + mkdir("some/dir", 0777); + s = format("-fprofile-generate=%s", x_realpath("some/dir")); + args_add(exp_cpp, s); + args_add(exp_cc, s); + args_add(exp_cc, "-c"); + free(s); + + CHECK(cc_process_args(orig, &act_cpp, &act_cc)); + CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp); + CHECK_ARGS_EQ_FREE12(exp_cc, act_cc); + + args_free(orig); +} + +TEST(fprofile_flag_with_nonexisting_dir_not_be_rewritten) +{ + struct args *orig = 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_cc = args_init_from_string( + "gcc -fprofile-generate=some/dir -c"); + struct args *act_cpp = NULL, *act_cc = NULL; + + create_file("foo.c", ""); + + CHECK(cc_process_args(orig, &act_cpp, &act_cc)); + CHECK_ARGS_EQ_FREE12(exp_cpp, act_cpp); + CHECK_ARGS_EQ_FREE12(exp_cc, act_cc); + + args_free(orig); +} + TEST_SUITE_END -- 2.47.3