29
29
#include " llvm/Support/Compression.h"
30
30
#include " llvm/Support/Debug.h"
31
31
#include " llvm/Support/Error.h"
32
+ #include " llvm/Support/PrefixMapper.h"
32
33
#include " llvm/Support/SMLoc.h"
33
34
#include " llvm/Support/VirtualFileSystem.h"
34
35
#include " llvm/Support/YAMLTraits.h"
@@ -98,8 +99,9 @@ struct SerializedGeneratedFileInfo {
98
99
};
99
100
100
101
struct DiagnosticSerializer {
101
- DiagnosticSerializer (llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
102
- : SrcMgr(FS) {}
102
+ DiagnosticSerializer (llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS,
103
+ llvm::PrefixMapper &Mapper)
104
+ : SrcMgr(FS), Mapper(Mapper) {}
103
105
104
106
using ReplayFunc = llvm::function_ref<llvm::Error(const DiagnosticInfo &)>;
105
107
@@ -111,10 +113,11 @@ struct DiagnosticSerializer {
111
113
static llvm::Error
112
114
emitDiagnosticsFromCached (llvm::StringRef Buffer, SourceManager &SrcMgr,
113
115
DiagnosticEngine &Diags,
116
+ llvm::PrefixMapper &Mapper,
114
117
const FrontendInputsAndOutputs &InAndOut) {
115
118
// Create a new DiagnosticSerializer since this cannot be shared with a
116
119
// serialization instance.
117
- DiagnosticSerializer DS (SrcMgr.getFileSystem ());
120
+ DiagnosticSerializer DS (SrcMgr.getFileSystem (), Mapper );
118
121
DS.addInputsToSourceMgr (InAndOut);
119
122
return DS.doEmitFromCached (Buffer, Diags);
120
123
}
@@ -128,7 +131,7 @@ struct DiagnosticSerializer {
128
131
// has references to input files to find subconsumer.
129
132
auto addInputToSourceMgr = [&](const InputFile &Input) {
130
133
if (Input.getFileName () != " -" )
131
- SrcMgr.getExternalSourceBufferID (Input.getFileName ());
134
+ SrcMgr.getExternalSourceBufferID (remapFilePath ( Input.getFileName () ));
132
135
return false ;
133
136
};
134
137
InAndOut.forEachInputProducingSupplementaryOutput (addInputToSourceMgr);
@@ -163,6 +166,9 @@ struct DiagnosticSerializer {
163
166
unsigned deserializeFile (const SerializedFile &File);
164
167
llvm::Error deserializeVirtualFile (const SerializedVirtualFile &VF);
165
168
llvm::Error deserializeGeneratedFileInfo (const SerializedGeneratedFileInfo &Info);
169
+ std::string remapFilePath (StringRef Path) {
170
+ return Mapper.mapToString (Path);
171
+ }
166
172
167
173
public:
168
174
std::vector<SerializedDiagnosticInfo> DiagInfos;
@@ -176,6 +182,7 @@ struct DiagnosticSerializer {
176
182
177
183
// Serializing SourceManager.
178
184
SourceManager SrcMgr;
185
+ llvm::PrefixMapper &Mapper;
179
186
180
187
// Mapping of the FileID between SourceManager from CompilerInstance vs.
181
188
// the serialized FileID in cached diagnostics. Lookup tables are
@@ -482,10 +489,19 @@ DiagnosticSerializer::deserializeFixIt(const SerializedFixIt &FI) {
482
489
483
490
unsigned DiagnosticSerializer::deserializeFile (const SerializedFile &File) {
484
491
assert (File.IncludeLoc .FileID == 0 && " IncludeLoc not supported yet" );
485
- return File.Content .empty ()
486
- ? SrcMgr.getExternalSourceBufferID (File.FileName )
487
- : SrcMgr.addNewSourceBuffer (llvm::MemoryBuffer::getMemBufferCopy (
488
- File.Content , File.FileName ));
492
+ auto FileName = remapFilePath (File.FileName );
493
+ if (File.Content .empty () && FileName == File.FileName )
494
+ return SrcMgr.getExternalSourceBufferID (FileName);
495
+
496
+ std::unique_ptr<llvm::MemoryBuffer> Content;
497
+ if (!File.Content .empty ())
498
+ Content = llvm::MemoryBuffer::getMemBufferCopy (File.Content , FileName);
499
+ else if (auto InputFileOrErr = swift::vfs::getFileOrSTDIN (
500
+ *SrcMgr.getFileSystem (), File.FileName ))
501
+ Content = llvm::MemoryBuffer::getMemBufferCopy (
502
+ (*InputFileOrErr)->getBuffer (), FileName);
503
+
504
+ return Content ? SrcMgr.addNewSourceBuffer (std::move (Content)) : 0u ;
489
505
}
490
506
491
507
llvm::Error
@@ -495,8 +511,8 @@ DiagnosticSerializer::deserializeVirtualFile(const SerializedVirtualFile &VF) {
495
511
return Range.takeError ();
496
512
unsigned Length = (const char *)Range->getEnd ().getOpaquePointerValue () -
497
513
(const char *)Range->getStart ().getOpaquePointerValue ();
498
- SrcMgr. createVirtualFile (Range-> getStart (), VF. FileName , VF.LineOffset ,
499
- Length);
514
+ auto FileName = remapFilePath ( VF.FileName );
515
+ SrcMgr. createVirtualFile (Range-> getStart (), FileName, VF. LineOffset , Length);
500
516
return llvm::Error::success ();
501
517
}
502
518
@@ -625,7 +641,14 @@ class CachingDiagnosticsProcessor::Implementation
625
641
: InstanceSourceMgr(Instance.getSourceMgr()),
626
642
InAndOut (
627
643
Instance.getInvocation().getFrontendOptions().InputsAndOutputs),
628
- Diags(Instance.getDiags()) {}
644
+ Diags(Instance.getDiags()) {
645
+ SmallVector<llvm::MappedPrefix, 4 > Prefixes;
646
+ llvm::MappedPrefix::transformJoinedIfValid (
647
+ Instance.getInvocation ().getFrontendOptions ().CacheReplayPrefixMap ,
648
+ Prefixes);
649
+ Mapper.addRange (Prefixes);
650
+ Mapper.sort ();
651
+ }
629
652
~Implementation () {}
630
653
631
654
void startDiagnosticCapture () {
@@ -648,7 +671,7 @@ class CachingDiagnosticsProcessor::Implementation
648
671
649
672
llvm::Error replayCachedDiagnostics (llvm::StringRef Buffer) {
650
673
return DiagnosticSerializer::emitDiagnosticsFromCached (
651
- Buffer, getDiagnosticSourceMgr (), Diags, InAndOut);
674
+ Buffer, getDiagnosticSourceMgr (), Diags, Mapper, InAndOut);
652
675
}
653
676
654
677
void handleDiagnostic (SourceManager &SM,
@@ -698,7 +721,7 @@ class CachingDiagnosticsProcessor::Implementation
698
721
// file system changes on later diagnostics.
699
722
if (!Serializer) {
700
723
Serializer.reset (
701
- new DiagnosticSerializer (InstanceSourceMgr.getFileSystem ()));
724
+ new DiagnosticSerializer (InstanceSourceMgr.getFileSystem (), Mapper ));
702
725
Serializer->addInputsToSourceMgr (InAndOut);
703
726
}
704
727
@@ -717,6 +740,7 @@ class CachingDiagnosticsProcessor::Implementation
717
740
SourceManager &InstanceSourceMgr;
718
741
const FrontendInputsAndOutputs &InAndOut;
719
742
DiagnosticEngine &Diags;
743
+ llvm::PrefixMapper Mapper;
720
744
721
745
llvm::unique_function<bool (StringRef)> serializedOutputCallback;
722
746
0 commit comments