Skip to content

Commit cc220d4

Browse files
committed
[lldb/Test] Use RAII for reproducer instrumentation data in unittest.
Use a RAII object to manage the lifetime of the reproducer instrumentation data.
1 parent 208a11a commit cc220d4

File tree

1 file changed

+55
-51
lines changed

1 file changed

+55
-51
lines changed

lldb/unittests/Utility/ReproducerInstrumentationTest.cpp

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,29 @@ class TestingRegistry : public Registry {
5050
TestingRegistry();
5151
};
5252

53-
static llvm::Optional<Serializer> g_serializer;
5453
static llvm::Optional<TestingRegistry> g_registry;
54+
static llvm::Optional<Serializer> g_serializer;
55+
56+
inline InstrumentationData GetTestInstrumentationData() {
57+
if (g_serializer)
58+
return InstrumentationData(*g_serializer, *g_registry);
59+
return InstrumentationData();
60+
}
61+
62+
class TestInstrumentationDataRAII {
63+
public:
64+
TestInstrumentationDataRAII(llvm::raw_string_ostream &os) {
65+
g_registry.emplace();
66+
g_serializer.emplace(os);
67+
}
68+
69+
~TestInstrumentationDataRAII() {
70+
g_registry.reset();
71+
g_serializer.reset();
72+
}
73+
};
5574

56-
#define LLDB_GET_INSTRUMENTATION_DATA() \
57-
g_serializer ? InstrumentationData(*g_serializer, *g_registry) \
58-
: InstrumentationData()
75+
#define LLDB_GET_INSTRUMENTATION_DATA() GetTestInstrumentationData()
5976

6077
enum class Class {
6178
Foo,
@@ -120,11 +137,6 @@ class InstrumentedBar : public Instrumented {
120137
double InstrumentedFoo::g_e = 0;
121138
bool InstrumentedFoo::g_f = false;
122139

123-
void ClearObjects() {
124-
g_registry.reset();
125-
g_serializer.reset();
126-
}
127-
128140
struct Validator {
129141
enum Validation { valid, invalid };
130142
Validator(Class clazz, Validation validation)
@@ -520,10 +532,10 @@ TEST(SerializationRountripTest, SerializeDeserializeObjectReference) {
520532
TEST(RecordReplayTest, InstrumentedFoo) {
521533
std::string str;
522534
llvm::raw_string_ostream os(str);
523-
g_registry.emplace();
524-
g_serializer.emplace(os);
525535

526536
{
537+
TestInstrumentationDataRAII data(os);
538+
527539
int b = 200;
528540
float c = 300.3f;
529541
double e = 400.4;
@@ -538,8 +550,6 @@ TEST(RecordReplayTest, InstrumentedFoo) {
538550
foo.Validate();
539551
}
540552

541-
ClearObjects();
542-
543553
TestingRegistry registry;
544554
Deserializer deserializer(os.str());
545555
registry.Replay(deserializer);
@@ -551,34 +561,34 @@ TEST(RecordReplayTest, InstrumentedFoo) {
551561
TEST(RecordReplayTest, InstrumentedFooSameThis) {
552562
std::string str;
553563
llvm::raw_string_ostream os(str);
554-
g_registry.emplace();
555-
g_serializer.emplace(os);
556564

557-
int b = 200;
558-
float c = 300.3f;
559-
double e = 400.4;
565+
{
566+
TestInstrumentationDataRAII data(os);
560567

561-
InstrumentedFoo *foo = new InstrumentedFoo(0);
562-
foo->A(100);
563-
foo->B(b);
564-
foo->C(&c);
565-
foo->D("bar");
566-
InstrumentedFoo::E(e);
567-
InstrumentedFoo::F();
568-
foo->Validate();
569-
foo->~InstrumentedFoo();
570-
571-
InstrumentedFoo *foo2 = new (foo) InstrumentedFoo(0);
572-
foo2->A(100);
573-
foo2->B(b);
574-
foo2->C(&c);
575-
foo2->D("bar");
576-
InstrumentedFoo::E(e);
577-
InstrumentedFoo::F();
578-
foo2->Validate();
579-
delete foo2;
580-
581-
ClearObjects();
568+
int b = 200;
569+
float c = 300.3f;
570+
double e = 400.4;
571+
572+
InstrumentedFoo *foo = new InstrumentedFoo(0);
573+
foo->A(100);
574+
foo->B(b);
575+
foo->C(&c);
576+
foo->D("bar");
577+
InstrumentedFoo::E(e);
578+
InstrumentedFoo::F();
579+
foo->Validate();
580+
foo->~InstrumentedFoo();
581+
582+
InstrumentedFoo *foo2 = new (foo) InstrumentedFoo(0);
583+
foo2->A(100);
584+
foo2->B(b);
585+
foo2->C(&c);
586+
foo2->D("bar");
587+
InstrumentedFoo::E(e);
588+
InstrumentedFoo::F();
589+
foo2->Validate();
590+
delete foo2;
591+
}
582592

583593
TestingRegistry registry;
584594
Deserializer deserializer(os.str());
@@ -591,10 +601,10 @@ TEST(RecordReplayTest, InstrumentedFooSameThis) {
591601
TEST(RecordReplayTest, InstrumentedBar) {
592602
std::string str;
593603
llvm::raw_string_ostream os(str);
594-
g_registry.emplace();
595-
g_serializer.emplace(os);
596604

597605
{
606+
TestInstrumentationDataRAII data(os);
607+
598608
InstrumentedBar bar;
599609
InstrumentedFoo foo = bar.GetInstrumentedFoo();
600610

@@ -615,8 +625,6 @@ TEST(RecordReplayTest, InstrumentedBar) {
615625
bar.Validate();
616626
}
617627

618-
ClearObjects();
619-
620628
TestingRegistry registry;
621629
Deserializer deserializer(os.str());
622630
registry.Replay(deserializer);
@@ -633,10 +641,10 @@ TEST(RecordReplayTest, InstrumentedBar) {
633641
TEST(RecordReplayTest, InstrumentedBarRef) {
634642
std::string str;
635643
llvm::raw_string_ostream os(str);
636-
g_registry.emplace();
637-
g_serializer.emplace(os);
638644

639645
{
646+
TestInstrumentationDataRAII data(os);
647+
640648
InstrumentedBar bar;
641649
InstrumentedFoo &foo = bar.GetInstrumentedFooRef();
642650

@@ -657,8 +665,6 @@ TEST(RecordReplayTest, InstrumentedBarRef) {
657665
bar.Validate();
658666
}
659667

660-
ClearObjects();
661-
662668
TestingRegistry registry;
663669
Deserializer deserializer(os.str());
664670
registry.Replay(deserializer);
@@ -671,10 +677,10 @@ TEST(RecordReplayTest, InstrumentedBarRef) {
671677
TEST(RecordReplayTest, InstrumentedBarPtr) {
672678
std::string str;
673679
llvm::raw_string_ostream os(str);
674-
g_registry.emplace();
675-
g_serializer.emplace(os);
676680

677681
{
682+
TestInstrumentationDataRAII data(os);
683+
678684
InstrumentedBar bar;
679685
InstrumentedFoo &foo = *(bar.GetInstrumentedFooPtr());
680686

@@ -695,8 +701,6 @@ TEST(RecordReplayTest, InstrumentedBarPtr) {
695701
bar.Validate();
696702
}
697703

698-
ClearObjects();
699-
700704
TestingRegistry registry;
701705
Deserializer deserializer(os.str());
702706
registry.Replay(deserializer);

0 commit comments

Comments
 (0)