|
| 1 | +//===- LifetimeAnnotations.cpp - -*--------------- C++------------------*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +#include "clang/Analysis/Analyses/LifetimeAnnotations.h" |
| 9 | +#include "clang/AST/ASTContext.h" |
| 10 | +#include "clang/AST/Attr.h" |
| 11 | +#include "clang/AST/Decl.h" |
| 12 | +#include "clang/AST/DeclCXX.h" |
| 13 | +#include "clang/AST/Type.h" |
| 14 | +#include "clang/AST/TypeLoc.h" |
| 15 | + |
| 16 | +namespace clang { |
| 17 | +namespace lifetimes { |
| 18 | + |
| 19 | +const FunctionDecl * |
| 20 | +getDeclWithMergedLifetimeBoundAttrs(const FunctionDecl *FD) { |
| 21 | + return FD != nullptr ? FD->getMostRecentDecl() : nullptr; |
| 22 | +} |
| 23 | + |
| 24 | +const CXXMethodDecl * |
| 25 | +getDeclWithMergedLifetimeBoundAttrs(const CXXMethodDecl *CMD) { |
| 26 | + const FunctionDecl *FD = CMD; |
| 27 | + return cast_if_present<CXXMethodDecl>( |
| 28 | + getDeclWithMergedLifetimeBoundAttrs(FD)); |
| 29 | +} |
| 30 | + |
| 31 | +bool isNormalAssignmentOperator(const FunctionDecl *FD) { |
| 32 | + OverloadedOperatorKind OO = FD->getDeclName().getCXXOverloadedOperator(); |
| 33 | + bool IsAssignment = OO == OO_Equal || isCompoundAssignmentOperator(OO); |
| 34 | + if (!IsAssignment) |
| 35 | + return false; |
| 36 | + QualType RetT = FD->getReturnType(); |
| 37 | + if (!RetT->isLValueReferenceType()) |
| 38 | + return false; |
| 39 | + ASTContext &Ctx = FD->getASTContext(); |
| 40 | + QualType LHST; |
| 41 | + auto *MD = dyn_cast<CXXMethodDecl>(FD); |
| 42 | + if (MD && MD->isCXXInstanceMember()) |
| 43 | + LHST = Ctx.getLValueReferenceType(MD->getFunctionObjectParameterType()); |
| 44 | + else |
| 45 | + LHST = FD->getParamDecl(0)->getType(); |
| 46 | + return Ctx.hasSameType(RetT, LHST); |
| 47 | +} |
| 48 | + |
| 49 | +bool isAssignmentOperatorLifetimeBound(const CXXMethodDecl *CMD) { |
| 50 | + CMD = getDeclWithMergedLifetimeBoundAttrs(CMD); |
| 51 | + return CMD && isNormalAssignmentOperator(CMD) && CMD->param_size() == 1 && |
| 52 | + CMD->getParamDecl(0)->hasAttr<clang::LifetimeBoundAttr>(); |
| 53 | +} |
| 54 | + |
| 55 | +bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD) { |
| 56 | + FD = getDeclWithMergedLifetimeBoundAttrs(FD); |
| 57 | + const TypeSourceInfo *TSI = FD->getTypeSourceInfo(); |
| 58 | + if (!TSI) |
| 59 | + return false; |
| 60 | + // Don't declare this variable in the second operand of the for-statement; |
| 61 | + // GCC miscompiles that by ending its lifetime before evaluating the |
| 62 | + // third operand. See gcc.gnu.org/PR86769. |
| 63 | + AttributedTypeLoc ATL; |
| 64 | + for (TypeLoc TL = TSI->getTypeLoc(); |
| 65 | + (ATL = TL.getAsAdjusted<AttributedTypeLoc>()); |
| 66 | + TL = ATL.getModifiedLoc()) { |
| 67 | + if (ATL.getAttrAs<clang::LifetimeBoundAttr>()) |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + return isNormalAssignmentOperator(FD); |
| 72 | +} |
| 73 | + |
| 74 | +} // namespace lifetimes |
| 75 | +} // namespace clang |
0 commit comments