<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - Don't recommend defining __has_* macros"
href="https://bugs.llvm.org/show_bug.cgi?id=42952">42952</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>Don't recommend defining __has_* macros
</td>
</tr>
<tr>
<th>Product</th>
<td>Documentation
</td>
</tr>
<tr>
<th>Version</th>
<td>trunk
</td>
</tr>
<tr>
<th>Hardware</th>
<td>All
</td>
</tr>
<tr>
<th>OS</th>
<td>All
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>General docs
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>clang@evan.coeusgroup.com
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre>In the "Feature Checking Macros" section of "Clang Language Extensions", the
manual currently recommends code like:
#ifndef __has_builtin // Optional of course.
#define __has_builtin(x) 0 // Compatibility with non-clang compilers.
#endif
...
#if __has_builtin(__builtin_trap)
__builtin_trap();
#else
abort();
#endif
...
This presents a problem in public headers, because other projects may want to
do something like:
#if defined(__has_builtin)
#define FOO_HAS_BUILTIN_TRAP __has_builtin(__builtin_trap)
#elif defined(__GNUC__) && (__GNUC__ >= 4)
#define FOO_HAS_BUILTIN_TRAP 1
#else
#define FOO_HAS_BUILTIN_TRAP 0
#endif
But if someone has already created a `#define __has_builtin(x) 0`, this check
would define FOO_HAS_BUILTIN_TRAP to 0 instead of 1 on GCC 4+, and AFAIK there
is no way to verify that you're using a "real" __has_builtin.
I'd suggest changing the examples to something like
#ifdef __has_builtin
#define my_has_builtin(x) __has_builtin(x)
#else
#define my_has_builtin(x) 0 // Compatibility with non-clang compilers.
#endif
...
#if my_has_builtin(__builtin_trap)
__builtin_trap();
#else
abort();
#endif
...
That retains most of the simplicity of the existing examples, but doesn't
encourage people to write code that can easily result in portability problems
while claiming to be for "compatibility with non-clang compilers".</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>