Author: Baranov Victor
Date: 2026-02-09T17:26:29+03:00
New Revision: f9528a3608a2aa4094c982dcb093bd191524c72d

URL: 
https://github.com/llvm/llvm-project/commit/f9528a3608a2aa4094c982dcb093bd191524c72d
DIFF: 
https://github.com/llvm/llvm-project/commit/f9528a3608a2aa4094c982dcb093bd191524c72d.diff

LOG: [LifetimeSafety] Add fixit verification with FileCheck (#180488)

Verify that produced messages/fixes are located in the right place.
With this patch, we can proceed to do
https://github.com/llvm/llvm-project/issues/180344

Added: 
    clang/test/Sema/warn-lifetime-safety-fixits.cpp

Modified: 
    

Removed: 
    


################################################################################
diff  --git a/clang/test/Sema/warn-lifetime-safety-fixits.cpp 
b/clang/test/Sema/warn-lifetime-safety-fixits.cpp
new file mode 100644
index 0000000000000..82b36caf8d141
--- /dev/null
+++ b/clang/test/Sema/warn-lifetime-safety-fixits.cpp
@@ -0,0 +1,140 @@
+// RUN: %clang_cc1 -fsyntax-only -std=c++17 -flifetime-safety-inference \
+// RUN:   -fexperimental-lifetime-safety-tu-analysis \
+// RUN:   -Wlifetime-safety-suggestions -Wno-dangling \
+// RUN:   -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
+
+struct View;
+
+struct [[gsl::Owner]] MyObj {
+  int id;
+  MyObj(int i) : id(i) {}
+  MyObj() {}
+  ~MyObj() {}
+  MyObj operator+(MyObj);
+  View getView() const [[clang::lifetimebound]];
+};
+
+struct [[gsl::Pointer()]] View {
+  View(const MyObj &);
+  View();
+  void use() const;
+};
+
+View return_view(View a) {
+  // CHECK: :[[@LINE-1]]:18: warning: parameter in intra-TU function should be 
marked {{\[\[}}clang::lifetimebound]] [-Wlifetime-safety-intra-tu-suggestions]
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:24-[[@LINE-2]]:24}:" 
{{\[\[}}clang::lifetimebound]]"
+  return a;
+}
+
+MyObj &return_multi(MyObj &a, bool c, MyObj &b) {
+  // CHECK-DAG: :[[@LINE-1]]:21: warning: parameter in intra-TU function 
should be marked
+  // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-2]]:29-[[@LINE-2]]:29}:" 
{{\[\[}}clang::lifetimebound]]"
+  // CHECK-DAG: :[[@LINE-3]]:39: warning: parameter in intra-TU function 
should be marked
+  // CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-4]]:47-[[@LINE-4]]:47}:" 
{{\[\[}}clang::lifetimebound]]"
+  if (c)
+    return a;
+  return b;
+}
+
+View return_partial(View a [[clang::lifetimebound]], bool c, View b) {
+  // CHECK: :[[@LINE-1]]:62: warning: parameter in intra-TU function should be 
marked
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:68-[[@LINE-2]]:68}:" 
{{\[\[}}clang::lifetimebound]]"
+  if (c)
+    return a;
+  return b;
+}
+
+View param_with_attr(View a [[maybe_unused]]) {
+  // CHECK: :[[@LINE-1]]:22: warning: parameter in intra-TU function should be 
marked
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:28-[[@LINE-2]]:28}:" 
{{\[\[}}clang::lifetimebound]]"
+  return a;
+}
+
+View param_default(View a = View()) {
+  // CHECK: :[[@LINE-1]]:20: warning: parameter in intra-TU function should be 
marked
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:35-[[@LINE-2]]:35}:" 
{{\[\[}}clang::lifetimebound]]"
+  return a;
+}
+
+// FIXME: Iterate over redecls and add [[clang::lifetimebound]]
+View multi_decl(View a);
+View multi_decl(View a);
+View multi_decl(View a) {
+  // CHECK: :[[@LINE-1]]:17: warning: parameter in intra-TU function should be 
marked
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:23-[[@LINE-2]]:23}:" 
{{\[\[}}clang::lifetimebound]]"
+  return a;
+}
+
+template <typename T>
+T *template_identity(T *a) {
+  // CHECK: :[[@LINE-1]]:22: warning: parameter in intra-TU function should be 
marked
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:26-[[@LINE-2]]:26}:" 
{{\[\[}}clang::lifetimebound]]"
+  return a;
+}
+
+MyObj *instantiate_template() {
+  MyObj local;
+  return template_identity(&local);
+}
+
+struct ViewMember {
+  ViewMember(int d) : data(d) {}
+  ~ViewMember() {}
+  MyObj data;
+
+  View get_view() {
+    // CHECK: :[[@LINE-1]]:18: warning: implicit this in intra-TU function 
should be marked
+    // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:18-[[@LINE-2]]:18}:" 
{{\[\[}}clang::lifetimebound]]"
+    return data;
+  }
+
+  View get_view_const() const {
+    // CHECK: :[[@LINE-1]]:30: warning: implicit this in intra-TU function 
should be marked
+    // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:30-[[@LINE-2]]:30}:" 
{{\[\[}}clang::lifetimebound]]"
+    return data;
+  }
+
+  const View get_view_const_noexcept() const noexcept {
+    // CHECK: :[[@LINE-1]]:54: warning: implicit this in intra-TU function 
should be marked
+    // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:54-[[@LINE-2]]:54}:" 
{{\[\[}}clang::lifetimebound]]"
+    return data;
+  }
+};
+
+struct Base {
+  Base() {}
+  virtual ~Base() {}
+  MyObj data;
+  virtual const MyObj &get_virtual() const {
+    // CHECK: :[[@LINE-1]]:43: warning: implicit this in intra-TU function 
should be marked
+    // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:43-[[@LINE-2]]:43}:" 
{{\[\[}}clang::lifetimebound]]"
+    return data;
+  }
+};
+
+struct Derived : Base {
+  const MyObj &get_virtual() const override {
+    // CHECK: :[[@LINE-1]]:35: warning: implicit this in intra-TU function 
should be marked
+    // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:35-[[@LINE-2]]:35}:" 
{{\[\[}}clang::lifetimebound]]"
+    return data;
+  }
+};
+
+struct DerivedFinal : Base {
+  const MyObj &get_virtual() const final {
+    // CHECK: :[[@LINE-1]]:35: warning: implicit this in intra-TU function 
should be marked
+    // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:35-[[@LINE-2]]:35}:" 
{{\[\[}}clang::lifetimebound]]"
+    return data;
+  }
+};
+
+struct OutOfLine {
+  OutOfLine() {}
+  ~OutOfLine() {}
+  const OutOfLine &get() const;
+};
+const OutOfLine &OutOfLine::get() const {
+  // CHECK: :[[@LINE-1]]:40: warning: implicit this in intra-TU function 
should be marked
+  // CHECK: fix-it:"{{.*}}":{[[@LINE-2]]:40-[[@LINE-2]]:40}:" 
{{\[\[}}clang::lifetimebound]]"
+  return *this;
+}


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

Reply via email to