1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // void push_back(const value_type& x);
17 #include "asan_testing.h"
19 // Flag that makes the copy constructor for CMyClass throw an exception
20 static bool gCopyConstructorShouldThow = false;
24 public: CMyClass(int tag);
25 public: CMyClass(const CMyClass& iOther);
28 bool equal(const CMyClass &rhs) const
29 { return fTag == rhs.fTag && fMagicValue == rhs.fMagicValue; }
34 private: static int kStartedConstructionMagicValue;
35 private: static int kFinishedConstructionMagicValue;
38 // Value for fMagicValue when the constructor has started running, but not yet finished
39 int CMyClass::kStartedConstructionMagicValue = 0;
40 // Value for fMagicValue when the constructor has finished running
41 int CMyClass::kFinishedConstructionMagicValue = 12345;
43 CMyClass::CMyClass(int tag) :
44 fMagicValue(kStartedConstructionMagicValue), fTag(tag)
46 // Signal that the constructor has finished running
47 fMagicValue = kFinishedConstructionMagicValue;
50 CMyClass::CMyClass(const CMyClass& iOther) :
51 fMagicValue(kStartedConstructionMagicValue), fTag(iOther.fTag)
53 // If requested, throw an exception _before_ setting fMagicValue to kFinishedConstructionMagicValue
54 if (gCopyConstructorShouldThow) {
55 throw std::exception();
57 // Signal that the constructor has finished running
58 fMagicValue = kFinishedConstructionMagicValue;
61 CMyClass::~CMyClass() {
62 // Only instances for which the constructor has finished running should be destructed
63 assert(fMagicValue == kFinishedConstructionMagicValue);
66 bool operator==(const CMyClass &lhs, const CMyClass &rhs) { return lhs.equal(rhs); }
70 CMyClass instance(42);
71 std::vector<CMyClass> vec;
73 vec.push_back(instance);
74 std::vector<CMyClass> vec2(vec);
75 assert(is_contiguous_container_asan_correct(vec));
76 assert(is_contiguous_container_asan_correct(vec2));
78 gCopyConstructorShouldThow = true;
80 vec.push_back(instance);
84 assert(is_contiguous_container_asan_correct(vec));