-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[Clang][ThreadSafety] Contextualize FieldDecl references in thread sa… #168028
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
base: main
Are you sure you want to change the base?
Conversation
…fety attribute arguments This enables the thread safety handler to handle field references appropriately in attribute arguments by creating proper TIL representations for FieldDecl references in translateDeclRefExpr. Earlier, the analyzer treated the reference to the field name mu without the context required for it to match on this->mu, which led to a false positive in clang/test/SemaCXX/warn-thread-safety-analysis.cpp. That false positive is resolved with the changes made here.
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-clang Author: Pranjal Prajapati (Pranjal095) Changes…fety attribute arguments This enables the thread safety handler to handle field references appropriately in attribute arguments by creating proper TIL representations for I noticed this while working on PR#167048, when the added Clang spelling for Full diff: https://github.com/llvm/llvm-project/pull/168028.diff 2 Files Affected:
diff --git a/clang/lib/Analysis/ThreadSafetyCommon.cpp b/clang/lib/Analysis/ThreadSafetyCommon.cpp
index ef48ae439c5f3..89a3087a59247 100644
--- a/clang/lib/Analysis/ThreadSafetyCommon.cpp
+++ b/clang/lib/Analysis/ThreadSafetyCommon.cpp
@@ -394,6 +394,13 @@ til::SExpr *SExprBuilder::translateDeclRefExpr(const DeclRefExpr *DRE,
if (const auto *VarD = dyn_cast<VarDecl>(VD))
return translateVariable(VarD, Ctx);
+ if (const auto *FD = dyn_cast<FieldDecl>(VD)) {
+ if (Ctx && Ctx->SelfArg) {
+ til::SExpr *E = new (Arena) til::SApply(SelfVar);
+ return new (Arena) til::Project(E, FD);
+ }
+ }
+
// For non-local variables, treat it as a reference to a named object.
return new (Arena) til::LiteralPtr(VD);
}
diff --git a/clang/test/SemaCXX/warn-thread-safety-analysis.cpp b/clang/test/SemaCXX/warn-thread-safety-analysis.cpp
index 0e91639a271c5..f68699f701851 100644
--- a/clang/test/SemaCXX/warn-thread-safety-analysis.cpp
+++ b/clang/test/SemaCXX/warn-thread-safety-analysis.cpp
@@ -3590,14 +3590,10 @@ void requireDecl(RelockableScope &scope) {
struct foo
{
Mutex mu;
- // expected-note@+1{{see attribute on parameter here}}
void require(RelockableScope &scope EXCLUSIVE_LOCKS_REQUIRED(mu));
void callRequire(){
RelockableScope scope(&mu);
- // TODO: False positive due to incorrect parsing of parameter attribute arguments.
require(scope);
- // expected-warning@-1{{calling function 'require' requires holding mutex 'mu' exclusively}}
- // expected-warning@-2{{mutex managed by 'scope' is 'mu' instead of 'mu'}}
}
};
|
|
@llvm/pr-subscribers-clang-analysis Author: Pranjal Prajapati (Pranjal095) Changes…fety attribute arguments This enables the thread safety handler to handle field references appropriately in attribute arguments by creating proper TIL representations for I noticed this while working on PR#167048, when the added Clang spelling for Full diff: https://github.com/llvm/llvm-project/pull/168028.diff 2 Files Affected:
diff --git a/clang/lib/Analysis/ThreadSafetyCommon.cpp b/clang/lib/Analysis/ThreadSafetyCommon.cpp
index ef48ae439c5f3..89a3087a59247 100644
--- a/clang/lib/Analysis/ThreadSafetyCommon.cpp
+++ b/clang/lib/Analysis/ThreadSafetyCommon.cpp
@@ -394,6 +394,13 @@ til::SExpr *SExprBuilder::translateDeclRefExpr(const DeclRefExpr *DRE,
if (const auto *VarD = dyn_cast<VarDecl>(VD))
return translateVariable(VarD, Ctx);
+ if (const auto *FD = dyn_cast<FieldDecl>(VD)) {
+ if (Ctx && Ctx->SelfArg) {
+ til::SExpr *E = new (Arena) til::SApply(SelfVar);
+ return new (Arena) til::Project(E, FD);
+ }
+ }
+
// For non-local variables, treat it as a reference to a named object.
return new (Arena) til::LiteralPtr(VD);
}
diff --git a/clang/test/SemaCXX/warn-thread-safety-analysis.cpp b/clang/test/SemaCXX/warn-thread-safety-analysis.cpp
index 0e91639a271c5..f68699f701851 100644
--- a/clang/test/SemaCXX/warn-thread-safety-analysis.cpp
+++ b/clang/test/SemaCXX/warn-thread-safety-analysis.cpp
@@ -3590,14 +3590,10 @@ void requireDecl(RelockableScope &scope) {
struct foo
{
Mutex mu;
- // expected-note@+1{{see attribute on parameter here}}
void require(RelockableScope &scope EXCLUSIVE_LOCKS_REQUIRED(mu));
void callRequire(){
RelockableScope scope(&mu);
- // TODO: False positive due to incorrect parsing of parameter attribute arguments.
require(scope);
- // expected-warning@-1{{calling function 'require' requires holding mutex 'mu' exclusively}}
- // expected-warning@-2{{mutex managed by 'scope' is 'mu' instead of 'mu'}}
}
};
|
…fety attribute arguments
This enables the thread safety handler to handle field references appropriately in attribute arguments by creating proper TIL representations for
FieldDeclreferences intranslateDeclRefExpr. Earlier, the analyzer treated the reference to the field namemuwithout the context required for it to match onthis->mu, which led to a false positive in clang/test/SemaCXX/warn-thread-safety-analysis.cpp. That false positive is resolved with the changes made here.I noticed this while working on PR#167048, when the added Clang spelling for
guarded_bywas failing to emulate the functionality of the corresponding GNU spelling due to the above reason.