Thread safety analysis: Consider static class members as inaccessible

This fixes the issue pointed out in D84604#2363134. For now we exclude
static members completely, we'll take them into account later.
This commit is contained in:
Aaron Puchert 2020-10-30 00:35:14 +01:00
parent 99f2779c18
commit bbed8cfe80
2 changed files with 35 additions and 2 deletions

View File

@ -1269,10 +1269,16 @@ bool ThreadSafetyAnalyzer::inCurrentScope(const CapabilityExpr &CapE) {
const threadSafety::til::SExpr *SExp = CapE.sexpr();
assert(SExp && "Null expressions should be ignored");
// Global variables are always in scope.
if (const auto *LP = dyn_cast<til::LiteralPtr>(SExp)) {
const ValueDecl *VD = LP->clangDecl();
return VD->isDefinedOutsideFunctionOrMethod();
// Variables defined in a function are always inaccessible.
if (!VD->isDefinedOutsideFunctionOrMethod())
return false;
// For now we consider static class members to be inaccessible.
if (isa<CXXRecordDecl>(VD->getDeclContext()))
return false;
// Global variables are always in scope.
return true;
}
// Members are in scope from methods of the same class.

View File

@ -118,6 +118,33 @@ void testNamespaceGlobals() EXCLUSIVE_LOCKS_REQUIRED(!globalMutex) {
ns::fq(); // expected-warning {{calling function 'fq' requires negative capability '!globalMutex'}}
}
class StaticMembers {
public:
void pub() EXCLUSIVE_LOCKS_REQUIRED(!publicMutex);
void prot() EXCLUSIVE_LOCKS_REQUIRED(!protectedMutex);
void priv() EXCLUSIVE_LOCKS_REQUIRED(!privateMutex);
void test() {
pub();
prot();
priv();
}
static Mutex publicMutex;
protected:
static Mutex protectedMutex;
private:
static Mutex privateMutex;
};
void testStaticMembers() {
StaticMembers x;
x.pub();
x.prot();
x.priv();
}
} // end namespace ScopeTest
namespace DoubleAttribute {