Skip to content

Commit 59b415b

Browse files
committed
partially address review feedback
1 parent b994311 commit 59b415b

File tree

5 files changed

+29
-25
lines changed

5 files changed

+29
-25
lines changed

clang/include/clang/Analysis/Analyses/ThreadSafety.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ namespace clang {
2626
class AnalysisDeclContext;
2727
class FunctionDecl;
2828
class NamedDecl;
29-
class Attr;
3029

3130
namespace threadSafety {
3231

@@ -231,8 +230,8 @@ class ThreadSafetyHandler {
231230
/// Warn that there is a cycle in acquired_before/after dependencies.
232231
virtual void handleBeforeAfterCycle(Name L1Name, SourceLocation Loc) {}
233232

234-
virtual void handleAttributeMismatch(const FunctionDecl *FD1,
235-
const FunctionDecl *FD2) {}
233+
virtual void handleAttributeMismatch(const NamedDecl *D1,
234+
const NamedDecl *D2) {}
236235

237236
/// Called by the analysis when starting analysis of a function.
238237
/// Used to issue suggestions for changes to annotations.

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4032,8 +4032,8 @@ def warn_acquired_before_after_cycle : Warning<
40324032
"cycle in acquired_before/after dependencies, starting with '%0'">,
40334033
InGroup<ThreadSafetyAnalysis>, DefaultIgnore;
40344034
def warn_attribute_mismatch : Warning<
4035-
"attribute mismatch between function definition and declaration of %0">,
4036-
InGroup<ThreadSafetyAnalysis>, DefaultIgnore;
4035+
"attribute mismatch between function declarations of %0">,
4036+
InGroup<ThreadSafetyAttributes>, DefaultIgnore;
40374037

40384038
// Thread safety warnings negative capabilities
40394039
def warn_acquire_requires_negative_cap : Warning<

clang/lib/Analysis/ThreadSafety.cpp

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ class ThreadSafetyAnalyzer {
10741074
void checkPtAccess(const FactSet &FSet, const Expr *Exp, AccessKind AK,
10751075
ProtectedOperationKind POK);
10761076

1077-
void checkMismatchedFunctionAttrs(const FunctionDecl *FD);
1077+
void checkMismatchedFunctionAttrs(const NamedDecl *ND);
10781078
};
10791079

10801080
} // namespace
@@ -2265,25 +2265,27 @@ static bool neverReturns(const CFGBlock *B) {
22652265
return false;
22662266
}
22672267

2268-
void ThreadSafetyAnalyzer::checkMismatchedFunctionAttrs(
2269-
const FunctionDecl *FD) {
2270-
FD = FD->getMostRecentDecl();
2268+
void ThreadSafetyAnalyzer::checkMismatchedFunctionAttrs( const NamedDecl *ND) {
22712269

2272-
auto collectCapabilities = [&](const FunctionDecl *FD) {
2273-
SmallVector<CapabilityExpr> Args;
2274-
for (const auto *A : FD->specific_attrs<RequiresCapabilityAttr>()) {
2270+
auto collectCapabilities = [&](const Decl *D) {
2271+
llvm::SmallVector<CapabilityExpr> Args;
2272+
for (const auto *A : D->specific_attrs<RequiresCapabilityAttr>()) {
22752273
for (const Expr *E : A->args())
22762274
Args.push_back(SxBuilder.translateAttrExpr(E, nullptr));
22772275
}
22782276
return Args;
22792277
};
22802278

2281-
auto FDArgs = collectCapabilities(FD);
2282-
for (const FunctionDecl *D = FD->getPreviousDecl(); D;
2279+
auto NDArgs = collectCapabilities(ND);
2280+
for (const Decl *D = ND->getPreviousDecl(); D;
22832281
D = D->getPreviousDecl()) {
22842282
auto DArgs = collectCapabilities(D);
2285-
if (DArgs.size() != FDArgs.size())
2286-
Handler.handleAttributeMismatch(FD, D);
2283+
2284+
for (const auto &[A, B] : zip_longest(NDArgs, DArgs)) {
2285+
if (!A || !B || !(*A).equals(*B)) {
2286+
Handler.handleAttributeMismatch(cast<NamedDecl>(ND), cast<NamedDecl>(D));
2287+
}
2288+
}
22872289
}
22882290
}
22892291

@@ -2306,8 +2308,8 @@ void ThreadSafetyAnalyzer::runAnalysis(AnalysisDeclContext &AC) {
23062308
const NamedDecl *D = walker.getDecl();
23072309
CurrentFunction = dyn_cast<FunctionDecl>(D);
23082310

2309-
if (CurrentFunction)
2310-
checkMismatchedFunctionAttrs(CurrentFunction);
2311+
if (isa<FunctionDecl, ObjCMethodDecl>(D))
2312+
checkMismatchedFunctionAttrs(D);
23112313

23122314
if (D->hasAttr<NoThreadSafetyAnalysisAttr>())
23132315
return;

clang/lib/Sema/AnalysisBasedWarnings.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,11 +2073,14 @@ class ThreadSafetyReporter : public clang::threadSafety::ThreadSafetyHandler {
20732073
Warnings.emplace_back(std::move(Warning), getNotes());
20742074
}
20752075

2076-
void handleAttributeMismatch(const FunctionDecl *FD1,
2077-
const FunctionDecl *FD2) override {
2078-
PartialDiagnosticAt Warning(FD2->getLocation(),
2079-
S.PDiag(diag::warn_attribute_mismatch) << FD1);
2076+
void handleAttributeMismatch(const NamedDecl *D1,
2077+
const NamedDecl *D2) override {
2078+
PartialDiagnosticAt Warning(D2->getLocation(),
2079+
S.PDiag(diag::warn_attribute_mismatch) << D1);
20802080
Warnings.emplace_back(std::move(Warning), getNotes());
2081+
2082+
PartialDiagnosticAt Note(D1->getLocation(), S.PDiag(diag::note_previous_decl) << D2);
2083+
Warnings.emplace_back(std::move(Note), getNotes());
20812084
}
20822085

20832086
void enterFunction(const FunctionDecl* FD) override {

clang/test/SemaCXX/warn-thread-safety-analysis.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,8 +2198,8 @@ namespace FunctionDefinitionTest {
21982198
class Foo {
21992199
public:
22002200
void foo1();
2201-
void foo2();
2202-
void foo3(Foo *other);
2201+
void foo2() EXCLUSIVE_LOCKS_REQUIRED(mu_);
2202+
void foo3(Foo *other) EXCLUSIVE_LOCKS_REQUIRED(other->mu_);
22032203

22042204
template<class T>
22052205
void fooT1(const T& dummy1);
@@ -2249,7 +2249,7 @@ void fooF1(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_) {
22492249
f->a = 1;
22502250
}
22512251

2252-
void fooF2(Foo *f);
2252+
void fooF2(Foo *f); // expected-warning {{attribute mismatch between function declarations of 'fooF2'}}
22532253
void fooF2(Foo *f) EXCLUSIVE_LOCKS_REQUIRED(f->mu_) {
22542254
f->a = 2;
22552255
}

0 commit comments

Comments
 (0)