Using Clang __builtin_assume if available.#5
Merged
Conversation
foonathan
reviewed
Jan 9, 2017
| #ifdef __GNUC__ | ||
| #ifdef __clang__ | ||
| #if __has_builtin(__builtin_assume) | ||
| #define DEBUG_ASSERT_ASSUME(Expr) __builtin_assume(Expr) |
Owner
There was a problem hiding this comment.
Can you use defined(__clang__) && __has_builtin... there and then just have a single #elif defined(__GNUC__)? Avoids the repetition.
Contributor
Author
|
That was my first tentative, but gcc does not compile. |
Owner
|
Ah, I see. Could you add a comment then? |
Contributor
Author
|
Where? Explaining the need for the repetition? |
Owner
|
Yes. |
Contributor
Author
|
Another possibility is: // Checking for clang must come first because clang also defines __GNUC__.
#ifndef DEBUG_ASSERT_ASSUME
#ifdef __clang__
// __has_builtin may not work in other compilers.
#if __has_builtin(__builtin_assume)
#define DEBUG_ASSERT_ASSUME(Expr) __builtin_assume(Expr)
#endif
#endif
#endif
#ifndef DEBUG_ASSERT_ASSUME
#ifdef __GNUC__
#define DEBUG_ASSERT_ASSUME(Expr) \
do \
{ \
if (!(Expr)) \
__builtin_unreachable(); \
} while (0)
#elif defined(_MSC_VER)
#define DEBUG_ASSERT_ASSUME(Expr) __assume(Expr)
#else
/// Hint to the compiler that a condition is `true`.
/// Define it yourself prior to including the header to override it.
#define DEBUG_ASSERT_ASSUME(Expr)
#endif
#endifDo you think it is better? |
Contributor
Author
|
I realized something that might be a problem. Compiling example.cpp with clang now emits this warning: I think clang is not able to understand that Expr does not have side effects. |
7af2d1c to
6ea3da0
Compare
Owner
|
I don't think it's a good idea then. |
Contributor
Author
|
Does MSC emit warnings? |
Owner
|
I'm afraid I haven't checked thoroughly but I haven't noticed any. |
73a15e8 to
52b09fa
Compare
Contributor
Author
|
Marking the lambda as a pure function (as it should be) solves the warnings. |
- Added an attribute to indicate that the assertion expression is pure (as it should be).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Clang must be verified first because it also defines
__GNUC__.__builtin_assumeis a recent addition (since 2014), so I also check if it is available.