-// -*- indent-tabs-mode:nil; -*-
+// -*- indent-tabs-mode:nil; c-basic-offset:4; -*-
//------------------------------------------------------------------------------
// Add MC2 annotations to C code.
// Copyright 2015 Patrick Lam <prof.lam@gmail.com>
virtual void run(const MatchFinder::MatchResult &Result) {
const Stmt * s = Result.Nodes.getNodeAs<Stmt>("s");
- rewrite.InsertText(s->getLocStart(), "MC2_enterLoop();\n", true, true);
+ rewrite.InsertText(rewrite.getSourceMgr().getExpansionLoc(s->getLocStart()),
+ "MC2_enterLoop();\n", true, true);
// annotate all returns with MC2_exitLoop()
// annotate all breaks that aren't further nested with MC2_exitLoop().
// need to find all breaks and returns embedded inside the loop
- rewrite.InsertTextAfterToken(s->getLocEnd().getLocWithOffset(1), "\nMC2_exitLoop();\n");
+ rewrite.InsertTextAfterToken(rewrite.getSourceMgr().getExpansionLoc(s->getLocEnd().getLocWithOffset(1)),
+ "\nMC2_exitLoop();\n");
}
private:
place = op->getLocEnd().getLocWithOffset(1);
else
place = s->getLocEnd();
- rewrite.InsertText(place.getLocWithOffset(1), nol.str(), true, true);
+ rewrite.InsertText(rewrite.getSourceMgr().getExpansionLoc(place.getLocWithOffset(1)),
+ nol.str(), true, true);
updateProvisionalName(DeferredUpdates, lhs, mcVar);
}
// record vars used in conditions
class BranchConditionRefactoringHandler : public MatchFinder::MatchCallback {
public:
- BranchConditionRefactoringHandler(Rewriter &Rewrite,
+ BranchConditionRefactoringHandler(Rewriter &rewrite,
std::set<const NamedDecl *> & DeclsInCond,
std::map<const NamedDecl *, std::string> &DeclToMCVar,
std::map<const Expr *, std::string> &ExprToMCVar,
std::map<const Expr *, SourceLocation> &Redirector,
std::vector<Update *> &DeferredUpdates) :
- Rewrite(Rewrite), DeclsInCond(DeclsInCond), DeclToMCVar(DeclToMCVar),
+ rewrite(rewrite), DeclsInCond(DeclsInCond), DeclToMCVar(DeclToMCVar),
ExprToMCVar(ExprToMCVar), Redirector(Redirector), DeferredUpdates(DeferredUpdates) {}
virtual void run(const MatchFinder::MatchResult &Result) {
// e.g. int _cond0 = x == y;
std::string SStr;
llvm::raw_string_ostream S(SStr);
- bc->printPretty(S, nullptr, Rewrite.getLangOpts());
+ bc->printPretty(S, nullptr, rewrite.getLangOpts());
const std::string &Str = S.str();
std::stringstream prel;
}
ExprToMCVar[cond] = condVarEncoded.str();
- Rewrite.InsertText(is->getLocStart(), prel.str(), false, true);
+ rewrite.InsertText(rewrite.getSourceMgr().getExpansionLoc(is->getLocStart()),
+ prel.str(), false, true);
// rewrite the binary op with the newly-inserted var
Expr * RO = bc->getRHS(); // used for location only
- int cl = Lexer::MeasureTokenLength(RO->getLocStart(), Rewrite.getSourceMgr(), Rewrite.getLangOpts());
- SourceRange SR(cond->getLocStart(), Rewrite.getSourceMgr().getExpansionLoc(RO->getLocStart()).getLocWithOffset(cl-1));
- Rewrite.ReplaceText(SR, condVar);
+ int cl = Lexer::MeasureTokenLength(RO->getLocStart(), rewrite.getSourceMgr(), rewrite.getLangOpts());
+ SourceRange SR(cond->getLocStart(), rewrite.getSourceMgr().getExpansionLoc(RO->getLocStart()).getLocWithOffset(cl-1));
+ rewrite.ReplaceText(SR, condVar);
} else {
std::string condVar = encodeCond(condCount++);
std::stringstream condVarEncoded;
std::string SStr;
llvm::raw_string_ostream S(SStr);
- cond->printPretty(S, nullptr, Rewrite.getLangOpts());
+ cond->printPretty(S, nullptr, rewrite.getLangOpts());
const std::string &Str = S.str();
std::stringstream prel;
// rewrite the call op with the newly-inserted var
SourceRange SR(cond->getLocStart(), cond->getLocEnd());
Redirector[cond] = is->getLocStart();
- Rewrite.ReplaceText(SR, condVar);
+ rewrite.ReplaceText(SR, condVar);
}
std::deque<const Decl *> q;
}
private:
- Rewriter &Rewrite;
+ Rewriter &rewrite;
std::set<const NamedDecl *> & DeclsInCond;
std::map<const NamedDecl *, std::string> &DeclToMCVar;
std::map<const Expr *, std::string> &ExprToMCVar;
BranchAnnotationHandler(Rewriter &rewrite,
std::map<const NamedDecl *, std::string> & DeclToMCVar,
std::map<const Expr *, std::string> & ExprToMCVar)
- : Rewrite(rewrite),
+ : rewrite(rewrite),
DeclToMCVar(DeclToMCVar),
ExprToMCVar(ExprToMCVar){}
virtual void run(const MatchFinder::MatchResult &Result) {
std::stringstream brline;
brline << "MCID " << brVar << ";\n";
- Rewrite.InsertText(is->getLocStart(), brline.str(), false, true);
+ rewrite.InsertText(rewrite.getSourceMgr().getExpansionLoc(is->getLocStart()),
+ brline.str(), false, true);
Stmt * ts = is->getThen(), * es = is->getElse();
bool tHasChild = hasChild(ts);
mergeStmt << "\tMC2_merge(" << brVar << ");\n";
- Rewrite.InsertText(tfl, tlineStart.str(), false, true);
+ rewrite.InsertText(rewrite.getSourceMgr().getExpansionLoc(tfl), tlineStart.str(), false, true);
Stmt * tls = NULL;
int extra_else_offset = 0;
if (!tHasChild || (!isa<ReturnStmt>(tls) && !isa<BreakStmt>(tls))) {
extra_else_offset = 0;
- Rewrite.InsertText(tsl.getLocWithOffset(1), mergeStmt.str(), true, true);
+ rewrite.InsertText(rewrite.getSourceMgr().getExpansionLoc(tsl.getLocWithOffset(1)),
+ mergeStmt.str(), true, true);
}
if (tHasChild && !isa<CompoundStmt>(ts)) {
- Rewrite.InsertText(tls->getLocStart(), "{", false, true);
- SourceLocation tend = Lexer::getLocForEndOfToken(tls->getLocStart(), 0, Rewrite.getSourceMgr(), Rewrite.getLangOpts());
- Rewrite.InsertText(tend.getLocWithOffset(2), "}", true, true);
- extra_else_offset++;
+ rewrite.InsertText(rewrite.getSourceMgr().getFileLoc(tls->getLocStart()), "{", false, true);
+ SourceLocation tend = Lexer::findLocationAfterToken(tls->getLocEnd(), tok::semi, rewrite.getSourceMgr(), rewrite.getLangOpts(), false);
+ rewrite.InsertText(tend, "}", true, true);
}
if (tHasChild && isa<CompoundStmt>(ts)) extra_else_offset++;
}
} else
el = es->getLocStart().getLocWithOffset(1);
- Rewrite.InsertText(el, eline.str(), false, true);
+ rewrite.InsertText(rewrite.getSourceMgr().getExpansionLoc(el), eline.str(), false, true);
if (eHasChild && !isa<CompoundStmt>(es)) {
- Rewrite.InsertText(el, "{", false, true);
- Rewrite.InsertText(es->getLocEnd().getLocWithOffset(1), "}", true, true);
+ rewrite.InsertText(rewrite.getSourceMgr().getExpansionLoc(el), "{", false, true);
+ rewrite.InsertText(rewrite.getSourceMgr().getExpansionLoc(es->getLocEnd().getLocWithOffset(1)), "}", true, true);
}
if (!eHasChild || (!isa<ReturnStmt>(els) && !isa<BreakStmt>(els)))
- Rewrite.InsertText(esl.getLocWithOffset(1), mergeStmt.str(), true, true);
+ rewrite.InsertText(rewrite.getSourceMgr().getExpansionLoc(esl.getLocWithOffset(1)), mergeStmt.str(), true, true);
}
else {
std::stringstream eCompoundLine;
eCompoundLine << " else { " << eline.str() << mergeStmt.str() << " }";
- SourceLocation tend = Lexer::getLocForEndOfToken(ts->getLocEnd(), 0, Rewrite.getSourceMgr(), Rewrite.getLangOpts());
- Rewrite.InsertText(tend.getLocWithOffset(extra_else_offset),
- eCompoundLine.str(), false, true);
+ SourceLocation tend = Lexer::findLocationAfterToken(ts->getLocEnd(), tok::semi, rewrite.getSourceMgr(), rewrite.getLangOpts(), false);
+ if (!tend.isValid())
+ tend = Lexer::getLocForEndOfToken(ts->getLocEnd(), 0, rewrite.getSourceMgr(), rewrite.getLangOpts());
+ rewrite.InsertText(tend.getLocWithOffset(1), eCompoundLine.str(), false, true);
}
}
private:
return s;
}
- Rewriter &Rewrite;
+ Rewriter &rewrite;
std::map<const NamedDecl *, std::string> &DeclToMCVar;
std::map<const Expr *, std::string> &ExprToMCVar;
};
std::stringstream brline;
brline << "MCID " << mc_rv << ";\n";
- rewrite.InsertText(s->getLocStart(), brline.str(), false, true);
+ rewrite.InsertText(rewrite.getSourceMgr().getExpansionLoc(s->getLocStart()),
+ brline.str(), false, true);
std::stringstream nol;
if (ce->getNumArgs() > 0) nol << ", ";
nol << "&" << mc_rv;
- rewrite.InsertTextBefore(ce->getRParenLoc(), nol.str());
+ rewrite.InsertTextBefore(rewrite.getSourceMgr().getExpansionLoc(ce->getRParenLoc()),
+ nol.str());
if (s && (ds = dyn_cast<DeclStmt>(s))) {
if (!ds->isSingleDecl()) {
nol << aa << ", ";
if (a->getLocEnd().isValid())
- rewrite.InsertTextBefore(a->getLocStart(), nol.str());
+ rewrite.InsertTextBefore(rewrite.getSourceMgr().getExpansionLoc(a->getLocStart()),
+ nol.str());
}
}
}
std::stringstream nol;
nol << "*retval = " << mrv << ";\n";
- rewrite.InsertText(rs->getLocStart(), nol.str(), false, true);
+ rewrite.InsertText(rewrite.getSourceMgr().getExpansionLoc(rs->getLocStart()),
+ nol.str(), false, true);
}
private:
nol << "MCID " << dn << "; ";
if (d->getLocStart().isValid())
- rewrite.InsertTextBefore(d->getLocStart(), nol.str());
+ rewrite.InsertTextBefore(rewrite.getSourceMgr().getExpansionLoc(d->getLocStart()),
+ nol.str());
}
private:
std::stringstream nol;
nol << "MCID " << encode(p->getName()) << ", ";
if (p->getLocStart().isValid())
- rewrite.InsertText(p->getLocStart(), nol.str(), false);
+ rewrite.InsertText(rewrite.getSourceMgr().getExpansionLoc(p->getLocStart()),
+ nol.str(), false);
if (p->getLocEnd().isValid())
LastParam = p->getLocEnd().getLocWithOffset(p->getName().size());
}
std::stringstream nol;
if (fd->param_size() > 0) nol << ", ";
nol << "MCID * retval";
- rewrite.InsertText(LastParam, nol.str(), false);
+ rewrite.InsertText(rewrite.getSourceMgr().getExpansionLoc(LastParam),
+ nol.str(), false);
}
}
MatcherSanity.matchAST(Context);
for (auto & u : DeferredUpdates) {
- R.InsertText(u->loc, u->update, true, true);
+ R.InsertText(R.getSourceMgr().getExpansionLoc(u->loc), u->update, true, true);
delete u;
}
DeferredUpdates.clear();