Skip to content

[clang] Warn on mismatched RequiresCapability attributes #67520

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Check ReleaseCapabilityAttr as well
  • Loading branch information
tbaederr committed Nov 25, 2024
commit a29f2403b3caba49c01237150c2f8635a1a08afc
40 changes: 25 additions & 15 deletions clang/lib/Analysis/ThreadSafety.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2265,27 +2265,37 @@ static bool neverReturns(const CFGBlock *B) {
return false;
}

void ThreadSafetyAnalyzer::checkMismatchedFunctionAttrs(const NamedDecl *ND) {
auto collectCapabilities = [&](const Decl *D) {
CapExprSet Caps;
for (const auto *A : D->specific_attrs<RequiresCapabilityAttr>()) {
for (const Expr *E : A->args())
Caps.push_back_nodup(SxBuilder.translateAttrExpr(E, nullptr));
}
return Caps;
};
template <typename AttrT>
static CapExprSet collectAttrArgs(SExprBuilder &SxBuilder, const Decl *D) {
CapExprSet Caps;
for (const auto *A : D->specific_attrs<AttrT>()) {
for (const Expr *E : A->args())
Caps.push_back_nodup(SxBuilder.translateAttrExpr(E, nullptr));
}
return Caps;
}

template <typename AttrT>
static void maybeDiagnoseFunctionAttrs(const NamedDecl *ND,
SExprBuilder &SxBuilder,
ThreadSafetyHandler &Handler) {

CapExprSet NDArgs = collectCapabilities(ND);
// FIXME: The diagnostic here is suboptimal. It would be better to print
// what attributes are missing in the first declaration.
CapExprSet NDArgs = collectAttrArgs<AttrT>(SxBuilder, ND);
for (const Decl *D = ND->getPreviousDecl(); D; D = D->getPreviousDecl()) {
CapExprSet DArgs = collectCapabilities(D);
CapExprSet DArgs = collectAttrArgs<AttrT>(SxBuilder, D);

for (const auto &[A, B] : zip_longest(NDArgs, DArgs)) {
if (!A || !B || !A->equals(*B))
Handler.handleAttributeMismatch(ND, cast<NamedDecl>(D));
}
if (NDArgs.size() != DArgs.size())
Handler.handleAttributeMismatch(ND, cast<NamedDecl>(D));
}
}

void ThreadSafetyAnalyzer::checkMismatchedFunctionAttrs(const NamedDecl *ND) {
maybeDiagnoseFunctionAttrs<RequiresCapabilityAttr>(ND, SxBuilder, Handler);
maybeDiagnoseFunctionAttrs<ReleaseCapabilityAttr>(ND, SxBuilder, Handler);
}

/// Check a function's CFG for thread-safety violations.
///
/// We traverse the blocks in the CFG, compute the set of mutexes that are held
Expand Down
Loading