Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:30

0001 //===-- FileCollector.h -----------------------------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_SUPPORT_FILECOLLECTOR_H
0010 #define LLVM_SUPPORT_FILECOLLECTOR_H
0011 
0012 #include "llvm/ADT/StringMap.h"
0013 #include "llvm/ADT/StringSet.h"
0014 #include "llvm/Support/VirtualFileSystem.h"
0015 #include <mutex>
0016 #include <string>
0017 
0018 namespace llvm {
0019 class FileCollectorFileSystem;
0020 class Twine;
0021 
0022 class FileCollectorBase {
0023 public:
0024   FileCollectorBase();
0025   virtual ~FileCollectorBase();
0026 
0027   void addFile(const Twine &file);
0028   void addDirectory(const Twine &Dir);
0029 
0030 protected:
0031   bool markAsSeen(StringRef Path) {
0032     if (Path.empty())
0033       return false;
0034     return Seen.insert(Path).second;
0035   }
0036 
0037   virtual void addFileImpl(StringRef SrcPath) = 0;
0038 
0039   virtual llvm::vfs::directory_iterator
0040   addDirectoryImpl(const llvm::Twine &Dir,
0041                    IntrusiveRefCntPtr<vfs::FileSystem> FS,
0042                    std::error_code &EC) = 0;
0043 
0044   /// Synchronizes access to internal data structures.
0045   std::mutex Mutex;
0046 
0047   /// Tracks already seen files so they can be skipped.
0048   StringSet<> Seen;
0049 };
0050 
0051 /// Captures file system interaction and generates data to be later replayed
0052 /// with the RedirectingFileSystem.
0053 ///
0054 /// For any file that gets accessed we eventually create:
0055 /// - a copy of the file inside Root
0056 /// - a record in RedirectingFileSystem mapping that maps:
0057 ///   current real path -> path to the copy in Root
0058 ///
0059 /// That intent is that later when the mapping is used by RedirectingFileSystem
0060 /// it simulates the state of FS that we collected.
0061 ///
0062 /// We generate file copies and mapping lazily - see writeMapping and copyFiles.
0063 /// We don't try to capture the state of the file at the exact time when it's
0064 /// accessed. Files might get changed, deleted ... we record only the "final"
0065 /// state.
0066 ///
0067 /// In order to preserve the relative topology of files we use their real paths
0068 /// as relative paths inside of the Root.
0069 class FileCollector : public FileCollectorBase {
0070 public:
0071   /// Helper utility that encapsulates the logic for canonicalizing a virtual
0072   /// path and a path to copy from.
0073   class PathCanonicalizer {
0074   public:
0075     struct PathStorage {
0076       SmallString<256> CopyFrom;
0077       SmallString<256> VirtualPath;
0078     };
0079 
0080     /// Canonicalize a pair of virtual and real paths.
0081     PathStorage canonicalize(StringRef SrcPath);
0082 
0083   private:
0084     /// Replace with a (mostly) real path, or don't modify. Resolves symlinks
0085     /// in the directory, using \a CachedDirs to avoid redundant lookups, but
0086     /// leaves the filename as a possible symlink.
0087     void updateWithRealPath(SmallVectorImpl<char> &Path);
0088 
0089     StringMap<std::string> CachedDirs;
0090   };
0091 
0092   /// \p Root is the directory where collected files are will be stored.
0093   /// \p OverlayRoot is VFS mapping root.
0094   /// \p Root directory gets created in copyFiles unless it already exists.
0095   FileCollector(std::string Root, std::string OverlayRoot);
0096 
0097   /// Write the yaml mapping (for the VFS) to the given file.
0098   std::error_code writeMapping(StringRef MappingFile);
0099 
0100   /// Copy the files into the root directory.
0101   ///
0102   /// When StopOnError is true (the default) we abort as soon as one file
0103   /// cannot be copied. This is relatively common, for example when a file was
0104   /// removed after it was added to the mapping.
0105   std::error_code copyFiles(bool StopOnError = true);
0106 
0107   /// Create a VFS that uses \p Collector to collect files accessed via \p
0108   /// BaseFS.
0109   static IntrusiveRefCntPtr<vfs::FileSystem>
0110   createCollectorVFS(IntrusiveRefCntPtr<vfs::FileSystem> BaseFS,
0111                      std::shared_ptr<FileCollector> Collector);
0112 
0113 private:
0114   friend FileCollectorFileSystem;
0115 
0116   void addFileToMapping(StringRef VirtualPath, StringRef RealPath) {
0117     if (sys::fs::is_directory(VirtualPath))
0118       VFSWriter.addDirectoryMapping(VirtualPath, RealPath);
0119     else
0120       VFSWriter.addFileMapping(VirtualPath, RealPath);
0121   }
0122 
0123 protected:
0124   void addFileImpl(StringRef SrcPath) override;
0125 
0126   llvm::vfs::directory_iterator
0127   addDirectoryImpl(const llvm::Twine &Dir,
0128                    IntrusiveRefCntPtr<vfs::FileSystem> FS,
0129                    std::error_code &EC) override;
0130 
0131   /// The directory where collected files are copied to in copyFiles().
0132   const std::string Root;
0133 
0134   /// The root directory where the VFS overlay lives.
0135   const std::string OverlayRoot;
0136 
0137   /// The yaml mapping writer.
0138   vfs::YAMLVFSWriter VFSWriter;
0139 
0140   /// Helper utility for canonicalizing paths.
0141   PathCanonicalizer Canonicalizer;
0142 };
0143 
0144 } // end namespace llvm
0145 
0146 #endif // LLVM_SUPPORT_FILECOLLECTOR_H