Skip to content

Commit 4862feb

Browse files
authored
[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.
1 parent 3e6f350 commit 4862feb

File tree

6 files changed

+12
-2
lines changed

6 files changed

+12
-2
lines changed

clang/lib/Sema/CheckExprLifetime.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ static bool isNormalAssignmentOperator(const FunctionDecl *FD) {
498498
return false;
499499
}
500500

501-
static bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD) {
501+
bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD) {
502502
const TypeSourceInfo *TSI = FD->getTypeSourceInfo();
503503
if (!TSI)
504504
return false;

clang/lib/Sema/CheckExprLifetime.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ void checkCaptureByLifetime(Sema &SemaRef, const CapturingEntity &Entity,
5757
void checkExprLifetimeMustTailArg(Sema &SemaRef,
5858
const InitializedEntity &Entity, Expr *Init);
5959

60+
bool implicitObjectParamIsLifetimeBound(const FunctionDecl *FD);
61+
6062
} // namespace clang::sema
6163

6264
#endif // LLVM_CLANG_SEMA_CHECK_EXPR_LIFETIME_H

clang/lib/Sema/SemaAPINotes.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#include "CheckExprLifetime.h"
1314
#include "TypeLocBuilder.h"
1415
#include "clang/APINotes/APINotesReader.h"
1516
#include "clang/AST/Decl.h"
@@ -568,7 +569,8 @@ static void ProcessAPINotes(Sema &S, FunctionOrMethod AnyFunc,
568569
static void ProcessAPINotes(Sema &S, CXXMethodDecl *Method,
569570
const api_notes::CXXMethodInfo &Info,
570571
VersionedInfoMetadata Metadata) {
571-
if (Info.This && Info.This->isLifetimebound()) {
572+
if (Info.This && Info.This->isLifetimebound() &&
573+
!sema::implicitObjectParamIsLifetimeBound(Method)) {
572574
auto MethodType = Method->getType();
573575
auto *attr = ::new (S.Context)
574576
LifetimeBoundAttr(S.Context, getPlaceholderAttrInfo());

clang/test/APINotes/Inputs/Headers/Lifetimebound.apinotes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Tags:
1212
Parameters:
1313
- Position: -1
1414
Lifetimebound: true
15+
- Name: annotateThis2
16+
Parameters:
17+
- Position: -1
18+
Lifetimebound: true
1519
- Name: methodToAnnotate
1620
Parameters:
1721
- Position: 0

clang/test/APINotes/Inputs/Headers/Lifetimebound.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ int *funcToAnnotate(int *p);
33
struct MyClass {
44
MyClass(int*);
55
int *annotateThis();
6+
int *annotateThis2() [[clang::lifetimebound]];
67
int *methodToAnnotate(int *p);
78
};

clang/test/APINotes/lifetimebound.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
// CHECK-METHOD-NEXT: LifetimeBoundAttr
1515

1616
// CHECK-METHOD-THIS: CXXMethodDecl {{.+}} annotateThis 'int *() {{\[\[}}clang::lifetimebound{{\]\]}}'
17+
// CHECK-METHOD-THIS: CXXMethodDecl {{.+}} annotateThis2 'int *() {{\[\[}}clang::lifetimebound{{\]\]}}'

0 commit comments

Comments
 (0)