Skip to content

Commit

Permalink
Merge pull request #699 from andreasfertig/pseudoDestructor
Browse files Browse the repository at this point in the history
Added support for pseudo destructor.
  • Loading branch information
andreasfertig authored Jan 28, 2025
2 parents be2c95f + 18aadc3 commit 87f6b1a
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2111,6 +2111,14 @@ bool CodeGenerator::InsideDecltype() const
}
//-----------------------------------------------------------------------------

void CodeGenerator::InsertArg(const CXXPseudoDestructorExpr* stmt)
{
InsertArg(stmt->getBase());

mOutputFormatHelper.Append(ArrowOrDot(stmt->isArrow()), "~", GetName(stmt->getDestroyedType()));
}
//-----------------------------------------------------------------------------

void CodeGenerator::InsertArg(const CXXMemberCallExpr* stmt)
{
CONDITIONAL_LAMBDA_SCOPE_HELPER(MemberCallExpr, not InsideDecltype())
Expand Down
1 change: 1 addition & 0 deletions CodeGeneratorTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ SUPPORTED_STMT(StmtExpr)
SUPPORTED_STMT(SourceLocExpr)
SUPPORTED_STMT(CXXParenListInitExpr)
SUPPORTED_STMT(CppInsightsCommentStmt)
SUPPORTED_STMT(CXXPseudoDestructorExpr)

#undef IGNORED_DECL
#undef IGNORED_STMT
Expand Down
36 changes: 36 additions & 0 deletions tests/PlacemenNew2Test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <new>

namespace Test {
template<typename T>
struct Apple
{
T x;
};

template<typename T>
void destroy(T* ptr) {
ptr->T::~T();
}
}

namespace West
{
int y;
}

int main()
{
char buffer[sizeof(Test::Apple<int>)];

auto f = new (&buffer) Test::Apple<int>;

f->Test::Apple<int>::~Apple<int>();
f->~Apple();

Test::destroy(f);

int x;
Test::destroy(&x);

Test::destroy(&West::y);
}
66 changes: 66 additions & 0 deletions tests/PlacemenNew2Test.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <new>

namespace Test
{
template<typename T>
struct Apple
{
T x;
};

/* First instantiated from: PlacemenNew2Test.cpp:23 */
#ifdef INSIGHTS_USE_TEMPLATE
template<>
struct Apple<int>
{
int x;
// inline Apple() noexcept = default;
};

#endif
template<typename T>
void destroy(T * ptr)
{
ptr->~T();
}

/* First instantiated from: PlacemenNew2Test.cpp:30 */
#ifdef INSIGHTS_USE_TEMPLATE
template<>
void destroy<Apple<int> >(Apple<int> * ptr)
{
ptr->~Apple();
}
#endif


/* First instantiated from: PlacemenNew2Test.cpp:33 */
#ifdef INSIGHTS_USE_TEMPLATE
template<>
void destroy<int>(int * ptr)
{
ptr->~int();
}
#endif


}

namespace West
{
int y;

}

int main()
{
char buffer[4];
Test::Apple<int> * f = new (reinterpret_cast<void *>(&buffer))Test::Apple<int>();
f->~Apple();
f->~Apple();
Test::destroy(f);
int x;
Test::destroy(&x);
Test::destroy(&West::y);
return 0;
}
21 changes: 21 additions & 0 deletions tests/PlacemenNewTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <new>

template<typename T>
struct Apple
{
T x;
};


int main()
{
char buffer[sizeof(Apple<int>)];

auto* f = new (&buffer) Apple<int>;

auto& r = *f;

r.~Apple<int>();

f->~Apple<int>();
}
28 changes: 28 additions & 0 deletions tests/PlacemenNewTest.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <new>

template<typename T>
struct Apple
{
T x;
};

/* First instantiated from: PlacemenNewTest.cpp:12 */
#ifdef INSIGHTS_USE_TEMPLATE
template<>
struct Apple<int>
{
int x;
// inline Apple() noexcept = default;
};

#endif

int main()
{
char buffer[4];
Apple<int> * f = new (reinterpret_cast<void *>(&buffer))Apple<int>();
Apple<int> & r = *f;
r.~Apple();
f->~Apple();
return 0;
}

0 comments on commit 87f6b1a

Please sign in to comment.