blob: 6615a25a620fa12a7dd02421bc4e86533f06a01a [file] [log] [blame]
Howard Hinnantf619e232013-01-11 20:36:59 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <vector>
11
12// void push_back(const value_type& x);
13
14#include <vector>
15#include <cassert>
16
Marshall Clow1f50f2d2014-05-08 14:14:06 +000017#include "asan_testing.h"
18
Howard Hinnantf619e232013-01-11 20:36:59 +000019// Flag that makes the copy constructor for CMyClass throw an exception
20static bool gCopyConstructorShouldThow = false;
21
22
23class CMyClass {
Marshall Clowcbb93052014-03-04 20:31:21 +000024 public: CMyClass(int tag);
Howard Hinnant171771a2013-07-08 21:06:38 +000025 public: CMyClass(const CMyClass& iOther);
26 public: ~CMyClass();
Howard Hinnantf619e232013-01-11 20:36:59 +000027
Marshall Clowcbb93052014-03-04 20:31:21 +000028 bool equal(const CMyClass &rhs) const
29 { return fTag == rhs.fTag && fMagicValue == rhs.fMagicValue; }
30 private:
31 int fMagicValue;
32 int fTag;
33
Howard Hinnant171771a2013-07-08 21:06:38 +000034 private: static int kStartedConstructionMagicValue;
35 private: static int kFinishedConstructionMagicValue;
Howard Hinnantf619e232013-01-11 20:36:59 +000036};
37
38// Value for fMagicValue when the constructor has started running, but not yet finished
39int CMyClass::kStartedConstructionMagicValue = 0;
40// Value for fMagicValue when the constructor has finished running
41int CMyClass::kFinishedConstructionMagicValue = 12345;
42
Marshall Clowcbb93052014-03-04 20:31:21 +000043CMyClass::CMyClass(int tag) :
44 fMagicValue(kStartedConstructionMagicValue), fTag(tag)
Howard Hinnantf619e232013-01-11 20:36:59 +000045{
Howard Hinnant171771a2013-07-08 21:06:38 +000046 // Signal that the constructor has finished running
47 fMagicValue = kFinishedConstructionMagicValue;
Howard Hinnantf619e232013-01-11 20:36:59 +000048}
49
Marshall Clowcbb93052014-03-04 20:31:21 +000050CMyClass::CMyClass(const CMyClass& iOther) :
51 fMagicValue(kStartedConstructionMagicValue), fTag(iOther.fTag)
Howard Hinnantf619e232013-01-11 20:36:59 +000052{
Howard Hinnant171771a2013-07-08 21:06:38 +000053 // If requested, throw an exception _before_ setting fMagicValue to kFinishedConstructionMagicValue
54 if (gCopyConstructorShouldThow) {
55 throw std::exception();
56 }
57 // Signal that the constructor has finished running
58 fMagicValue = kFinishedConstructionMagicValue;
Howard Hinnantf619e232013-01-11 20:36:59 +000059}
60
61CMyClass::~CMyClass() {
Howard Hinnant171771a2013-07-08 21:06:38 +000062 // Only instances for which the constructor has finished running should be destructed
63 assert(fMagicValue == kFinishedConstructionMagicValue);
Howard Hinnantf619e232013-01-11 20:36:59 +000064}
65
Marshall Clowcbb93052014-03-04 20:31:21 +000066bool operator==(const CMyClass &lhs, const CMyClass &rhs) { return lhs.equal(rhs); }
67
Howard Hinnantf619e232013-01-11 20:36:59 +000068int main()
69{
Marshall Clowcbb93052014-03-04 20:31:21 +000070 CMyClass instance(42);
Howard Hinnant171771a2013-07-08 21:06:38 +000071 std::vector<CMyClass> vec;
Howard Hinnantf619e232013-01-11 20:36:59 +000072
Howard Hinnant171771a2013-07-08 21:06:38 +000073 vec.push_back(instance);
Marshall Clowcbb93052014-03-04 20:31:21 +000074 std::vector<CMyClass> vec2(vec);
Marshall Clow1f50f2d2014-05-08 14:14:06 +000075 assert(is_contiguous_container_asan_correct(vec));
76 assert(is_contiguous_container_asan_correct(vec2));
Howard Hinnantf619e232013-01-11 20:36:59 +000077
Howard Hinnant171771a2013-07-08 21:06:38 +000078 gCopyConstructorShouldThow = true;
79 try {
80 vec.push_back(instance);
81 }
82 catch (...) {
Marshall Clowcbb93052014-03-04 20:31:21 +000083 assert(vec==vec2);
Marshall Clow1f50f2d2014-05-08 14:14:06 +000084 assert(is_contiguous_container_asan_correct(vec));
Howard Hinnant171771a2013-07-08 21:06:38 +000085 }
Howard Hinnantf619e232013-01-11 20:36:59 +000086}