@@ -28,6 +28,18 @@ using namespace llvm;
28
28
29
29
namespace {
30
30
31
+ static std::string remapPath (std::map<llvm::StringRef, llvm::StringRef, std::greater<llvm::StringRef>>
32
+ &PrefixMap, llvm::StringRef Path) {
33
+ if (PrefixMap.empty ())
34
+ return Path.str ();
35
+
36
+ SmallString<256 > P = Path;
37
+ for (const auto &Entry : PrefixMap)
38
+ if (llvm::sys::path::replace_path_prefix (P, Entry.first , Entry.second ))
39
+ break ;
40
+ return P.str ().str ();
41
+ }
42
+
31
43
typedef function_ref<bool (const IndexUnitReader::DependencyInfo &)> DependencyReceiver;
32
44
typedef function_ref<bool (const IndexUnitReader::IncludeInfo &)> IncludeReceiver;
33
45
@@ -43,14 +55,15 @@ class IndexUnitReaderImpl {
43
55
bool IsSystemUnit;
44
56
bool IsModuleUnit;
45
57
bool IsDebugCompilation;
46
- StringRef WorkingDir;
47
- StringRef OutputFile;
48
- StringRef SysrootPath;
58
+ SmallString< 128 > WorkingDir;
59
+ SmallString< 128 > OutputFile;
60
+ SmallString< 128 > SysrootPath;
49
61
StringRef ModuleName;
50
62
SmallString<128 > MainFilePath;
51
63
StringRef Target;
52
64
std::vector<FileBitPath> Paths;
53
65
StringRef PathsBuffer;
66
+ std::map<llvm::StringRef, llvm::StringRef, std::greater<llvm::StringRef>> PrefixMap;
54
67
55
68
struct ModuleInfo {
56
69
unsigned NameOffset;
@@ -60,15 +73,16 @@ class IndexUnitReaderImpl {
60
73
StringRef ModuleNamesBuffer;
61
74
62
75
bool init (std::unique_ptr<MemoryBuffer> Buf, sys::TimePoint<> ModTime,
76
+ std::map<llvm::StringRef, llvm::StringRef, std::greater<llvm::StringRef>> PrefixMap,
63
77
std::string &Error);
64
78
65
79
StringRef getProviderIdentifier () const { return ProviderIdentifier; }
66
80
StringRef getProviderVersion () const { return ProviderVersion; }
67
81
68
82
sys::TimePoint<> getModificationTime () const { return ModTime; }
69
- StringRef getWorkingDirectory () const { return WorkingDir; }
70
- StringRef getOutputFile () const { return OutputFile; }
71
- StringRef getSysrootPath () const { return SysrootPath; }
83
+ StringRef getWorkingDirectory () const { return WorkingDir. str () ; }
84
+ StringRef getOutputFile () const { return OutputFile. str () ; }
85
+ StringRef getSysrootPath () const { return SysrootPath. str () ; }
72
86
StringRef getTarget () const { return Target; }
73
87
74
88
StringRef getModuleName () const { return ModuleName; }
@@ -88,6 +102,10 @@ class IndexUnitReaderImpl {
88
102
return PathsBuffer.substr (Offset, Size);
89
103
}
90
104
105
+ std::string getAndRemapPathFromBuffer (size_t Offset, size_t Size) {
106
+ return remapPath (PrefixMap, getPathFromBuffer (Offset, Size));
107
+ }
108
+
91
109
void constructFilePath (SmallVectorImpl<char > &Path, int PathIndex);
92
110
93
111
StringRef getModuleName (int ModuleIndex);
@@ -205,9 +223,9 @@ class IndexUnitBitstreamVisitor : public BitstreamVisitor<IndexUnitBitstreamVisi
205
223
break ;
206
224
case UNIT_PATH_BUFFER:
207
225
Reader.PathsBuffer = Blob;
208
- Reader.WorkingDir = Reader.getPathFromBuffer (WorkDirOffset, WorkDirSize);
209
- Reader.OutputFile = Reader.getPathFromBuffer (OutputFileOffset, OutputFileSize);
210
- Reader.SysrootPath = Reader.getPathFromBuffer (SysrootOffset, SysrootSize);
226
+ Reader.WorkingDir = Reader.getAndRemapPathFromBuffer (WorkDirOffset, WorkDirSize);
227
+ Reader.OutputFile = Reader.getAndRemapPathFromBuffer (OutputFileOffset, OutputFileSize);
228
+ Reader.SysrootPath = Reader.getAndRemapPathFromBuffer (SysrootOffset, SysrootSize);
211
229
212
230
// now we can populate the main file's path
213
231
Reader.constructFilePath (Reader.MainFilePath , MainPathIndex);
@@ -270,9 +288,12 @@ class IndexUnitBlockBitstreamVisitor : public BitstreamVisitor<IndexUnitBlockBit
270
288
} // anonymous namespace
271
289
272
290
bool IndexUnitReaderImpl::init (std::unique_ptr<MemoryBuffer> Buf,
273
- sys::TimePoint<> ModTime, std::string &Error) {
291
+ sys::TimePoint<> ModTime,
292
+ std::map<llvm::StringRef, llvm::StringRef, std::greater<llvm::StringRef>> PrefixMap,
293
+ std::string &Error) {
274
294
this ->ModTime = ModTime;
275
295
this ->MemBuf = std::move (Buf);
296
+ this ->PrefixMap = PrefixMap;
276
297
llvm::BitstreamCursor Stream (*MemBuf);
277
298
278
299
if (Stream.AtEndOfStream ()) {
@@ -374,6 +395,13 @@ void IndexUnitReaderImpl::constructFilePath(SmallVectorImpl<char> &PathBuf,
374
395
sys::path::append (PathBuf,
375
396
getPathFromBuffer (Path.Dir .Offset , Path.Dir .Size ),
376
397
getPathFromBuffer (Path.Filename .Offset , Path.Filename .Size ));
398
+ if (Path.PrefixKind == UNIT_PATH_PREFIX_NONE && !PrefixMap.empty ()) {
399
+ SmallString<256 > PathStr;
400
+ PathStr.assign (PathBuf);
401
+ std::string Remapped = remapPath (PrefixMap, PathStr.str ());
402
+ PathBuf.clear ();
403
+ PathBuf.append (Remapped.begin (), Remapped.end ());
404
+ }
377
405
}
378
406
379
407
StringRef IndexUnitReaderImpl::getModuleName (int ModuleIndex) {
@@ -391,15 +419,18 @@ StringRef IndexUnitReaderImpl::getModuleName(int ModuleIndex) {
391
419
std::unique_ptr<IndexUnitReader>
392
420
IndexUnitReader::createWithUnitFilename (StringRef UnitFilename,
393
421
StringRef StorePath,
422
+ std::map<llvm::StringRef, llvm::StringRef, std::greater<llvm::StringRef>> PrefixMap,
394
423
std::string &Error) {
395
424
SmallString<128 > PathBuf = StorePath;
396
425
appendUnitSubDir (PathBuf);
397
426
sys::path::append (PathBuf, UnitFilename);
398
- return createWithFilePath (PathBuf.str (), Error);
427
+ return createWithFilePath (PathBuf.str (), PrefixMap, Error);
399
428
}
400
429
401
430
std::unique_ptr<IndexUnitReader>
402
- IndexUnitReader::createWithFilePath (StringRef FilePath, std::string &Error) {
431
+ IndexUnitReader::createWithFilePath (StringRef FilePath,
432
+ std::map<llvm::StringRef, llvm::StringRef, std::greater<llvm::StringRef>> PrefixMap,
433
+ std::string &Error) {
403
434
int FD;
404
435
std::error_code EC = sys::fs::openFileForRead (FilePath, FD);
405
436
if (EC) {
@@ -435,7 +466,7 @@ IndexUnitReader::createWithFilePath(StringRef FilePath, std::string &Error) {
435
466
436
467
std::unique_ptr<IndexUnitReaderImpl> Impl (new IndexUnitReaderImpl ());
437
468
bool Err = Impl->init (std::move (*ErrOrBuf), FileStat.getLastModificationTime (),
438
- Error);
469
+ PrefixMap, Error);
439
470
if (Err)
440
471
return nullptr ;
441
472
0 commit comments