-
Notifications
You must be signed in to change notification settings - Fork 15.7k
Effect analysis: correctly detect (x ? a : b) as nonblocking when a and b are
#111224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@llvm/pr-subscribers-clang Author: Doug Wyatt (dougsonos) ChangesFix: Effect analysis: correctly detect Full diff: https://github.com/llvm/llvm-project/pull/111224.diff 2 Files Affected:
diff --git a/clang/lib/Sema/SemaFunctionEffects.cpp b/clang/lib/Sema/SemaFunctionEffects.cpp
index 0fb18d207a50ba..0ac5de29f66aa7 100644
--- a/clang/lib/Sema/SemaFunctionEffects.cpp
+++ b/clang/lib/Sema/SemaFunctionEffects.cpp
@@ -1048,15 +1048,14 @@ class Analyzer {
}
void checkIndirectCall(CallExpr *Call, QualType CalleeType) {
- auto *FPT =
- CalleeType->getAs<FunctionProtoType>(); // Null if FunctionType.
FunctionEffectKindSet CalleeEffects;
- if (FPT)
- CalleeEffects.insert(FPT->getFunctionEffects());
+ if (FunctionEffectsRef Effects = FunctionEffectsRef::get(CalleeType);
+ !Effects.empty())
+ CalleeEffects.insert(Effects);
auto Check1Effect = [&](FunctionEffect Effect, bool Inferring) {
- if (FPT == nullptr || Effect.shouldDiagnoseFunctionCall(
- /*direct=*/false, CalleeEffects))
+ if (Effect.shouldDiagnoseFunctionCall(
+ /*direct=*/false, CalleeEffects))
addViolation(Inferring, Effect, ViolationID::CallsExprWithoutEffect,
Call->getBeginLoc());
};
diff --git a/clang/test/Sema/attr-nonblocking-constraints.cpp b/clang/test/Sema/attr-nonblocking-constraints.cpp
index c694860069c960..ff8caf0e573403 100644
--- a/clang/test/Sema/attr-nonblocking-constraints.cpp
+++ b/clang/test/Sema/attr-nonblocking-constraints.cpp
@@ -156,6 +156,16 @@ void nb10(
static_cast<void (*)()>(fp1)(); // expected-warning {{function with 'nonblocking' attribute must not call non-'nonblocking' expression}}
}
+// Expression involving indirection
+int nb10a() [[clang::nonblocking]];
+int nb10b() [[clang::nonblocking]];
+
+int nb10c(bool x) [[clang::nonblocking]]
+{
+ // Warns that the expression is not nonblocking.
+ return (x ? nb10a : nb10b)();
+}
+
// Interactions with nonblocking(false)
void nb11_no_inference_1() [[clang::nonblocking(false)]] // expected-note {{function does not permit inference of 'nonblocking'}}
{
|
| FunctionEffectKindSet CalleeEffects; | ||
| if (FPT) | ||
| CalleeEffects.insert(FPT->getFunctionEffects()); | ||
| if (FunctionEffectsRef Effects = FunctionEffectsRef::get(CalleeType); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FunctionEffectsRef::get() knows how to pick through indirections (references/pointers) to get to a FunctionProtoType so just reuse that.
| auto Check1Effect = [&](FunctionEffect Effect, bool Inferring) { | ||
| if (FPT == nullptr || Effect.shouldDiagnoseFunctionCall( | ||
| /*direct=*/false, CalleeEffects)) | ||
| if (Effect.shouldDiagnoseFunctionCall( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The check for a null FPT is/was superfluous; in that case the callee never has any declared effects and shouldDiagnoseFunctionCall will complain.
(f ? a : b) as nonblocking when a and b are(x ? a : b) as nonblocking when a and b are
cjappl
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
Will let a little time pass to let Sirraide chime in, because this section of the code isn't my forte. If you want this merged ASAP @dougsonos and you feel confident in it, ping me and I can merge.
Sirraide
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
@dougsonos Maybe you should ask for commit access since you’ll likely be contributing more patches to this in the future as I understand it? CC @AaronBallman Does that still work like it used to or did your maintainers rfc change something about that too? |
|
Oh, also, are there any other places where we should maybe be using |
Good question. I looked for other uses of Thanks for the quick review. More patches? My favorite of Murphy's Laws of Programming is "if it is useful, it will have to be changed." |
A few days ago at least, the other RTSan coauthor followed the "old way" of emailing Chris Lattner to get access. He followed the same process I did a few months ago (email him asking for access with a list of commits you have contributed). Not sure if that has changed in the last week or not. |
… when a and b are (llvm#111224) Correctly detect `(x ? a : b)` as nonblocking when `a` and `b` are. Use `FunctionEffectsRef::get` to get to the actual effect set instead of trying to retrieve it manually via the `FunctionProtoType` as we may have to look through function pointers etc. in some cases. --------- Co-authored-by: Doug Wyatt <dwyatt@apple.com>
Fix: Effect analysis: correctly detect
(x ? a : b)as nonblocking when a and b are