-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[Clang] Add C++11 spelling support for guarded_by attribute #167048
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
This enables [[clang::guarded_by(x)]], the C++11 attribute spelling for guarded_by. Note that this required the thread safety handler to handle field references appropriately in attribute arguments, which was brought about by creating proper TIL representations for FieldDecl references in translateDeclRefExpr. This in turn happily combats a known false positive present in clang/test/SemaCXX/warn-thread-safety-analysis.cpp which was (but no longer) marked as a TODO.
|
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) ChangesThis enables Partially resolves #158116 Full diff: https://github.com/llvm/llvm-project/pull/167048.diff 4 Files Affected:
diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index 1013bfc575747..d3477ccd40ff5 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -4129,7 +4129,7 @@ def NoThreadSafetyAnalysis : InheritableAttr {
}
def GuardedBy : InheritableAttr {
- let Spellings = [GNU<"guarded_by">];
+ let Spellings = [Clang<"guarded_by", 0>];
let Args = [ExprArgument<"Arg">];
let LateParsed = LateAttrParseExperimentalExt;
let TemplateDependent = 1;
@@ -5017,10 +5017,6 @@ def HLSLUnparsedSemantic : HLSLAnnotationAttr {
let Documentation = [InternalOnly];
}
-def HLSLUserSemantic : HLSLSemanticAttr</* Indexable= */ 1> {
- let Documentation = [InternalOnly];
-}
-
def HLSLSV_Position : HLSLSemanticAttr</* Indexable= */ 1> {
let Documentation = [HLSLSV_PositionDocs];
}
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/gh158116.cpp b/clang/test/SemaCXX/gh158116.cpp
new file mode 100644
index 0000000000000..1b28f37cc3166
--- /dev/null
+++ b/clang/test/SemaCXX/gh158116.cpp
@@ -0,0 +1,25 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wthread-safety -std=c++11 %s
+
+class __attribute__((lockable)) Mutex {
+public:
+ void lock() __attribute__((exclusive_lock_function));
+ void unlock() __attribute__((unlock_function));
+};
+
+class Foo {
+ Mutex mu;
+ int y __attribute__((guarded_by(mu)));
+ int z [[clang::guarded_by(mu)]];
+
+ void func1() {
+ y = 0; // expected-warning{{writing variable 'y' requires holding}}
+ z = 1; // expected-warning{{writing variable 'z' requires holding}}
+ }
+
+ void func2() {
+ mu.lock();
+ y = 2;
+ z = 3;
+ mu.unlock();
+ }
+};
\ No newline at end of file
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) ChangesThis enables Partially resolves #158116 Full diff: https://github.com/llvm/llvm-project/pull/167048.diff 4 Files Affected:
diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td
index 1013bfc575747..d3477ccd40ff5 100644
--- a/clang/include/clang/Basic/Attr.td
+++ b/clang/include/clang/Basic/Attr.td
@@ -4129,7 +4129,7 @@ def NoThreadSafetyAnalysis : InheritableAttr {
}
def GuardedBy : InheritableAttr {
- let Spellings = [GNU<"guarded_by">];
+ let Spellings = [Clang<"guarded_by", 0>];
let Args = [ExprArgument<"Arg">];
let LateParsed = LateAttrParseExperimentalExt;
let TemplateDependent = 1;
@@ -5017,10 +5017,6 @@ def HLSLUnparsedSemantic : HLSLAnnotationAttr {
let Documentation = [InternalOnly];
}
-def HLSLUserSemantic : HLSLSemanticAttr</* Indexable= */ 1> {
- let Documentation = [InternalOnly];
-}
-
def HLSLSV_Position : HLSLSemanticAttr</* Indexable= */ 1> {
let Documentation = [HLSLSV_PositionDocs];
}
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/gh158116.cpp b/clang/test/SemaCXX/gh158116.cpp
new file mode 100644
index 0000000000000..1b28f37cc3166
--- /dev/null
+++ b/clang/test/SemaCXX/gh158116.cpp
@@ -0,0 +1,25 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -Wthread-safety -std=c++11 %s
+
+class __attribute__((lockable)) Mutex {
+public:
+ void lock() __attribute__((exclusive_lock_function));
+ void unlock() __attribute__((unlock_function));
+};
+
+class Foo {
+ Mutex mu;
+ int y __attribute__((guarded_by(mu)));
+ int z [[clang::guarded_by(mu)]];
+
+ void func1() {
+ y = 0; // expected-warning{{writing variable 'y' requires holding}}
+ z = 1; // expected-warning{{writing variable 'z' requires holding}}
+ }
+
+ void func2() {
+ mu.lock();
+ y = 2;
+ z = 3;
+ mu.unlock();
+ }
+};
\ No newline at end of file
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'}}
}
};
|
erichkeane
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.
The spelling addition is roughly acceptable, but this doesn't accomplish even 'partial' fix of the bug. The idea of the bug report is that you should analyze the existing attributes that were mentioned in the bug report, and spend some time analyzing them to figure out WHICH need different spellings.
This enables
[[clang::guarded_by(x)]], the C++11 attribute spelling forguarded_by. Note that this required the thread safety handler to handle field references appropriately in attribute arguments, which was brought about by creating proper TIL representations forFieldDeclreferences intranslateDeclRefExpr. This in turn happily combats a known false positive present in clang/test/SemaCXX/warn-thread-safety-analysis.cpp which was (but no longer) marked as a TODO.Partially resolves #158116