[cfe-dev] Best way to disable warnings by using a compile option

Andy Gibbs via cfe-dev cfe-dev at lists.llvm.org
Wed Jun 1 09:30:32 PDT 2016


Hi,

I'm messing around creating a compiler option for a specialised hardware 
target that I'm working on.  It involves changing the behaviour of the 
compiler in small ways but one thing that I'd particularly like to be able to 
do is disable certain compiler warnings when this flag is in use.  I've got all 
the relevant warnings grouped together in DiagnosticsGroup.td, but I'm trying 
to find the best way to bring this under the control of the single compiler 
option.

My approach so far has been to push the -Wno-... flag early in the 
Clang::ConstructJob function in lib/Driver/Tools.cpp:

[...]
  // Invoke ourselves in -cc1 mode.
  //
  // FIXME: Implement custom jobs for internal actions.
  CmdArgs.push_back("-cc1");

  // Add the "effective" target triple.
  CmdArgs.push_back("-triple");
  CmdArgs.push_back(Args.MakeArgString(TripleStr));

  // -my-special-hardware option.
  if (Args.hasArg(options::OPT_my_special_hardware)) {
    CmdArgs.push_back("-my-special-hardware");
    CmdArgs.push_back("-Wno-irrelevant-warnings");
  }
[...]

This actually works quite well: it disables the warnings by default even if I 
invoke the compiler with -Wall or similar, and very usefully it can be 
overridden at the command line, by...

clang ... -my-special-hardware -Wirrelevant-warnings

or, for a specific warning in the group, by...

clang ... -my-special-hardware -Wspecific-irrelevant-warning

However, I can't help thinking this is not the most intelligent approach and 
that there must be a much better way.  I'm also not 100% sure that my approach 
is necessarily valid, given that it relies on the compiler spawning itself 
again -- or, apart from using the -cc1 flag explicitly from the command line, 
will the compiler always do this?

It would be wonderful if someone could give me a pointer, please.  :o)

Thanks!

Andy





More information about the cfe-dev mailing list