Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * info.c: filename/linenumber information for parser/interpreter |
3 | | * |
4 | | * Copyright (C) 2007-2016 David Lutterkort |
5 | | * |
6 | | * This library is free software; you can redistribute it and/or |
7 | | * modify it under the terms of the GNU Lesser General Public |
8 | | * License as published by the Free Software Foundation; either |
9 | | * version 2.1 of the License, or (at your option) any later version. |
10 | | * |
11 | | * This library is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | | * Lesser General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU Lesser General Public |
17 | | * License along with this library; if not, write to the Free Software |
18 | | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 | | * |
20 | | * Author: David Lutterkort <lutter@redhat.com> |
21 | | */ |
22 | | |
23 | | #include <config.h> |
24 | | #include <stdbool.h> |
25 | | |
26 | | #include "info.h" |
27 | | #include "internal.h" |
28 | | #include "memory.h" |
29 | | #include "ref.h" |
30 | | #include "errcode.h" |
31 | | |
32 | | /* |
33 | | * struct string |
34 | | */ |
35 | 0 | struct string *make_string(char *str) { |
36 | 0 | struct string *string; |
37 | 0 | make_ref(string); |
38 | 0 | string->str = str; |
39 | 0 | return string; |
40 | 0 | } |
41 | | |
42 | 1.52k | struct string *dup_string(const char *str) { |
43 | 1.52k | struct string *string; |
44 | 1.52k | make_ref(string); |
45 | 1.52k | if (str == NULL) |
46 | 0 | string->str = strdup(""); |
47 | 1.52k | else |
48 | 1.52k | string->str = strdup(str); |
49 | 1.52k | if (string->str == NULL) |
50 | 0 | unref(string, string); |
51 | 1.52k | return string; |
52 | 1.52k | } |
53 | | |
54 | 173k | void free_string(struct string *string) { |
55 | 173k | if (string == NULL) |
56 | 0 | return; |
57 | 173k | assert(string->ref == 0); |
58 | 173k | free(string->str); |
59 | 173k | free(string); |
60 | 173k | } |
61 | | |
62 | | /* |
63 | | * struct info |
64 | | */ |
65 | 0 | char *format_info(struct info *info) { |
66 | 0 | const char *fname; |
67 | 0 | char *result = NULL; |
68 | 0 | int r = 0; |
69 | |
|
70 | 0 | if (info == NULL) { |
71 | 0 | return strdup("(no file info)"); |
72 | 0 | } |
73 | | |
74 | 0 | int fl = info->first_line, ll = info->last_line; |
75 | 0 | int fc = info->first_column, lc = info->last_column; |
76 | 0 | fname = (info->filename != NULL) ? info->filename->str : "(unknown file)"; |
77 | |
|
78 | 0 | if (fl > 0) { |
79 | 0 | if (fl == ll) { |
80 | 0 | if (fc == lc) { |
81 | 0 | r = xasprintf(&result, "%s:%d.%d:", fname, fl, fc); |
82 | 0 | } else { |
83 | 0 | r = xasprintf(&result, "%s:%d.%d-.%d:", fname, fl, fc, lc); |
84 | 0 | } |
85 | 0 | } else { |
86 | 0 | r = xasprintf(&result, "%s:%d.%d-%d.%d:", fname, fl, fc, ll, lc); |
87 | 0 | } |
88 | 0 | } else { |
89 | 0 | r = xasprintf(&result, "%s:", fname); |
90 | 0 | } |
91 | 0 | return (r == -1) ? NULL : result; |
92 | 0 | } |
93 | | |
94 | 0 | void print_info(FILE *out, struct info *info) { |
95 | 0 | if (info == NULL) { |
96 | 0 | fprintf(out, "(no file info):"); |
97 | 0 | return; |
98 | 0 | } |
99 | 0 | fprintf(out, "%s:", |
100 | 0 | info->filename != NULL ? info->filename->str : "(unknown file)"); |
101 | 0 | if (info->first_line > 0) { |
102 | 0 | if (info->first_line == info->last_line) { |
103 | 0 | if (info->first_column == info->last_column) { |
104 | 0 | fprintf(out, "%d.%d:", info->first_line, info->first_column); |
105 | 0 | } else { |
106 | 0 | fprintf(out, "%d.%d-.%d:", info->first_line, |
107 | 0 | info->first_column, info->last_column); |
108 | 0 | } |
109 | 0 | } else { |
110 | 0 | fprintf(out, "%d.%d-%d.%d:", |
111 | 0 | info->first_line, info->first_column, |
112 | 0 | info->last_line, info->last_column); |
113 | 0 | } |
114 | 0 | } |
115 | 0 | } |
116 | | |
117 | 0 | bool typecheck_p(const struct info *info) { |
118 | 0 | return (info->error->aug->flags & AUG_TYPE_CHECK) != 0; |
119 | 0 | } |
120 | | |
121 | 50.2k | void free_info(struct info *info) { |
122 | 50.2k | if (info == NULL) |
123 | 0 | return; |
124 | 50.2k | assert(info->ref == 0); |
125 | 50.2k | unref(info->filename, string); |
126 | 50.2k | free(info); |
127 | 50.2k | } |
128 | | |
129 | 0 | struct span *make_span(struct info *info) { |
130 | 0 | struct span *span = NULL; |
131 | 0 | if (ALLOC(span) < 0) { |
132 | 0 | return NULL; |
133 | 0 | } |
134 | | /* UINT_MAX means span is not initialized yet */ |
135 | 0 | span->span_start = UINT_MAX; |
136 | 0 | span->filename = ref(info->filename); |
137 | 0 | return span; |
138 | 0 | } |
139 | | |
140 | 0 | void free_span(struct span *span) { |
141 | 0 | if (span == NULL) |
142 | 0 | return; |
143 | 0 | unref(span->filename, string); |
144 | 0 | free(span); |
145 | 0 | } |
146 | | |
147 | 0 | void print_span(struct span *span) { |
148 | 0 | if (span == NULL) |
149 | 0 | return; |
150 | 0 | printf("%s label=(%i:%i) value=(%i:%i) span=(%i,%i)\n", |
151 | 0 | span->filename->str, |
152 | 0 | span->label_start, span->label_end, |
153 | 0 | span->value_start, span->value_end, |
154 | 0 | span->span_start, span->span_end); |
155 | 0 | } |
156 | | |
157 | 0 | void update_span(struct span *node_info, int x, int y) { |
158 | 0 | if (node_info == NULL) |
159 | 0 | return; |
160 | 0 | if (node_info->span_start == UINT_MAX) { |
161 | 0 | node_info->span_start = x; |
162 | 0 | node_info->span_end = y; |
163 | 0 | } else { |
164 | 0 | if (node_info->span_start > x) |
165 | 0 | node_info->span_start = x; |
166 | 0 | if (node_info->span_end < y) |
167 | 0 | node_info->span_end = y; |
168 | 0 | } |
169 | 0 | } |
170 | | |
171 | | /* |
172 | | * Local variables: |
173 | | * indent-tabs-mode: nil |
174 | | * c-indent-level: 4 |
175 | | * c-basic-offset: 4 |
176 | | * tab-width: 4 |
177 | | * End: |
178 | | */ |