-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.cpp
405 lines (347 loc) · 12 KB
/
util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
Copyright (c) 2010-2011, Intel Corporation
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** @file util.cpp
@brief Various small utility routines.
*/
#include "util.h"
#include "module.h"
#ifdef ISPC_IS_WINDOWS
#include <shlwapi.h>
#else
#include <alloca.h>
#endif
#include <stdio.h>
#include <stdio.h>
#include <assert.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#ifdef ISPC_IS_WINDOWS
#include <io.h>
#include <direct.h>
#include <windows.h>
#else
#include <sys/ioctl.h>
#include <unistd.h>
#include <errno.h>
#endif // ISPC_IS_WINDOWS
#include <set>
/** Returns the width of the terminal where the compiler is running.
Finding this out may fail in a variety of reasonable situations (piping
compiler output to 'less', redirecting output to a file, running the
compiler under a debuffer; in this case, just return a reasonable
default.
*/
static int
lTerminalWidth() {
#if defined(ISPC_IS_WINDOWS)
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
if (h == INVALID_HANDLE_VALUE || h == NULL)
return 80;
CONSOLE_SCREEN_BUFFER_INFO bufferInfo = { 0 };
GetConsoleScreenBufferInfo(h, &bufferInfo);
return bufferInfo.dwSize.X;
#else
struct winsize w;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) < 0)
return 80;
return w.ws_col;
#endif // ISPC_IS_WINDOWS
}
/** Given a pointer into a string, find the end of the current word and
return a pointer to its last character.
*/
static const char *
lFindWordEnd(const char *buf) {
while (*buf != '\0' && !isspace(*buf))
++buf;
return buf;
}
/** When printing error messages, we sometimes want to include the source
file line for context. This function print the line(s) of the file
corresponding to the provided SourcePos and underlines the range of the
SourcePos with '^' symbols.
*/
static void
lPrintFileLineContext(SourcePos p) {
if (p.first_line == 0)
return;
FILE *f = fopen(p.name, "r");
if (!f)
return;
int c, curLine = 1;
while ((c = fgetc(f)) != EOF) {
if (curLine >= p.first_line && curLine <= p.last_line)
fputc(c, stderr);
if (c == '\n')
++curLine;
if (curLine > p.last_line)
break;
}
int i = 1;
for (; i < p.first_column; ++i)
fputc(' ', stderr);
fputc('^', stderr);
++i;
for (; i < p.last_column; ++i)
fputc('^', stderr);
fputc('\n', stderr);
fputc('\n', stderr);
fclose(f);
}
/** Print the given string to the given FILE, assuming the given output
column width. Break words as needed to avoid words spilling past the
last column. */
static void
lPrintWithWordBreaks(const char *buf, int columnWidth, FILE *out) {
#ifdef ISPC_IS_WINDOWS
fputs(buf, out);
#else
int column = 0;
assert(strchr(buf, ':') != NULL);
int indent = strchr(buf, ':') - buf + 2;
int width = std::max(40, columnWidth - 2);
// Collect everything into a string and print it all at once at the end
// -> try to avoid troubles with mangled error messages with
// multi-threaded builds.
std::string outStr;
const char *msgPos = buf;
while (true) {
while (*msgPos != '\0' && isspace(*msgPos))
++msgPos;
if (*msgPos == '\0')
break;
const char *wordEnd = lFindWordEnd(msgPos);
if (column > indent && column + wordEnd - msgPos > width) {
// This word would overflow, so start a new line
column = indent;
outStr.push_back('\n');
// Indent to the same column as the ":" at the start of the
// message
for (int i = 0; i < indent; ++i)
outStr.push_back(' ');
}
// Finally go and copy the word
while (msgPos != wordEnd) {
outStr.push_back(*msgPos++);
++column;
}
outStr.push_back(' ');
++column;
}
outStr.push_back('\n');
fputs(outStr.c_str(), out);
#endif
}
/** Helper function for Error(), Warning(), etc.
@param type The type of message being printed (e.g. "Warning")
@param p Position in source file that is connected to the message
being printed
@param fmt printf()-style format string
@param args Arguments with values for format string % entries
*/
static void
lPrint(const char *type, SourcePos p, const char *fmt, va_list args) {
#ifdef ISPC_IS_WINDOWS
char errorBuf[2048], formattedBuf[2048];
if (vsnprintf_s(errorBuf, sizeof(errorBuf), _TRUNCATE, fmt, args) == -1) {
fprintf(stderr, "vsnprintf_s() error!\n");
return;
}
if (p.first_line == 0) {
// We don't have a valid SourcePos, so create a message without it
if (sprintf_s(formattedBuf, sizeof(formattedBuf), "%s: %s\n",
type, errorBuf) == -1) {
fprintf(stderr, "vsnprintf_s() error!\n");
exit(1);
}
}
else {
// Create an error message that includes the file and line number
if (sprintf_s(formattedBuf, sizeof(formattedBuf), "%s(%d): %s: %s\n",
p.name, p.first_line, type, errorBuf) == -1) {
fprintf(stderr, "vsnprintf_s() error!\n");
exit(1);
}
}
#else
char *errorBuf, *formattedBuf;
if (vasprintf(&errorBuf, fmt, args) == -1) {
fprintf(stderr, "vasprintf() unable to allocate memory!\n");
abort();
}
if (p.first_line == 0) {
// We don't have a valid SourcePos, so create a message without it
if (asprintf(&formattedBuf, "%s: %s\n", type, errorBuf) == -1) {
fprintf(stderr, "asprintf() unable to allocate memory!\n");
exit(1);
}
}
else {
// Create an error message that includes the file and line number
if (asprintf(&formattedBuf, "%s:%d:%d: %s: %s\n", p.name,
p.first_line, p.first_column, type, errorBuf) == -1) {
fprintf(stderr, "asprintf() unable to allocate memory!\n");
exit(1);
}
}
#endif
// Now that we've done all that work, see if we've already printed the
// exact same error message. If so, return, so we don't redundantly
// print it and annoy the user.
static std::set<std::string> printed;
if (printed.find(formattedBuf) != printed.end())
return;
printed.insert(formattedBuf);
lPrintWithWordBreaks(formattedBuf, lTerminalWidth(), stderr);
lPrintFileLineContext(p);
#ifndef ISPC_IS_WINDOWS
free(errorBuf);
free(formattedBuf);
#endif // !ISPC_IS_WINDOWS
}
void
Error(SourcePos p, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
lPrint("Error", p, fmt, args);
++m->errorCount;
va_end(args);
}
void
Debug(SourcePos p, const char *fmt, ...) {
if (!g->debugPrint)
return;
va_list args;
va_start(args, fmt);
lPrint("Debug", p, fmt, args);
va_end(args);
}
void
Warning(SourcePos p, const char *fmt, ...) {
if (g->disableWarnings)
return;
va_list args;
va_start(args, fmt);
lPrint("Warning", p, fmt, args);
va_end(args);
}
void
PerformanceWarning(SourcePos p, const char *fmt, ...) {
if (!g->emitPerfWarnings)
return;
va_list args;
va_start(args, fmt);
lPrint("Performance Warning", p, fmt, args);
va_end(args);
}
void
FatalError(const char *file, int line, const char *message) {
fprintf(stderr, "%s(%d): FATAL ERROR: %s\n", file, line, message);
abort();
}
///////////////////////////////////////////////////////////////////////////
// http://en.wikipedia.org/wiki/Levenshtein_distance
int
StringEditDistance(const std::string &str1, const std::string &str2, int maxDist) {
int n1 = (int)str1.size(), n2 = (int)str2.size();
int nmax = std::max(n1, n2);
int *current = (int *)alloca((nmax+1) * sizeof(int));
int *previous = (int *)alloca((nmax+1) * sizeof(int));
for (int i = 0; i <= n2; ++i)
previous[i] = i;
for (int y = 1; y <= n1; ++y) {
current[0] = y;
int rowBest = y;
for (int x = 1; x <= n2; ++x) {
current[x] = std::min(previous[x-1] + (str1[y-1] == str2[x-1] ? 0 : 1),
std::min(current[x-1], previous[x])+1);
rowBest = std::min(rowBest, current[x]);
}
if (maxDist != 0 && rowBest > maxDist)
return maxDist + 1;
std::swap(current, previous);
}
return previous[n2];
}
std::vector<std::string>
MatchStrings(const std::string &str, const std::vector<std::string> &options) {
const int maxDelta = 2;
std::vector<std::string> matches[maxDelta+1];
// For all of the options that are up to maxDelta edit distance, store
// them in the element of matches[] that corresponds to their edit
// distance.
for (int i = 0; i < (int)options.size(); ++i) {
int dist = StringEditDistance(str, options[i], maxDelta+1);
if (dist <= maxDelta)
matches[dist].push_back(options[i]);
}
// And return the first one of them, if any, that has at least one
// match.
for (int i = 0; i <= maxDelta; ++i) {
if (matches[i].size())
return matches[i];
}
return std::vector<std::string>();
}
void
GetDirectoryAndFileName(const std::string ¤tDirectory,
const std::string &relativeName,
std::string *directory, std::string *filename) {
#ifdef ISPC_IS_WINDOWS
char path[MAX_PATH];
const char *combPath = PathCombine(path, currentDirectory.c_str(),
relativeName.c_str());
assert(combPath != NULL);
const char *filenamePtr = PathFindFileName(combPath);
*filename = filenamePtr;
*directory = std::string(combPath, filenamePtr - combPath);
#else
// We need a fully qualified path. First, see if the current file name
// is fully qualified itself--in that case, the current working
// directory isn't needed.
// @todo This probably needs to be smarter for Windows...
std::string fullPath;
if (relativeName[0] == '/')
fullPath = relativeName;
else {
fullPath = g->currentDirectory;
if (fullPath[fullPath.size()-1] != '/')
fullPath.push_back('/');
fullPath += relativeName;
}
// now, we need to separate it into the base name and the directory
const char *fp = fullPath.c_str();
const char *basenameStart = strrchr(fp, '/');
assert(basenameStart != NULL);
++basenameStart;
assert(basenameStart != '\0');
*filename = basenameStart;
*directory = std::string(fp, basenameStart - fp);
#endif // ISPC_IS_WINDOWS
}