clang  14.0.0git
SerializablePathCollection.cpp
Go to the documentation of this file.
1 //===--- SerializablePathCollection.cpp -- 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 
10 #include "llvm/Support/Path.h"
11 
12 using namespace llvm;
13 using namespace clang;
14 using namespace clang::index;
15 
16 StringPool::StringOffsetSize StringPool::add(StringRef Str) {
17  const std::size_t Offset = Buffer.size();
18  Buffer += Str;
19  return StringPool::StringOffsetSize(Offset, Str.size());
20 }
21 
22 size_t PathPool::addFilePath(RootDirKind Root,
24  StringRef Filename) {
25  FilePaths.emplace_back(DirPath(Root, Dir), Paths.add(Filename));
26  return FilePaths.size() - 1;
27 }
28 
29 StringPool::StringOffsetSize PathPool::addDirPath(StringRef Dir) {
30  return Paths.add(Dir);
31 }
32 
33 llvm::ArrayRef<PathPool::FilePath> PathPool::getFilePaths() const {
34  return FilePaths;
35 }
36 
37 StringRef PathPool::getPaths() const { return Paths.getBuffer(); }
38 
39 SerializablePathCollection::SerializablePathCollection(
40  StringRef CurrentWorkDir, StringRef SysRoot, llvm::StringRef OutputFile)
41  : WorkDir(CurrentWorkDir),
42  SysRoot(llvm::sys::path::parent_path(SysRoot).empty() ? StringRef()
43  : SysRoot),
44  WorkDirPath(Paths.addDirPath(WorkDir)),
45  SysRootPath(Paths.addDirPath(SysRoot)),
46  OutputFilePath(Paths.addDirPath(OutputFile)) {}
47 
49  auto FileIt = UniqueFiles.find(&FE);
50  if (FileIt != UniqueFiles.end())
51  return FileIt->second;
52 
53  const auto Dir = tryStoreDirPath(sys::path::parent_path(FE.getName()));
54  const auto FileIdx =
55  Paths.addFilePath(Dir.Root, Dir.Path, sys::path::filename(FE.getName()));
56 
57  UniqueFiles.try_emplace(&FE, FileIdx);
58  return FileIdx;
59 }
60 
61 PathPool::DirPath SerializablePathCollection::tryStoreDirPath(StringRef Dir) {
62  // We don't want to strip separator if Dir is "/" - so we check size > 1.
63  while (Dir.size() > 1 && llvm::sys::path::is_separator(Dir.back()))
64  Dir = Dir.drop_back();
65 
66  auto DirIt = UniqueDirs.find(Dir);
67  if (DirIt != UniqueDirs.end())
68  return DirIt->second;
69 
70  const std::string OrigDir = Dir.str();
71 
73  if (!SysRoot.empty() && Dir.startswith(SysRoot) &&
74  llvm::sys::path::is_separator(Dir[SysRoot.size()])) {
76  Dir = Dir.drop_front(SysRoot.size());
77  } else if (!WorkDir.empty() && Dir.startswith(WorkDir) &&
78  llvm::sys::path::is_separator(Dir[WorkDir.size()])) {
80  Dir = Dir.drop_front(WorkDir.size());
81  }
82 
83  if (Root != PathPool::RootDirKind::Regular) {
84  while (!Dir.empty() && llvm::sys::path::is_separator(Dir.front()))
85  Dir = Dir.drop_front();
86  }
87 
88  PathPool::DirPath Result(Root, Paths.addDirPath(Dir));
89  UniqueDirs.try_emplace(OrigDir, Result);
90  return Result;
91 }
llvm
Definition: Dominators.h:30
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
Filename
StringRef Filename
Definition: Format.cpp:2379
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::DirPath
Definition: SerializablePathCollection.h:53
Offset
unsigned Offset
Definition: Format.cpp:2381
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::FileEntry
Cached information about one file (either on disk or in the virtual file system).
Definition: FileEntry.h:331
clang::index::PathPool::RootDirKind::SysRoot
@ SysRoot
clang::index
Definition: CommentToXML.h:22
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::StringOffsetSize
Definition: SerializablePathCollection.h:32
clang
Definition: CalledOnceCheck.h:17
clang::FileEntry::getName
StringRef getName() const
Definition: FileEntry.h:364
clang::index::PathPool::addFilePath
size_t addFilePath(RootDirKind Root, const StringPool::StringOffsetSize &Dir, StringRef Filename)
Definition: SerializablePathCollection.cpp:22
clang::index::PathPool::RootDirKind::Regular
@ Regular
SerializablePathCollection.h