@@ -2048,9 +2048,9 @@ HeaderFileInfoTrait::getFile(const internal_key_type &Key) {
2048
2048
if (!Key.Imported )
2049
2049
return FileMgr.getOptionalFileRef (Key.Filename );
2050
2050
2051
- std::string Resolved = std::string (Key. Filename );
2052
- Reader.ResolveImportedPath (M, Resolved );
2053
- return FileMgr.getOptionalFileRef (Resolved);
2051
+ auto Resolved =
2052
+ ASTReader::ResolveImportedPath ( Reader.getPathBuf (), Key. Filename , M );
2053
+ return FileMgr.getOptionalFileRef (* Resolved);
2054
2054
}
2055
2055
2056
2056
unsigned HeaderFileInfoTrait::ComputeHash (internal_key_ref ikey) {
@@ -2513,11 +2513,12 @@ InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
2513
2513
std::tie (R.FilenameAsRequested , R.Filename ) = [&]() {
2514
2514
uint16_t AsRequestedLength = Record[7 ];
2515
2515
2516
- std::string NameAsRequested = Blob.substr (0 , AsRequestedLength). str ( );
2517
- std::string Name = Blob.substr (AsRequestedLength). str ( );
2516
+ StringRef NameAsRequestedRef = Blob.substr (0 , AsRequestedLength);
2517
+ StringRef NameRef = Blob.substr (AsRequestedLength);
2518
2518
2519
- ResolveImportedPath (F, NameAsRequested);
2520
- ResolveImportedPath (F, Name);
2519
+ std::string NameAsRequested =
2520
+ ResolveImportedPathAndAllocate (PathBuf, NameAsRequestedRef, F);
2521
+ std::string Name = ResolveImportedPathAndAllocate (PathBuf, NameRef, F);
2521
2522
2522
2523
if (Name.empty ())
2523
2524
Name = NameAsRequested;
@@ -2743,23 +2744,38 @@ InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2743
2744
return IF;
2744
2745
}
2745
2746
2746
- // / If we are loading a relocatable PCH or module file, and the filename
2747
- // / is not an absolute path, add the system or module root to the beginning of
2748
- // / the file name.
2749
- void ASTReader::ResolveImportedPath (ModuleFile &M, std::string &Filename) {
2750
- // Resolve relative to the base directory, if we have one.
2751
- if (!M.BaseDirectory .empty ())
2752
- return ResolveImportedPath (Filename, M.BaseDirectory );
2747
+ ASTReader::TemporarilyOwnedStringRef
2748
+ ASTReader::ResolveImportedPath (SmallString<0 > &Buf, StringRef Path,
2749
+ ModuleFile &ModF) {
2750
+ return ResolveImportedPath (Buf, Path, ModF.BaseDirectory );
2753
2751
}
2754
2752
2755
- void ASTReader::ResolveImportedPath (std::string &Filename, StringRef Prefix) {
2756
- if (Filename.empty () || llvm::sys::path::is_absolute (Filename) ||
2757
- Filename == " <built-in>" || Filename == " <command line>" )
2758
- return ;
2753
+ ASTReader::TemporarilyOwnedStringRef
2754
+ ASTReader::ResolveImportedPath (SmallString<0 > &Buf, StringRef Path,
2755
+ StringRef Prefix) {
2756
+ assert (Buf.capacity () != 0 && " Overlapping ResolveImportedPath calls" );
2757
+
2758
+ if (Prefix.empty () || Path.empty () || llvm::sys::path::is_absolute (Path) ||
2759
+ Path == " <built-in>" || Path == " <command line>" )
2760
+ return {Path, Buf};
2761
+
2762
+ Buf.clear ();
2763
+ llvm::sys::path::append (Buf, Prefix, Path);
2764
+ StringRef ResolvedPath{Buf.data (), Buf.size ()};
2765
+ return {ResolvedPath, Buf};
2766
+ }
2759
2767
2760
- SmallString<128 > Buffer;
2761
- llvm::sys::path::append (Buffer, Prefix, Filename);
2762
- Filename.assign (Buffer.begin (), Buffer.end ());
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 ();
2763
2779
}
2764
2780
2765
2781
static bool isDiagnosedResult (ASTReader::ASTReadResult ARR, unsigned Caps) {
@@ -3187,8 +3203,8 @@ ASTReader::ReadControlBlock(ModuleFile &F,
3187
3203
case ORIGINAL_FILE:
3188
3204
F.OriginalSourceFileID = FileID::get (Record[0 ]);
3189
3205
F.ActualOriginalSourceFileName = std::string (Blob);
3190
- F.OriginalSourceFileName = F. ActualOriginalSourceFileName ;
3191
- ResolveImportedPath (F , F.OriginalSourceFileName );
3206
+ F.OriginalSourceFileName = ResolveImportedPathAndAllocate (
3207
+ PathBuf , F.ActualOriginalSourceFileName , F );
3192
3208
break ;
3193
3209
3194
3210
case ORIGINAL_FILE_ID:
@@ -5477,6 +5493,8 @@ bool ASTReader::readASTFileControlBlock(
5477
5493
RecordData Record;
5478
5494
std::string ModuleDir;
5479
5495
bool DoneWithControlBlock = false ;
5496
+ SmallString<0 > PathBuf;
5497
+ PathBuf.reserve (256 );
5480
5498
while (!DoneWithControlBlock) {
5481
5499
Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance ();
5482
5500
if (!MaybeEntry) {
@@ -5559,9 +5577,9 @@ bool ASTReader::readASTFileControlBlock(
5559
5577
break ;
5560
5578
case MODULE_MAP_FILE: {
5561
5579
unsigned Idx = 0 ;
5562
- auto Path = ReadString (Record, Idx);
5563
- ResolveImportedPath (Path , ModuleDir);
5564
- Listener.ReadModuleMapFile (Path);
5580
+ std::string PathStr = ReadString (Record, Idx);
5581
+ auto Path = ResolveImportedPath (PathBuf, PathStr , ModuleDir);
5582
+ Listener.ReadModuleMapFile (* Path);
5565
5583
break ;
5566
5584
}
5567
5585
case INPUT_FILE_OFFSETS: {
@@ -5608,10 +5626,9 @@ bool ASTReader::readASTFileControlBlock(
5608
5626
break ;
5609
5627
case INPUT_FILE:
5610
5628
bool Overridden = static_cast <bool >(Record[3 ]);
5611
- std::string Filename = std::string (Blob);
5612
- ResolveImportedPath (Filename, ModuleDir);
5629
+ auto Filename = ResolveImportedPath (PathBuf, Blob, ModuleDir);
5613
5630
shouldContinue = Listener.visitInputFile (
5614
- Filename, isSystemFile, Overridden, /* IsExplicitModule*/ false );
5631
+ * Filename, isSystemFile, Overridden, /* IsExplicitModule= */ false );
5615
5632
break ;
5616
5633
}
5617
5634
if (!shouldContinue)
@@ -5646,9 +5663,9 @@ bool ASTReader::readASTFileControlBlock(
5646
5663
// Skip Size, ModTime and Signature
5647
5664
Idx += 1 + 1 + ASTFileSignature::size;
5648
5665
std::string ModuleName = ReadString (Record, Idx);
5649
- std::string Filename = ReadString (Record, Idx);
5650
- ResolveImportedPath (Filename , ModuleDir);
5651
- Listener.visitImport (ModuleName, Filename);
5666
+ std::string FilenameStr = ReadString (Record, Idx);
5667
+ auto Filename = ResolveImportedPath (PathBuf, FilenameStr , ModuleDir);
5668
+ Listener.visitImport (ModuleName, * Filename);
5652
5669
}
5653
5670
break ;
5654
5671
}
@@ -5901,9 +5918,8 @@ llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
5901
5918
// FIXME: This doesn't work for framework modules as `Filename` is the
5902
5919
// name as written in the module file and does not include
5903
5920
// `Headers/`, so this path will never exist.
5904
- std::string Filename = std::string (Blob);
5905
- ResolveImportedPath (F, Filename);
5906
- if (auto Umbrella = PP.getFileManager ().getOptionalFileRef (Filename)) {
5921
+ auto Filename = ResolveImportedPath (PathBuf, Blob, F);
5922
+ if (auto Umbrella = PP.getFileManager ().getOptionalFileRef (*Filename)) {
5907
5923
if (!CurrentModule->getUmbrellaHeaderAsWritten ()) {
5908
5924
// FIXME: NameAsWritten
5909
5925
ModMap.setUmbrellaHeaderAsWritten (CurrentModule, *Umbrella, Blob, " " );
@@ -5931,18 +5947,16 @@ llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
5931
5947
break ;
5932
5948
5933
5949
case SUBMODULE_TOPHEADER: {
5934
- std::string HeaderName (Blob);
5935
- ResolveImportedPath (F, HeaderName);
5936
- CurrentModule->addTopHeaderFilename (HeaderName);
5950
+ auto HeaderName = ResolveImportedPath (PathBuf, Blob, F);
5951
+ CurrentModule->addTopHeaderFilename (*HeaderName);
5937
5952
break ;
5938
5953
}
5939
5954
5940
5955
case SUBMODULE_UMBRELLA_DIR: {
5941
5956
// See comments in SUBMODULE_UMBRELLA_HEADER
5942
- std::string Dirname = std::string (Blob);
5943
- ResolveImportedPath (F, Dirname);
5957
+ auto Dirname = ResolveImportedPath (PathBuf, Blob, F);
5944
5958
if (auto Umbrella =
5945
- PP.getFileManager ().getOptionalDirectoryRef (Dirname)) {
5959
+ PP.getFileManager ().getOptionalDirectoryRef (* Dirname)) {
5946
5960
if (!CurrentModule->getUmbrellaDirAsWritten ()) {
5947
5961
// FIXME: NameAsWritten
5948
5962
ModMap.setUmbrellaDirAsWritten (CurrentModule, *Umbrella, Blob, " " );
@@ -9597,17 +9611,13 @@ std::string ASTReader::ReadString(const RecordDataImpl &Record, unsigned &Idx) {
9597
9611
9598
9612
std::string ASTReader::ReadPath (ModuleFile &F, const RecordData &Record,
9599
9613
unsigned &Idx) {
9600
- std::string Filename = ReadString (Record, Idx);
9601
- ResolveImportedPath (F, Filename);
9602
- return Filename;
9614
+ return ReadPath (F.BaseDirectory , Record, Idx);
9603
9615
}
9604
9616
9605
9617
std::string ASTReader::ReadPath (StringRef BaseDirectory,
9606
9618
const RecordData &Record, unsigned &Idx) {
9607
9619
std::string Filename = ReadString (Record, Idx);
9608
- if (!BaseDirectory.empty ())
9609
- ResolveImportedPath (Filename, BaseDirectory);
9610
- return Filename;
9620
+ return ResolveImportedPathAndAllocate (PathBuf, Filename, BaseDirectory);
9611
9621
}
9612
9622
9613
9623
VersionTuple ASTReader::ReadVersionTuple (const RecordData &Record,
@@ -10512,6 +10522,8 @@ ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
10512
10522
UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
10513
10523
SourceMgr.setExternalSLocEntrySource (this );
10514
10524
10525
+ PathBuf.reserve (256 );
10526
+
10515
10527
for (const auto &Ext : Extensions) {
10516
10528
auto BlockName = Ext->getExtensionMetadata ().BlockName ;
10517
10529
auto Known = ModuleFileExtensions.find (BlockName);
0 commit comments