@@ -2037,9 +2037,9 @@ HeaderFileInfoTrait::getFile(const internal_key_type &Key) {
2037
2037
if (!Key.Imported )
2038
2038
return FileMgr.getOptionalFileRef (Key.Filename );
2039
2039
2040
- std::string Resolved = std::string (Key. Filename );
2041
- Reader.ResolveImportedPath (M, Resolved );
2042
- return FileMgr.getOptionalFileRef (Resolved);
2040
+ auto Resolved =
2041
+ ASTReader::ResolveImportedPath ( Reader.getPathBuf (), Key. Filename , M );
2042
+ return FileMgr.getOptionalFileRef (* Resolved);
2043
2043
}
2044
2044
2045
2045
unsigned HeaderFileInfoTrait::ComputeHash (internal_key_ref ikey) {
@@ -2502,11 +2502,12 @@ InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
2502
2502
std::tie (R.FilenameAsRequested , R.Filename ) = [&]() {
2503
2503
uint16_t AsRequestedLength = Record[7 ];
2504
2504
2505
- std::string NameAsRequested = Blob.substr (0 , AsRequestedLength). str ( );
2506
- std::string Name = Blob.substr (AsRequestedLength). str ( );
2505
+ StringRef NameAsRequestedRef = Blob.substr (0 , AsRequestedLength);
2506
+ StringRef NameRef = Blob.substr (AsRequestedLength);
2507
2507
2508
- ResolveImportedPath (F, NameAsRequested);
2509
- ResolveImportedPath (F, Name);
2508
+ std::string NameAsRequested =
2509
+ ResolveImportedPathAndAllocate (PathBuf, NameAsRequestedRef, F);
2510
+ std::string Name = ResolveImportedPathAndAllocate (PathBuf, NameRef, F);
2510
2511
2511
2512
if (Name.empty ())
2512
2513
Name = NameAsRequested;
@@ -2736,23 +2737,38 @@ InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2736
2737
return IF;
2737
2738
}
2738
2739
2739
- // / If we are loading a relocatable PCH or module file, and the filename
2740
- // / is not an absolute path, add the system or module root to the beginning of
2741
- // / the file name.
2742
- void ASTReader::ResolveImportedPath (ModuleFile &M, std::string &Filename) {
2743
- // Resolve relative to the base directory, if we have one.
2744
- if (!M.BaseDirectory .empty ())
2745
- return ResolveImportedPath (Filename, M.BaseDirectory );
2740
+ ASTReader::TemporarilyOwnedStringRef
2741
+ ASTReader::ResolveImportedPath (SmallString<0 > &Buf, StringRef Path,
2742
+ ModuleFile &ModF) {
2743
+ return ResolveImportedPath (Buf, Path, ModF.BaseDirectory );
2746
2744
}
2747
2745
2748
- void ASTReader::ResolveImportedPath (std::string &Filename, StringRef Prefix) {
2749
- if (Filename.empty () || llvm::sys::path::is_absolute (Filename) ||
2750
- Filename == " <built-in>" || Filename == " <command line>" )
2751
- return ;
2746
+ ASTReader::TemporarilyOwnedStringRef
2747
+ ASTReader::ResolveImportedPath (SmallString<0 > &Buf, StringRef Path,
2748
+ StringRef Prefix) {
2749
+ assert (Buf.capacity () != 0 && " Overlapping ResolveImportedPath calls" );
2750
+
2751
+ if (Prefix.empty () || Path.empty () || llvm::sys::path::is_absolute (Path) ||
2752
+ Path == " <built-in>" || Path == " <command line>" )
2753
+ return {Path, Buf};
2754
+
2755
+ Buf.clear ();
2756
+ llvm::sys::path::append (Buf, Prefix, Path);
2757
+ StringRef ResolvedPath{Buf.data (), Buf.size ()};
2758
+ return {ResolvedPath, Buf};
2759
+ }
2752
2760
2753
- SmallString<128 > Buffer;
2754
- llvm::sys::path::append (Buffer, Prefix, Filename);
2755
- Filename.assign (Buffer.begin (), Buffer.end ());
2761
+ std::string ASTReader::ResolveImportedPathAndAllocate (SmallString<0 > &Buf,
2762
+ StringRef P,
2763
+ ModuleFile &ModF) {
2764
+ return ResolveImportedPathAndAllocate (Buf, P, ModF.BaseDirectory );
2765
+ }
2766
+
2767
+ std::string ASTReader::ResolveImportedPathAndAllocate (SmallString<0 > &Buf,
2768
+ StringRef P,
2769
+ StringRef Prefix) {
2770
+ auto ResolvedPath = ResolveImportedPath (Buf, P, Prefix);
2771
+ return ResolvedPath->str ();
2756
2772
}
2757
2773
2758
2774
static bool isDiagnosedResult (ASTReader::ASTReadResult ARR, unsigned Caps) {
@@ -3183,8 +3199,8 @@ ASTReader::ReadControlBlock(ModuleFile &F,
3183
3199
case ORIGINAL_FILE:
3184
3200
F.OriginalSourceFileID = FileID::get (Record[0 ]);
3185
3201
F.ActualOriginalSourceFileName = std::string (Blob);
3186
- F.OriginalSourceFileName = F. ActualOriginalSourceFileName ;
3187
- ResolveImportedPath (F , F.OriginalSourceFileName );
3202
+ F.OriginalSourceFileName = ResolveImportedPathAndAllocate (
3203
+ PathBuf , F.ActualOriginalSourceFileName , F );
3188
3204
break ;
3189
3205
3190
3206
case ORIGINAL_FILE_ID:
@@ -5474,6 +5490,8 @@ bool ASTReader::readASTFileControlBlock(
5474
5490
RecordData Record;
5475
5491
std::string ModuleDir;
5476
5492
bool DoneWithControlBlock = false ;
5493
+ SmallString<0 > PathBuf;
5494
+ PathBuf.reserve (256 );
5477
5495
while (!DoneWithControlBlock) {
5478
5496
Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance ();
5479
5497
if (!MaybeEntry) {
@@ -5556,9 +5574,9 @@ bool ASTReader::readASTFileControlBlock(
5556
5574
break ;
5557
5575
case MODULE_MAP_FILE: {
5558
5576
unsigned Idx = 0 ;
5559
- auto Path = ReadString (Record, Idx);
5560
- ResolveImportedPath (Path , ModuleDir);
5561
- Listener.ReadModuleMapFile (Path);
5577
+ std::string PathStr = ReadString (Record, Idx);
5578
+ auto Path = ResolveImportedPath (PathBuf, PathStr , ModuleDir);
5579
+ Listener.ReadModuleMapFile (* Path);
5562
5580
break ;
5563
5581
}
5564
5582
case INPUT_FILE_OFFSETS: {
@@ -5605,10 +5623,9 @@ bool ASTReader::readASTFileControlBlock(
5605
5623
break ;
5606
5624
case INPUT_FILE:
5607
5625
bool Overridden = static_cast <bool >(Record[3 ]);
5608
- std::string Filename = std::string (Blob);
5609
- ResolveImportedPath (Filename, ModuleDir);
5626
+ auto Filename = ResolveImportedPath (PathBuf, Blob, ModuleDir);
5610
5627
shouldContinue = Listener.visitInputFile (
5611
- Filename, isSystemFile, Overridden, /* IsExplicitModule*/ false );
5628
+ * Filename, isSystemFile, Overridden, /* IsExplicitModule= */ false );
5612
5629
break ;
5613
5630
}
5614
5631
if (!shouldContinue)
@@ -5643,12 +5660,12 @@ bool ASTReader::readASTFileControlBlock(
5643
5660
// Skip Size, ModTime and Signature
5644
5661
Idx += 1 + 1 + ASTFileSignature::size;
5645
5662
std::string ModuleName = ReadString (Record, Idx);
5646
- std::string Filename = ReadString (Record, Idx);
5647
- ResolveImportedPath (Filename , ModuleDir);
5663
+ std::string FilenameStr = ReadString (Record, Idx);
5664
+ auto Filename = ResolveImportedPath (PathBuf, FilenameStr , ModuleDir);
5648
5665
std::string CacheKey = ReadString (Record, Idx);
5649
5666
if (!CacheKey.empty ())
5650
- Listener.readModuleCacheKey (ModuleName, Filename, CacheKey);
5651
- Listener.visitImport (ModuleName, Filename);
5667
+ Listener.readModuleCacheKey (ModuleName, * Filename, CacheKey);
5668
+ Listener.visitImport (ModuleName, * Filename);
5652
5669
}
5653
5670
break ;
5654
5671
}
@@ -5916,9 +5933,8 @@ llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
5916
5933
// FIXME: This doesn't work for framework modules as `Filename` is the
5917
5934
// name as written in the module file and does not include
5918
5935
// `Headers/`, so this path will never exist.
5919
- std::string Filename = std::string (Blob);
5920
- ResolveImportedPath (F, Filename);
5921
- if (auto Umbrella = PP.getFileManager ().getOptionalFileRef (Filename)) {
5936
+ auto Filename = ResolveImportedPath (PathBuf, Blob, F);
5937
+ if (auto Umbrella = PP.getFileManager ().getOptionalFileRef (*Filename)) {
5922
5938
if (!CurrentModule->getUmbrellaHeaderAsWritten ()) {
5923
5939
// FIXME: NameAsWritten
5924
5940
ModMap.setUmbrellaHeaderAsWritten (CurrentModule, *Umbrella, Blob, " " );
@@ -5946,18 +5962,16 @@ llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
5946
5962
break ;
5947
5963
5948
5964
case SUBMODULE_TOPHEADER: {
5949
- std::string HeaderName (Blob);
5950
- ResolveImportedPath (F, HeaderName);
5951
- CurrentModule->addTopHeaderFilename (HeaderName);
5965
+ auto HeaderName = ResolveImportedPath (PathBuf, Blob, F);
5966
+ CurrentModule->addTopHeaderFilename (*HeaderName);
5952
5967
break ;
5953
5968
}
5954
5969
5955
5970
case SUBMODULE_UMBRELLA_DIR: {
5956
5971
// See comments in SUBMODULE_UMBRELLA_HEADER
5957
- std::string Dirname = std::string (Blob);
5958
- ResolveImportedPath (F, Dirname);
5972
+ auto Dirname = ResolveImportedPath (PathBuf, Blob, F);
5959
5973
if (auto Umbrella =
5960
- PP.getFileManager ().getOptionalDirectoryRef (Dirname)) {
5974
+ PP.getFileManager ().getOptionalDirectoryRef (* Dirname)) {
5961
5975
if (!CurrentModule->getUmbrellaDirAsWritten ()) {
5962
5976
// FIXME: NameAsWritten
5963
5977
ModMap.setUmbrellaDirAsWritten (CurrentModule, *Umbrella, Blob, " " );
@@ -9503,17 +9517,13 @@ std::string ASTReader::ReadString(const RecordDataImpl &Record, unsigned &Idx) {
9503
9517
9504
9518
std::string ASTReader::ReadPath (ModuleFile &F, const RecordData &Record,
9505
9519
unsigned &Idx) {
9506
- std::string Filename = ReadString (Record, Idx);
9507
- ResolveImportedPath (F, Filename);
9508
- return Filename;
9520
+ return ReadPath (F.BaseDirectory , Record, Idx);
9509
9521
}
9510
9522
9511
9523
std::string ASTReader::ReadPath (StringRef BaseDirectory,
9512
9524
const RecordData &Record, unsigned &Idx) {
9513
9525
std::string Filename = ReadString (Record, Idx);
9514
- if (!BaseDirectory.empty ())
9515
- ResolveImportedPath (Filename, BaseDirectory);
9516
- return Filename;
9526
+ return ResolveImportedPathAndAllocate (PathBuf, Filename, BaseDirectory);
9517
9527
}
9518
9528
9519
9529
VersionTuple ASTReader::ReadVersionTuple (const RecordData &Record,
@@ -10380,6 +10390,8 @@ ASTReader::ASTReader(Preprocessor &PP, InMemoryModuleCache &ModuleCache,
10380
10390
UseGlobalIndex(UseGlobalIndex), CurrSwitchCaseStmts(&SwitchCaseStmts) {
10381
10391
SourceMgr.setExternalSLocEntrySource (this );
10382
10392
10393
+ PathBuf.reserve (256 );
10394
+
10383
10395
for (const auto &Ext : Extensions) {
10384
10396
auto BlockName = Ext->getExtensionMetadata ().BlockName ;
10385
10397
auto Known = ModuleFileExtensions.find (BlockName);
0 commit comments