Skip to content

Commit

Permalink
Fixed #454: Added missing support for noexcept transformation.
Browse files Browse the repository at this point in the history
The first implementation left member functions untransformed. This patch
adds support for them.
  • Loading branch information
andreasfertig committed Feb 22, 2022
1 parent a4c4796 commit 06571f3
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 39 deletions.
82 changes: 43 additions & 39 deletions CodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,47 @@ void CodeGenerator::InsertArg(const CoreturnStmt* stmt)
}
//-----------------------------------------------------------------------------

void CodeGenerator::InsertMethodBody(const FunctionDecl* stmt)
{
if(stmt->doesThisDeclarationHaveABody()) {
mOutputFormatHelper.AppendNewLine();

const auto exSpec = stmt->getExceptionSpecType();
const bool showNoexcept =
GetInsightsOptions().UseShowNoexcept && is{exSpec}.any_of(EST_BasicNoexcept, EST_NoexceptTrue);

if(showNoexcept) {
mHaveException = true;
mOutputFormatHelper.OpenScope();
mOutputFormatHelper.Append(kwTrySpace);
}

// handle C++ [basic.start.main] §5: main can have no return statement
if(stmt->hasImplicitReturnZero()) {
// TODO replace with ranges::find_if
const auto cmpBody = dyn_cast<CompoundStmt>(stmt->getBody())->body();
mRequiresImplicitReturnZero =
std::end(cmpBody) ==
std::find_if(cmpBody.begin(), cmpBody.end(), [](const Stmt* e) { return isa<ReturnStmt>(e); });
}

InsertArg(stmt->getBody());

if(showNoexcept) {
mOutputFormatHelper.Append(" catch(...) "sv);
mOutputFormatHelper.OpenScope();
mOutputFormatHelper.Append("std::terminate();"sv);
mOutputFormatHelper.CloseScope();
mOutputFormatHelper.CloseScope();
}

mOutputFormatHelper.AppendNewLine();
} else {
mOutputFormatHelper.AppendSemiNewLine();
}
}
//-----------------------------------------------------------------------------

void CodeGenerator::InsertArg(const FunctionDecl* stmt)
{
// LAMBDA_SCOPE_HELPER(VarDecl);
Expand All @@ -1089,42 +1130,7 @@ void CodeGenerator::InsertArg(const FunctionDecl* stmt)
InsertFunctionNameWithReturnType(*stmt);

if(not InsertLambdaStaticInvoker(dyn_cast_or_null<CXXMethodDecl>(stmt))) {
if(stmt->doesThisDeclarationHaveABody()) {
mOutputFormatHelper.AppendNewLine();

const auto exSpec = stmt->getExceptionSpecType();
const bool showNoexcept =
GetInsightsOptions().UseShowNoexcept && is{exSpec}.any_of(EST_BasicNoexcept, EST_NoexceptTrue);

if(showNoexcept) {
mHaveException = true;
mOutputFormatHelper.OpenScope();
mOutputFormatHelper.Append(kwTrySpace);
}

// handle C++ [basic.start.main] §5: main can have no return statement
if(stmt->hasImplicitReturnZero()) {
// TODO replace with ranges::find_if
const auto cmpBody = dyn_cast<CompoundStmt>(stmt->getBody())->body();
mRequiresImplicitReturnZero =
std::end(cmpBody) ==
std::find_if(cmpBody.begin(), cmpBody.end(), [](const Stmt* e) { return isa<ReturnStmt>(e); });
}

InsertArg(stmt->getBody());

if(showNoexcept) {
mOutputFormatHelper.Append(" catch(...) "sv);
mOutputFormatHelper.OpenScope();
mOutputFormatHelper.Append("std::terminate();"sv);
mOutputFormatHelper.CloseScope();
mOutputFormatHelper.CloseScope();
}

mOutputFormatHelper.AppendNewLine();
} else {
mOutputFormatHelper.AppendSemiNewLine();
}
InsertMethodBody(stmt);
}

InsertTemplateGuardEnd(stmt);
Expand Down Expand Up @@ -2350,9 +2356,7 @@ void CodeGenerator::InsertCXXMethodDecl(const CXXMethodDecl* stmt, SkipBody skip
}

if((SkipBody::No == skipBody) && stmt->doesThisDeclarationHaveABody() && not stmt->isLambdaStaticInvoker()) {
mOutputFormatHelper.AppendNewLine();
InsertArg(stmt->getBody());
mOutputFormatHelper.AppendNewLine();
InsertMethodBody(stmt);

} else if(not InsertLambdaStaticInvoker(stmt) || (SkipBody::Yes == skipBody)) {
mOutputFormatHelper.AppendSemiNewLine();
Expand Down
1 change: 1 addition & 0 deletions CodeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ class CodeGenerator

STRONG_BOOL(SkipBody);
void InsertCXXMethodDecl(const CXXMethodDecl* stmt, SkipBody skipBody);
void InsertMethodBody(const FunctionDecl* stmt);

/// \brief Generalized function to insert either a \c CXXConstructExpr or \c CXXUnresolvedConstructExpr
template<typename T>
Expand Down
16 changes: 16 additions & 0 deletions tests/Issue454.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// cmdlineinsights:-edu-show-noexcept

struct Data
{
int i;
double d;
double func() const noexcept{
// should't also this show a try ... catch block?
return d*i;
}
};


int func() noexcept {
return 4*6;
}
30 changes: 30 additions & 0 deletions tests/Issue454.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <exception> // for noexcept transformation
// cmdlineinsights:-edu-show-noexcept

struct Data
{
int i;
double d;
inline double func() const noexcept
{
try {
return this->d * static_cast<double>(this->i);
} catch(...) {
std::terminate();
}
}

};




int func() noexcept
{
try {
return 4 * 6;
} catch(...) {
std::terminate();
}
}

0 comments on commit 06571f3

Please sign in to comment.