diff options
author | Sam McCall <sam.mccall@gmail.com> | 2020-04-11 11:02:46 +0200 |
---|---|---|
committer | Sam McCall <sam.mccall@gmail.com> | 2020-06-08 14:03:08 +0200 |
commit | 615673f3a10e98f33e2db64512be0452145236fe (patch) | |
tree | 884c9403876ad0bd4365ce8936c8c76f2a76f005 /clang/lib/Frontend/PrecompiledPreamble.cpp | |
parent | cda166c37ce98b0934c9c4780ad39ce6885da724 (diff) | |
download | llvm-615673f3a10e98f33e2db64512be0452145236fe.zip llvm-615673f3a10e98f33e2db64512be0452145236fe.tar.gz llvm-615673f3a10e98f33e2db64512be0452145236fe.tar.bz2 |
[Preamble] Invalidate preamble when missing headers become present.
Summary:
To avoid excessive extra stat()s, only check the possible locations of
headers that weren't found at all (leading to a compile error).
For headers that *were* found, we don't check for files earlier on the
search path that could override them.
Reviewers: kadircet
Subscribers: javed.absar, jkorous, arphaman, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D77942
Diffstat (limited to 'clang/lib/Frontend/PrecompiledPreamble.cpp')
-rw-r--r-- | clang/lib/Frontend/PrecompiledPreamble.cpp | 101 |
1 files changed, 97 insertions, 4 deletions
diff --git a/clang/lib/Frontend/PrecompiledPreamble.cpp b/clang/lib/Frontend/PrecompiledPreamble.cpp index 442ad63..31c5f18 100644 --- a/clang/lib/Frontend/PrecompiledPreamble.cpp +++ b/clang/lib/Frontend/PrecompiledPreamble.cpp @@ -19,15 +19,19 @@ #include "clang/Frontend/CompilerInvocation.h" #include "clang/Frontend/FrontendActions.h" #include "clang/Frontend/FrontendOptions.h" +#include "clang/Lex/HeaderSearch.h" #include "clang/Lex/Lexer.h" #include "clang/Lex/Preprocessor.h" #include "clang/Lex/PreprocessorOptions.h" #include "clang/Serialization/ASTWriter.h" +#include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringSet.h" +#include "llvm/ADT/iterator_range.h" #include "llvm/Config/llvm-config.h" #include "llvm/Support/CrashRecoveryContext.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Support/Path.h" #include "llvm/Support/Process.h" #include "llvm/Support/VirtualFileSystem.h" #include <limits> @@ -74,6 +78,68 @@ public: bool needSystemDependencies() override { return true; } }; +// Collects files whose existence would invalidate the preamble. +// Collecting *all* of these would make validating it too slow though, so we +// just find all the candidates for 'file not found' diagnostics. +// +// A caveat that may be significant for generated files: we'll omit files under +// search path entries whose roots don't exist when the preamble is built. +// These are pruned by InitHeaderSearch and so we don't see the search path. +// It would be nice to include them but we don't want to duplicate all the rest +// of the InitHeaderSearch logic to reconstruct them. +class MissingFileCollector : public PPCallbacks { + llvm::StringSet<> &Out; + const HeaderSearch &Search; + const SourceManager &SM; + +public: + MissingFileCollector(llvm::StringSet<> &Out, const HeaderSearch &Search, + const SourceManager &SM) + : Out(Out), Search(Search), SM(SM) {} + + void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, + StringRef FileName, bool IsAngled, + CharSourceRange FilenameRange, const FileEntry *File, + StringRef SearchPath, StringRef RelativePath, + const Module *Imported, + SrcMgr::CharacteristicKind FileType) override { + // File is null if it wasn't found. + // (We have some false negatives if PP recovered e.g. <foo> -> "foo") + if (File != nullptr) + return; + + // If it's a rare absolute include, we know the full path already. + if (llvm::sys::path::is_absolute(FileName)) { + Out.insert(FileName); + return; + } + + // Reconstruct the filenames that would satisfy this directive... + llvm::SmallString<256> Buf; + auto NotFoundRelativeTo = [&](const DirectoryEntry *DE) { + Buf = DE->getName(); + llvm::sys::path::append(Buf, FileName); + llvm::sys::path::remove_dots(Buf, /*remove_dot_dot=*/true); + Out.insert(Buf); + }; + // ...relative to the including file. + if (!IsAngled) { + if (const FileEntry *IncludingFile = + SM.getFileEntryForID(SM.getFileID(IncludeTok.getLocation()))) + if (IncludingFile->getDir()) + NotFoundRelativeTo(IncludingFile->getDir()); + } + // ...relative to the search paths. + for (const auto &Dir : llvm::make_range( + IsAngled ? Search.angled_dir_begin() : Search.search_dir_begin(), + Search.search_dir_end())) { + // No support for frameworks or header maps yet. + if (Dir.isNormalDir()) + NotFoundRelativeTo(Dir.getDir()); + } + } +}; + /// Keeps a track of files to be deleted in destructor. class TemporaryFiles { public: @@ -353,6 +419,11 @@ llvm::ErrorOr<PrecompiledPreamble> PrecompiledPreamble::Build( Clang->getPreprocessor().addPPCallbacks(std::move(DelegatedPPCallbacks)); if (auto CommentHandler = Callbacks.getCommentHandler()) Clang->getPreprocessor().addCommentHandler(CommentHandler); + llvm::StringSet<> MissingFiles; + Clang->getPreprocessor().addPPCallbacks( + std::make_unique<MissingFileCollector>( + MissingFiles, Clang->getPreprocessor().getHeaderSearchInfo(), + Clang->getSourceManager())); if (llvm::Error Err = Act->Execute()) return errorToErrorCode(std::move(Err)); @@ -387,9 +458,9 @@ llvm::ErrorOr<PrecompiledPreamble> PrecompiledPreamble::Build( } } - return PrecompiledPreamble(std::move(Storage), std::move(PreambleBytes), - PreambleEndsAtStartOfLine, - std::move(FilesInPreamble)); + return PrecompiledPreamble( + std::move(Storage), std::move(PreambleBytes), PreambleEndsAtStartOfLine, + std::move(FilesInPreamble), std::move(MissingFiles)); } PreambleBounds PrecompiledPreamble::getBounds() const { @@ -446,6 +517,7 @@ bool PrecompiledPreamble::CanReuse(const CompilerInvocation &Invocation, // First, make a record of those files that have been overridden via // remapping or unsaved_files. std::map<llvm::sys::fs::UniqueID, PreambleFileHash> OverriddenFiles; + llvm::StringSet<> OverriddenAbsPaths; // Either by buffers or files. for (const auto &R : PreprocessorOpts.RemappedFiles) { llvm::vfs::Status Status; if (!moveOnNoError(VFS->status(R.second), Status)) { @@ -453,6 +525,10 @@ bool PrecompiledPreamble::CanReuse(const CompilerInvocation &Invocation, // horrible happened. return false; } + // If a mapped file was previously missing, then it has changed. + llvm::SmallString<128> MappedPath(R.first); + if (!VFS->makeAbsolute(MappedPath)) + OverriddenAbsPaths.insert(MappedPath); OverriddenFiles[Status.getUniqueID()] = PreambleFileHash::createForFile( Status.getSize(), llvm::sys::toTimeT(Status.getLastModificationTime())); @@ -468,6 +544,10 @@ bool PrecompiledPreamble::CanReuse(const CompilerInvocation &Invocation, OverriddenFiles[Status.getUniqueID()] = PreambleHash; else OverridenFileBuffers[RB.first] = PreambleHash; + + llvm::SmallString<128> MappedPath(RB.first); + if (!VFS->makeAbsolute(MappedPath)) + OverriddenAbsPaths.insert(MappedPath); } // Check whether anything has changed. @@ -505,6 +585,17 @@ bool PrecompiledPreamble::CanReuse(const CompilerInvocation &Invocation, F.second.ModTime) return false; } + for (const auto &F : MissingFiles) { + // A missing file may be "provided" by an override buffer or file. + if (OverriddenAbsPaths.count(F.getKey())) + return false; + // If a file previously recorded as missing exists as a regular file, then + // consider the preamble out-of-date. + if (auto Status = VFS->status(F.getKey())) { + if (Status->isRegularFile()) + return false; + } + } return true; } @@ -525,8 +616,10 @@ void PrecompiledPreamble::OverridePreamble( PrecompiledPreamble::PrecompiledPreamble( PCHStorage Storage, std::vector<char> PreambleBytes, bool PreambleEndsAtStartOfLine, - llvm::StringMap<PreambleFileHash> FilesInPreamble) + llvm::StringMap<PreambleFileHash> FilesInPreamble, + llvm::StringSet<> MissingFiles) : Storage(std::move(Storage)), FilesInPreamble(std::move(FilesInPreamble)), + MissingFiles(std::move(MissingFiles)), PreambleBytes(std::move(PreambleBytes)), PreambleEndsAtStartOfLine(PreambleEndsAtStartOfLine) { assert(this->Storage.getKind() != PCHStorage::Kind::Empty); |