Skip to content

Commit 777a67b

Browse files
PR#72453 : Exceeding maximum file name length (llvm#72654)
1 parent 4ed696c commit 777a67b

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

llvm/include/llvm/Analysis/DOTGraphTraitsPass.h

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#include "llvm/Analysis/CFGPrinter.h"
1717
#include "llvm/Support/FileSystem.h"
1818
#include "llvm/Support/GraphWriter.h"
19+
#include <unordered_set>
20+
21+
static std::unordered_set<std::string> nameObj;
1922

2023
namespace llvm {
2124

@@ -83,10 +86,28 @@ struct DOTGraphTraitsViewer
8386
StringRef Name;
8487
};
8588

89+
static void shortenFileName(std::string &FN, unsigned char len = 250) {
90+
91+
FN = FN.substr(0, len);
92+
93+
auto strLen = FN.length();
94+
while (strLen > 0) {
95+
if (auto it = nameObj.find(FN); it != nameObj.end()) {
96+
FN = FN.substr(0, --len);
97+
} else {
98+
nameObj.insert(FN);
99+
break;
100+
}
101+
strLen--;
102+
}
103+
}
104+
86105
template <typename GraphT>
87106
void printGraphForFunction(Function &F, GraphT Graph, StringRef Name,
88107
bool IsSimple) {
89-
std::string Filename = Name.str() + "." + F.getName().str() + ".dot";
108+
std::string Filename = Name.str() + "." + F.getName().str();
109+
shortenFileName(Filename);
110+
Filename = Filename + ".dot";
90111
std::error_code EC;
91112

92113
errs() << "Writing '" << Filename << "'...";
@@ -272,6 +293,7 @@ class DOTGraphTraitsModulePrinterWrapperPass : public ModulePass {
272293

273294
bool runOnModule(Module &M) override {
274295
GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
296+
shortenFileName(Name);
275297
std::string Filename = Name + ".dot";
276298
std::error_code EC;
277299

@@ -301,7 +323,9 @@ class DOTGraphTraitsModulePrinterWrapperPass : public ModulePass {
301323
template <typename GraphT>
302324
void WriteDOTGraphToFile(Function &F, GraphT &&Graph,
303325
std::string FileNamePrefix, bool IsSimple) {
304-
std::string Filename = FileNamePrefix + "." + F.getName().str() + ".dot";
326+
std::string Filename = FileNamePrefix + "." + F.getName().str();
327+
shortenFileName(Filename);
328+
Filename = Filename + ".dot";
305329
std::error_code EC;
306330

307331
errs() << "Writing '" << Filename << "'...";

0 commit comments

Comments
 (0)