@@ -2048,8 +2048,7 @@ 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);
2051
+ StringRef Resolved = Reader.ResolveImportedPath (Key.Filename , M);
2053
2052
return FileMgr.getOptionalFileRef (Resolved);
2054
2053
}
2055
2054
@@ -2513,11 +2512,10 @@ InputFileInfo ASTReader::getInputFileInfo(ModuleFile &F, unsigned ID) {
2513
2512
std::tie (R.FilenameAsRequested , R.Filename ) = [&]() {
2514
2513
uint16_t AsRequestedLength = Record[7 ];
2515
2514
2516
- std::string NameAsRequested = Blob.substr (0 , AsRequestedLength).str ();
2517
- std::string Name = Blob.substr (AsRequestedLength).str ();
2518
-
2519
- ResolveImportedPath (F, NameAsRequested);
2520
- ResolveImportedPath (F, Name);
2515
+ std::string NameAsRequested =
2516
+ ResolveImportedPath (Blob.substr (0 , AsRequestedLength), F).str ();
2517
+ std::string Name =
2518
+ ResolveImportedPath (Blob.substr (AsRequestedLength), F).str ();
2521
2519
2522
2520
if (Name.empty ())
2523
2521
Name = NameAsRequested;
@@ -2746,20 +2744,15 @@ InputFile ASTReader::getInputFile(ModuleFile &F, unsigned ID, bool Complain) {
2746
2744
// / If we are loading a relocatable PCH or module file, and the filename
2747
2745
// / is not an absolute path, add the system or module root to the beginning of
2748
2746
// / 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 );
2753
- }
2754
-
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 ;
2747
+ StringRef ASTReader::ResolveImportedPath (SmallVectorImpl<char > &Buffer,
2748
+ StringRef Path, StringRef Prefix) {
2749
+ if (Prefix.empty () || Path.empty () || llvm::sys::path::is_absolute (Path) ||
2750
+ Path == " <built-in>" || Path == " <command line>" )
2751
+ return Path;
2759
2752
2760
- SmallString< 128 > Buffer;
2761
- llvm::sys::path::append (Buffer, Prefix, Filename );
2762
- Filename. assign ( Buffer.begin (), Buffer.end ()) ;
2753
+ Buffer. clear () ;
2754
+ llvm::sys::path::append (Buffer, Prefix, Path );
2755
+ return { Buffer.data (), Buffer.size ()} ;
2763
2756
}
2764
2757
2765
2758
static bool isDiagnosedResult (ASTReader::ASTReadResult ARR, unsigned Caps) {
@@ -3187,8 +3180,8 @@ ASTReader::ReadControlBlock(ModuleFile &F,
3187
3180
case ORIGINAL_FILE:
3188
3181
F.OriginalSourceFileID = FileID::get (Record[0 ]);
3189
3182
F.ActualOriginalSourceFileName = std::string (Blob);
3190
- F.OriginalSourceFileName = F. ActualOriginalSourceFileName ;
3191
- ResolveImportedPath (F, F. OriginalSourceFileName );
3183
+ F.OriginalSourceFileName =
3184
+ ResolveImportedPath (F. ActualOriginalSourceFileName , F). str ( );
3192
3185
break ;
3193
3186
3194
3187
case ORIGINAL_FILE_ID:
@@ -5477,6 +5470,7 @@ bool ASTReader::readASTFileControlBlock(
5477
5470
RecordData Record;
5478
5471
std::string ModuleDir;
5479
5472
bool DoneWithControlBlock = false ;
5473
+ SmallString<256 > PathBuf;
5480
5474
while (!DoneWithControlBlock) {
5481
5475
Expected<llvm::BitstreamEntry> MaybeEntry = Stream.advance ();
5482
5476
if (!MaybeEntry) {
@@ -5559,8 +5553,8 @@ bool ASTReader::readASTFileControlBlock(
5559
5553
break ;
5560
5554
case MODULE_MAP_FILE: {
5561
5555
unsigned Idx = 0 ;
5562
- auto Path = ReadString (Record, Idx);
5563
- ResolveImportedPath (Path , ModuleDir);
5556
+ std::string PathStr = ReadString (Record, Idx);
5557
+ StringRef Path = ResolveImportedPath (PathBuf, PathStr , ModuleDir);
5564
5558
Listener.ReadModuleMapFile (Path);
5565
5559
break ;
5566
5560
}
@@ -5608,8 +5602,7 @@ bool ASTReader::readASTFileControlBlock(
5608
5602
break ;
5609
5603
case INPUT_FILE:
5610
5604
bool Overridden = static_cast <bool >(Record[3 ]);
5611
- std::string Filename = std::string (Blob);
5612
- ResolveImportedPath (Filename, ModuleDir);
5605
+ StringRef Filename = ResolveImportedPath (PathBuf, Blob, ModuleDir);
5613
5606
shouldContinue = Listener.visitInputFile (
5614
5607
Filename, isSystemFile, Overridden, /* IsExplicitModule*/ false );
5615
5608
break ;
@@ -5646,8 +5639,9 @@ bool ASTReader::readASTFileControlBlock(
5646
5639
// Skip Size, ModTime and Signature
5647
5640
Idx += 1 + 1 + ASTFileSignature::size;
5648
5641
std::string ModuleName = ReadString (Record, Idx);
5649
- std::string Filename = ReadString (Record, Idx);
5650
- ResolveImportedPath (Filename, ModuleDir);
5642
+ std::string FilenameStr = ReadString (Record, Idx);
5643
+ StringRef Filename =
5644
+ ResolveImportedPath (PathBuf, FilenameStr, ModuleDir);
5651
5645
Listener.visitImport (ModuleName, Filename);
5652
5646
}
5653
5647
break ;
@@ -5901,8 +5895,7 @@ llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
5901
5895
// FIXME: This doesn't work for framework modules as `Filename` is the
5902
5896
// name as written in the module file and does not include
5903
5897
// `Headers/`, so this path will never exist.
5904
- std::string Filename = std::string (Blob);
5905
- ResolveImportedPath (F, Filename);
5898
+ StringRef Filename = ResolveImportedPath (Blob, F);
5906
5899
if (auto Umbrella = PP.getFileManager ().getOptionalFileRef (Filename)) {
5907
5900
if (!CurrentModule->getUmbrellaHeaderAsWritten ()) {
5908
5901
// FIXME: NameAsWritten
@@ -5931,16 +5924,14 @@ llvm::Error ASTReader::ReadSubmoduleBlock(ModuleFile &F,
5931
5924
break ;
5932
5925
5933
5926
case SUBMODULE_TOPHEADER: {
5934
- std::string HeaderName (Blob);
5935
- ResolveImportedPath (F, HeaderName);
5927
+ StringRef HeaderName = ResolveImportedPath (Blob, F);
5936
5928
CurrentModule->addTopHeaderFilename (HeaderName);
5937
5929
break ;
5938
5930
}
5939
5931
5940
5932
case SUBMODULE_UMBRELLA_DIR: {
5941
5933
// See comments in SUBMODULE_UMBRELLA_HEADER
5942
- std::string Dirname = std::string (Blob);
5943
- ResolveImportedPath (F, Dirname);
5934
+ StringRef Dirname = ResolveImportedPath (Blob, F);
5944
5935
if (auto Umbrella =
5945
5936
PP.getFileManager ().getOptionalDirectoryRef (Dirname)) {
5946
5937
if (!CurrentModule->getUmbrellaDirAsWritten ()) {
@@ -9598,16 +9589,13 @@ std::string ASTReader::ReadString(const RecordDataImpl &Record, unsigned &Idx) {
9598
9589
std::string ASTReader::ReadPath (ModuleFile &F, const RecordData &Record,
9599
9590
unsigned &Idx) {
9600
9591
std::string Filename = ReadString (Record, Idx);
9601
- ResolveImportedPath (F, Filename);
9602
- return Filename;
9592
+ return ResolveImportedPath (Filename, F).str ();
9603
9593
}
9604
9594
9605
9595
std::string ASTReader::ReadPath (StringRef BaseDirectory,
9606
9596
const RecordData &Record, unsigned &Idx) {
9607
9597
std::string Filename = ReadString (Record, Idx);
9608
- if (!BaseDirectory.empty ())
9609
- ResolveImportedPath (Filename, BaseDirectory);
9610
- return Filename;
9598
+ return ResolveImportedPath (Filename, BaseDirectory).str ();
9611
9599
}
9612
9600
9613
9601
VersionTuple ASTReader::ReadVersionTuple (const RecordData &Record,
0 commit comments