[cfe-dev] Can I tell clang to NOT accept unknown flags?

Miles Bader miles at gnu.org
Thu Sep 29 21:16:58 PDT 2011


Clifford Yapp <cliffyapp at gmail.com>
writes:
> I'm building a large project with clang, and there are configure
> mechanisms set up that are intended to test whether a compiler
> supports a variety of flags.  This gets tricky if clang accepts ALL
> flags and ignores unknowns - is there a way (something like the
> opposite of -Qunused-arguments) that will allow tests to fail for
> unknown arguments?

clang does emit a warning message for unrecognized options, so in my
configure scripts I just look for that (in a somewhat loose manner
that hopefully will catch similar behavior in other compilers).

E.g., assuming the option is in OPT, instead of just:

   ...
   CXXFLAGS="$_SAVED_CXXFLAGS $OPT"
   ...
   AC_COMPILE_IFELSE([AC_LANG_SOURCE([int x;])], [opt_ok=yes], [opt_ok=no])
   ...

I use:

   ...
   CXXFLAGS="$_SAVED_CXXFLAGS $OPT"
   ...
   AC_COMPILE_IFELSE(
     [AC_LANG_SOURCE([int x;])],
     [opt_ok=yes
      # Some compilers only warn about unsupported options, so
      # try to detect such warnings.
      if test -s conftest.err && grep ".*$OPT" conftest.err >/dev/null; then
        opt_ok=no
      fi],
     [opt_ok=no])
   ...

(result in $opt_ok)

[This is for C++, but the C equivalent should be obvious]

For the case where multiple options are being checked together, clang
might reject any of them, so I use a loop:

   AC_COMPILE_IFELSE(
     [AC_LANG_SOURCE([int x;])],
     [opt_ok=yes
      # Some compilers only warn about unsupported options, so
      # try to detect such warnings.
      if test -s conftest.err; then
        for ONE_OPT in $OPT; do
          if grep ".*$ONE_OPT" conftest.err >/dev/null; then
            opt_ok=no
            break
          fi
        done
      fi],
     [opt_ok=no])

etc.

For the above in context, see SNOGRAY_CHECK_CXX_FLAGS and
SNOGRAY_CHECK_CXX_FLAG in:

https://github.com/snogglethorpe/snogray/blob/master/configure.ac


-Miles

-- 
Guilt, n. The condition of one who is known to have committed an indiscretion,
as distinguished from the state of him who has covered his tracks.




More information about the cfe-dev mailing list