Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove try catch #157

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions clang_delta/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ add_executable(clang_delta
RemovePointer.h
RemoveTrivialBaseTemplate.cpp
RemoveTrivialBaseTemplate.h
RemoveTryCatch.cpp
RemoveTryCatch.h
RemoveUnresolvedBase.cpp
RemoveUnresolvedBase.h
RemoveUnusedEnumMember.cpp
Expand Down
136 changes: 136 additions & 0 deletions clang_delta/RemoveTryCatch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
//===----------------------------------------------------------------------===//
//
// Copyright (c) 2012, 2013, 2015 The University of Utah
// Copyright (c) 2012 Konstantin Tokarev <annulen@yandex.ru>
// All rights reserved.
//
// This file is distributed under the University of Illinois Open Source
// License. See the file COPYING for details.
//
//===----------------------------------------------------------------------===//

#if HAVE_CONFIG_H
# include <config.h>
#endif

#include "RemoveTryCatch.h"

#include <cctype>
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/ASTContext.h"
#include "clang/Basic/SourceManager.h"

#include "TransformationManager.h"

using namespace clang;

static const char *DescriptionMsg =
"Remove catch blocks and if not present the try block as well. \n";

static RegisterTransformation<RemoveTryCatch>
Trans("remove-try-catch", DescriptionMsg);

class RemoveTryCatchAnalysisVisitor : public
RecursiveASTVisitor<RemoveTryCatchAnalysisVisitor> {
public:

explicit RemoveTryCatchAnalysisVisitor(RemoveTryCatch *Instance)
: ConsumerInstance(Instance)
{ }

bool VisitCXXTryStmt(CXXTryStmt *CTS);

private:

RemoveTryCatch *ConsumerInstance;
};

bool RemoveTryCatchAnalysisVisitor::VisitCXXTryStmt(
CXXTryStmt *CTS)
{
if (ConsumerInstance->isInIncludedFile(CTS)) {
return true;
}

// Count try block
++ConsumerInstance->ValidInstanceNum;

if (ConsumerInstance->TransformationCounter ==
ConsumerInstance->ValidInstanceNum) {
ConsumerInstance->TheTryCatchStmt = CTS;
}

int TmpInstanceNum = ConsumerInstance->ValidInstanceNum;

// Count all catch blocks
ConsumerInstance->ValidInstanceNum += CTS->getNumHandlers();

// Early exit if the transformation counter is less than the index of any
// catch block
if (ConsumerInstance->TransformationCounter <= TmpInstanceNum) {
return true;
}

// Early exit if the transformation counter is higher than the index of any
// catch block
if (ConsumerInstance->TransformationCounter >
ConsumerInstance->ValidInstanceNum) {
return true;
}

TransAssert(ConsumerInstance->TransformationCounter > TmpInstanceNum);

int CatchIdx = ConsumerInstance->TransformationCounter - TmpInstanceNum - 1;
ConsumerInstance->TheTryCatchStmt = CTS->getHandler(CatchIdx);

// If the last catch block is removed the "try" has to be removed as well
if (CTS->getNumHandlers() == 1) {
ConsumerInstance->RewriteTryStmt = CTS;
}

return true;
}

void RemoveTryCatch::Initialize(ASTContext &context)
{
Transformation::Initialize(context);
AnalysisVisitor = new RemoveTryCatchAnalysisVisitor(this);
}

void RemoveTryCatch::HandleTranslationUnit(ASTContext &Ctx)
{
AnalysisVisitor->TraverseDecl(Ctx.getTranslationUnitDecl());

if (QueryInstanceOnly)
return;

if (TransformationCounter > ValidInstanceNum) {
TransError = TransMaxInstanceError;
return;
}

Ctx.getDiagnostics().setSuppressAllDiagnostics(false);

TransAssert(TheTryCatchStmt && "NULL TheTryCatchStmt!");

removeStmt();

if (Ctx.getDiagnostics().hasErrorOccurred() ||
Ctx.getDiagnostics().hasFatalErrorOccurred())
TransError = TransInternalError;
}

void RemoveTryCatch::removeStmt()
{
SourceRange Range = TheTryCatchStmt->getSourceRange();
TheRewriter.RemoveText(Range);

if (RewriteTryStmt != nullptr) {
TheRewriter.RemoveText(RewriteTryStmt->getLocStart(), 3);
}
}

RemoveTryCatch::~RemoveTryCatch()
{
delete AnalysisVisitor;
}
59 changes: 59 additions & 0 deletions clang_delta/RemoveTryCatch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//===----------------------------------------------------------------------===//
//
// Copyright (c) 2012 The University of Utah
// Copyright (c) 2012 Konstantin Tokarev <annulen@yandex.ru>
// All rights reserved.
//
// This file is distributed under the University of Illinois Open Source
// License. See the file COPYING for details.
//
//===----------------------------------------------------------------------===//

#ifndef REMOVE_TRY_CATCH_H
#define REMOVE_TRY_CATCH_H

#include <string>
#include "llvm/ADT/DenseMap.h"
#include "Transformation.h"

namespace clang {
class Stmt;
}

class RemoveTryCatchAnalysisVisitor;

class RemoveTryCatch : public Transformation {
friend class RemoveTryCatchAnalysisVisitor;

public:

RemoveTryCatch(const char *TransName, const char *Desc)
: Transformation(TransName, Desc),
AnalysisVisitor(0),
RewriteTryStmt(0),
TheTryCatchStmt(0)
{ }

~RemoveTryCatch();

private:

virtual void Initialize(clang::ASTContext &context);

virtual void HandleTranslationUnit(clang::ASTContext &Ctx);

void removeStmt();

RemoveTryCatchAnalysisVisitor *AnalysisVisitor;

clang::Stmt *RewriteTryStmt;
clang::Stmt *TheTryCatchStmt;

// Unimplemented
RemoveTryCatch();

RemoveTryCatch(const RemoveTryCatch &);

void operator=(const RemoveTryCatch &);
};
#endif
1 change: 1 addition & 0 deletions creduce/creduce.in
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,7 @@ my @all_methods = (
{ "name" => "pass_clang", "arg" => "replace-dependent-name", "pri" => 257, "C" => 1, },
{ "name" => "pass_clang", "arg" => "simplify-recursive-template-instantiation", "pri" => 258, "C" => 1, },
{ "name" => "pass_clang", "arg" => "vector-to-array", "pri" => 259, "C" => 1, },
{ "name" => "pass_clang", "arg" => "remove-try-catch", "pri" => 260, "C" => 1, },
{ "name" => "pass_clang", "arg" => "combine-global-var", "last_pass_pri" => 990, "C" => 1, },
{ "name" => "pass_clang", "arg" => "combine-local-var", "last_pass_pri" => 991, "C" => 1, },
{ "name" => "pass_clang", "arg" => "simplify-struct-union-decl", "last_pass_pri" => 992, "C" => 1, },
Expand Down