Issue 169278
Summary Detect escape-to-field
Labels
Assignees
Reporter usx95
    https://godbolt.org/z/d59vnhW8Y
```
#include <iostream>
#include <string>
#include <string_view>

std::string GLOBAL = "global string";

struct S {
    std::string* p;

    // Test 1.
 void foo1() {
        std::string stack;
        p = &stack;  // warning: escapes to field 'p'.
        // At function exit, p points to stack addr.
 }

    // Test 2.
    void foo2() {
        std::string stack;
 p = &stack;
        p = &GLOBAL;  // p is saved before function exit.
 }

    // Test 3.
    // Member function which can use all fields (implicit use of 'this')
    void bar();
    void foo3() {
        {
 std::string stack;
            p = &stack;  // warning: escapes to field 'p'.
        }  // destroyed here.
        bar();        // later used here (via use of 'this').
        p = &GLOBAL;  // this does not save escape now.
    }
};
```

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to