Skip to content

Commit e58aa08

Browse files
committed
Warn on RequiresCapability attribute mismatches between declarations
1 parent ab1709e commit e58aa08

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
@@ -4031,6 +4031,9 @@ def warn_acquired_before : Warning<
40314031
def warn_acquired_before_after_cycle : Warning<
40324032
"cycle in acquired_before/after dependencies, starting with '%0'">,
40334033
InGroup<ThreadSafetyAnalysis>, DefaultIgnore;
4034+
def warn_attribute_mismatch : Warning<
4035+
"attribute mismatch between function declarations of %0">,
4036+
InGroup<ThreadSafetyAttributes>, DefaultIgnore;
40344037

40354038

40364039
// Thread safety warnings negative capabilities

clang/lib/Sema/SemaDeclAttr.cpp

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

58445844
D->addAttr(RCA);
5845+
5846+
if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
5847+
5848+
auto collectExprs = [](const FunctionDecl *FuncDecl) {
5849+
std::set<const ValueDecl*> Args;
5850+
for (const auto *A : FuncDecl->specific_attrs<RequiresCapabilityAttr>()) {
5851+
for (const Expr *E : A->args()) {
5852+
if (const auto *DRE = dyn_cast<DeclRefExpr>(E))
5853+
Args.insert(DRE->getDecl());
5854+
}
5855+
}
5856+
return Args;
5857+
};
5858+
auto ThisDecl = collectExprs(FD);
5859+
for (const FunctionDecl *P = FD->getPreviousDecl(); P;
5860+
P = P->getPreviousDecl()) {
5861+
auto PrevDecl = collectExprs(P);
5862+
// FIXME: It would be nice to mention _what_ attribute isn't matched and maybe
5863+
// even where the previous declaration was?
5864+
if (ThisDecl.size() != PrevDecl.size())
5865+
S.Diag(D->getLocation(), diag::warn_attribute_mismatch) << FD;
5866+
}
5867+
5868+
}
58455869
}
58465870

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

0 commit comments

Comments
 (0)