Files
TheRock/compiler/amd-llvm/clang/test/Sema/warn-lifetime-safety-lifetimebound.cpp
T
derek.holloway 7502bf64a4
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
Flatten subprojects into a single base monorepo
2026-09-02 16:34:47 -07:00

169 lines
5.2 KiB
C++

// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety-lifetimebound-violation -Wno-dangling -verify %s
#include "Inputs/lifetime-analysis.h"
struct [[gsl::Owner]] MyObj {
int id;
~MyObj() {} // Non-trivial destructor
};
struct [[gsl::Pointer()]] View {
View(const MyObj &); // Borrows from MyObj
View();
void use() const;
};
bool cond();
View not_lb(const MyObj &obj);
View lb(const MyObj &obj [[clang::lifetimebound]]);
View return_through_unannotated_passthrough(
const MyObj &obj [[clang::lifetimebound]]) { // expected-warning {{could not verify that the return value can be lifetime bound to 'obj'}}
return not_lb(obj);
}
View return_through_lifetimebound_passthrough(
const MyObj &obj [[clang::lifetimebound]]) {
return lb(obj);
}
View lose_lb(const MyObj &obj [[clang::lifetimebound]]) { // expected-warning {{could not verify that the return value can be lifetime bound to 'obj'}}
return not_lb(obj);
}
View return_through_alias(const MyObj &obj [[clang::lifetimebound]]) {
const MyObj &alias = obj;
return alias;
}
View return_alias_through_unannotated_passthrough(
const MyObj &obj [[clang::lifetimebound]]) { // expected-warning {{could not verify that the return value can be lifetime bound to 'obj'}}
const MyObj &alias = obj;
return not_lb(alias);
}
View global_view;
View assign_param_to_global(
const MyObj &obj [[clang::lifetimebound]]) { // expected-warning {{could not verify that the return value can be lifetime bound to 'obj'}}
global_view = obj; // Wrong kind of escape.
return View(); // Unrelated view.
}
struct EnclosingState {
View member;
View assign_param_to_field(
const MyObj &obj [[clang::lifetimebound]]) { // expected-warning {{could not verify that the return value can be lifetime bound to 'obj'}}
member = obj; // Wrong kind of escape.
return View(); // Unrelated view.
}
};
View not_lb_view(View v);
View lb_view(View v [[clang::lifetimebound]]);
View return_through_two_lifetimebound_calls(
const MyObj &obj [[clang::lifetimebound]]) {
return lb_view(lb(obj));
}
View return_through_nested_broken_chain(
const MyObj &obj [[clang::lifetimebound]]) { // expected-warning {{could not verify that the return value can be lifetime bound to 'obj'}}
return not_lb_view(lb_view(View(obj)));
}
View return_constructed_view_through_unannotated_forwarder(
const MyObj &obj [[clang::lifetimebound]]) { // expected-warning {{could not verify that the return value can be lifetime bound to 'obj'}}
return not_lb_view(View(obj));
}
View return_constructed_view_through_lifetimebound_forwarder(
const MyObj &obj [[clang::lifetimebound]]) {
return lb_view(View(obj));
}
View verify_each_annotated_param_independently(
const MyObj &a [[clang::lifetimebound]],
const MyObj &b [[clang::lifetimebound]], // expected-warning {{could not verify that the return value can be lifetime bound to 'b'}}
const MyObj &c [[clang::lifetimebound]]) { // expected-warning {{could not verify that the return value can be lifetime bound to 'c'}}
return cond() ? a : not_lb(b);
}
View unnamed_lifetimebound_param(
[[clang::lifetimebound]] const MyObj &) { // expected-warning {{could not verify that the return value can be lifetime bound to an unnamed parameter}}
return View();
}
View annotated_decl_but_not_def_not_returned(const MyObj &obj [[clang::lifetimebound]]); // expected-warning {{could not verify that the return value can be lifetime bound to 'obj'}}
View annotated_decl_but_not_def_not_returned(const MyObj &obj) {
return not_lb(obj);
}
struct BadThisReturn {
MyObj data;
View get() const [[clang::lifetimebound]] { // expected-warning {{could not verify that the return value can be lifetime bound to the implicit this parameter}}
return not_lb(data);
}
};
struct GoodThisReturn {
MyObj data;
View get() const [[clang::lifetimebound]] {
return data;
}
};
struct RedeclaredThis {
MyObj data;
View get() const [[clang::lifetimebound]]; // expected-warning {{could not verify that the return value can be lifetime bound to the implicit this parameter}}
};
View RedeclaredThis::get() const {
return not_lb(data);
}
struct ThisAndParam {
MyObj data;
View get(const MyObj &obj [[clang::lifetimebound]]) const [[clang::lifetimebound]] { // expected-warning {{could not verify that the return value can be lifetime bound to the implicit this parameter}}
return lb(obj);
}
};
struct ThisAndMixedParams {
MyObj data;
View get(
const MyObj &a [[clang::lifetimebound]],
const MyObj &b,
const MyObj &c [[clang::lifetimebound]]) const // expected-warning {{could not verify that the return value can be lifetime bound to 'c'}}
[[clang::lifetimebound]] { // expected-warning {{could not verify that the return value can be lifetime bound to the implicit this parameter}}
return cond() ? lb(a) : not_lb(b);
}
};
struct AssignmentCorr {
AssignmentCorr& operator=(const AssignmentCorr& other) {
return *this;
}
};
struct AssignementIncorr {
AssignementIncorr& operator=(const AssignementIncorr& other) {
AssignementIncorr tmp;
return tmp;
}
};
void implicit_lifetimebound_in_nested_std_namespace() {
(void)std::basic_string_view<char>("hello");
}