Permalink
Browse files

refactor warning() and reuse it in gencode.

  • Loading branch information...
1 parent 348a52e commit 11e86f0b2034ea1778209cc9056ec4a51f322c7b @kjs kjs committed Jun 25, 2012
Showing with 16 additions and 11 deletions.
  1. +6 −7 src/gencode.c
  2. +9 −4 src/semcheck.c
  3. +1 −0 src/semcheck.h
View
@@ -28,6 +28,8 @@ This happens in gencode_number().
#include "decl.h"
#include "instr.h"
+#include "semcheck.h" /* for warning(). */
+
#include "ann.h"
#define OUT stdout
@@ -168,11 +170,8 @@ unfreeze_registers(M1_compiler *comp, m1_symboltable *table) {
assert(comp->registers[type][regno] == REG_SYMBOL);
comp->registers[type][regno] = REG_UNUSED;
}
- else { /* if no register is allocated, it's an unused variable. Emit a warning*/
-
- fprintf(stderr, "%s:%d: warning: unused variable '%s'\n",
- comp->current_filename, iter->line, iter->name);
- ++comp->warnings;
+ else { /* if no register is allocated, it's an unused variable. Emit a warning*/
+ warning(comp, iter->line, "unused variable '%s'\n", iter->name);
}
iter = sym_iter_next(iter);
@@ -730,8 +729,8 @@ OBJECT_LINK------> L3
initialized yet. Emit a warning.
*/
if (!is_target) {
- fprintf(stderr, "%s:%d: warning: use of uninitialized variable '%s'\n",
- comp->current_filename, obj->sym->line, obj->sym->name);
+ warning(comp, obj->sym->line, "use of uninitialized variable '%s'\n",
+ obj->sym->name);
}
}
View
@@ -63,11 +63,16 @@ type_error(M1_compiler *comp, unsigned line, char *msg, ...) {
}
/* Emit a warning. */
-static void
-warning(M1_compiler *comp, unsigned line, char *msg) {
- assert(comp != NULL);
- fprintf(stderr, "%s:%d: warning: %s\n", comp->current_filename, line, msg);
+void
+warning(M1_compiler *comp, unsigned line, char *msg, ...) {
+ va_list argp;
++comp->warnings;
+
+ fprintf(stderr, "%s:%d: warning: ", comp->current_filename, line);
+ va_start(argp, msg);
+ vfprintf(stderr, msg, argp);
+ va_end(argp);
+ fprintf(stderr, "\n");
}
View
@@ -5,5 +5,6 @@
#include "ast.h"
extern void check(struct M1_compiler *comp, struct m1_chunk *ast);
+extern void warning(M1_compiler *comp, unsigned line, char *msg, ...);
#endif

0 comments on commit 11e86f0

Please sign in to comment.