commit 9b051fbda28cda30728dc5104d07a6af940f3fe3
parent bb88979fbbef70e5443cc1cb1ff90cdd53ac0e96
Author: Kyle Milz <kyle@0x30.net>
Date: Wed, 10 Aug 2016 17:33:40 -0600
src: add new rewrite error counter
Diffstat:
3 files changed, 20 insertions(+), 25 deletions(-)
diff --git a/src/check.in b/src/check.in
@@ -45,6 +45,7 @@ FINE[8]="Switch statements"
FINE[9]="Return statement values"
FINE[10]="Call expressions"
FINE[11]="Total statements"
+FINE[12]="Errors rewriting source"
fine_len=${#FINE[@]}
echo -n Checking .
diff --git a/src/inst_visitor.cc b/src/inst_visitor.cc
@@ -33,39 +33,27 @@ RewriteASTVisitor::VisitStmt(clang::Stmt *s)
if (clang::isa<clang::IfStmt>(s)) {
s = clang::cast<clang::IfStmt>(s)->getCond();
- if (modify_stmt(s) == false)
- return true;
- m_counters[IF_STMT]++;
+ modify_stmt(s, m_counters[IF_STMT]);
}
else if (clang::isa<clang::ForStmt>(s)) {
s = clang::cast<clang::ForStmt>(s)->getCond();
- if (modify_stmt(s) == false)
- return true;
- m_counters[FOR_STMT]++;
+ modify_stmt(s, m_counters[FOR_STMT]);
}
else if (clang::isa<clang::WhileStmt>(s)) {
s = clang::cast<clang::WhileStmt>(s)->getCond();
- if (modify_stmt(s) == false)
- return true;
- m_counters[WHILE_STMT]++;
+ modify_stmt(s, m_counters[WHILE_STMT]);
}
else if (clang::isa<clang::DoStmt>(s)) {
s = clang::cast<clang::DoStmt>(s)->getCond();
- if (modify_stmt(s) == false)
- return true;
- m_counters[DOWHILE_STMT]++;
+ modify_stmt(s, m_counters[DOWHILE_STMT]);
}
else if (clang::isa<clang::SwitchStmt>(s)) {
s = clang::cast<clang::SwitchStmt>(s)->getCond();
- if (modify_stmt(s) == false)
- return true;
- m_counters[SWITCH_STMT]++;
+ modify_stmt(s, m_counters[SWITCH_STMT]);
}
else if (clang::isa<clang::ReturnStmt>(s)) {
s = clang::cast<clang::ReturnStmt>(s)->getRetValue();
- if (modify_stmt(s) == false)
- return true;
- m_counters[RET_STMT_VAL]++;
+ modify_stmt(s, m_counters[RET_STMT_VAL]);
}
/*
else if (isa<BreakStmt>(s) || isa<ContinueStmt>(s) ||
@@ -75,29 +63,33 @@ RewriteASTVisitor::VisitStmt(clang::Stmt *s)
}
*/
else if (clang::isa<clang::CallExpr>(s)) {
- if (modify_stmt(s) == false)
- return true;
- m_counters[CALL_EXPR]++;
+ modify_stmt(s, m_counters[CALL_EXPR]);
}
return true;
}
bool
-RewriteASTVisitor::modify_stmt(clang::Stmt *s)
+RewriteASTVisitor::modify_stmt(clang::Stmt *s, int &counter)
{
if (s == NULL)
return false;
+ // If x = y is the original statement on line 19 then we try rewriting
+ // as (++citrun_lines[19], x = y).
std::stringstream ss;
ss << "(++_citrun_lines["
<< m_SM.getPresumedLineNumber(s->getLocStart()) - 1
<< "], ";
- if (m_TheRewriter.InsertTextBefore(s->getLocStart(), ss.str()))
+
+ if (m_TheRewriter.InsertTextBefore(s->getLocStart(), ss.str())) {
// writing failed, don't attempt to add ")"
+ m_counters[REWRITE_ERROR]++;
return false;
+ }
m_TheRewriter.InsertTextAfter(real_loc_end(s), ")");
+ counter++;
return true;
}
diff --git a/src/inst_visitor.h b/src/inst_visitor.h
@@ -13,6 +13,7 @@ enum counters {
RET_STMT_VAL,
CALL_EXPR,
TOTAL_STMT,
+ REWRITE_ERROR,
NCOUNTERS
};
@@ -30,7 +31,8 @@ public:
"Switch statements",
"Return statement values",
"Call expressions",
- "Total statements"
+ "Total statements",
+ "Errors rewriting source code"
}),
m_TheRewriter(R),
m_SM(R.getSourceMgr())
@@ -44,7 +46,7 @@ public:
std::array<std::string, NCOUNTERS> m_counter_descr;
private:
- bool modify_stmt(clang::Stmt *);
+ bool modify_stmt(clang::Stmt *, int &);
clang::SourceLocation real_loc_end(clang::Stmt *);
clang::Rewriter &m_TheRewriter;