Skip to content

Commit

Permalink
Only accept -f(no-)color-diagnostics for Clang
Browse files Browse the repository at this point in the history
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: ccache#740.
  • Loading branch information
jrosdahl committed Dec 30, 2020
1 parent 76eb7f4 commit 61ce8c4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/argprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 16 additions & 1 deletion test/suites/color_diagnostics.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 61ce8c4

Please sign in to comment.