Skip to content

Commit

Permalink
Only reject -f(no-)color-diagnostics for known GCC compiler
Browse files Browse the repository at this point in the history
61ce8c4 made it so that ccache rejects -f(no-)color-diagnostics early
for non-Clang compilers. This was needed to avoid false cache hits for
the GCC case, but it had the side effect of rejecting
-fcolor-diagnostics for unknown compilers as well, such as for a
compiler named c++ that in reality is clang++ (which actually accepts
-f(no-)color-diagnostics).

Fix this by simply doing the special case for GCC instead of non-Clang.
This is OK since the speculative handling of color diagnostics options
is only done for GCC and Clang, not for other compiler types.

Fixes #806.
  • Loading branch information
jrosdahl committed Mar 13, 2021
1 parent 3c96cee commit 97a40af
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/argprocessing.cpp
Expand Up @@ -690,13 +690,13 @@ process_arg(Context& ctx,
return nullopt;
}

if (config.compiler_type() != CompilerType::clang
if (config.compiler_type() == CompilerType::gcc
&& (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:
// Special case: If a GCC 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
Expand Down
16 changes: 16 additions & 0 deletions test/suites/color_diagnostics.bash
Expand Up @@ -122,6 +122,7 @@ color_diagnostics_test() {
if $CCACHE_COMPILE -fcolor-diagnostics -c test.c >&/dev/null; then
test_failed "-fcolor-diagnostics unexpectedly accepted by GCC"
fi
expect_stat 'unsupported compiler option' 1

# ---------------------------------------------------------------------
TEST "-fcolor-diagnostics not accepted for GCC for cached result"
Expand All @@ -135,6 +136,21 @@ color_diagnostics_test() {
if $CCACHE_COMPILE -fcolor-diagnostics -c test.c >&/dev/null; then
test_failed "-fcolor-diagnostics unexpectedly accepted by GCC"
fi
expect_stat 'unsupported compiler option' 1

# ---------------------------------------------------------------------
TEST "-fcolor-diagnostics passed to underlying compiler for unknown compiler type"

generate_code 1 test.c

if CCACHE_COMPILERTYPE=other $CCACHE_COMPILE -fcolor-diagnostics -c test.c >&/dev/null; then
test_failed "-fcolor-diagnostics unexpectedly accepted by GCC"
fi

# Verify that -fcolor-diagnostics was passed to the compiler for the
# unknown compiler case, i.e. ccache did not exit early with
# "unsupported compiler option".
expect_stat 'compile failed' 1
fi

if $COMPILER_TYPE_CLANG; then
Expand Down

0 comments on commit 97a40af

Please sign in to comment.