Clang Project

clang_source_code/test/Analysis/diagnostics/dtors.cpp
1// RUN: %clang_analyze_cc1 -w -analyzer-checker=core,cplusplus -analyzer-output=text -verify %s
2
3namespace no_crash_on_delete_dtor {
4// We were crashing when producing diagnostics for this code, but not for the
5// report that it currently emits. Instead, Static Analyzer was thinking that
6// p.get()->foo() is a null dereference because it was dropping
7// constraints over x too early and took a different branch next time
8// we call .get().
9struct S {
10  void foo();
11  ~S();
12};
13
14struct smart_ptr {
15  int x;
16  S *s;
17  smart_ptr(S *);
18  S *get() {
19    return (x || 0) ? nullptr : s; // expected-note{{Left side of '||' is false}}
20                                   // expected-note@-1{{'?' condition is false}}
21                                   // expected-warning@-2{{Use of memory after it is freed}}
22                                   // expected-note@-3{{Use of memory after it is freed}}
23  }
24};
25
26void bar(smart_ptr p) {
27  delete p.get(); // expected-note{{Memory is released}}
28  p.get()->foo(); // expected-note{{Calling 'smart_ptr::get'}}
29}
30} // namespace no_crash_on_delete_dtor
31