Skip to content

Commit 96f8542

Browse files
committed
Warn on RequiresCapability attribute mismatches between declarations
1 parent 7b583e1 commit 96f8542

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4033,6 +4033,9 @@ def warn_acquired_before : Warning<
40334033
def warn_acquired_before_after_cycle : Warning<
40344034
"cycle in acquired_before/after dependencies, starting with '%0'">,
40354035
InGroup<ThreadSafetyAnalysis>, DefaultIgnore;
4036+
def warn_attribute_mismatch : Warning<
4037+
"attribute mismatch between function declarations of %0">,
4038+
InGroup<ThreadSafetyAttributes>, DefaultIgnore;
40364039

40374040

40384041
// Thread safety warnings negative capabilities

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5794,6 +5794,30 @@ static void handleRequiresCapabilityAttr(Sema &S, Decl *D,
57945794
RequiresCapabilityAttr(S.Context, AL, Args.data(), Args.size());
57955795

57965796
D->addAttr(RCA);
5797+
5798+
if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
5799+
5800+
auto collectExprs = [](const FunctionDecl *FuncDecl) {
5801+
std::set<const ValueDecl*> Args;
5802+
for (const auto *A : FuncDecl->specific_attrs<RequiresCapabilityAttr>()) {
5803+
for (const Expr *E : A->args()) {
5804+
if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
5805+
Args.insert(DRE->getDecl());
5806+
}
5807+
}
5808+
return Args;
5809+
};
5810+
auto ThisDecl = collectExprs(FD);
5811+
for (const FunctionDecl *P = FD->getPreviousDecl(); P;
5812+
P = P->getPreviousDecl()) {
5813+
auto PrevDecl = collectExprs(P);
5814+
// FIXME: It would be nice to mention _what_ attribute isn't matched and maybe
5815+
// even where the previous declaration was?
5816+
if (ThisDecl.size() != PrevDecl.size())
5817+
S.Diag(D->getLocation(), diag::warn_attribute_mismatch) << FD;
5818+
}
5819+
5820+
}
57975821
}
57985822

57995823
static void handleDeprecatedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {

0 commit comments

Comments
 (0)