@@ -1636,90 +1636,6 @@ static void checkConfigMacros(Preprocessor &PP, Module *M,
16361636 }
16371637}
16381638
1639- // / Write a new timestamp file with the given path.
1640- static void writeTimestampFile (StringRef TimestampFile) {
1641- std::error_code EC;
1642- llvm::raw_fd_ostream Out (TimestampFile.str (), EC, llvm::sys::fs::OF_None);
1643- }
1644-
1645- // / Prune the module cache of modules that haven't been accessed in
1646- // / a long time.
1647- static void pruneModuleCache (const HeaderSearchOptions &HSOpts) {
1648- llvm::sys::fs::file_status StatBuf;
1649- llvm::SmallString<128 > TimestampFile;
1650- TimestampFile = HSOpts.ModuleCachePath ;
1651- assert (!TimestampFile.empty ());
1652- llvm::sys::path::append (TimestampFile, " modules.timestamp" );
1653-
1654- // Try to stat() the timestamp file.
1655- if (std::error_code EC = llvm::sys::fs::status (TimestampFile, StatBuf)) {
1656- // If the timestamp file wasn't there, create one now.
1657- if (EC == std::errc::no_such_file_or_directory) {
1658- writeTimestampFile (TimestampFile);
1659- }
1660- return ;
1661- }
1662-
1663- // Check whether the time stamp is older than our pruning interval.
1664- // If not, do nothing.
1665- time_t TimeStampModTime =
1666- llvm::sys::toTimeT (StatBuf.getLastModificationTime ());
1667- time_t CurrentTime = time (nullptr );
1668- if (CurrentTime - TimeStampModTime <= time_t (HSOpts.ModuleCachePruneInterval ))
1669- return ;
1670-
1671- // Write a new timestamp file so that nobody else attempts to prune.
1672- // There is a benign race condition here, if two Clang instances happen to
1673- // notice at the same time that the timestamp is out-of-date.
1674- writeTimestampFile (TimestampFile);
1675-
1676- // Walk the entire module cache, looking for unused module files and module
1677- // indices.
1678- std::error_code EC;
1679- for (llvm::sys::fs::directory_iterator Dir (HSOpts.ModuleCachePath , EC),
1680- DirEnd;
1681- Dir != DirEnd && !EC; Dir.increment (EC)) {
1682- // If we don't have a directory, there's nothing to look into.
1683- if (!llvm::sys::fs::is_directory (Dir->path ()))
1684- continue ;
1685-
1686- // Walk all of the files within this directory.
1687- for (llvm::sys::fs::directory_iterator File (Dir->path (), EC), FileEnd;
1688- File != FileEnd && !EC; File.increment (EC)) {
1689- // We only care about module and global module index files.
1690- StringRef Extension = llvm::sys::path::extension (File->path ());
1691- if (Extension != " .pcm" && Extension != " .timestamp" &&
1692- llvm::sys::path::filename (File->path ()) != " modules.idx" )
1693- continue ;
1694-
1695- // Look at this file. If we can't stat it, there's nothing interesting
1696- // there.
1697- if (llvm::sys::fs::status (File->path (), StatBuf))
1698- continue ;
1699-
1700- // If the file has been used recently enough, leave it there.
1701- time_t FileAccessTime = llvm::sys::toTimeT (StatBuf.getLastAccessedTime ());
1702- if (CurrentTime - FileAccessTime <=
1703- time_t (HSOpts.ModuleCachePruneAfter )) {
1704- continue ;
1705- }
1706-
1707- // Remove the file.
1708- llvm::sys::fs::remove (File->path ());
1709-
1710- // Remove the timestamp file.
1711- std::string TimpestampFilename = File->path () + " .timestamp" ;
1712- llvm::sys::fs::remove (TimpestampFilename);
1713- }
1714-
1715- // If we removed all of the files in the directory, remove the directory
1716- // itself.
1717- if (llvm::sys::fs::directory_iterator (Dir->path (), EC) ==
1718- llvm::sys::fs::directory_iterator () && !EC)
1719- llvm::sys::fs::remove (Dir->path ());
1720- }
1721- }
1722-
17231639void CompilerInstance::createASTReader () {
17241640 if (TheASTReader)
17251641 return ;
@@ -1730,11 +1646,10 @@ void CompilerInstance::createASTReader() {
17301646 // If we're implicitly building modules but not currently recursively
17311647 // building a module, check whether we need to prune the module cache.
17321648 if (getSourceManager ().getModuleBuildStack ().empty () &&
1733- !getPreprocessor ().getHeaderSearchInfo ().getModuleCachePath ().empty () &&
1734- getHeaderSearchOpts ().ModuleCachePruneInterval > 0 &&
1735- getHeaderSearchOpts ().ModuleCachePruneAfter > 0 ) {
1736- pruneModuleCache (getHeaderSearchOpts ());
1737- }
1649+ !getPreprocessor ().getHeaderSearchInfo ().getModuleCachePath ().empty ())
1650+ ModCache->maybePrune (getHeaderSearchOpts ().ModuleCachePath ,
1651+ getHeaderSearchOpts ().ModuleCachePruneInterval ,
1652+ getHeaderSearchOpts ().ModuleCachePruneAfter );
17381653
17391654 HeaderSearchOptions &HSOpts = getHeaderSearchOpts ();
17401655 std::string Sysroot = HSOpts.Sysroot ;
0 commit comments