[clang] [llvm] [LifetimeSafety] Add lifetimebound inference for std::make_unique (PR #191632)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 15 11:45:58 PDT 2026
github-actions[bot] wrote:
<!--LLVM CODE FORMAT COMMENT: {clang-format}-->
:warning: C/C++ code formatter, clang-format found issues in your code. :warning:
<details>
<summary>
You can test this locally with the following command:
</summary>
``````````bash
git-clang-format --diff origin/main HEAD --extensions h,cpp -- clang/lib/Analysis/LifetimeSafety/FactsGenerator.cpp clang/lib/Analysis/LifetimeSafety/Origins.cpp clang/lib/Sema/SemaAttr.cpp clang/lib/Sema/SemaTemplateInstantiateDecl.cpp clang/test/Sema/Inputs/lifetime-analysis.h clang/test/Sema/warn-lifetime-analysis-nocfg.cpp clang/test/Sema/warn-lifetime-safety-dangling-field.cpp clang/test/Sema/warn-lifetime-safety-suggestions.cpp clang/test/Sema/warn-lifetime-safety.cpp --diff_from_common_commit
``````````
:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:
</details>
<details>
<summary>
View the diff from clang-format here.
</summary>
``````````diff
diff --git a/clang/lib/Sema/SemaAttr.cpp b/clang/lib/Sema/SemaAttr.cpp
index c103bf0af..e6822411a 100644
--- a/clang/lib/Sema/SemaAttr.cpp
+++ b/clang/lib/Sema/SemaAttr.cpp
@@ -222,7 +222,8 @@ void Sema::inferGslOwnerPointerAttribute(CXXRecordDecl *Record) {
}
static const CXXNewExpr *findCXXNewExpr(const Stmt *S) {
- if (!S) return nullptr;
+ if (!S)
+ return nullptr;
if (const auto *E = dyn_cast<CXXNewExpr>(S))
return E;
for (const Stmt *Child : S->children())
@@ -265,8 +266,7 @@ void Sema::inferLifetimeBoundAttribute(FunctionDecl *FD) {
// Handle std::make_unique to propagate lifetimebound attributes from the
// constructed type's constructor to make_unique's parameters by looking
// into its body.
- if (FD->getDeclName().isIdentifier() &&
- FD->getName() == "make_unique") {
+ if (FD->getDeclName().isIdentifier() && FD->getName() == "make_unique") {
const FunctionDecl *BodyDecl = nullptr;
if (FD->getBody(BodyDecl))
if (const CXXNewExpr *NewExpr = findCXXNewExpr(BodyDecl->getBody()))
@@ -275,8 +275,8 @@ void Sema::inferLifetimeBoundAttribute(FunctionDecl *FD) {
for (unsigned i = 0; i < Ctor->getNumParams(); ++i)
if (Ctor->getParamDecl(i)->hasAttr<LifetimeBoundAttr>() &&
i < FD->getNumParams())
- FD->getParamDecl(i)->addAttr(
- LifetimeBoundAttr::CreateImplicit(Context, FD->getLocation()));
+ FD->getParamDecl(i)->addAttr(LifetimeBoundAttr::CreateImplicit(
+ Context, FD->getLocation()));
return;
}
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index a633f8af1..81f730cec 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -5969,7 +5969,6 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
// context seems wrong. Investigate more.
ActOnFinishFunctionBody(Function, Body.get(), /*IsInstantiation=*/true);
-
inferLifetimeBoundAttribute(Function);
checkReferenceToTULocalFromOtherTU(Function, PointOfInstantiation);
``````````
</details>
https://github.com/llvm/llvm-project/pull/191632
More information about the cfe-commits
mailing list