Skip to content

Commit 82a3214

Browse files
authored
[Clang][Sema] Fix crash with const qualified member operator new (#80327)
We should diagnose a const qualified member operator new but we fail to do so and this leads to crash during debug info generation. The fix is to diagnose this as ill-formed in the front-end. Fixes: #79748
1 parent b49fa21 commit 82a3214

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ Bug Fixes to C++ Support
192192
and (`#79745 <https://github.com/llvm/llvm-project/issues/79745>`_)
193193
- Fix incorrect code generation caused by the object argument of ``static operator()`` and ``static operator[]`` calls not being evaluated.
194194
Fixes (`#67976 <https://github.com/llvm/llvm-project/issues/67976>`_)
195+
- Fix crash and diagnostic with const qualified member operator new.
196+
Fixes (`#79748 <https://github.com/llvm/llvm-project/issues/79748>`_)
195197

196198
Bug Fixes to AST Handling
197199
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaType.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5907,15 +5907,24 @@ static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state,
59075907
// - the type-id in the default argument of a type-parameter, or
59085908
// - the type-id of a template-argument for a type-parameter
59095909
//
5910+
// C++23 [dcl.fct]p6 (P0847R7)
5911+
// ... A member-declarator with an explicit-object-parameter-declaration
5912+
// shall not include a ref-qualifier or a cv-qualifier-seq and shall not be
5913+
// declared static or virtual ...
5914+
//
59105915
// FIXME: Checking this here is insufficient. We accept-invalid on:
59115916
//
59125917
// template<typename T> struct S { void f(T); };
59135918
// S<int() const> s;
59145919
//
59155920
// ... for instance.
59165921
if (IsQualifiedFunction &&
5917-
!(Kind == Member && !D.isExplicitObjectMemberFunction() &&
5918-
D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_static) &&
5922+
// Check for non-static member function and not and
5923+
// explicit-object-parameter-declaration
5924+
(Kind != Member || D.isExplicitObjectMemberFunction() ||
5925+
D.getDeclSpec().getStorageClassSpec() == DeclSpec::SCS_static ||
5926+
(D.getContext() == clang::DeclaratorContext::Member &&
5927+
D.isStaticMember())) &&
59195928
!IsTypedefName && D.getContext() != DeclaratorContext::TemplateArg &&
59205929
D.getContext() != DeclaratorContext::TemplateTypeArg) {
59215930
SourceLocation Loc = D.getBeginLoc();

clang/test/SemaCXX/function-type-qual.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,21 @@ void instantiateArrayDecay() {
3737
int a[1];
3838
arrayDecay(a);
3939
}
40+
41+
namespace GH79748 {
42+
typedef decltype(sizeof(0)) size_t;
43+
struct A {
44+
void* operator new(size_t bytes) const; //expected-error {{static member function cannot have 'const' qualifier}}
45+
void* operator new[](size_t bytes) const; //expected-error {{static member function cannot have 'const' qualifier}}
46+
47+
void operator delete(void*) const; //expected-error {{static member function cannot have 'const' qualifier}}
48+
void operator delete[](void*) const; //expected-error {{static member function cannot have 'const' qualifier}}
49+
};
50+
struct B {
51+
void* operator new(size_t bytes) volatile; //expected-error {{static member function cannot have 'volatile' qualifier}}
52+
void* operator new[](size_t bytes) volatile; //expected-error {{static member function cannot have 'volatile' qualifier}}
53+
54+
void operator delete(void*) volatile; //expected-error {{static member function cannot have 'volatile' qualifier}}
55+
void operator delete[](void*) volatile; //expected-error {{static member function cannot have 'volatile' qualifier}}
56+
};
57+
}

0 commit comments

Comments
 (0)