Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -1463,9 +1463,13 @@ def ConstInit : InheritableAttr {

def Constructor : InheritableAttr {
let Spellings = [GCC<"constructor">];
let Args = [DefaultIntArgument<"Priority", 65535>];
let Args = [ExprArgument<"Priority", 1>];
let Subjects = SubjectList<[Function]>;
let TemplateDependent = 1;
let Documentation = [CtorDtorDocs];
let AdditionalMembers = [{
static constexpr unsigned int DefaultPriority = 65535;
}];
}

def CPUSpecific : InheritableAttr {
Expand Down Expand Up @@ -1797,9 +1801,13 @@ def Deprecated : InheritableAttr {

def Destructor : InheritableAttr {
let Spellings = [GCC<"destructor">];
let Args = [DefaultIntArgument<"Priority", 65535>];
let Args = [ExprArgument<"Priority", 1>];
let Subjects = SubjectList<[Function]>;
let TemplateDependent = 1;
let Documentation = [CtorDtorDocs];
let AdditionalMembers = [{
static constexpr unsigned int DefaultPriority = 65535;
}];
}

def EmptyBases : InheritableAttr, TargetSpecificAttr<TargetMicrosoftRecordLayout> {
Expand Down
14 changes: 12 additions & 2 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6324,10 +6324,20 @@ void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD,

SetLLVMFunctionAttributesForDefinition(D, Fn);

auto getPriority = [this](auto *Attr) -> int {
int priority = Attr->DefaultPriority;
Expr *E = Attr->getPriority();
if (E) {
if (auto CE = E->getIntegerConstantExpr(this->getContext()))
priority = CE->getExtValue();
}
return priority;
};

if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>())
AddGlobalCtor(Fn, CA->getPriority());
AddGlobalCtor(Fn, getPriority(CA));
if (const DestructorAttr *DA = D->getAttr<DestructorAttr>())
AddGlobalDtor(Fn, DA->getPriority(), true);
AddGlobalDtor(Fn, getPriority(DA), true);
if (getLangOpts().OpenMP && D->hasAttr<OMPDeclareTargetDeclAttr>())
getOpenMPRuntime().emitDeclareTargetFunction(D, GV);
}
Expand Down
39 changes: 27 additions & 12 deletions clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2152,29 +2152,44 @@ static void handleUnusedAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
D->addAttr(::new (S.Context) UnusedAttr(S.Context, AL));
}

static std::optional<Expr *> sharedGetConstructorDestructorAttrExpr(Sema &S, const ParsedAttr &AL) {
Expr *E = nullptr;
if (AL.getNumArgs() == 1) {
E = AL.getArgAsExpr(0);
if (E->isValueDependent()) {
if (!E->isTypeDependent() && !E->getType()->isIntegerType()) {
S.Diag(AL.getLoc(), diag::err_attribute_argument_type)
<< AL << AANT_ArgumentIntegerConstant << E->getSourceRange();
return std::nullopt;
}
} else {
uint32_t priority;
if (!S.checkUInt32Argument(AL, AL.getArgAsExpr(0), priority)) {
return std::nullopt;
}
}
}
return E;
}

static void handleConstructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
uint32_t priority = ConstructorAttr::DefaultPriority;
if (S.getLangOpts().HLSL && AL.getNumArgs()) {
S.Diag(AL.getLoc(), diag::err_hlsl_init_priority_unsupported);
return;
}
if (AL.getNumArgs() &&
!S.checkUInt32Argument(AL, AL.getArgAsExpr(0), priority))
auto E = sharedGetConstructorDestructorAttrExpr(S, AL);
if (!E.has_value())
return;
S.Diag(D->getLocation(), diag::warn_global_constructor)
<< D->getSourceRange();

D->addAttr(::new (S.Context) ConstructorAttr(S.Context, AL, priority));
S.Diag(D->getLocation(), diag::warn_global_constructor) << D->getSourceRange();
D->addAttr(::new (S.Context) ConstructorAttr(S.Context, AL, E.value()));
}

static void handleDestructorAttr(Sema &S, Decl *D, const ParsedAttr &AL) {
uint32_t priority = DestructorAttr::DefaultPriority;
if (AL.getNumArgs() &&
!S.checkUInt32Argument(AL, AL.getArgAsExpr(0), priority))
auto E = sharedGetConstructorDestructorAttrExpr(S, AL);
if (!E.has_value())
return;
S.Diag(D->getLocation(), diag::warn_global_destructor) << D->getSourceRange();

D->addAttr(::new (S.Context) DestructorAttr(S.Context, AL, priority));
D->addAttr(::new (S.Context) DestructorAttr(S.Context, AL, E.value()));
}

template <typename AttrTy>
Expand Down
3 changes: 2 additions & 1 deletion clang/test/AST/ast-dump-attr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ __attribute__((pointer_with_type_tag(unsigned1,1,2)));

void TestInt(void) __attribute__((constructor(123)));
// CHECK: FunctionDecl{{.*}}TestInt
// CHECK-NEXT: ConstructorAttr{{.*}} 123
// CHECK-NEXT: ConstructorAttr
// CHECK-NEXT: IntegerLiteral{{.*}} 123

static int TestString __attribute__((alias("alias1")));
// CHECK: VarDecl{{.*}}TestString
Expand Down
15 changes: 15 additions & 0 deletions clang/test/CodeGenCXX/constructor-attr.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// RUN: %clang_cc1 -triple %itanium_abi_triple -emit-llvm -o - %s | FileCheck %s

// CHECK: @llvm.global_ctors
// CHECK-SAME: i32 65535, ptr @_ZN3Foo3fooEv
// CHECK-SAME: i32 101, ptr @_Z22template_dependent_cxxILi101EEvv
// CHECK-SAME: i32 102, ptr @_Z22template_dependent_gnuILi102EEvv
// CHECK-SAME: i32 104, ptr @_Z23template_dependent_nttpIiLi104EEvv

// PR6521
void bar();
Expand All @@ -10,3 +14,14 @@ struct Foo {
bar();
}
};

template <int P>
[[gnu::constructor(P)]] void template_dependent_cxx() {}
template <int P>
__attribute__((constructor(P))) void template_dependent_gnu() {}
template <typename T, int P = sizeof(T) * 26>
[[gnu::constructor(P)]] void template_dependent_nttp() {}

template void template_dependent_cxx<101>();
template void template_dependent_gnu<102>();
template void template_dependent_nttp<int>();
23 changes: 23 additions & 0 deletions clang/test/CodeGenCXX/destructor-attr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %clang_cc1 -triple %itanium_abi_triple -emit-llvm -o - %s | FileCheck %s

// CHECK: @llvm.global_dtors
// CHECK-SAME: i32 65535, ptr @_ZN3Foo3fooEv
// CHECK-SAME: i32 101, ptr @_Z22template_dependent_cxxILi101EEvv
// CHECK-SAME: i32 104, ptr @_Z23template_dependent_nttpIiLi104EEvv

// PR6521
void bar();
struct Foo {
// CHECK-LABEL: define linkonce_odr {{.*}}void @_ZN3Foo3fooEv
static void foo() __attribute__((destructor)) {
bar();
}
};

template <int P>
[[gnu::destructor(P)]] void template_dependent_cxx() {}
template <typename T, int P = sizeof(T) * 26>
[[gnu::destructor(P)]] void template_dependent_nttp() {}

template void template_dependent_cxx<101>();
template void template_dependent_nttp<int>();
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ int f(void) __attribute__((constructor(1)));
int f(void) __attribute__((constructor(1,2))); // expected-error {{'constructor' attribute takes no more than 1 argument}}
int f(void) __attribute__((constructor(1.0))); // expected-error {{'constructor' attribute requires an integer constant}}
int f(void) __attribute__((constructor(0x100000000))); // expected-error {{integer constant expression evaluates to value 4294967296 that cannot be represented in a 32-bit unsigned integer type}}
template <int *I> [[gnu::constructor(I)]] void f(); // expected-error {{'gnu::constructor' attribute requires an integer constant}}

int x __attribute__((destructor)); // expected-warning {{'destructor' attribute only applies to functions}}
int y __attribute__((destructor)); // expected-warning {{'destructor' attribute only applies to functions}}
int f(void) __attribute__((destructor));
int f(void) __attribute__((destructor(1)));
int f(void) __attribute__((destructor(1,2))); // expected-error {{'destructor' attribute takes no more than 1 argument}}
int f(void) __attribute__((destructor(1.0))); // expected-error {{'destructor' attribute requires an integer constant}}
template <int *I> [[gnu::destructor(I)]] void f(); // expected-error {{'gnu::destructor' attribute requires an integer constant}}

void knr() __attribute__((constructor));
Loading