diff --git a/llvm/include/llvm/Support/GraphWriter.h b/llvm/include/llvm/Support/GraphWriter.h --- a/llvm/include/llvm/Support/GraphWriter.h +++ b/llvm/include/llvm/Support/GraphWriter.h @@ -318,6 +318,9 @@ return O; } +static std::string replace_illegal_filename_chars(std::string filename, + const char replacement_char); + std::string createGraphFilename(const Twine &Name, int &FD); /// Writes graph into a provided {@code Filename}. diff --git a/llvm/lib/Support/GraphWriter.cpp b/llvm/lib/Support/GraphWriter.cpp --- a/llvm/lib/Support/GraphWriter.cpp +++ b/llvm/lib/Support/GraphWriter.cpp @@ -76,10 +76,32 @@ return Colors[ColorNumber % NumColors]; } +static std::string +llvm::replace_illegal_filename_chars(std::string filename, + const char replacement_char) { +#ifdef _WIN32 + std::string illegalChars = "\\/:?\"<>|"; +#else + std::string illegalChars = "/"; +#endif + + for (std::string::iterator it = illegalChars.begin(); it < illegalChars.end(); + ++it) { + std::replace(filename.begin(), filename.end(), *it, replacement_char); + } + + return filename; +} + std::string llvm::createGraphFilename(const Twine &Name, int &FD) { FD = -1; SmallString<128> Filename; - std::error_code EC = sys::fs::createTemporaryFile(Name, "dot", FD, Filename); + + // Replace illegal characters in graph filename with '_' if needed + std::string CleansedName = replace_illegal_filename_chars(Name.str(), '_'); + + std::error_code EC = + sys::fs::createTemporaryFile(CleansedName, "dot", FD, Filename); if (EC) { errs() << "Error: " << EC.message() << "\n"; return "";