@@ -2048,7 +2048,8 @@ HeaderFileInfoTrait::getFile(const internal_key_type &Key) {
2048
2048
if (!Key.Imported )
2049
2049
return FileMgr.getOptionalFileRef (Key.Filename );
2050
2050
2051
- auto Resolved = Reader.ResolveImportedPath (Key.Filename , M);
2051
+ auto Resolved =
2052
+ ASTReader::ResolveImportedPath (Reader.getPathBuf (), Key.Filename , M);
2052
2053
return FileMgr.getOptionalFileRef (*Resolved);
2053
2054
}
2054
2055
@@ -2512,10 +2513,12 @@ InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
2512
2513
std::tie (R.FilenameAsRequested , R.Filename ) = [&]() {
2513
2514
uint16_t AsRequestedLength = Record[7 ];
2514
2515
2516
+ StringRef NameAsRequestedRef = Blob.substr (0 , AsRequestedLength);
2517
+ StringRef NameRef = Blob.substr (AsRequestedLength);
2518
+
2515
2519
std::string NameAsRequested =
2516
- ResolveImportedPath (Blob.substr (0 , AsRequestedLength), F)->str ();
2517
- std::string Name =
2518
- ResolveImportedPath (Blob.substr (AsRequestedLength), F)->str ();
2520
+ ResolveImportedPathAndAllocate (PathBuf, NameAsRequestedRef, F);
2521
+ std::string Name = ResolveImportedPathAndAllocate (PathBuf, NameRef, F);
2519
2522
2520
2523
if (Name.empty ())
2521
2524
Name = NameAsRequested;
@@ -2741,18 +2744,38 @@ InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2741
2744
return IF;
2742
2745
}
2743
2746
2744
- // / If we are loading a relocatable PCH or module file, and the filename
2745
- // / is not an absolute path, add the system or module root to the beginning of
2746
- // / the file name.
2747
- StringRef ASTReader::ResolveImportedPath (SmallVectorImpl<char > &Buffer,
2748
- StringRef Path, StringRef Prefix) {
2747
+ ASTReader::TemporarilyOwnedStringRef
2748
+ ASTReader::ResolveImportedPath (SmallString<0 > &Buf, StringRef Path,
2749
+ ModuleFile &ModF) {
2750
+ return ResolveImportedPath (Buf, Path, ModF.BaseDirectory );
2751
+ }
2752
+
2753
+ ASTReader::TemporarilyOwnedStringRef
2754
+ ASTReader::ResolveImportedPath (SmallString<0 > &Buf, StringRef Path,
2755
+ StringRef Prefix) {
2756
+ assert (Buf.capacity () != 0 && " Overlapping ResolveImportedPath calls" );
2757
+
2749
2758
if (Prefix.empty () || Path.empty () || llvm::sys::path::is_absolute (Path) ||
2750
2759
Path == " <built-in>" || Path == " <command line>" )
2751
- return Path;
2760
+ return { Path, Buf} ;
2752
2761
2753
- Buffer.clear ();
2754
- llvm::sys::path::append (Buffer, Prefix, Path);
2755
- return {Buffer.data (), Buffer.size ()};
2762
+ Buf.clear ();
2763
+ llvm::sys::path::append (Buf, Prefix, Path);
2764
+ StringRef ResolvedPath{Buf.data (), Buf.size ()};
2765
+ return {ResolvedPath, Buf};
2766
+ }
2767
+
2768
+ std::string ASTReader::ResolveImportedPathAndAllocate (SmallString<0 > &Buf,
2769
+ StringRef P,
2770
+ ModuleFile &ModF) {
2771
+ return ResolveImportedPathAndAllocate (Buf, P, ModF.BaseDirectory );
2772
+ }
2773
+
2774
+ std::string ASTReader::ResolveImportedPathAndAllocate (SmallString<0 > &Buf,
2775
+ StringRef P,
2776
+ StringRef Prefix) {
2777
+ auto ResolvedPath = ResolveImportedPath (Buf, P, Prefix);
2778
+ return ResolvedPath->str ();
2756
2779
}
2757
2780
2758
2781
static bool isDiagnosedResult (ASTReader::ASTReadResult ARR, unsigned Caps) {
@@ -3180,8 +3203,8 @@ ASTReader::ReadControlBlock(ModuleFile &F,
3180
3203
case ORIGINAL_FILE:
3181
3204
F.OriginalSourceFileID = FileID::get (Record[0 ]);
3182
3205
F.ActualOriginalSourceFileName = std::string (Blob);
3183
- F.OriginalSourceFileName =
3184
- ResolveImportedPath ( F.ActualOriginalSourceFileName , F)-> str ( );
3206
+ F.OriginalSourceFileName = ResolveImportedPathAndAllocate (
3207
+ PathBuf, F.ActualOriginalSourceFileName , F);
3185
3208
break ;
3186
3209
3187
3210
case ORIGINAL_FILE_ID:
@@ -5470,7 +5493,8 @@ bool ASTReader::readASTFileControlBlock(
5470
5493
RecordData Record;
5471
5494
std::string ModuleDir;
5472
5495
bool DoneWithControlBlock = false ;
5473
- SmallString<256 > PathBuf;
5496
+ SmallString<0 > PathBuf;
5497
+ PathBuf.reserve (256 );
5474
5498
while (!DoneWithControlBlock) {
5475
5499
Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance ();
5476
5500
if (!MaybeEntry) {
@@ -5554,8 +5578,8 @@ bool ASTReader::readASTFileControlBlock(
5554
5578
case MODULE_MAP_FILE: {
5555
5579
unsigned Idx = 0 ;
5556
5580
std::string PathStr = ReadString (Record, Idx);
5557
- StringRef Path = ResolveImportedPath (PathBuf, PathStr, ModuleDir);
5558
- Listener.ReadModuleMapFile (Path);
5581
+ auto Path = ResolveImportedPath (PathBuf, PathStr, ModuleDir);
5582
+ Listener.ReadModuleMapFile (* Path);
5559
5583
break ;
5560
5584
}
5561
5585
case INPUT_FILE_OFFSETS: {
@@ -5602,9 +5626,9 @@ bool ASTReader::readASTFileControlBlock(
5602
5626
break ;
5603
5627
case INPUT_FILE:
5604
5628
bool Overridden = static_cast <bool >(Record[3 ]);
5605
- StringRef Filename = ResolveImportedPath (PathBuf, Blob, ModuleDir);
5629
+ auto Filename = ResolveImportedPath (PathBuf, Blob, ModuleDir);
5606
5630
shouldContinue = Listener.visitInputFile (
5607
- Filename, isSystemFile, Overridden, /* IsExplicitModule*/ false );
5631
+ * Filename, isSystemFile, Overridden, /* IsExplicitModule*/ false );
5608
5632
break ;
5609
5633
}
5610
5634
if (!shouldContinue)
@@ -5640,9 +5664,8 @@ bool ASTReader::readASTFileControlBlock(
5640
5664
Idx += 1 + 1 + ASTFileSignature::size;
5641
5665
std::string ModuleName = ReadString (Record, Idx);
5642
5666
std::string FilenameStr = ReadString (Record, Idx);
5643
- StringRef Filename =
5644
- ResolveImportedPath (PathBuf, FilenameStr, ModuleDir);
5645
- Listener.visitImport (ModuleName, Filename);
5667
+ auto Filename = ResolveImportedPath (PathBuf, FilenameStr, ModuleDir);
5668
+ Listener.visitImport (ModuleName, *Filename);
5646
5669
}
5647
5670
break ;
5648
5671
}
@@ -5895,7 +5918,7 @@ llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
5895
5918
// FIXME: This doesn't work for framework modules as `Filename` is the
5896
5919
// name as written in the module file and does not include
5897
5920
// `Headers/`, so this path will never exist.
5898
- auto Filename = ResolveImportedPath (Blob, F);
5921
+ auto Filename = ResolveImportedPath (PathBuf, Blob, F);
5899
5922
if (auto Umbrella = PP.getFileManager ().getOptionalFileRef (*Filename)) {
5900
5923
if (!CurrentModule->getUmbrellaHeaderAsWritten ()) {
5901
5924
// FIXME: NameAsWritten
@@ -5924,14 +5947,14 @@ llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
5924
5947
break ;
5925
5948
5926
5949
case SUBMODULE_TOPHEADER: {
5927
- auto HeaderName = ResolveImportedPath (Blob, F);
5950
+ auto HeaderName = ResolveImportedPath (PathBuf, Blob, F);
5928
5951
CurrentModule->addTopHeaderFilename (*HeaderName);
5929
5952
break ;
5930
5953
}
5931
5954
5932
5955
case SUBMODULE_UMBRELLA_DIR: {
5933
5956
// See comments in SUBMODULE_UMBRELLA_HEADER
5934
- auto Dirname = ResolveImportedPath (Blob, F);
5957
+ auto Dirname = ResolveImportedPath (PathBuf, Blob, F);
5935
5958
if (auto Umbrella =
5936
5959
PP.getFileManager ().getOptionalDirectoryRef (*Dirname)) {
5937
5960
if (!CurrentModule->getUmbrellaDirAsWritten ()) {
@@ -9588,14 +9611,13 @@ std::string ASTReader::ReadString(const RecordDataImpl &Record, unsigned &Idx) {
9588
9611
9589
9612
std::string ASTReader::ReadPath (ModuleFile &F, const RecordData &Record,
9590
9613
unsigned &Idx) {
9591
- std::string Filename = ReadString (Record, Idx);
9592
- return ResolveImportedPath (Filename, F)->str ();
9614
+ return ReadPath (F.BaseDirectory , Record, Idx);
9593
9615
}
9594
9616
9595
9617
std::string ASTReader::ReadPath (StringRef BaseDirectory,
9596
9618
const RecordData &Record, unsigned &Idx) {
9597
9619
std::string Filename = ReadString (Record, Idx);
9598
- return ResolveImportedPath ( Filename, BaseDirectory)-> str ( );
9620
+ return ResolveImportedPathAndAllocate (PathBuf, Filename, BaseDirectory);
9599
9621
}
9600
9622
9601
9623
VersionTuple ASTReader::ReadVersionTuple (const RecordData &Record,
@@ -10500,8 +10522,7 @@ ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
10500
10522
UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
10501
10523
SourceMgr.setExternalSLocEntrySource (this );
10502
10524
10503
- PathBuf.emplace ();
10504
- PathBuf->reserve (256 );
10525
+ PathBuf.reserve (256 );
10505
10526
10506
10527
for (const auto &Ext : Extensions) {
10507
10528
auto BlockName = Ext->getExtensionMetadata ().BlockName ;
0 commit comments