diff options
Diffstat (limited to 'test/Analysis/initializer.cpp')
-rw-r--r-- | test/Analysis/initializer.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/test/Analysis/initializer.cpp b/test/Analysis/initializer.cpp index a71e35d..b31c315 100644 --- a/test/Analysis/initializer.cpp +++ b/test/Analysis/initializer.cpp @@ -143,3 +143,57 @@ namespace DefaultMemberInitializers { clang_analyzer_eval(w.p[1] == 'y'); // expected-warning{{TRUE}} } } + +namespace ReferenceInitialization { + struct OtherStruct { + OtherStruct(int i); + ~OtherStruct(); + }; + + struct MyStruct { + MyStruct(int i); + MyStruct(OtherStruct os); + + void method() const; + }; + + void referenceInitializeLocal() { + const MyStruct &myStruct(5); + myStruct.method(); // no-warning + } + + void referenceInitializeMultipleLocals() { + const MyStruct &myStruct1(5), myStruct2(5), &myStruct3(5); + myStruct1.method(); // no-warning + myStruct2.method(); // no-warning + myStruct3.method(); // no-warning + } + + void referenceInitializeLocalWithCleanup() { + const MyStruct &myStruct(OtherStruct(5)); + myStruct.method(); // no-warning + } + + struct HasMyStruct { + const MyStruct &ms; // expected-note {{reference member declared here}} + const MyStruct &msWithCleanups; // expected-note {{reference member declared here}} + + // clang's Sema issues a warning when binding a reference member to a + // temporary value. + HasMyStruct() : ms(5), msWithCleanups(OtherStruct(5)) { + // expected-warning@-1 {{binding reference member 'ms' to a temporary value}} + // expected-warning@-2 {{binding reference member 'msWithCleanups' to a temporary value}} + + // At this point the members are not garbage so we should not expect an + // analyzer warning here even though binding a reference member + // to a member is a terrible idea. + ms.method(); // no-warning + msWithCleanups.method(); // no-warning + } + }; + + void referenceInitializeField() { + HasMyStruct hms; + } + +}; |