]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Only accept -f(no-)color-diagnostics for Clang
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 30 Dec 2020 20:22:26 +0000 (21:22 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 30 Dec 2020 20:52:45 +0000 (21:52 +0100)
If a non-Clang compiler gets -f(no-)color-diagnostics then bail out and
just execute the compiler. The reason is that we don't include
-f(no-)color-diagnostics in the hash so there can be a false cache hit
in the following scenario:

  1. ccache gcc -c example.c                      # adds a cache entry
  2. ccache gcc -c example.c -fcolor-diagnostics  # unexpectedly succeeds

Closes: #740.
src/argprocessing.cpp
test/suites/color_diagnostics.bash

index 1783f9f0323cc6ee352abff3a0011b2df657dd0c..983ccdb45607a8924c2caf106bf97331cd645d92 100644 (file)
@@ -688,6 +688,19 @@ process_arg(Context& ctx,
     return nullopt;
   }
 
+  if (config.compiler_type() != CompilerType::clang
+      && (args[i] == "-fcolor-diagnostics"
+          || args[i] == "-fno-color-diagnostics")) {
+    // Special case: If a non-Clang compiler gets -f(no-)color-diagnostics we'll
+    // bail out and just execute the compiler. The reason is that we don't
+    // include -f(no-)color-diagnostics in the hash so there can be a false
+    // cache hit in the following scenario:
+    //
+    //   1. ccache gcc -c example.c                      # adds a cache entry
+    //   2. ccache gcc -c example.c -fcolor-diagnostics  # unexpectedly succeeds
+    return Statistic::unsupported_compiler_option;
+  }
+
   if (args[i] == "-fcolor-diagnostics" || args[i] == "-fdiagnostics-color"
       || args[i] == "-fdiagnostics-color=always") {
     state.color_diagnostics = ColorDiagnostics::always;
index 64c7d4f1829e35e1191dbb99feb3469c95508b84..4ec99f4022e1992ef8ab0ebe220a624d381f68e1 100644 (file)
@@ -113,17 +113,32 @@ color_diagnostics_test() {
     expect_stat 'cache miss' 1
     expect_stat 'cache hit (preprocessed)' 1
 
-    # -------------------------------------------------------------------------
     if $COMPILER_TYPE_GCC; then
+        # ---------------------------------------------------------------------
         TEST "-fcolor-diagnostics not accepted for GCC"
 
         generate_code 1 test.c
+
+        if $CCACHE_COMPILE -fcolor-diagnostics -c test.c >&/dev/null; then
+            test_failed "-fcolor-diagnostics unexpectedly accepted by GCC"
+        fi
+
+        # ---------------------------------------------------------------------
+        TEST "-fcolor-diagnostics not accepted for GCC for cached result"
+
+        generate_code 1 test.c
+
+        if ! $CCACHE_COMPILE -c test.c >&/dev/null; then
+            test_failed "unknown error compiling"
+        fi
+
         if $CCACHE_COMPILE -fcolor-diagnostics -c test.c >&/dev/null; then
             test_failed "-fcolor-diagnostics unexpectedly accepted by GCC"
         fi
     fi
 
     while read -r case; do
+        # ---------------------------------------------------------------------
         TEST "Cache object shared across ${case} (run_second_cpp=$run_second_cpp)"
 
         color_diagnostics_generate_code test1.c