diff --git a/llvm/tools/llvm-cov/CodeCoverage.cpp b/llvm/tools/llvm-cov/CodeCoverage.cpp --- a/llvm/tools/llvm-cov/CodeCoverage.cpp +++ b/llvm/tools/llvm-cov/CodeCoverage.cpp @@ -80,6 +80,13 @@ /// directory, recursively collect all of the paths within the directory. void collectPaths(const std::string &Path); + /// Check if the two given two files are the same file. + bool isEquivalentFile(StringRef FilePath1, StringRef FilePath2); + + /// Retrieve a file status with a cache. + Optional + CodeCoverageTool::getFileStatus(StringRef FilePath); + /// Return a memory buffer for the given source file. ErrorOr getSourceFile(StringRef SourceFile); @@ -153,6 +160,9 @@ /// remapped to, when using -path-equivalence. Optional> PathRemapping; + // File status cache used when finding the same file. + StringMap> FileStatusCache; + /// The architecture the coverage mapping data targets. std::vector CoverageArches; @@ -239,6 +249,29 @@ } } +Optional +CodeCoverageTool::getFileStatus(StringRef FilePath) { + auto It = FileStatusCache.find(FilePath); + if (It != FileStatusCache.end()) + return It->second; + + Optional result; + sys::fs::file_status status; + if (!sys::fs::status(FilePath, status)) + result = status; + + FileStatusCache[FilePath] = result; + return result; +} + +bool CodeCoverageTool::isEquivalentFile(StringRef FilePath1, + StringRef FilePath2) { + auto file_status1 = getFileStatus(FilePath1); + auto file_status2 = getFileStatus(FilePath1); + return file_status1.hasValue() && file_status2.hasValue() && + sys::fs::equivalent(file_status1.getValue(), file_status2.getValue()); +} + ErrorOr CodeCoverageTool::getSourceFile(StringRef SourceFile) { // If we've remapped filenames, look up the real location for this file. @@ -248,9 +281,11 @@ if (Loc != RemappedFilenames.end()) SourceFile = Loc->second; } + for (const auto &Files : LoadedSourceFiles) - if (sys::fs::equivalent(SourceFile, Files.first)) + if (isEquivalentFile(SourceFile, Files.first)) return *Files.second; + auto Buffer = MemoryBuffer::getFile(SourceFile); if (auto EC = Buffer.getError()) { error(EC.message(), SourceFile);