[llvm] [ADT] Add `[[clang::lifetimebound]]` annotations to Twine.h (PR #210474)

via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 18 07:39:02 PDT 2026


================
@@ -174,15 +174,17 @@ class Twine {
   }
 
   /// Construct a binary twine.
-  explicit Twine(const Twine &LHS, const Twine &RHS)
+  explicit Twine(const Twine &LHS LLVM_LIFETIME_BOUND,
+                 const Twine &RHS LLVM_LIFETIME_BOUND)
       : LHSKind(TwineKind), RHSKind(TwineKind) {
     this->LHS.twine = &LHS;
     this->RHS.twine = &RHS;
     assert(isValid() && "Invalid twine!");
   }
 
   /// Construct a twine from explicit values.
-  explicit Twine(Child LHS, NodeKind LHSKind, Child RHS, NodeKind RHSKind)
+  explicit Twine(Child LHS LLVM_LIFETIME_BOUND, NodeKind LHSKind,
----------------
isuckatcs wrote:

Hmm, here `Child` is not necessarily lifetime bound I think. It has a bunch of non-pointer values as well, in which case LHS and RHS doesn't have to outlive the current `Twine`.

```c++
union Child {
    ...
    char character;
    unsigned int decUI;
    int decI;
    unsigned long decUL;
    long decL;
    unsigned long long decULL;
    long long decLL;
    uint64_t uHex;
  };
```

I expect to see false positives from this annotation. I guess it would be the best to separate these cases somehow, but it is way beyond our GSoC scope. Maybe we can make some magic with `std::enable_if` and type traits to have an annotated and a non-annotated overload of this constructor.

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


More information about the llvm-commits mailing list