From cfca06d7963fa0909f90483b42a6d7d194d01e08 Mon Sep 17 00:00:00 2001 From: Dimitry Andric Date: Sun, 26 Jul 2020 19:36:28 +0000 Subject: Vendor import of llvm-project master 2e10b7a39b9, the last commit before the llvmorg-12-init tag, from which release/11.x was branched. --- llvm/lib/Support/GraphWriter.cpp | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'llvm/lib/Support/GraphWriter.cpp') diff --git a/llvm/lib/Support/GraphWriter.cpp b/llvm/lib/Support/GraphWriter.cpp index c689a81925d4c..d8aae92603231 100644 --- a/llvm/lib/Support/GraphWriter.cpp +++ b/llvm/lib/Support/GraphWriter.cpp @@ -76,17 +76,42 @@ StringRef llvm::DOT::getColorString(unsigned ColorNumber) { return Colors[ColorNumber % NumColors]; } +static std::string replaceIllegalFilenameChars(std::string Filename, + const char ReplacementChar) { +#ifdef _WIN32 + std::string IllegalChars = "\\/:?\"<>|"; +#else + std::string IllegalChars = "/"; +#endif + + for (char IllegalChar : IllegalChars) { + std::replace(Filename.begin(), Filename.end(), IllegalChar, + ReplacementChar); + } + + 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); + + // Windows can't always handle long paths, so limit the length of the name. + std::string N = Name.str(); + N = N.substr(0, std::min(N.size(), 140)); + + // Replace illegal characters in graph Filename with '_' if needed + std::string CleansedName = replaceIllegalFilenameChars(N, '_'); + + std::error_code EC = + sys::fs::createTemporaryFile(CleansedName, "dot", FD, Filename); if (EC) { errs() << "Error: " << EC.message() << "\n"; return ""; } errs() << "Writing '" << Filename << "'... "; - return Filename.str(); + return std::string(Filename.str()); } // Execute the graph viewer. Return true if there were errors. @@ -147,7 +172,7 @@ static const char *getProgramName(GraphProgram::Name program) { bool llvm::DisplayGraph(StringRef FilenameRef, bool wait, GraphProgram::Name program) { - std::string Filename = FilenameRef; + std::string Filename = std::string(FilenameRef); std::string ErrMsg; std::string ViewerPath; GraphSession S; -- cgit v1.2.3