[clang] Reland: [clang] Diagnose dangling issues for the "Container<GSLPointer>" case. #107213 (PR #108344)

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 12 16:23:50 PDT 2024


================
@@ -633,4 +633,12 @@ std::optional<int*> test4(int a) {
   return std::make_optional(nullptr); // fine
 }
 
+template <typename T>
+struct [[gsl::Owner]] StatusOr {
+  const T &value() [[clang::lifetimebound]];
+};
----------------
usx95 wrote:

Some ideas: https://godbolt.org/z/Y6nPM9j1b
Feel free to expand and add more. 
```cpp
#include<string>
#include <vector>

template <typename T>
struct [[gsl::Owner]] StatusOr {
  const T &value() const [[clang::lifetimebound]];
};

template<typename T>
struct [[gsl::Pointer]] Span {
    Span(const std::vector<T> &V): V(V){}
    const std::vector<T> &V;

    const int& getField() [[clang::lifetimebound]] { return field;}
    int field;
};

/////// From Owner<Pointer> ///////

// Pointer from Owner<Pointer>
std::string_view test6(StatusOr<std::string_view> aa) {
  return aa.value(); // Ok.
}

// Pointer<Owner<Pointer>>> from Owner<Pointer>
Span<int*> test9(std::vector<int*> V) {
  return V; // No error. !!FIXME: GH108463!!
}

/////// From Owner<Owner<Pointer>> ///////

// Pointer from Owner<Owner<Pointer>>
int* test8(StatusOr<StatusOr<int*>> aa) {
  return aa.value().value(); // Ok.
}
// Owner<Pointer> from Owner<Owner<Pointer>>
std::vector<int*> test5(StatusOr<std::vector<int*>> aa) {
  return aa.value(); // Ok.
}

// Pointer<Owner<Pointer>>> from Owner<Owner<Pointer>>
Span<int*> test8(StatusOr<std::vector<int*>> aa) {
  return aa.value(); // Error. Ok.
}

/////// From Owner<Owner> ///////

// Pointer<Owner>> from Owner<Owner>
Span<int> test9(StatusOr<std::vector<int>> aa) {
  return aa.value(); // Error. Ok.
}

/////// From Owner<Pointer<Owner>> ///////

// Pointer<Owner>> from Owner<Pointer<Owner>>
Span<int> test9(StatusOr<Span<int>> aa) {
  return aa.value(); // Ok.
}

// Lifetimebound and gsl::Pointer.
const int& test10(Span<int> a) {
  return a.getField();
}
```

https://github.com/llvm/llvm-project/pull/108344


More information about the cfe-commits mailing list