annotate lwasm/unicorns.c @ 226:7c2c2239ec9c

Make unicorns grok errors and warnings. Added unicorn formatted error and warning output and also a framework to handle errors which know which character position in the line they occurred in.
author William Astle <lost@l-w.ca>
date Sun, 15 Jul 2012 21:19:43 -0600
parents 3864d96ee8c7
children 042c7b74faf0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
222
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
1 /*
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
2 unicorns.c
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
3
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
4 Copyright © 2012 William Astle
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
5
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
6 This file is part of LWTOOLS.
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
7
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
9 terms of the GNU General Public License as published by the Free Software
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
10 Foundation, either version 3 of the License, or (at your option) any later
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
11 version.
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
12
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful, but WITHOUT
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
16 more details.
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
17
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License along with
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
19 this program. If not, see <http://www.gnu.org/licenses/>.
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
20 */
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
21
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
22 /*
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
23 This adds output to lwasm that is suitable for IDEs and other tools
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
24 that are interesting in the doings of the assembler.
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
25 */
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
26
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
27 #include <stdio.h>
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
28 #include <string.h>
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
29
224
3864d96ee8c7 Make unicorns notice referenced files better
William Astle <lost@l-w.ca>
parents: 223
diff changeset
30 #include "input.h"
222
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
31 #include "lwasm.h"
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
32 #include "lw_alloc.h"
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
33
223
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
34 static void print_urlencoding(FILE *stream, const char *string)
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
35 {
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
36 for ( ; *string; string++)
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
37 {
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
38 if (*string < 33 || *string > 126 || strchr("$&+,/:;=?@\"<>#%{}|\\^~[]`", *string))
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
39 {
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
40 fprintf(stream, "%%%02X", *string);
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
41 }
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
42 else
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
43 {
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
44 fputc(*string, stream);
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
45 }
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
46 }
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
47 }
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
48
226
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
49 static void show_unicorn_error(FILE *fp, line_t *l, lwasm_error_t *ee, const char *tag)
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
50 {
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
51 fprintf(fp, "%s: lineno=%d,filename=", tag, l -> lineno);
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
52 print_urlencoding(fp, l -> linespec);
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
53 fputs(",message=", fp);
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
54 print_urlencoding(fp, ee -> mess);
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
55 if (ee -> charpos > 0)
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
56 fprintf(fp, ",col=%d", ee -> charpos);
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
57 fputc('\n', fp);
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
58 }
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
59
222
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
60 void lwasm_do_unicorns(asmstate_t *as)
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
61 {
224
3864d96ee8c7 Make unicorns notice referenced files better
William Astle <lost@l-w.ca>
parents: 223
diff changeset
62 struct ifl *ifl;
222
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
63 macrotab_t *me;
223
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
64 structtab_t *se;
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
65 int i;
226
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
66 line_t *l;
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
67 lwasm_error_t *ee;
223
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
68
222
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
69 /* output file list */
224
3864d96ee8c7 Make unicorns notice referenced files better
William Astle <lost@l-w.ca>
parents: 223
diff changeset
70 for (ifl = ifl_head; ifl; ifl = ifl -> next)
222
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
71 {
223
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
72 fputs("RESOURCE: type=file,filename=", stdout);
224
3864d96ee8c7 Make unicorns notice referenced files better
William Astle <lost@l-w.ca>
parents: 223
diff changeset
73 print_urlencoding(stdout, ifl -> fn);
223
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
74 fputc('\n', stdout);
222
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
75 }
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
76
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
77 /* output macro list */
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
78 for (me = as -> macros; me; me = me -> next)
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
79 {
223
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
80 fprintf(stdout, "RESOURCE: type=macro,name=%s,lineno=%d,filename=", me -> name, me -> definedat -> lineno);
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
81 print_urlencoding(stdout, me -> definedat -> linespec);
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
82 fputs(",flags=", stdout);
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
83 if (me -> flags & macro_noexpand)
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
84 fputs("noexpand", stdout);
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
85 fputs(",def=", stdout);
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
86 for (i = 0; i < me -> numlines; i++)
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
87 {
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
88 if (i)
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
89 fputc(';', stdout);
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
90 print_urlencoding(stdout, me -> lines[i]);
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
91 }
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
92 fputc('\n', stdout);
222
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
93 }
223
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
94
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
95 /* output structure list */
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
96 for (se = as -> structs; se; se = se -> next)
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
97 {
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
98 fprintf(stdout, "RESOURCE: type=struct,name=%s,lineno=%d,filename=", se -> name, se -> definedat -> lineno);
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
99 print_urlencoding(stdout, se -> definedat -> linespec);
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
100 fputc('\n', stdout);
211fc8038b8d More unicorn stuff - structs and macros
William Astle <lost@l-w.ca>
parents: 222
diff changeset
101 }
226
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
102
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
103 /* output error and warning lists */
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
104 for (l = as -> line_head; l; l = l -> next)
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
105 {
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
106 if (l -> err)
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
107 {
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
108 for (ee = l -> err; ee; ee = ee -> next)
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
109 {
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
110 show_unicorn_error(stdout, l, ee, "ERROR");
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
111 }
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
112 }
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
113
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
114 if (l -> warn)
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
115 {
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
116 for (ee = l -> warn; ee; ee = ee -> next)
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
117 {
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
118 show_unicorn_error(stdout, l, ee, "WARNING");
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
119 }
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
120 }
7c2c2239ec9c Make unicorns grok errors and warnings.
William Astle <lost@l-w.ca>
parents: 224
diff changeset
121 }
222
03f7192fcd20 Add --unicorns option for IDE integration
William Astle <lost@l-w.ca>
parents:
diff changeset
122 }