[arcmt] Fix test/ARCMT/remove-statements.m regression due to

Objective-C method buffering(rdar://10056942)

Turned out the same issue existed for C++ inline methods.

llvm-svn: 138960
This commit is contained in:
Argyrios Kyrtzidis 2011-09-01 20:53:18 +00:00
parent 7018d5bcfb
commit cbbc0141f6
6 changed files with 88 additions and 33 deletions

View File

@ -1116,6 +1116,19 @@ public:
/// \returns true if LHS source location comes before RHS, false otherwise. /// \returns true if LHS source location comes before RHS, false otherwise.
bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const; bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
/// \brief Comparison function class.
class LocBeforeThanCompare : public std::binary_function<SourceLocation,
SourceLocation, bool> {
SourceManager &SM;
public:
explicit LocBeforeThanCompare(SourceManager &SM) : SM(SM) { }
bool operator()(SourceLocation LHS, SourceLocation RHS) const {
return SM.isBeforeInTranslationUnit(LHS, RHS);
}
};
/// \brief Determines the order of 2 source locations in the "source location /// \brief Determines the order of 2 source locations in the "source location
/// address space". /// address space".
bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const { bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const {

View File

@ -22,25 +22,68 @@
#include "Transforms.h" #include "Transforms.h"
#include "Internals.h" #include "Internals.h"
#include "clang/AST/StmtVisitor.h" #include "clang/AST/StmtVisitor.h"
#include "clang/Basic/SourceManager.h"
using namespace clang; using namespace clang;
using namespace arcmt; using namespace arcmt;
using namespace trans; using namespace trans;
static bool isEmptyARCMTMacroStatement(NullStmt *S,
std::vector<SourceLocation> &MacroLocs,
ASTContext &Ctx) {
if (!S->hasLeadingEmptyMacro())
return false;
SourceLocation SemiLoc = S->getSemiLoc();
if (SemiLoc.isInvalid() || SemiLoc.isMacroID())
return false;
if (MacroLocs.empty())
return false;
SourceManager &SM = Ctx.getSourceManager();
std::vector<SourceLocation>::iterator
I = std::upper_bound(MacroLocs.begin(), MacroLocs.end(), SemiLoc,
SourceManager::LocBeforeThanCompare(SM));
--I;
SourceLocation
AfterMacroLoc = I->getFileLocWithOffset(getARCMTMacroName().size());
assert(AfterMacroLoc.isFileID());
if (AfterMacroLoc == SemiLoc)
return true;
int RelOffs = 0;
if (!SM.isInSameSLocAddrSpace(AfterMacroLoc, SemiLoc, &RelOffs))
return false;
if (RelOffs < 0)
return false;
// We make the reasonable assumption that a semicolon after 100 characters
// means that it is not the next token after our macro. If this assumption
// fails it is not critical, we will just fail to clear out, e.g., an empty
// 'if'.
if (RelOffs - getARCMTMacroName().size() > 100)
return false;
SourceLocation AfterMacroSemiLoc = findSemiAfterLocation(AfterMacroLoc, Ctx);
return AfterMacroSemiLoc == SemiLoc;
}
namespace { namespace {
/// \brief Returns true if the statement became empty due to previous /// \brief Returns true if the statement became empty due to previous
/// transformations. /// transformations.
class EmptyChecker : public StmtVisitor<EmptyChecker, bool> { class EmptyChecker : public StmtVisitor<EmptyChecker, bool> {
ASTContext &Ctx; ASTContext &Ctx;
llvm::DenseSet<unsigned> &MacroLocs; std::vector<SourceLocation> &MacroLocs;
public: public:
EmptyChecker(ASTContext &ctx, llvm::DenseSet<unsigned> &macroLocs) EmptyChecker(ASTContext &ctx, std::vector<SourceLocation> &macroLocs)
: Ctx(ctx), MacroLocs(macroLocs) { } : Ctx(ctx), MacroLocs(macroLocs) { }
bool VisitNullStmt(NullStmt *S) { bool VisitNullStmt(NullStmt *S) {
return isMacroLoc(S->getLeadingEmptyMacroLoc()); return isEmptyARCMTMacroStatement(S, MacroLocs, Ctx);
} }
bool VisitCompoundStmt(CompoundStmt *S) { bool VisitCompoundStmt(CompoundStmt *S) {
if (S->body_empty()) if (S->body_empty())
@ -102,23 +145,14 @@ public:
return false; return false;
return Visit(S->getSubStmt()); return Visit(S->getSubStmt());
} }
private:
bool isMacroLoc(SourceLocation loc) {
if (loc.isInvalid()) return false;
return MacroLocs.count(loc.getRawEncoding());
}
}; };
class EmptyStatementsRemover : class EmptyStatementsRemover :
public RecursiveASTVisitor<EmptyStatementsRemover> { public RecursiveASTVisitor<EmptyStatementsRemover> {
MigrationPass &Pass; MigrationPass &Pass;
llvm::DenseSet<unsigned> &MacroLocs;
public: public:
EmptyStatementsRemover(MigrationPass &pass, EmptyStatementsRemover(MigrationPass &pass) : Pass(pass) { }
llvm::DenseSet<unsigned> &macroLocs)
: Pass(pass), MacroLocs(macroLocs) { }
bool TraverseStmtExpr(StmtExpr *E) { bool TraverseStmtExpr(StmtExpr *E) {
CompoundStmt *S = E->getSubStmt(); CompoundStmt *S = E->getSubStmt();
@ -138,17 +172,12 @@ public:
return true; return true;
} }
bool isMacroLoc(SourceLocation loc) {
if (loc.isInvalid()) return false;
return MacroLocs.count(loc.getRawEncoding());
}
ASTContext &getContext() { return Pass.Ctx; } ASTContext &getContext() { return Pass.Ctx; }
private: private:
void check(Stmt *S) { void check(Stmt *S) {
if (!S) return; if (!S) return;
if (EmptyChecker(Pass.Ctx, MacroLocs).Visit(S)) { if (EmptyChecker(Pass.Ctx, Pass.ARCMTMacroLocs).Visit(S)) {
Transaction Trans(Pass.TA); Transaction Trans(Pass.TA);
Pass.TA.removeStmt(S); Pass.TA.removeStmt(S);
} }
@ -157,8 +186,8 @@ private:
} // anonymous namespace } // anonymous namespace
static bool isBodyEmpty(CompoundStmt *body, static bool isBodyEmpty(CompoundStmt *body, ASTContext &Ctx,
ASTContext &Ctx, llvm::DenseSet<unsigned> &MacroLocs) { std::vector<SourceLocation> &MacroLocs) {
for (CompoundStmt::body_iterator for (CompoundStmt::body_iterator
I = body->body_begin(), E = body->body_end(); I != E; ++I) I = body->body_begin(), E = body->body_end(); I != E; ++I)
if (!EmptyChecker(Ctx, MacroLocs).Visit(*I)) if (!EmptyChecker(Ctx, MacroLocs).Visit(*I))
@ -167,8 +196,7 @@ static bool isBodyEmpty(CompoundStmt *body,
return true; return true;
} }
static void removeDeallocMethod(MigrationPass &pass, static void removeDeallocMethod(MigrationPass &pass) {
llvm::DenseSet<unsigned> &MacroLocs) {
ASTContext &Ctx = pass.Ctx; ASTContext &Ctx = pass.Ctx;
TransformActions &TA = pass.TA; TransformActions &TA = pass.TA;
DeclContext *DC = Ctx.getTranslationUnitDecl(); DeclContext *DC = Ctx.getTranslationUnitDecl();
@ -183,7 +211,7 @@ static void removeDeallocMethod(MigrationPass &pass,
ObjCMethodDecl *MD = *MI; ObjCMethodDecl *MD = *MI;
if (MD->getMethodFamily() == OMF_dealloc) { if (MD->getMethodFamily() == OMF_dealloc) {
if (MD->hasBody() && if (MD->hasBody() &&
isBodyEmpty(MD->getCompoundBody(), Ctx, MacroLocs)) { isBodyEmpty(MD->getCompoundBody(), Ctx, pass.ARCMTMacroLocs)) {
Transaction Trans(TA); Transaction Trans(TA);
TA.remove(MD->getSourceRange()); TA.remove(MD->getSourceRange());
} }
@ -194,14 +222,9 @@ static void removeDeallocMethod(MigrationPass &pass,
} }
void trans::removeEmptyStatementsAndDealloc(MigrationPass &pass) { void trans::removeEmptyStatementsAndDealloc(MigrationPass &pass) {
llvm::DenseSet<unsigned> MacroLocs; EmptyStatementsRemover(pass).TraverseDecl(pass.Ctx.getTranslationUnitDecl());
for (unsigned i = 0, e = pass.ARCMTMacroLocs.size(); i != e; ++i)
MacroLocs.insert(pass.ARCMTMacroLocs[i].getRawEncoding());
EmptyStatementsRemover(pass, MacroLocs) removeDeallocMethod(pass);
.TraverseDecl(pass.Ctx.getTranslationUnitDecl());
removeDeallocMethod(pass, MacroLocs);
for (unsigned i = 0, e = pass.ARCMTMacroLocs.size(); i != e; ++i) { for (unsigned i = 0, e = pass.ARCMTMacroLocs.size(); i != e; ++i) {
Transaction Trans(pass.TA); Transaction Trans(pass.TA);

View File

@ -91,6 +91,18 @@ bool trans::canApplyWeak(ASTContext &Ctx, QualType type) {
/// source location will be invalid. /// source location will be invalid.
SourceLocation trans::findLocationAfterSemi(SourceLocation loc, SourceLocation trans::findLocationAfterSemi(SourceLocation loc,
ASTContext &Ctx) { ASTContext &Ctx) {
SourceLocation SemiLoc = findSemiAfterLocation(loc, Ctx);
if (SemiLoc.isInvalid())
return SourceLocation();
return SemiLoc.getFileLocWithOffset(1);
}
/// \brief \arg Loc is the end of a statement range. This returns the location
/// of the semicolon following the statement.
/// If no semicolon is found or the location is inside a macro, the returned
/// source location will be invalid.
SourceLocation trans::findSemiAfterLocation(SourceLocation loc,
ASTContext &Ctx) {
SourceManager &SM = Ctx.getSourceManager(); SourceManager &SM = Ctx.getSourceManager();
if (loc.isMacroID()) { if (loc.isMacroID()) {
if (!Lexer::isAtEndOfMacroExpansion(loc, SM, Ctx.getLangOptions())) if (!Lexer::isAtEndOfMacroExpansion(loc, SM, Ctx.getLangOptions()))
@ -119,7 +131,7 @@ SourceLocation trans::findLocationAfterSemi(SourceLocation loc,
if (tok.isNot(tok::semi)) if (tok.isNot(tok::semi))
return SourceLocation(); return SourceLocation();
return tok.getLocation().getFileLocWithOffset(1); return tok.getLocation();
} }
bool trans::hasSideEffects(Expr *E, ASTContext &Ctx) { bool trans::hasSideEffects(Expr *E, ASTContext &Ctx) {

View File

@ -54,6 +54,12 @@ bool canApplyWeak(ASTContext &Ctx, QualType type);
/// source location will be invalid. /// source location will be invalid.
SourceLocation findLocationAfterSemi(SourceLocation loc, ASTContext &Ctx); SourceLocation findLocationAfterSemi(SourceLocation loc, ASTContext &Ctx);
/// \brief \arg Loc is the end of a statement range. This returns the location
/// of the semicolon following the statement.
/// If no semicolon is found or the location is inside a macro, the returned
/// source location will be invalid.
SourceLocation findSemiAfterLocation(SourceLocation loc, ASTContext &Ctx);
bool hasSideEffects(Expr *E, ASTContext &Ctx); bool hasSideEffects(Expr *E, ASTContext &Ctx);
bool isGlobalVar(Expr *E); bool isGlobalVar(Expr *E);
/// \brief Returns "nil" or "0" if 'nil' macro is not actually defined. /// \brief Returns "nil" or "0" if 'nil' macro is not actually defined.

View File

@ -14,6 +14,8 @@ struct foo {
NSAutoreleasePool *pool = [NSAutoreleasePool new]; NSAutoreleasePool *pool = [NSAutoreleasePool new];
[[[NSString string] retain] release]; [[[NSString string] retain] release];
[pool drain]; [pool drain];
if (s)
[s release];
} }
~foo(){ [s release]; } ~foo(){ [s release]; }
private: private:

View File

@ -1,7 +1,6 @@
// RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fobjc-nonfragile-abi -fsyntax-only -fobjc-arc -x objective-c %s.result // RUN: %clang_cc1 -triple x86_64-apple-darwin10 -fobjc-nonfragile-abi -fsyntax-only -fobjc-arc -x objective-c %s.result
// RUN: arcmt-test --args -triple x86_64-apple-darwin10 -fobjc-nonfragile-abi -fsyntax-only -x objective-c %s > %t // RUN: arcmt-test --args -triple x86_64-apple-darwin10 -fobjc-nonfragile-abi -fsyntax-only -x objective-c %s > %t
// RUN: diff %t %s.result // RUN: diff %t %s.result
// XFAIL: *
#include "Common.h" #include "Common.h"