================
@@ -25,16 +25,43 @@
 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
 #include "llvm/ADT/STLExtras.h"
-#include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringExtras.h"
 
 #include <iterator>
 #include <utility>
-#include <variant>
 
 using namespace clang;
 using namespace ento;
 
+static const MemRegion *getFirstArgRegion(const CallEvent &Call) {
+  return Call.getArgSVal(0).getAsRegion();
+}
+
+static const MemRegion *getCXXThisRegion(const CallEvent &Call) {
+  return cast<CXXMemberCall>(Call).getCXXThisVal().getAsRegion();
+}
+
+static const MemRegion *getObjectUnderConstruction(const CallEvent &Call) {
+  if (std::optional<SVal> Object = Call.getReturnValueUnderConstruction())
+    return Object->getAsRegion();
+  return nullptr;
+}
+
+static const MemRegion *getCXXDestructorThisRegion(const CallEvent &Call) {
+  return cast<CXXDestructorCall>(Call).getCXXThisVal().getAsRegion();
----------------
steakhal wrote:

`Call` may not be a `CXXDestructorCall`. For example on this claude generated 
example the new clang crashes:
```c++
  void sleep(int);
  namespace std {
  struct mutex { void lock(); void unlock(); };
  template <class M> struct lock_guard {
    M *m_;
    explicit lock_guard(M &m) : m_(&m) { m_->lock(); }
    ~lock_guard() { m_->unlock(); }
  };
  }
  void explicit_dtor(std::mutex &m) {
    auto *g = new std::lock_guard<std::mutex>(m);
    sleep(1);
    g->~lock_guard();   // CXXMemberCall, not CXXDestructorCall
    sleep(1);
  }
    clang -cc1 -analyze -analyzer-checker=core,unix.BlockInCriticalSection 
-std=c++17

    base (HEAD^):
  min.cpp:12:3: warning: Call to blocking function 'sleep' inside of critical 
section
  min.cpp:14:3: warning: Call to blocking function 'sleep' inside of critical 
section
  2 warnings generated.
    patched:
  Assertion failed: (isa<To>(Val) && "cast<Ty>() argument of incompatible 
type!"), function cast, file Casting.h, line
```

 It also crashes when the destructor has no body available - allegedly.

Suggested regression test for 
clang/test/Analysis/block-in-critical-section-raii.cpp:
```c++
  // Explicit destructor call: modelled as a CXXMemberCall, not a 
CXXDestructorCall.
  void explicit_dtor(std::mutex &m) {
    auto *g = new std::unique_lock<std::mutex>(m);
    sleep(1); // expected-warning {{Call to blocking function 'sleep' inside of 
critical section}}
    g->~unique_lock();
    sleep(1); // expected-warning {{Call to blocking function 'sleep' inside of 
critical section}}
  }
```

https://github.com/llvm/llvm-project/pull/224230
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to