Commit 40ea9c95 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Generate tracebacks correctly for 'raise' statements

parent 14b247f7
......@@ -637,6 +637,9 @@ private:
AST_expr* remapLangPrimitive(AST_LangPrimitive* node) {
AST_LangPrimitive* rtn = new AST_LangPrimitive(node->opcode);
rtn->col_offset = node->col_offset;
rtn->lineno = node->lineno;
for (AST_expr* arg : node->args) {
rtn->args.push_back(remapExpr(arg));
}
......@@ -841,6 +844,8 @@ public:
AST_Invoke* invoke = new AST_Invoke(node);
invoke->normal_dest = normal_dest;
invoke->exc_dest = exc_dest;
invoke->col_offset = node->col_offset;
invoke->lineno = node->lineno;
curblock->push_back(invoke);
curblock->connectTo(normal_dest);
......@@ -1015,11 +1020,15 @@ public:
s_target->value = remapExpr(s->value);
s_target->slice = remapExpr(s->slice);
s_target->ctx_type = AST_TYPE::Store;
s_target->col_offset = s->col_offset;
s_target->lineno = s->lineno;
remapped_target = s_target;
AST_Subscript* s_lhs = new AST_Subscript();
s_lhs->value = s_target->value;
s_lhs->slice = s_target->slice;
s_lhs->col_offset = s->col_offset;
s_lhs->lineno = s->lineno;
s_lhs->ctx_type = AST_TYPE::Load;
remapped_lhs = remapExpr(s_lhs);
......@@ -1033,12 +1042,16 @@ public:
a_target->value = remapExpr(a->value);
a_target->attr = a->attr;
a_target->ctx_type = AST_TYPE::Store;
a_target->col_offset = a->col_offset;
a_target->lineno = a->lineno;
remapped_target = a_target;
AST_Attribute* a_lhs = new AST_Attribute();
a_lhs->value = a_target->value;
a_lhs->attr = a->attr;
a_lhs->ctx_type = AST_TYPE::Load;
a_lhs->col_offset = a->col_offset;
a_lhs->lineno = a->lineno;
remapped_lhs = remapExpr(a_lhs);
break;
......@@ -1051,6 +1064,8 @@ public:
binop->op_type = node->op_type;
binop->left = remapped_lhs;
binop->right = remapExpr(node->value);
binop->col_offset = node->col_offset;
binop->lineno = node->lineno;
AST_stmt* assign = makeAssign(remapped_target, binop);
push_back(assign);
return true;
......@@ -1097,6 +1112,7 @@ public:
int i = 0;
for (auto v : node->values) {
AST_Print* remapped = new AST_Print();
remapped->col_offset = node->col_offset;
remapped->lineno = node->lineno;
// TODO not good to reuse 'dest' like this
remapped->dest = dest;
......@@ -1116,6 +1132,8 @@ public:
assert(node->nl);
AST_Print* final = new AST_Print();
final->col_offset = node->col_offset;
final->lineno = node->lineno;
// TODO not good to reuse 'dest' like this
final->dest = dest;
final->nl = node->nl;
......@@ -1405,6 +1423,9 @@ public:
bool visit_raise(AST_Raise* node) override {
AST_Raise* remapped = new AST_Raise();
remapped->col_offset = node->col_offset;
remapped->lineno = node->lineno;
if (node->arg0)
remapped->arg0 = remapExpr(node->arg0);
if (node->arg1)
......
......@@ -28,8 +28,6 @@ class BoxedInt;
class BoxedList;
class BoxedString;
// "raw" raising function that will engage the unwinding machinery
void raiseRaw(Box*) __attribute__((__noreturn__));
// user-level raise function that implements some python-level semantics
extern "C" void raise1(Box*) __attribute__((__noreturn__));
extern "C" void raise2(Box*, Box*) __attribute__((__noreturn__));
......
......@@ -99,7 +99,15 @@ void unwindExc(Box* exc_obj) {
abort();
}
void raiseRaw(Box* exc_obj) {
static std::vector<const LineInfo*> getTracebackEntries();
static std::vector<const LineInfo*> last_tb;
void raiseExc(Box* exc_obj) __attribute__((__noreturn__));
void raiseExc(Box* exc_obj) {
auto entries = getTracebackEntries();
last_tb = std::move(entries);
// Using libgcc:
throw exc_obj;
......@@ -109,7 +117,6 @@ void raiseRaw(Box* exc_obj) {
abort();
}
static std::vector<const LineInfo*> last_tb;
void printLastTraceback() {
fprintf(stderr, "Traceback (most recent call last):\n");
......@@ -188,7 +195,7 @@ void raise1(Box* b) {
BoxedClass* c = static_cast<BoxedClass*>(b);
if (isSubclass(c, Exception)) {
auto exc_obj = exceptionNew1(c);
raiseRaw(exc_obj);
raiseExc(exc_obj);
} else {
raiseExcHelper(TypeError, "exceptions must be old-style classes or derived from BaseException, not %s",
getTypeName(b)->c_str());
......@@ -197,13 +204,10 @@ void raise1(Box* b) {
// TODO: should only allow throwing of old-style classes or things derived
// from BaseException:
raiseRaw(b);
raiseExc(b);
}
void raiseExcHelper(BoxedClass* cls, const char* msg, ...) {
auto entries = getTracebackEntries();
last_tb = std::move(entries);
if (msg != NULL) {
va_list ap;
va_start(ap, msg);
......@@ -220,10 +224,10 @@ void raiseExcHelper(BoxedClass* cls, const char* msg, ...) {
BoxedString* message = boxStrConstant(buf);
Box* exc_obj = exceptionNew2(cls, message);
raiseRaw(exc_obj);
raiseExc(exc_obj);
} else {
Box* exc_obj = exceptionNew1(cls);
raiseRaw(exc_obj);
raiseExc(exc_obj);
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment