diff options
-rw-r--r-- | include/clang/Analysis/Analyses/ThreadSafety.h | 2 | ||||
-rw-r--r-- | lib/Analysis/ThreadSafety.cpp | 4 | ||||
-rw-r--r-- | lib/Sema/AnalysisBasedWarnings.cpp | 28 | ||||
-rw-r--r-- | test/Sema/warn-thread-safety-analysis.c | 4 | ||||
-rw-r--r-- | test/SemaCXX/warn-thread-safety-analysis.cpp | 8 |
5 files changed, 24 insertions, 22 deletions
diff --git a/include/clang/Analysis/Analyses/ThreadSafety.h b/include/clang/Analysis/Analyses/ThreadSafety.h index 2f0c68c8d6..79cdac2527 100644 --- a/include/clang/Analysis/Analyses/ThreadSafety.h +++ b/include/clang/Analysis/Analyses/ThreadSafety.h @@ -119,9 +119,11 @@ public: /// \param Kind -- the capability's name parameter (role, mutex, etc). /// \param Expected -- the kind of lock expected. /// \param Received -- the kind of lock received. + /// \param LocLocked -- The SourceLocation of the Lock. /// \param Loc -- The SourceLocation of the Unlock. virtual void handleIncorrectUnlockKind(StringRef Kind, Name LockName, LockKind Expected, LockKind Received, + SourceLocation LocLocked, SourceLocation Loc) {} /// Warn about lock function calls for locks which are already held. diff --git a/lib/Analysis/ThreadSafety.cpp b/lib/Analysis/ThreadSafety.cpp index 1887e5431e..bd65ea711b 100644 --- a/lib/Analysis/ThreadSafety.cpp +++ b/lib/Analysis/ThreadSafety.cpp @@ -1335,8 +1335,8 @@ void ThreadSafetyAnalyzer::removeLock(FactSet &FSet, const CapabilityExpr &Cp, // Generic lock removal doesn't care about lock kind mismatches, but // otherwise diagnose when the lock kinds are mismatched. if (ReceivedKind != LK_Generic && LDat->kind() != ReceivedKind) { - Handler.handleIncorrectUnlockKind(DiagKind, Cp.toString(), - LDat->kind(), ReceivedKind, UnlockLoc); + Handler.handleIncorrectUnlockKind(DiagKind, Cp.toString(), LDat->kind(), + ReceivedKind, LDat->loc(), UnlockLoc); } LDat->handleUnlock(FSet, FactMan, Cp, UnlockLoc, FullyRemove, Handler, diff --git a/lib/Sema/AnalysisBasedWarnings.cpp b/lib/Sema/AnalysisBasedWarnings.cpp index 6120d2b9df..00fa33f960 100644 --- a/lib/Sema/AnalysisBasedWarnings.cpp +++ b/lib/Sema/AnalysisBasedWarnings.cpp @@ -1642,6 +1642,13 @@ class ThreadSafetyReporter : public clang::threadSafety::ThreadSafetyHandler { return ONS; } + OptionalNotes makeLockedHereNote(SourceLocation LocLocked, StringRef Kind) { + return LocLocked.isValid() + ? getNotes(PartialDiagnosticAt( + LocLocked, S.PDiag(diag::note_locked_here) << Kind)) + : getNotes(); + } + public: ThreadSafetyReporter(Sema &S, SourceLocation FL, SourceLocation FEL) : S(S), FunLocation(FL), FunEndLocation(FEL), @@ -1679,13 +1686,15 @@ class ThreadSafetyReporter : public clang::threadSafety::ThreadSafetyHandler { void handleIncorrectUnlockKind(StringRef Kind, Name LockName, LockKind Expected, LockKind Received, + SourceLocation LocLocked, SourceLocation Loc) override { if (Loc.isInvalid()) Loc = FunLocation; PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_unlock_kind_mismatch) << Kind << LockName << Received << Expected); - Warnings.emplace_back(std::move(Warning), getNotes()); + Warnings.emplace_back(std::move(Warning), + makeLockedHereNote(LocLocked, Kind)); } void handleDoubleLock(StringRef Kind, Name LockName, SourceLocation LocLocked, @@ -1694,12 +1703,8 @@ class ThreadSafetyReporter : public clang::threadSafety::ThreadSafetyHandler { Loc = FunLocation; PartialDiagnosticAt Warning(Loc, S.PDiag(diag::warn_double_lock) << Kind << LockName); - OptionalNotes Notes = - LocLocked.isValid() - ? getNotes(PartialDiagnosticAt( - LocLocked, S.PDiag(diag::note_locked_here) << Kind)) - : getNotes(); - Warnings.emplace_back(std::move(Warning), std::move(Notes)); + Warnings.emplace_back(std::move(Warning), + makeLockedHereNote(LocLocked, Kind)); } void handleMutexHeldEndOfScope(StringRef Kind, Name LockName, @@ -1726,13 +1731,8 @@ class ThreadSafetyReporter : public clang::threadSafety::ThreadSafetyHandler { PartialDiagnosticAt Warning(LocEndOfScope, S.PDiag(DiagID) << Kind << LockName); - if (LocLocked.isValid()) { - PartialDiagnosticAt Note(LocLocked, S.PDiag(diag::note_locked_here) - << Kind); - Warnings.emplace_back(std::move(Warning), getNotes(Note)); - return; - } - Warnings.emplace_back(std::move(Warning), getNotes()); + Warnings.emplace_back(std::move(Warning), + makeLockedHereNote(LocLocked, Kind)); } void handleExclusiveAndShared(StringRef Kind, Name LockName, diff --git a/test/Sema/warn-thread-safety-analysis.c b/test/Sema/warn-thread-safety-analysis.c index 52dacfce06..11b314008c 100644 --- a/test/Sema/warn-thread-safety-analysis.c +++ b/test/Sema/warn-thread-safety-analysis.c @@ -117,11 +117,11 @@ int main() { (void)(*d_ == 1); mutex_unlock(foo_.mu_); - mutex_exclusive_lock(&mu1); + mutex_exclusive_lock(&mu1); // expected-note {{mutex acquired here}} mutex_shared_unlock(&mu1); // expected-warning {{releasing mutex 'mu1' using shared access, expected exclusive access}} mutex_exclusive_unlock(&mu1); // expected-warning {{releasing mutex 'mu1' that was not held}} - mutex_shared_lock(&mu1); + mutex_shared_lock(&mu1); // expected-note {{mutex acquired here}} mutex_exclusive_unlock(&mu1); // expected-warning {{releasing mutex 'mu1' using exclusive access, expected shared access}} mutex_shared_unlock(&mu1); // expected-warning {{releasing mutex 'mu1' that was not held}} diff --git a/test/SemaCXX/warn-thread-safety-analysis.cpp b/test/SemaCXX/warn-thread-safety-analysis.cpp index 4435131a47..ac89ea86f0 100644 --- a/test/SemaCXX/warn-thread-safety-analysis.cpp +++ b/test/SemaCXX/warn-thread-safety-analysis.cpp @@ -726,26 +726,26 @@ void shared_bad_2() { } void shared_bad_3() { - sls_mu.Lock(); + sls_mu.Lock(); // expected-note {{mutex acquired here}} sls_mu.ReaderUnlock(); // \ // expected-warning {{releasing mutex 'sls_mu' using shared access, expected exclusive access}} } void shared_bad_4() { - sls_mu.ReaderLock(); + sls_mu.ReaderLock(); // expected-note {{mutex acquired here}} sls_mu.ExclusiveUnlock(); // \ // expected-warning {{releasing mutex 'sls_mu' using exclusive access, expected shared access}} } void shared_bad_5() { - sls_mu.Lock(); + sls_mu.Lock(); // expected-note {{mutex acquired here}} sls_mu.PromoteShared(); // \ // expected-warning {{releasing mutex 'sls_mu' using shared access, expected exclusive access}} sls_mu.ExclusiveUnlock(); } void shared_bad_6() { - sls_mu.ReaderLock(); + sls_mu.ReaderLock(); // expected-note {{mutex acquired here}} sls_mu.DemoteExclusive(); // \ // expected-warning {{releasing mutex 'sls_mu' using exclusive access, expected shared access}} sls_mu.ReaderUnlock(); |