Author: Muhammad Bassiouni Date: 2026-11-14T17:39:58+02:00 New Revision: c0005dfe81fedaf852727b81a07d728f94ecfa2d
URL: https://github.com/llvm/llvm-project/commit/c0005dfe81fedaf852727b81a07d728f94ecfa2d DIFF: https://github.com/llvm/llvm-project/commit/c0005dfe81fedaf852727b81a07d728f94ecfa2d.diff LOG: [Clang] Fix crash instantiating functions defaulted outside class templates (#240376) A function that is defaulted outside its class template is not defaulted when its declaration is instantiated, so the instantiation of its definition reuses the info recorded at its definition (#229560). However, `SetDeclDefaulted` returned for such a pattern before recording the floating-point features in effect there, so the pattern usually had no info, and the transfer set the info bit with a null pointer. If non-default floating-point features were in effect at the point of instantiation, `SetDeclDefaulted` then tried to store them and asserted that the function already had the info. Even without the crash, the instantiation took the floating-point features in effect at its point of instantiation rather than at its definition, which also crashed codegen when those were strict but the function was not. Fixes #240351 Added: clang/test/SemaCXX/GH240351.cpp Modified: clang/lib/AST/Decl.cpp clang/lib/Sema/SemaDeclCXX.cpp clang/lib/Sema/SemaTemplateInstantiateDecl.cpp clang/test/CodeGenCXX/defaulted-function-fp-features.cpp Removed: ################################################################################ diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index 87901d0a08a9a1..3fc5eed34dc617 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -3164,6 +3164,7 @@ FunctionDecl::DefaultedOrDeletedFunctionInfo::Create( void FunctionDecl::setDefaultedOrDeletedInfo( DefaultedOrDeletedFunctionInfo *Info) { + assert(Info && "expected defaulted or deleted function info"); assert(!FunctionDeclBits.HasDefaultedOrDeletedInfo && "already have this"); assert(!Body && "can't replace function body with defaulted function info"); diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index cd021c660bf7be..62cbcc03ea42fe 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -19542,15 +19542,24 @@ void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc, Scope *S) { // Defer checking functions that are defaulted in a dependent context. if (FD->isDependentContext()) { - // The unqualified lookup of the prefix operator for a postfix operator - // defaulted outside its class can only be performed now, while we have the - // scope of the function-body. Instantiation reuses its result. (When it is - // defaulted in its class, the lookup is performed once the class is - // complete.) - if (DefKind.isPostfixOperator() && - !isa<CXXRecordDecl>(FD->getLexicalDeclContext())) - CheckExplicitlyDefaultedPostfixOperator(S, FD, - DefKind.asPostfixOperator()); + // A function defaulted outside its class is not defaulted when its + // declaration is instantiated, so record what the instantiation of its + // definition needs from here: the floating-point features in effect and, + // for a postfix operator, the unqualified lookup of the prefix operator, + // which can only be performed now, while we have the scope of the + // function-body. The info is recorded even for default floating-point + // features, so that the instantiation does not take those in effect at its + // point of instantiation. (When the function is defaulted in its class, + // the lookup is performed once the class is complete.) + if (!isa<CXXRecordDecl>(FD->getLexicalDeclContext())) { + if (DefKind.isPostfixOperator()) + CheckExplicitlyDefaultedPostfixOperator(S, FD, + DefKind.asPostfixOperator()); + else if (!FD->getDefaultedOrDeletedInfo()) + FD->setDefaultedOrDeletedInfo( + FunctionDecl::DefaultedOrDeletedFunctionInfo::Create( + Context, /*Lookups=*/{}, CurFPFeatureOverrides())); + } return; } @@ -19582,8 +19591,10 @@ void Sema::SetDeclDefaulted(Decl *Dcl, SourceLocation DefaultLoc, Scope *S) { // Only allocate DefaultedOrDeletedFunctionInfo if we actually have // non-default FP features to stash. This avoids memory overhead for - // the vast majority of defaulted functions. - if (!FD->getDefaultedOrDeletedInfo() && + // the vast majority of defaulted functions. A postfix operator defaulted + // with a scope gets its info, which includes these features, when the + // lookup of its prefix operator is performed below. + if (!FD->getDefaultedOrDeletedInfo() && !(DefKind.isPostfixOperator() && S) && CurFPFeatureOverrides().requiresTrailingStorage()) { FD->setDefaultedOrDeletedInfo( FunctionDecl::DefaultedOrDeletedFunctionInfo::Create( diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index a4ce596c7bfb1e..ead896eaa303c4 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -6270,12 +6270,13 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation, if (PatternDecl->isDefaulted()) { RebuildTypeSourceInfoForDefaultSpecialMembers(); // A function defaulted outside its class was not defaulted when its - // declaration was instantiated, so transfer the unqualified lookups - // performed at its definition now. They were performed from namespace - // scope, so they don't need to be instantiated. + // declaration was instantiated, so transfer the info recorded at its + // definition now: the floating-point features in effect there, and any + // unqualified lookups, which were performed from namespace scope and so + // don't need to be instantiated. if (!Function->getDefaultedOrDeletedInfo()) - Function->setDefaultedOrDeletedInfo( - PatternDecl->getDefaultedOrDeletedInfo()); + if (auto *DFI = PatternDecl->getDefaultedOrDeletedInfo()) + Function->setDefaultedOrDeletedInfo(DFI); SetDeclDefaulted(Function, PatternDecl->getLocation()); } else { DeclContext *DC = Function->getLexicalDeclContext(); diff --git a/clang/test/CodeGenCXX/defaulted-function-fp-features.cpp b/clang/test/CodeGenCXX/defaulted-function-fp-features.cpp index 3da4aa6976677b..563448d1959a2a 100644 --- a/clang/test/CodeGenCXX/defaulted-function-fp-features.cpp +++ b/clang/test/CodeGenCXX/defaulted-function-fp-features.cpp @@ -1,4 +1,4 @@ -// RUN: %clang_cc1 -triple x86_64-unknown-linux -std=c++20 -emit-llvm -o - %s | FileCheck %s +// RUN: %clang_cc1 -triple x86_64-unknown-linux -std=c++20 -emit-llvm -o - %s | FileCheck %s --check-prefixes=CHECK,OGCG // RUN: %if cir-enabled %{ %clang_cc1 -fclangir -triple x86_64-unknown-linux -std=c++20 -emit-llvm -o - %s | FileCheck %s %} // CHECK-DAG: define {{.*}} @_ZeqRK6TargetS1_({{.*}}) [[NORMAL_ATTRS:#[0-9]+]] @@ -11,6 +11,12 @@ // CHECK-DAG: define {{.*}} @_ZeqRK14TemplateTargetIfES2_({{.*}}) [[NORMAL_ATTRS]] // CHECK-DAG: define {{.*}} @_ZeqRK20StrictTemplateTargetIdES2_({{.*}}) [[STRICT_ATTRS]] +// Templates defaulted outside the class +// CHECK-DAG: define {{.*}} @_ZNK15OutOfLineTargetIeEeqERKS0_({{.*}}) [[NORMAL_ATTRS]] +// CHECK-DAG: fcmp oeq x86_fp80 +// CHECK-DAG: define {{.*}} @_ZNK21StrictOutOfLineTargetIeEeqERKS0_({{.*}}) [[STRICT_ATTRS]] +// OGCG-DAG: call i1 @llvm.experimental.constrained.fcmp.f80({{.*}}, metadata !"fpexcept.strict") + // --- NON-STRICT DECLARATIONS (at top of file, default FP is non-strict) --- struct Target { @@ -36,6 +42,15 @@ struct TemplateTarget { friend bool operator==(const TemplateTarget&, const TemplateTarget&) = default; }; +template <typename T> +struct OutOfLineTarget { + T d; + bool operator==(const OutOfLineTarget&) const; +}; + +template <typename T> +bool OutOfLineTarget<T>::operator==(const OutOfLineTarget&) const = default; + // Trigger instantiation of TemplateTarget<double>::operator== in non-strict context. bool test_template_non_strict(TemplateTarget<double> a, TemplateTarget<double> b) { return a == b; @@ -61,6 +76,10 @@ bool test_template_strict(TemplateTarget<float> a, TemplateTarget<float> b) { return a == b; } +// Instantiate OutOfLineTarget<long double>::operator== in strict context. +// It should still be non-strict because it was defaulted in non-strict context. +template bool OutOfLineTarget<long double>::operator==(const OutOfLineTarget<long double>&) const; + // Strict declarations (must get strictfp) struct StrictTarget { @@ -87,6 +106,16 @@ struct StrictTemplateTarget { }; +template <typename T> +struct StrictOutOfLineTarget { + T d; + bool operator==(const StrictOutOfLineTarget&) const; +}; + +template <typename T> +bool StrictOutOfLineTarget<T>::operator==(const StrictOutOfLineTarget&) const = default; + + // --- NON-STRICT CONTEXT AGAIN --- #pragma STDC FENV_ACCESS OFF @@ -96,6 +125,12 @@ bool test_strict_template_non_strict(StrictTemplateTarget<double> a, StrictTempl return a == b; } +// Trigger instantiation of StrictOutOfLineTarget<long double>::operator== in non-strict context. +// It should be strict because it was defaulted in strict context. +bool test_strict_out_of_line_non_strict(StrictOutOfLineTarget<long double> a, StrictOutOfLineTarget<long double> b) { + return a == b; +} + // CHECK-DAG: attributes [[STRICT_ATTRS]] = { {{.*}}strictfp{{.*}} } // CHECK-DAG: attributes [[NORMAL_ATTRS]] = { {{.*}} } diff --git a/clang/test/SemaCXX/GH240351.cpp b/clang/test/SemaCXX/GH240351.cpp new file mode 100644 index 00000000000000..9daee4136515f0 --- /dev/null +++ b/clang/test/SemaCXX/GH240351.cpp @@ -0,0 +1,88 @@ +// RUN: %clang_cc1 -std=c++17 -fsyntax-only -Wno-c++2d-extensions -verify %s +// RUN: %clang_cc1 -std=c++2d -fsyntax-only -verify %s +// RUN: %clang_cc1 -std=c++17 -fsyntax-only -Wno-c++2d-extensions -verify -DFP_PRAGMA %s +// RUN: %clang_cc1 -std=c++2d -fsyntax-only -verify -DFP_PRAGMA %s +// expected-no-diagnostics + +// A function defaulted outside its class template is not defaulted when its +// declaration is instantiated. Instantiating its definition while non-default +// floating-point features are in effect used to crash. + +namespace optnone { +// Instantiating an optnone function leaves its floating-point features in +// effect for the next pending instantiation. +template <typename = int> struct a { + a(); +}; +template <typename b> a<b>::a() = default; +template <typename f, typename g> __attribute__((optnone)) void h(f, g) {} +void i(int j) { + h(j, j); + a k; +} +} // namespace optnone + +namespace special_members { +template <typename T> struct S { + constexpr S(); + constexpr S(const S &); + S &operator=(const S &); +}; +template <typename T> constexpr S<T>::S() = default; +template <typename T> constexpr S<T>::S(const S &) = default; +template <typename T> S<T> &S<T>::operator=(const S &) = default; + +template <typename T> struct D { + D(); + ~D(); +}; +template <typename T> D<T>::D() = default; +template <typename T> D<T>::~D() = default; + +void f() { +#ifdef FP_PRAGMA +#pragma clang fp contract(fast) +#endif + // The constexpr constructors are instantiated here, under the pragma. + constexpr S<int> s; + constexpr S<int> t = s; + S<int> u; + u = t; + D<int> d; +} +} // namespace special_members + +#ifdef FP_PRAGMA +// This stays in effect until the end of the file, where pending instantiations +// are performed. +#pragma clang fp contract(fast) +#endif + +namespace postfix { +// The pattern of a postfix operator defaulted outside its class holds the +// result of the prefix operator lookup, which the instantiation reuses. +template <typename T> struct P { + P &operator++(); + P operator++(int); +}; +template <typename T> P<T> P<T>::operator++(int) = default; + +// The floating-point features of a postfix operator defaulted outside its +// class are recorded along with the result of the prefix operator lookup. +struct M { + M &operator++(); + M operator++(int); +}; +M M::operator++(int) = default; + +struct N { + N &operator--(); +}; +N operator--(N &, int) = default; + +void g(P<int> p, M m, N n) { + p++; + m++; + n--; +} +} // namespace postfix _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
