Commit Graph

3 Commits

Author SHA1 Message Date
Nico Weber b2348f4ced Add -Wexpansion-to-undefined: warn when using `defined` in a macro definition.
[cpp.cond]p4:
  Prior to evaluation, macro invocations in the list of preprocessing
  tokens that will become the controlling constant expression are replaced
  (except for those macro names modified by the 'defined' unary operator),
  just as in normal text. If the token 'defined' is generated as a result
  of this replacement process or use of the 'defined' unary operator does
  not match one of the two specified forms prior to macro replacement, the
  behavior is undefined.

This isn't an idle threat, consider this program:
  #define FOO
  #define BAR defined(FOO)
  #if BAR
  ...
  #else
  ...
  #endif
clang and gcc will pick the #if branch while Visual Studio will take the
#else branch.  Emit a warning about this undefined behavior.

One problem is that this also applies to function-like macros. While the
example above can be written like

    #if defined(FOO) && defined(BAR)
    #defined HAVE_FOO 1
    #else
    #define HAVE_FOO 0
    #endif

there is no easy way to rewrite a function-like macro like `#define FOO(x)
(defined __foo_##x && __foo_##x)`.  Function-like macros like this are used in
practice, and compilers seem to not have differing behavior in that case. So
this a default-on warning only for object-like macros. For function-like
macros, it is an extension warning that only shows up with `-pedantic`.
(But it's undefined behavior in both cases.)

llvm-svn: 258128
2016-01-19 15:15:31 +00:00
Andy Gibbs c6e68daac0 Prior to adding the new "expected-no-diagnostics" directive to VerifyDiagnosticConsumer, make the necessary adjustment to 580 test-cases which will henceforth require this new directive.
llvm-svn: 166280
2012-10-19 12:44:48 +00:00
Eli Friedman b3bfd84ebb A couple fixes for preprocessor expressions:
1. Be more tolerant of comments in -CC (comment-preserving) mode.  We were missing a few cases.

2. Make sure to expand the second FOO in "#if defined FOO FOO".  (See also
r97253, which addressed the case of "#if defined(FOO FOO".)

Fixes PR10286.

llvm-svn: 136748
2011-08-03 00:04:13 +00:00