Author: aaronpuchert Date: Mon Mar 18 16:26:54 2019 New Revision: 356427 URL: http://llvm.org/viewvc/llvm-project?rev=356427&view=rev Log: Thread safety analysis: Add note for unlock kind mismatch
Summary: Similar to D56967, we add the existing diag::note_locked_here to tell the user where we saw the locking that isn't matched correctly. Reviewers: aaron.ballman, delesley Reviewed By: aaron.ballman Subscribers: cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D59455 Modified: cfe/trunk/include/clang/Analysis/Analyses/ThreadSafety.h cfe/trunk/lib/Analysis/ThreadSafety.cpp cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp cfe/trunk/test/Sema/warn-thread-safety-analysis.c cfe/trunk/test/SemaCXX/warn-thread-safety-analysis.cpp Modified: cfe/trunk/include/clang/Analysis/Analyses/ThreadSafety.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Analysis/Analyses/ThreadSafety.h?rev=356427&r1=356426&r2=356427&view=diff ============================================================================== --- cfe/trunk/include/clang/Analysis/Analyses/ThreadSafety.h (original) +++ cfe/trunk/include/clang/Analysis/Analyses/ThreadSafety.h Mon Mar 18 16:26:54 2019 @@ -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. Modified: cfe/trunk/lib/Analysis/ThreadSafety.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Analysis/ThreadSafety.cpp?rev=356427&r1=356426&r2=356427&view=diff ============================================================================== --- cfe/trunk/lib/Analysis/ThreadSafety.cpp (original) +++ cfe/trunk/lib/Analysis/ThreadSafety.cpp Mon Mar 18 16:26:54 2019 @@ -1335,8 +1335,8 @@ void ThreadSafetyAnalyzer::removeLock(Fa // 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, Modified: cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp?rev=356427&r1=356426&r2=356427&view=diff ============================================================================== --- cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp (original) +++ cfe/trunk/lib/Sema/AnalysisBasedWarnings.cpp Mon Mar 18 16:26:54 2019 @@ -1642,6 +1642,13 @@ class ThreadSafetyReporter : public clan 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 clan 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 clan 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 clan 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, Modified: cfe/trunk/test/Sema/warn-thread-safety-analysis.c URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-thread-safety-analysis.c?rev=356427&r1=356426&r2=356427&view=diff ============================================================================== --- cfe/trunk/test/Sema/warn-thread-safety-analysis.c (original) +++ cfe/trunk/test/Sema/warn-thread-safety-analysis.c Mon Mar 18 16:26:54 2019 @@ -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}} Modified: cfe/trunk/test/SemaCXX/warn-thread-safety-analysis.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/warn-thread-safety-analysis.cpp?rev=356427&r1=356426&r2=356427&view=diff ============================================================================== --- cfe/trunk/test/SemaCXX/warn-thread-safety-analysis.cpp (original) +++ cfe/trunk/test/SemaCXX/warn-thread-safety-analysis.cpp Mon Mar 18 16:26:54 2019 @@ -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(); _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits