user10678532
user10678532

Reputation:

Disabling the "did you mean..." suggestions in GCC

Newer versions of GCC come with what I consider to be an irritating misfeature: they try to second-guess me, emitting useless "did you mean?" suggestions that drown out other warnings.

For example:

$ gcc -Wall -c -o /dev/null -xc - <<'EOT'
#include <stdlib.h>

void foo(char *b, size_t z){ readlink("/foo", b, z); }
EOT
<stdin>: In function ‘foo’:
<stdin>:3:30: warning: implicit declaration of function ‘readlink’;
did you mean ‘realloc’? [-Wimplicit-function-declaration]

No, I did not mean realloc; I just forgot to include the unistd.h header.

Is there any way to turn this off?

I want to have it print just implicit declaration of function ‘readlink’ [-Wimplicit-function-declaration], without the helpful suggestion.

Upvotes: 0

Views: 757

Answers (3)

Gabor Simon
Gabor Simon

Reputation: 76

Just a little addition to the solution by @ImpudentSnob, to filter out those suggestion lines as well:

  1. Find your gcc, like in my case: ~/.espressif/.../xtensa-esp32-elf-gcc
  2. Rename it by adding '.didyoumean' to the end of the name, like xtensa-esp32-elf-gcc.didyoumean
  3. Put this script there, with the original gcc name:
#!/bin/bash

set -o pipefail
{ $0.didyoumean "$@" 2>&1 >&3 | awk '{if (sub("; did you mean [^?]*[?]", "")) cnt=4} (cnt!=1){print} (cnt>0){cnt--}' >&2;} 3>&1

Upvotes: 1

klutt
klutt

Reputation: 31409

I don't think there is a solution within gcc except for making your own modification. A non-optimal solution is piping into sed.

gcc main.c 2>&1 | sed -E "s/; did you mean .*\?//"

What I'm doing here is redirecting stderr (where the warnings are printed) to stdout with 2>&1 and then I made a simple replace script that removes that phrase.

It adds some complexity, and you lose the colours, but it does what you asked for.

Upvotes: 0

user10678532
user10678532

Reputation:

It turns out that it's simply hardcoded and there are no ways to turn that off.

From the gcc source:

3328       warned = warning_at
3329         (&richloc, OPT_Wimplicit_function_declaration,
3330          G_("implicit declaration of function %qE; did you mean %qs?"),
3331          id, suggestion);

[And no, neither lookup_name_fuzzy() nor hint.suggestion () will take any command line option into consideration when returning their results]

Filtering the error output with some shell sorcery, eg. with

{ gcc ... 2>&1 >&3 | sed 's/; did you mean [^?]*[?]//' >&2; } 3>&1

is not really a solution, first because it cannot be used in makefiles (the error status of the command line will be that of the sed or whatever filter, not that of gcc), and second because it will still leave around things like:

 void foo(char *b, size_t z){ readlink("/foo", b, z); }
                              ^~~~~~~~
                              realloc

which will need some complicated logic to filter out.

[using bash/kshisms like 2> >(...) won't help with the 1st point either, because make will use /bin/sh, not the user's shell]

Upvotes: 4

Related Questions