41 #ifdef ISPC_HOST_IS_LINUX 44 #elif defined(ISPC_HOST_IS_WINDOWS) 48 #define alloca _alloca 50 #endif // ISPC_HOST_IS_LINUX 56 #ifdef ISPC_HOST_IS_WINDOWS 62 #include <sys/ioctl.h> 64 #endif // ISPC_HOST_IS_WINDOWS 68 #include <llvm/IR/DataLayout.h> 80 #if defined(ISPC_HOST_IS_WINDOWS) 81 HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
82 if (h == INVALID_HANDLE_VALUE || h == NULL)
84 CONSOLE_SCREEN_BUFFER_INFO bufferInfo = {{0}};
85 GetConsoleScreenBufferInfo(h, &bufferInfo);
86 return bufferInfo.dwSize.X;
89 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) < 0)
92 #endif // ISPC_HOST_IS_WINDOWS 96 static bool r = (getenv(
"TERM") != NULL && strcmp(getenv(
"TERM"),
"dumb") != 0);
97 #ifndef ISPC_HOST_IS_WINDOWS 100 #endif // !ISPC_HOST_IS_WINDOWS 136 while (*buf !=
'\0' && !isspace(*buf))
150 FILE *f = fopen(p.
name,
"r");
155 while ((c = fgetc(f)) != EOF) {
189 while (*buf !=
'\0') {
190 if (*buf ==
'\033') {
191 while (*buf !=
'\0' && *buf !=
'm')
197 if (--numColons == 0)
211 #ifdef ISPC_HOST_IS_WINDOWS 216 int width = std::max(40, columnWidth - 2);
223 const char *msgPos = buf;
225 if (*msgPos ==
'\033') {
229 outStr.push_back(*msgPos++);
230 }
while (*msgPos !=
'\0' && *msgPos !=
'm');
232 }
else if (*msgPos ==
'\n') {
235 outStr.push_back(
'\n');
236 for (
int i = 0; i < indent; ++i)
237 outStr.push_back(
' ');
240 while (*msgPos ==
' ') {
241 outStr.push_back(
' ');
247 while (*msgPos !=
'\0' && isspace(*msgPos))
253 if (column > indent && column + wordEnd - msgPos > width) {
256 outStr.push_back(
'\n');
259 for (
int i = 0; i < indent; ++i)
260 outStr.push_back(
' ');
264 while (msgPos != wordEnd) {
265 outStr.push_back(*msgPos++);
268 outStr.push_back(
' ');
271 outStr.push_back(
'\n');
272 fputs(outStr.c_str(), out);
276 #ifdef ISPC_HOST_IS_WINDOWS 278 int vasprintf(
char **sptr,
const char *fmt, va_list argv) {
279 int wanted = vsnprintf(*sptr = NULL, 0, fmt, argv);
280 if ((wanted < 0) || ((*sptr = (
char *)malloc(1 + wanted)) == NULL))
283 return vsprintf(*sptr, fmt, argv);
286 int asprintf(
char **sptr,
const char *fmt, ...) {
290 retval = vasprintf(sptr, fmt, argv);
304 static void lPrint(
const char *type,
bool isError,
SourcePos p,
const char *fmt, va_list args) {
305 char *errorBuf, *formattedBuf;
306 if (vasprintf(&errorBuf, fmt, args) == -1) {
307 fprintf(stderr,
"vasprintf() unable to allocate memory!\n");
316 fprintf(stderr,
"asprintf() unable to allocate memory!\n");
325 fprintf(stderr,
"asprintf() unable to allocate memory!\n");
331 indent = std::min(indent, 8);
336 static std::set<std::string> printed;
337 if (printed.find(formattedBuf) != printed.end()) {
342 printed.insert(formattedBuf);
362 lPrint(
"Error",
true, p, fmt, args);
367 #ifndef ISPC_NO_DUMPS 373 lPrint(
"Debug",
false, p, fmt, args);
380 std::map<std::pair<int, std::string>,
bool>::iterator turnOffWarnings_it =
382 if ((turnOffWarnings_it !=
g->
turnOffWarnings.end()) && (turnOffWarnings_it->second ==
false))
398 std::string stdlibFile =
"stdlib.ispc";
399 std::string sourcePosName = p.
name;
401 (sourcePosName.length() >= stdlibFile.length() &&
402 sourcePosName.compare(sourcePosName.length() - stdlibFile.length(), stdlibFile.length(), stdlibFile) == 0) ||
406 std::map<std::pair<int, std::string>,
bool>::iterator turnOffWarnings_it =
416 lPrint(
"Performance Warning",
false, p, fmt, args);
421 static bool printed =
false;
426 fprintf(stderr,
"***\n" 427 "*** Please file a bug report at https://github.com/ispc/ispc/issues\n" 428 "*** (Including as much information as you can about how to " 429 "reproduce this error).\n" 430 "*** You have apparently encountered a bug in the compiler that we'd " 431 "like to fix!\n***\n");
434 [[noreturn]]
void FatalError(
const char *file,
int line,
const char *message) {
435 fprintf(stderr,
"%s(%d): FATAL ERROR: %s\n", file, line, message);
440 void DoAssert(
const char *file,
int line,
const char *expr) {
441 fprintf(stderr,
"%s:%u: Assertion failed: \"%s\".\n", file, line, expr);
447 Error(pos,
"Assertion failed (%s:%u): \"%s\".", file, line, expr);
462 int n1 = (int)str1.size(), n2 = (int)str2.size();
463 int nmax = std::max(n1, n2);
465 int *current = (
int *)alloca((nmax + 1) *
sizeof(int));
466 int *previous = (
int *)alloca((nmax + 1) *
sizeof(int));
468 for (
int i = 0; i <= n2; ++i)
471 for (
int y = 1; y <= n1; ++y) {
475 for (
int x = 1; x <= n2; ++x) {
476 current[x] = std::min(previous[x - 1] + (str1[y - 1] == str2[x - 1] ? 0 : 1),
477 std::min(current[x - 1], previous[x]) + 1);
478 rowBest = std::min(rowBest, current[x]);
481 if (maxDist != 0 && rowBest > maxDist)
484 std::swap(current, previous);
490 std::vector<std::string>
MatchStrings(
const std::string &str,
const std::vector<std::string> &options) {
491 if (str.size() == 0 || (str.size() == 1 && !isalpha(str[0])))
493 return std::vector<std::string>();
495 const int maxDelta = 2;
496 std::vector<std::string> matches[maxDelta + 1];
501 for (
int i = 0; i < (int)options.size(); ++i) {
503 if (dist <= maxDelta)
504 matches[dist].push_back(options[i]);
509 for (
int i = 0; i <= maxDelta; ++i) {
510 if (matches[i].size())
513 return std::vector<std::string>();
517 std::string *directory, std::string *filename) {
518 #ifdef ISPC_HOST_IS_WINDOWS 520 const char *combPath = PathCombine(path, currentDirectory.c_str(), relativeName.c_str());
522 const char *filenamePtr = PathFindFileName(combPath);
523 *filename = filenamePtr;
524 *directory = std::string(combPath, filenamePtr - combPath);
530 std::string fullPath;
531 if (relativeName[0] ==
'/')
532 fullPath = relativeName;
535 if (fullPath[fullPath.size() - 1] !=
'/')
536 fullPath.push_back(
'/');
537 fullPath += relativeName;
541 const char *fp = fullPath.c_str();
542 const char *basenameStart = strrchr(fp,
'/');
543 Assert(basenameStart != NULL);
545 Assert(basenameStart[0] !=
'\0');
546 *filename = basenameStart;
547 *directory = std::string(fp, basenameStart - fp);
548 #endif // ISPC_HOST_IS_WINDOWS 552 std::set<std::string> result;
554 Assert(str.find(
'-') != str.npos);
556 size_t pos_prev = 0, pos;
558 pos = str.find(
'-', pos_prev);
559 std::string substr = str.substr(pos_prev, pos - pos_prev);
560 result.insert(substr);
563 }
while (pos != str.npos);
569 if (lib_dl.empty()) {
582 llvm::DataLayout d1(module_dl);
583 llvm::DataLayout d2(lib_dl);
585 std::string module_dl_canonic = d1.getStringRepresentation();
586 std::string lib_dl_canonic = d2.getStringRepresentation();
589 std::set<std::string> module_dl_set =
lGetStringArray(module_dl_canonic);
594 for (std::set<std::string>::iterator it = lib_dl_set.begin(); it != lib_dl_set.end(); ++it) {
599 std::set<std::string>::iterator module_match = std::find(module_dl_set.begin(), module_dl_set.end(), *it);
600 if (module_match == module_dl_set.end()) {
606 module_dl_set.erase(module_match);
611 for (std::set<std::string>::iterator it = module_dl_set.begin(); it != module_dl_set.end(); ++it) {
612 if ((*it)[0] ==
'v' || (*it)[0] ==
'f') {
622 Assert(filepath !=
nullptr);
623 if (!strcmp(filepath,
"-")) {
static const char * lStartBold()
static void lPrintBugText()
std::map< std::pair< int, std::string >, bool > turnOffWarnings
static const char * lStartRed()
std::vector< std::string > MatchStrings(const std::string &str, const std::vector< std::string > &options)
bool VerifyDataLayoutCompatibility(const std::string &module_dl, const std::string &lib_dl)
void FatalError(const char *file, int line, const char *message)
static const char * lResetColor()
void PerformanceWarning(SourcePos p, const char *fmt,...)
static void lPrintFileLineContext(SourcePos p)
void DoAssertPos(SourcePos pos, const char *file, int line, const char *expr)
char currentDirectory[1024]
void GetDirectoryAndFileName(const std::string ¤tDirectory, const std::string &relativeName, std::string *directory, std::string *filename)
static const char * lStartBlue()
Representation of a range of positions in a source file.
static bool lHaveANSIColors()
static const char * lFindWordEnd(const char *buf)
void Error(SourcePos p, const char *fmt,...)
int StringEditDistance(const std::string &str1, const std::string &str2, int maxDist)
static void lPrint(const char *type, bool isError, SourcePos p, const char *fmt, va_list args)
static int lFindIndent(int numColons, const char *buf)
void Debug(SourcePos p, const char *fmt,...)
void PrintWithWordBreaks(const char *buf, int indent, int columnWidth, FILE *out)
static std::set< std::string > lGetStringArray(const std::string &str)
Declaration of the Module class, which is the ispc-side representation of the results of compiling a ...
void Warning(SourcePos p, const char *fmt,...)
void DoAssert(const char *file, int line, const char *expr)
bool IsStdin(const char *filepath)