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

Generate tracebacks correctly for 'raise' statements

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