@@ -1185,51 +1185,36 @@ static bool cleanPathForOutput(FileManager &FileMgr,
11851185 return Changed | llvm::sys::path::remove_dots (Path);
11861186}
11871187
1188- // / Adjusts the given filename to only write out the portion of the
1189- // / filename that is not part of the system root directory.
1188+ // / Adjusts the given filename to only write out the portion of the filename
1189+ // / that is not part of the base directory.
11901190// /
11911191// / \param Filename the file name to adjust.
11921192// /
1193- // / \param BaseDir When non-NULL , the PCH file is a relocatable AST file and
1194- // / the returned filename will be adjusted by this root directory .
1193+ // / \param BasePath When not empty , the AST file is relocatable and the returned
1194+ // / filename will be adjusted to be relative to this path .
11951195// /
1196- // / \returns either the original filename (if it needs no adjustment) or the
1197- // / adjusted filename (which points into the @p Filename parameter).
1198- static const char *
1199- adjustFilenameForRelocatableAST (const char *Filename, StringRef BaseDir) {
1200- assert (Filename && " No file name to adjust?" );
1201-
1202- if (BaseDir.empty ())
1203- return Filename;
1204-
1205- // Verify that the filename and the system root have the same prefix.
1206- unsigned Pos = 0 ;
1207- for (; Filename[Pos] && Pos < BaseDir.size (); ++Pos)
1208- if (Filename[Pos] != BaseDir[Pos])
1209- return Filename; // Prefixes don't match.
1210-
1211- // We hit the end of the filename before we hit the end of the system root.
1212- if (!Filename[Pos])
1213- return Filename;
1214-
1215- // If there's not a path separator at the end of the base directory nor
1216- // immediately after it, then this isn't within the base directory.
1217- if (!llvm::sys::path::is_separator (Filename[Pos])) {
1218- if (!llvm::sys::path::is_separator (BaseDir.back ()))
1219- return Filename;
1220- } else {
1221- // If the file name has a '/' at the current position, skip over the '/'.
1222- // We distinguish relative paths from absolute paths by the
1223- // absence of '/' at the beginning of relative paths.
1224- //
1225- // FIXME: This is wrong. We distinguish them by asking if the path is
1226- // absolute, which isn't the same thing. And there might be multiple '/'s
1227- // in a row. Use a better mechanism to indicate whether we have emitted an
1228- // absolute or relative path.
1229- ++Pos;
1230- }
1196+ // / \returns true when \c Filename was adjusted, false otherwise.
1197+ static bool adjustFilenameForRelocatableAST (SmallVectorImpl<char > &Filename,
1198+ StringRef BasePath) {
1199+ auto FileIt = llvm::sys::path::begin ({Filename.begin (), Filename.size ()});
1200+ auto FileEnd = llvm::sys::path::end ({Filename.begin (), Filename.size ()});
1201+
1202+ auto BaseIt = llvm::sys::path::begin (BasePath);
1203+ auto BaseEnd = llvm::sys::path::end (BasePath);
1204+
1205+ for (; FileIt != FileEnd && BaseIt != BaseEnd; ++FileIt, ++BaseIt)
1206+ if (*FileIt != *BaseIt)
1207+ return false ;
1208+
1209+ if (FileIt == FileEnd)
1210+ return false ;
1211+
1212+ SmallString<128 > Clean;
1213+ for (; FileIt != FileEnd; ++FileIt)
1214+ llvm::sys::path::append (Clean, *FileIt);
12311215
1232- return Filename + Pos;
1216+ Filename.assign (Clean.begin (), Clean.end ());
1217+ return true ;
12331218}
12341219
12351220std::pair<ASTFileSignature, ASTFileSignature>
@@ -1712,7 +1697,7 @@ void ASTWriter::WriteControlBlock(Preprocessor &PP, StringRef isysroot) {
17121697
17131698 AddString (HSOpts.Sysroot , Record);
17141699 AddString (HSOpts.ResourceDir , Record);
1715- AddString (HSOpts.ModuleCachePath , Record);
1700+ AddPath (HSOpts.ModuleCachePath , Record);
17161701 AddString (HSOpts.ModuleUserBuildPath , Record);
17171702 Record.push_back (HSOpts.DisableModuleHash );
17181703 Record.push_back (HSOpts.ImplicitModuleMaps );
@@ -5342,16 +5327,8 @@ bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
53425327 return false ;
53435328
53445329 bool Changed = cleanPathForOutput (PP->getFileManager (), Path);
5345-
5346- // Remove a prefix to make the path relative, if relevant.
5347- const char *PathBegin = Path.data ();
5348- const char *PathPtr =
5349- adjustFilenameForRelocatableAST (PathBegin, BaseDirectory);
5350- if (PathPtr != PathBegin) {
5351- Path.erase (Path.begin (), Path.begin () + (PathPtr - PathBegin));
5330+ if (adjustFilenameForRelocatableAST (Path, BaseDirectory))
53525331 Changed = true ;
5353- }
5354-
53555332 return Changed;
53565333}
53575334
0 commit comments