[clang][APINotes] Do not add duplicate lifetimebound annotations (#117194)

In case a method already is lifetimebound annotated we should not add a
second annotation to the type.
This commit is contained in:
Gábor Horváth
2024-11-21 23:25:37 +00:00
committed by GitHub
parent 3e6f3508ad
commit 4862febdce
6 changed files with 12 additions and 2 deletions

View File

@@ -498,7 +498,7 @@ static bool isNormalAssignmentOperator(const FunctionDecl *FD) {
return false;
}
static bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD) {
bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD) {
const TypeSourceInfo *TSI = FD->getTypeSourceInfo();
if (!TSI)
return false;

View File

@@ -57,6 +57,8 @@ void checkCaptureByLifetime(Sema &SemaRef, const CapturingEntity &Entity,
void checkExprLifetimeMustTailArg(Sema &SemaRef,
const InitializedEntity &Entity, Expr *Init);
bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD);
} // namespace clang::sema
#endif // LLVM_CLANG_SEMA_CHECK_EXPR_LIFETIME_H

View File

@@ -10,6 +10,7 @@
//
//===----------------------------------------------------------------------===//
#include "CheckExprLifetime.h"
#include "TypeLocBuilder.h"
#include "clang/APINotes/APINotesReader.h"
#include "clang/AST/Decl.h"
@@ -568,7 +569,8 @@ static void ProcessAPINotes(Sema &S, FunctionOrMethod AnyFunc,
static void ProcessAPINotes(Sema &S, CXXMethodDecl *Method,
const api_notes::CXXMethodInfo &Info,
VersionedInfoMetadata Metadata) {
if (Info.This && Info.This->isLifetimebound()) {
if (Info.This && Info.This->isLifetimebound() &&
!sema::implicitObjectParamIsLifetimeBound(Method)) {
auto MethodType = Method->getType();
auto *attr = ::new (S.Context)
LifetimeBoundAttr(S.Context, getPlaceholderAttrInfo());

View File

@@ -12,6 +12,10 @@ Tags:
Parameters:
- Position: -1
Lifetimebound: true
- Name: annotateThis2
Parameters:
- Position: -1
Lifetimebound: true
- Name: methodToAnnotate
Parameters:
- Position: 0

View File

@@ -3,5 +3,6 @@ int *funcToAnnotate(int *p);
struct MyClass {
MyClass(int*);
int *annotateThis();
int *annotateThis2() [[clang::lifetimebound]];
int *methodToAnnotate(int *p);
};

View File

@@ -14,3 +14,4 @@
// CHECK-METHOD-NEXT: LifetimeBoundAttr
// CHECK-METHOD-THIS: CXXMethodDecl {{.+}} annotateThis 'int *() {{\[\[}}clang::lifetimebound{{\]\]}}'
// CHECK-METHOD-THIS: CXXMethodDecl {{.+}} annotateThis2 'int *() {{\[\[}}clang::lifetimebound{{\]\]}}'