[llvm-bugs] [Bug 45323] New: Spurious -Wthread-safety warning in template class

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Mar 26 21:44:42 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=45323

            Bug ID: 45323
           Summary: Spurious -Wthread-safety warning in template class
           Product: clang
           Version: unspecified
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: efriedma at quicinc.com
                CC: elizabeth.andrews at intel.com, htmldeveloper at gmail.com,
                    llvm-bugs at lists.llvm.org, neeilans at live.com,
                    richard-llvm at metafoo.co.uk

Testcase:
#include <mutex>
template <typename T>
struct X {
    std::mutex mutex_;
    void Run() {
        auto guard = std::lock_guard{mutex_};
    }
};
void (X<int>::*x)() = &X<int>::Run;

Command:

clang++ -stdlib=libc++ -D_LIBCPP_ENABLE_THREAD_SAFETY_ANNOTATIONS
-Wthread-safety -std=gnu++2a -fsyntax-only 

Prints the following warning:

/tmp/build2.cpp:7:5: warning: releasing mutex 'guard' that was not held
[-Wthread-safety-analysis]
    }
    ^
/tmp/build2.cpp:9:32: note: in instantiation of member function 'X<int>::Run'
requested here
void (X<int>::*x)() = &X<int>::Run;
                               ^
1 warning generated.

godbolt link: https://godbolt.org/z/knYGyE


I think this regressed in https://github.com/llvm/llvm-project/commit/878a24ee.
 (I haven't precisely verified it, but it's the only relevant commit around
that day.)

Diff in AST before template instantiation:

-| |       `-VarDecl  <col:9, col:44> col:14 guard 'auto' cinit
-| |         `-CXXUnresolvedConstructExpr  <col:22, col:44>
'std::lock_guard':'lock_guard' 'std::lock_guard':'lock_guard' list
-| |           `-InitListExpr  <col:37, col:44> 'void'
-| |             `-MemberExpr  <col:38> 'std::mutex':'std::__1::mutex' lvalue
->mutex_
-| |               `-CXXThisExpr  <col:38> 'X<T> *' implicit this
+| |       `-VarDecl  <col:9, col:44> col:14 guard
'std::__1::lock_guard<std::__1::mutex>':'std::__1::lock_guard<std::__1::mutex>'
cinit destroyed
+| |         `-ExprWithCleanups  <col:22, col:44>
'std::lock_guard<mutex>':'std::__1::lock_guard<std::__1::mutex>'
+| |           `-CXXBindTemporaryExpr  <col:22, col:44>
'std::lock_guard<mutex>':'std::__1::lock_guard<std::__1::mutex>' (CXXTemporary
)
+| |             `-CXXTemporaryObjectExpr  <col:22, col:44>
'std::lock_guard<mutex>':'std::__1::lock_guard<std::__1::mutex>' 'void
(std::__1::lock_guard<std::__1::mutex>::mutex_type &)' list
+| |               `-MemberExpr  <col:38> 'std::mutex':'std::__1::mutex' lvalue
->mutex_
+| |                 `-CXXThisExpr  <col:38> 'X<T> *' implicit this

Diff in AST after template instantiation:

-|   |         `-CXXBindTemporaryExpr  <col:22, col:44>
'std::lock_guard<mutex>':'std::__1::lock_guard<std::__1::mutex>' (CXXTemporary
)
-|   |           `-CXXTemporaryObjectExpr  <col:22, col:44>
'std::lock_guard<mutex>':'std::__1::lock_guard<std::__1::mutex>' 'void
(std::__1::lock_guard<std::__1::mutex>::mutex_type &)' list
-|   |             `-MemberExpr  <col:38> 'std::mutex':'std::__1::mutex' lvalue
->mutex_
-|   |               `-CXXThisExpr  <col:38> 'X<int> *' implicit this
+|   |         `-CXXFunctionalCastExpr  <col:22, col:44>
'std::lock_guard<mutex>':'std::__1::lock_guard<std::__1::mutex>' functional
cast to std::lock_guard <ConstructorConversion>
+|   |           `-CXXBindTemporaryExpr  <col:22, col:44>
'std::lock_guard<mutex>':'std::__1::lock_guard<std::__1::mutex>' (CXXTemporary
)
+|   |             `-CXXConstructExpr  <col:22, col:44>
'std::lock_guard<mutex>':'std::__1::lock_guard<std::__1::mutex>' 'void
(std::__1::lock_guard<std::__1::mutex>::mutex_type &)'
+|   |               `-MemberExpr  <col:38> 'std::mutex':'std::__1::mutex'
lvalue ->mutex_
+|   |                 `-CXXThisExpr  <col:38> 'X<int> *' implicit this


The AST after template instantiation looks suspicious.  But I'm not sure
whether the difference is actually a miscompile, or it's a legitimate variation
that's confusing the thread safety analysis.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200327/5cf8a622/attachment.html>


More information about the llvm-bugs mailing list