Index: include/clang/Analysis/Analyses/ThreadSafety.h =================================================================== --- include/clang/Analysis/Analyses/ThreadSafety.h +++ include/clang/Analysis/Analyses/ThreadSafety.h @@ -119,9 +119,11 @@ /// \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. Index: lib/Analysis/ThreadSafety.cpp =================================================================== --- lib/Analysis/ThreadSafety.cpp +++ lib/Analysis/ThreadSafety.cpp @@ -1335,8 +1335,8 @@ // 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, Index: lib/Sema/AnalysisBasedWarnings.cpp =================================================================== --- lib/Sema/AnalysisBasedWarnings.cpp +++ lib/Sema/AnalysisBasedWarnings.cpp @@ -1642,6 +1642,13 @@ 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 @@ 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 @@ 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 @@ 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, Index: test/Sema/warn-thread-safety-analysis.c =================================================================== --- test/Sema/warn-thread-safety-analysis.c +++ test/Sema/warn-thread-safety-analysis.c @@ -117,11 +117,11 @@ (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}} Index: test/SemaCXX/warn-thread-safety-analysis.cpp =================================================================== --- test/SemaCXX/warn-thread-safety-analysis.cpp +++ test/SemaCXX/warn-thread-safety-analysis.cpp @@ -726,26 +726,26 @@ } 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();