Skip to content

Commit

Permalink
Hack together -Wlifetime-extension. Not ready for prime time.
Browse files Browse the repository at this point in the history
Still gives false positives on any use of a `std::initializer_list` variable
(because it extends the lifetime of its backing array).

No attempt to update the unit tests.
  • Loading branch information
Quuxplusone committed Mar 4, 2020
1 parent 1044ee8 commit 99ca9fc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticGroups.td
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ def FormatZeroLength : DiagGroup<"format-zero-length">;

def InvalidIOSDeploymentTarget : DiagGroup<"invalid-ios-deployment-target">;

def LifetimeExtension : DiagGroup<"lifetime-extension">;

def CXX17CompatMangling : DiagGroup<"c++17-compat-mangling">;
def : DiagGroup<"c++1z-compat-mangling", [CXX17CompatMangling]>;
// Name of this warning in GCC.
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -10613,4 +10613,8 @@ def warn_sycl_kernel_return_type : Warning<
"function template with 'sycl_kernel' attribute must have a 'void' return type">,
InGroup<IgnoredAttributes>;

def warn_lifetime_extension: Warning<
"binding temporary of type %0 to a reference relies on lifetime extension">,
InGroup<LifetimeExtension>;

} // end of sema component.
3 changes: 3 additions & 0 deletions clang/lib/Sema/SemaAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ static void addGslOwnerPointerAttributeIfNotExisting(ASTContext &Context,

void Sema::inferGslPointerAttribute(NamedDecl *ND,
CXXRecordDecl *UnderlyingRecord) {
return;
// don't do any of this

if (!UnderlyingRecord)
return;

Expand Down
7 changes: 7 additions & 0 deletions clang/lib/Sema/SemaInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7356,6 +7356,13 @@ void Sema::checkInitializerLifetime(const InitializedEntity &Entity,

auto *MTE = dyn_cast<MaterializeTemporaryExpr>(L);

if (LK == LK_Extended && MTE != nullptr) {
VarDecl *VD = dyn_cast<VarDecl>(ExtendingEntity->getDecl());
if (!(VD && VD->getNameAsString().substr(0,7) == "__range")) {
this->Diag(MTE->getSourceRange().getBegin(), diag::warn_lifetime_extension) << L->getType() << MTE->getSourceRange();
}
}

bool IsGslPtrInitWithGslTempOwner = false;
bool IsLocalGslOwner = false;
if (pathOnlyInitializesGslPointer(Path)) {
Expand Down

0 comments on commit 99ca9fc

Please sign in to comment.