-
Notifications
You must be signed in to change notification settings - Fork 15.1k
[LifetimeSafety] Implement support for lifetimebound attribute #158489
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
clang/include/clang/Analysis/Analyses/LifetimeAnnotations.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| //===- LifetimeAnnotations.h - -*--------------- C++--------------------*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // Helper functions to inspect and infer lifetime annotations. | ||
| //===----------------------------------------------------------------------===// | ||
| #ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMEANNOTATIONS_H | ||
| #define LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMEANNOTATIONS_H | ||
|
|
||
| #include "clang/AST/DeclCXX.h" | ||
|
|
||
| namespace clang { | ||
| namespace lifetimes { | ||
|
|
||
| /// Returns the most recent declaration of the method to ensure all | ||
| /// lifetime-bound attributes from redeclarations are considered. | ||
| const FunctionDecl *getDeclWithMergedLifetimeBoundAttrs(const FunctionDecl *FD); | ||
|
|
||
| /// Returns the most recent declaration of the method to ensure all | ||
| /// lifetime-bound attributes from redeclarations are considered. | ||
| const CXXMethodDecl * | ||
| getDeclWithMergedLifetimeBoundAttrs(const CXXMethodDecl *CMD); | ||
|
|
||
| // Return true if this is an "normal" assignment operator. | ||
| // We assume that a normal assignment operator always returns *this, that is, | ||
| // an lvalue reference that is the same type as the implicit object parameter | ||
| // (or the LHS for a non-member operator==). | ||
| bool isNormalAssignmentOperator(const FunctionDecl *FD); | ||
|
|
||
| /// Returns true if this is an assignment operator where the parameter | ||
| /// has the lifetimebound attribute. | ||
| bool isAssignmentOperatorLifetimeBound(const CXXMethodDecl *CMD); | ||
|
|
||
| /// Returns true if the implicit object parameter (this) should be considered | ||
| /// lifetimebound, either due to an explicit lifetimebound attribute on the | ||
| /// method or because it's a normal assignment operator. | ||
| bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD); | ||
| } // namespace lifetimes | ||
| } // namespace clang | ||
|
|
||
| #endif // LLVM_CLANG_ANALYSIS_ANALYSES_LIFETIMEANNOTATIONS_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| //===- LifetimeAnnotations.cpp - -*--------------- C++------------------*-===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| #include "clang/Analysis/Analyses/LifetimeAnnotations.h" | ||
| #include "clang/AST/ASTContext.h" | ||
| #include "clang/AST/Attr.h" | ||
| #include "clang/AST/Decl.h" | ||
| #include "clang/AST/DeclCXX.h" | ||
| #include "clang/AST/Type.h" | ||
| #include "clang/AST/TypeLoc.h" | ||
|
|
||
| namespace clang { | ||
| namespace lifetimes { | ||
|
|
||
| const FunctionDecl * | ||
| getDeclWithMergedLifetimeBoundAttrs(const FunctionDecl *FD) { | ||
| return FD != nullptr ? FD->getMostRecentDecl() : nullptr; | ||
| } | ||
|
|
||
| const CXXMethodDecl * | ||
| getDeclWithMergedLifetimeBoundAttrs(const CXXMethodDecl *CMD) { | ||
| const FunctionDecl *FD = CMD; | ||
| return cast_if_present<CXXMethodDecl>( | ||
| getDeclWithMergedLifetimeBoundAttrs(FD)); | ||
| } | ||
|
|
||
| bool isNormalAssignmentOperator(const FunctionDecl *FD) { | ||
| OverloadedOperatorKind OO = FD->getDeclName().getCXXOverloadedOperator(); | ||
| bool IsAssignment = OO == OO_Equal || isCompoundAssignmentOperator(OO); | ||
| if (!IsAssignment) | ||
| return false; | ||
| QualType RetT = FD->getReturnType(); | ||
| if (!RetT->isLValueReferenceType()) | ||
| return false; | ||
| ASTContext &Ctx = FD->getASTContext(); | ||
| QualType LHST; | ||
| auto *MD = dyn_cast<CXXMethodDecl>(FD); | ||
| if (MD && MD->isCXXInstanceMember()) | ||
| LHST = Ctx.getLValueReferenceType(MD->getFunctionObjectParameterType()); | ||
| else | ||
| LHST = FD->getParamDecl(0)->getType(); | ||
| return Ctx.hasSameType(RetT, LHST); | ||
| } | ||
|
|
||
| bool isAssignmentOperatorLifetimeBound(const CXXMethodDecl *CMD) { | ||
| CMD = getDeclWithMergedLifetimeBoundAttrs(CMD); | ||
| return CMD && isNormalAssignmentOperator(CMD) && CMD->param_size() == 1 && | ||
| CMD->getParamDecl(0)->hasAttr<clang::LifetimeBoundAttr>(); | ||
| } | ||
|
|
||
| bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD) { | ||
| FD = getDeclWithMergedLifetimeBoundAttrs(FD); | ||
| const TypeSourceInfo *TSI = FD->getTypeSourceInfo(); | ||
| if (!TSI) | ||
| return false; | ||
| // Don't declare this variable in the second operand of the for-statement; | ||
| // GCC miscompiles that by ending its lifetime before evaluating the | ||
| // third operand. See gcc.gnu.org/PR86769. | ||
| AttributedTypeLoc ATL; | ||
| for (TypeLoc TL = TSI->getTypeLoc(); | ||
| (ATL = TL.getAsAdjusted<AttributedTypeLoc>()); | ||
| TL = ATL.getModifiedLoc()) { | ||
| if (ATL.getAttrAs<clang::LifetimeBoundAttr>()) | ||
| return true; | ||
| } | ||
|
|
||
| return isNormalAssignmentOperator(FD); | ||
| } | ||
|
|
||
| } // namespace lifetimes | ||
| } // namespace clang |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.