Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:56

0001 //===--- SerializablePathCollection.h -- Index of paths ---------*- 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_CLANG_INDEXSERIALIZATION_SERIALIZABLEPATHCOLLECTION_H
0010 #define LLVM_CLANG_INDEXSERIALIZATION_SERIALIZABLEPATHCOLLECTION_H
0011 
0012 #include "clang/Basic/FileManager.h"
0013 #include "llvm/ADT/APInt.h"
0014 #include "llvm/ADT/DenseMap.h"
0015 #include "llvm/ADT/SmallString.h"
0016 #include "llvm/ADT/StringMap.h"
0017 #include "llvm/ADT/StringRef.h"
0018 #include "llvm/ADT/iterator.h"
0019 
0020 #include <string>
0021 #include <vector>
0022 
0023 namespace clang {
0024 namespace index {
0025 
0026 /// Pool of strings
0027 class StringPool {
0028   llvm::SmallString<512> Buffer;
0029 
0030 public:
0031   struct StringOffsetSize {
0032     std::size_t Offset;
0033     std::size_t Size;
0034 
0035     StringOffsetSize(size_t Offset, size_t Size) : Offset(Offset), Size(Size) {}
0036   };
0037 
0038   StringOffsetSize add(StringRef Str);
0039   StringRef getBuffer() const { return Buffer; }
0040 };
0041 
0042 /// Pool of filesystem paths backed by a StringPool
0043 class PathPool {
0044 public:
0045   /// Special root directory of a filesystem path.
0046   enum class RootDirKind {
0047     Regular = 0,
0048     CurrentWorkDir = 1,
0049     SysRoot = 2,
0050   };
0051 
0052   struct DirPath {
0053     RootDirKind Root;
0054     StringPool::StringOffsetSize Path;
0055 
0056     DirPath(RootDirKind Root, const StringPool::StringOffsetSize &Path)
0057         : Root(Root), Path(Path) {}
0058   };
0059 
0060   struct FilePath {
0061     DirPath Dir;
0062     StringPool::StringOffsetSize Filename;
0063 
0064     FilePath(const DirPath &Dir, const StringPool::StringOffsetSize &Filename)
0065         : Dir(Dir), Filename(Filename) {}
0066   };
0067 
0068   /// \returns index of the newly added file in FilePaths.
0069   size_t addFilePath(RootDirKind Root, const StringPool::StringOffsetSize &Dir,
0070                      StringRef Filename);
0071 
0072   /// \returns offset in Paths and size of newly added directory.
0073   StringPool::StringOffsetSize addDirPath(StringRef Dir);
0074 
0075   llvm::ArrayRef<FilePath> getFilePaths() const;
0076 
0077   StringRef getPaths() const;
0078 
0079 private:
0080   StringPool Paths;
0081   std::vector<FilePath> FilePaths;
0082 };
0083 
0084 /// Stores file paths and produces serialization-friendly representation.
0085 class SerializablePathCollection {
0086   std::string WorkDir;
0087   std::string SysRoot;
0088 
0089   PathPool Paths;
0090   llvm::DenseMap<const clang::FileEntry *, std::size_t> UniqueFiles;
0091   llvm::StringMap<PathPool::DirPath, llvm::BumpPtrAllocator> UniqueDirs;
0092 
0093 public:
0094   const StringPool::StringOffsetSize WorkDirPath;
0095   const StringPool::StringOffsetSize SysRootPath;
0096   const StringPool::StringOffsetSize OutputFilePath;
0097 
0098   SerializablePathCollection(llvm::StringRef CurrentWorkDir,
0099                              llvm::StringRef SysRoot,
0100                              llvm::StringRef OutputFile);
0101 
0102   /// \returns buffer containing all the paths.
0103   llvm::StringRef getPathsBuffer() const { return Paths.getPaths(); }
0104 
0105   /// \returns file paths (no directories) backed by buffer exposed in
0106   /// getPathsBuffer.
0107   ArrayRef<PathPool::FilePath> getFilePaths() const {
0108     return Paths.getFilePaths();
0109   }
0110 
0111   /// Stores path to \p FE if it hasn't been stored yet.
0112   /// \returns index to array exposed by getPathsBuffer().
0113   size_t tryStoreFilePath(FileEntryRef FE);
0114 
0115 private:
0116   /// Stores \p Path if it is non-empty.
0117   /// Warning: this method doesn't check for uniqueness.
0118   /// \returns offset of \p Path value begin in buffer with stored paths.
0119   StringPool::StringOffsetSize storePath(llvm::StringRef Path);
0120 
0121   /// Stores \p dirStr path if it hasn't been stored yet.
0122   PathPool::DirPath tryStoreDirPath(llvm::StringRef dirStr);
0123 };
0124 
0125 } // namespace index
0126 } // namespace clang
0127 
0128 #endif // LLVM_CLANG_INDEXSERIALIZATION_SERIALIZABLEPATHCOLLECTION_H