clang  14.0.0git
SerializablePathCollection.h
Go to the documentation of this file.
1 //===--- SerializablePathCollection.h -- Index of paths ---------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_CLANG_INDEX_SerializablePathCollection_H
10 #define LLVM_CLANG_INDEX_SerializablePathCollection_H
11 
13 #include "llvm/ADT/APInt.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/iterator.h"
20 
21 #include <string>
22 #include <vector>
23 
24 namespace clang {
25 namespace index {
26 
27 /// Pool of strings
28 class StringPool {
30 
31 public:
35 
36  StringOffsetSize(size_t Offset, size_t Size) : Offset(Offset), Size(Size) {}
37  };
38 
39  StringOffsetSize add(StringRef Str);
40  StringRef getBuffer() const { return Buffer; }
41 };
42 
43 /// Pool of filesystem paths backed by a StringPool
44 class PathPool {
45 public:
46  /// Special root directory of a filesystem path.
47  enum class RootDirKind {
48  Regular = 0,
49  CurrentWorkDir = 1,
50  SysRoot = 2,
51  };
52 
53  struct DirPath {
56 
58  : Root(Root), Path(Path) {}
59  };
60 
61  struct FilePath {
64 
66  : Dir(Dir), Filename(Filename) {}
67  };
68 
69  /// \returns index of the newly added file in FilePaths.
71  StringRef Filename);
72 
73  /// \returns offset in Paths and size of newly added directory.
75 
77 
78  StringRef getPaths() const;
79 
80 private:
81  StringPool Paths;
82  std::vector<FilePath> FilePaths;
83 };
84 
85 /// Stores file paths and produces serialization-friendly representation.
87  std::string WorkDir;
88  std::string SysRoot;
89 
90  PathPool Paths;
91  llvm::DenseMap<const clang::FileEntry *, std::size_t> UniqueFiles;
92  llvm::StringMap<PathPool::DirPath, llvm::BumpPtrAllocator> UniqueDirs;
93 
94 public:
98 
99  SerializablePathCollection(llvm::StringRef CurrentWorkDir,
100  llvm::StringRef SysRoot,
101  llvm::StringRef OutputFile);
102 
103  /// \returns buffer containing all the paths.
104  llvm::StringRef getPathsBuffer() const { return Paths.getPaths(); }
105 
106  /// \returns file paths (no directories) backed by buffer exposed in
107  /// getPathsBuffer.
109  return Paths.getFilePaths();
110  }
111 
112  /// Stores path to \p FE if it hasn't been stored yet.
113  /// \returns index to array exposed by getPathsBuffer().
114  size_t tryStoreFilePath(const clang::FileEntry &FE);
115 
116 private:
117  /// Stores \p Path if it is non-empty.
118  /// Warning: this method doesn't check for uniqueness.
119  /// \returns offset of \p Path value begin in buffer with stored paths.
120  StringPool::StringOffsetSize storePath(llvm::StringRef Path);
121 
122  /// Stores \p dirStr path if it hasn't been stored yet.
123  PathPool::DirPath tryStoreDirPath(llvm::StringRef dirStr);
124 };
125 
126 } // namespace index
127 } // namespace clang
128 
129 #endif // LLVM_CLANG_INDEX_SerializablePathCollection_H
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::index::SerializablePathCollection::OutputFilePath
const StringPool::StringOffsetSize OutputFilePath
Definition: SerializablePathCollection.h:97
clang::index::SerializablePathCollection::getFilePaths
ArrayRef< PathPool::FilePath > getFilePaths() const
Definition: SerializablePathCollection.h:108
clang::index::SerializablePathCollection::SysRootPath
const StringPool::StringOffsetSize SysRootPath
Definition: SerializablePathCollection.h:96
clang::index::PathPool::FilePath::Dir
DirPath Dir
Definition: SerializablePathCollection.h:62
Filename
StringRef Filename
Definition: Format.cpp:2379
clang::index::SerializablePathCollection
Stores file paths and produces serialization-friendly representation.
Definition: SerializablePathCollection.h:86
clang::index::SerializablePathCollection::tryStoreFilePath
size_t tryStoreFilePath(const clang::FileEntry &FE)
Stores path to FE if it hasn't been stored yet.
Definition: SerializablePathCollection.cpp:48
clang::index::PathPool::getFilePaths
llvm::ArrayRef< FilePath > getFilePaths() const
Definition: SerializablePathCollection.cpp:33
clang::index::PathPool::DirPath
Definition: SerializablePathCollection.h:53
clang::index::StringPool
Pool of strings.
Definition: SerializablePathCollection.h:28
size_t
__SIZE_TYPE__ size_t
The unsigned integer type of the result of the sizeof operator.
Definition: opencl-c-base.h:102
clang::index::PathPool::addDirPath
StringPool::StringOffsetSize addDirPath(StringRef Dir)
Definition: SerializablePathCollection.cpp:29
clang::index::PathPool::DirPath::Root
RootDirKind Root
Definition: SerializablePathCollection.h:54
clang::index::PathPool::DirPath::DirPath
DirPath(RootDirKind Root, const StringPool::StringOffsetSize &Path)
Definition: SerializablePathCollection.h:57
llvm::SmallString< 512 >
clang::index::SerializablePathCollection::SerializablePathCollection
SerializablePathCollection(llvm::StringRef CurrentWorkDir, llvm::StringRef SysRoot, llvm::StringRef OutputFile)
Definition: SerializablePathCollection.cpp:39
clang::FileEntry
Cached information about one file (either on disk or in the virtual file system).
Definition: FileEntry.h:331
clang::index::SerializablePathCollection::WorkDirPath
const StringPool::StringOffsetSize WorkDirPath
Definition: SerializablePathCollection.h:95
clang::index::PathPool::RootDirKind::SysRoot
@ SysRoot
clang::index::StringPool::add
StringOffsetSize add(StringRef Str)
Definition: SerializablePathCollection.cpp:16
clang::index::PathPool::FilePath::FilePath
FilePath(const DirPath &Dir, const StringPool::StringOffsetSize &Filename)
Definition: SerializablePathCollection.h:65
clang::index::PathPool::RootDirKind::CurrentWorkDir
@ CurrentWorkDir
llvm::ArrayRef
Definition: LLVM.h:34
clang::index::PathPool::RootDirKind
RootDirKind
Special root directory of a filesystem path.
Definition: SerializablePathCollection.h:47
clang::index::StringPool::getBuffer
StringRef getBuffer() const
Definition: SerializablePathCollection.h:40
clang::index::PathPool::FilePath::Filename
StringPool::StringOffsetSize Filename
Definition: SerializablePathCollection.h:63
clang::index::SerializablePathCollection::getPathsBuffer
llvm::StringRef getPathsBuffer() const
Definition: SerializablePathCollection.h:104
clang::index::PathPool::FilePath
Definition: SerializablePathCollection.h:61
clang::index::StringPool::StringOffsetSize
Definition: SerializablePathCollection.h:32
clang
Definition: CalledOnceCheck.h:17
clang::index::StringPool::StringOffsetSize::Offset
std::size_t Offset
Definition: SerializablePathCollection.h:33
clang::index::StringPool::StringOffsetSize::StringOffsetSize
StringOffsetSize(size_t Offset, size_t Size)
Definition: SerializablePathCollection.h:36
clang::index::PathPool::DirPath::Path
StringPool::StringOffsetSize Path
Definition: SerializablePathCollection.h:55
FileManager.h
clang::index::PathPool::addFilePath
size_t addFilePath(RootDirKind Root, const StringPool::StringOffsetSize &Dir, StringRef Filename)
Definition: SerializablePathCollection.cpp:22
clang::index::PathPool::getPaths
StringRef getPaths() const
Definition: SerializablePathCollection.cpp:37
clang::index::StringPool::StringOffsetSize::Size
std::size_t Size
Definition: SerializablePathCollection.h:34
clang::index::PathPool
Pool of filesystem paths backed by a StringPool.
Definition: SerializablePathCollection.h:44
clang::index::PathPool::RootDirKind::Regular
@ Regular