Thread safety analysis: More consistent warning message

Other warning messages for negative capabilities also mention their
kind, and the double space was ugly.

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D84603
This commit is contained in:
Aaron Puchert 2020-09-01 23:13:28 +02:00
parent 431bb8b318
commit 8ca00c5cdc
6 changed files with 21 additions and 4 deletions

View File

@ -202,6 +202,14 @@ public:
virtual void handleNegativeNotHeld(StringRef Kind, Name LockName, Name Neg, virtual void handleNegativeNotHeld(StringRef Kind, Name LockName, Name Neg,
SourceLocation Loc) {} SourceLocation Loc) {}
/// Warn when calling a function that a negative capability is not held.
/// \param D -- The decl for the function requiring the negative capability.
/// \param LockName -- The name for the lock expression, to be printed in the
/// diagnostic.
/// \param Loc -- The location of the protected operation.
virtual void handleNegativeNotHeld(const NamedDecl *D, Name LockName,
SourceLocation Loc) {}
/// Warn when a function is called while an excluded mutex is locked. For /// Warn when a function is called while an excluded mutex is locked. For
/// example, the mutex may be locked inside the function. /// example, the mutex may be locked inside the function.
/// \param Kind -- the capability's name parameter (role, mutex, etc). /// \param Kind -- the capability's name parameter (role, mutex, etc).

View File

@ -3477,6 +3477,9 @@ def warn_acquired_before_after_cycle : Warning<
def warn_acquire_requires_negative_cap : Warning< def warn_acquire_requires_negative_cap : Warning<
"acquiring %0 '%1' requires negative capability '%2'">, "acquiring %0 '%1' requires negative capability '%2'">,
InGroup<ThreadSafetyNegative>, DefaultIgnore; InGroup<ThreadSafetyNegative>, DefaultIgnore;
def warn_fun_requires_negative_cap : Warning<
"calling function %0 requires negative capability '%1'">,
InGroup<ThreadSafetyAnalysis>, DefaultIgnore;
// Thread safety warnings on pass by reference // Thread safety warnings on pass by reference
def warn_guarded_pass_by_reference : Warning< def warn_guarded_pass_by_reference : Warning<

View File

@ -1641,8 +1641,7 @@ void BuildLockset::warnIfMutexNotHeld(const NamedDecl *D, const Expr *Exp,
// Otherwise the negative requirement must be propagated to the caller. // Otherwise the negative requirement must be propagated to the caller.
LDat = FSet.findLock(Analyzer->FactMan, Cp); LDat = FSet.findLock(Analyzer->FactMan, Cp);
if (!LDat) { if (!LDat) {
Analyzer->Handler.handleMutexNotHeld("", D, POK, Cp.toString(), Analyzer->Handler.handleNegativeNotHeld(D, Cp.toString(), Loc);
LK_Shared, Loc);
} }
return; return;
} }

View File

@ -1892,6 +1892,13 @@ class ThreadSafetyReporter : public clang::threadSafety::ThreadSafetyHandler {
Warnings.emplace_back(std::move(Warning), getNotes()); Warnings.emplace_back(std::move(Warning), getNotes());
} }
void handleNegativeNotHeld(const NamedDecl *D, Name LockName,
SourceLocation Loc) override {
PartialDiagnosticAt Warning(
Loc, S.PDiag(diag::warn_fun_requires_negative_cap) << D << LockName);
Warnings.emplace_back(std::move(Warning), getNotes());
}
void handleFunExcludesLock(StringRef Kind, Name FunName, Name LockName, void handleFunExcludesLock(StringRef Kind, Name FunName, Name LockName,
SourceLocation Loc) override { SourceLocation Loc) override {
PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_fun_excludes_mutex) PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_fun_excludes_mutex)

View File

@ -4985,7 +4985,7 @@ public:
} }
void bar() { void bar() {
bar2(); // expected-warning {{calling function 'bar2' requires holding '!mu'}} bar2(); // expected-warning {{calling function 'bar2' requires negative capability '!mu'}}
} }
void bar2() EXCLUSIVE_LOCKS_REQUIRED(!mu) { void bar2() EXCLUSIVE_LOCKS_REQUIRED(!mu) {

View File

@ -50,7 +50,7 @@ public:
} }
void bar() { void bar() {
baz(); // expected-warning {{calling function 'baz' requires holding '!mu'}} baz(); // expected-warning {{calling function 'baz' requires negative capability '!mu'}}
} }
void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu) { void baz() EXCLUSIVE_LOCKS_REQUIRED(!mu) {