diff options
author | Nico Weber <nicolasweber@gmx.de> | 2016-01-19 15:15:31 +0000 |
---|---|---|
committer | Nico Weber <nicolasweber@gmx.de> | 2016-01-19 15:15:31 +0000 |
commit | 7a21676b47547a0d1c2b9d123c8c78ec01f1c4b0 (patch) | |
tree | b065c9971b4eb266e13e6ecc9285e0290514139b /test/Lexer | |
parent | fe46d7ff72602631f89c7c0e1ea183db06deffaa (diff) |
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.)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@258128 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/Lexer')
-rw-r--r-- | test/Lexer/cxx-features.cpp | 1 |
1 files changed, 1 insertions, 0 deletions
diff --git a/test/Lexer/cxx-features.cpp b/test/Lexer/cxx-features.cpp index 6c4a092b1c..b0f382ec25 100644 --- a/test/Lexer/cxx-features.cpp +++ b/test/Lexer/cxx-features.cpp @@ -6,6 +6,7 @@ // expected-no-diagnostics +// FIXME using `defined` in a macro has undefined behavior. #if __cplusplus < 201103L #define check(macro, cxx98, cxx11, cxx1y) cxx98 == 0 ? defined(__cpp_##macro) : __cpp_##macro != cxx98 #elif __cplusplus < 201304L |