xref: /freebsd-src/contrib/llvm-project/clang/lib/IndexSerialization/SerializablePathCollection.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
1*e8d8bef9SDimitry Andric //===--- SerializablePathCollection.cpp -- Index of paths -------*- C++ -*-===//
2*e8d8bef9SDimitry Andric //
3*e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*e8d8bef9SDimitry Andric //
7*e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8*e8d8bef9SDimitry Andric 
9*e8d8bef9SDimitry Andric #include "clang/IndexSerialization/SerializablePathCollection.h"
10*e8d8bef9SDimitry Andric #include "llvm/Support/Path.h"
11*e8d8bef9SDimitry Andric 
12*e8d8bef9SDimitry Andric using namespace llvm;
13*e8d8bef9SDimitry Andric using namespace clang;
14*e8d8bef9SDimitry Andric using namespace clang::index;
15*e8d8bef9SDimitry Andric 
16*e8d8bef9SDimitry Andric StringPool::StringOffsetSize StringPool::add(StringRef Str) {
17*e8d8bef9SDimitry Andric   const std::size_t Offset = Buffer.size();
18*e8d8bef9SDimitry Andric   Buffer += Str;
19*e8d8bef9SDimitry Andric   return StringPool::StringOffsetSize(Offset, Str.size());
20*e8d8bef9SDimitry Andric }
21*e8d8bef9SDimitry Andric 
22*e8d8bef9SDimitry Andric size_t PathPool::addFilePath(RootDirKind Root,
23*e8d8bef9SDimitry Andric                              const StringPool::StringOffsetSize &Dir,
24*e8d8bef9SDimitry Andric                              StringRef Filename) {
25*e8d8bef9SDimitry Andric   FilePaths.emplace_back(DirPath(Root, Dir), Paths.add(Filename));
26*e8d8bef9SDimitry Andric   return FilePaths.size() - 1;
27*e8d8bef9SDimitry Andric }
28*e8d8bef9SDimitry Andric 
29*e8d8bef9SDimitry Andric StringPool::StringOffsetSize PathPool::addDirPath(StringRef Dir) {
30*e8d8bef9SDimitry Andric   return Paths.add(Dir);
31*e8d8bef9SDimitry Andric }
32*e8d8bef9SDimitry Andric 
33*e8d8bef9SDimitry Andric llvm::ArrayRef<PathPool::FilePath> PathPool::getFilePaths() const {
34*e8d8bef9SDimitry Andric   return FilePaths;
35*e8d8bef9SDimitry Andric }
36*e8d8bef9SDimitry Andric 
37*e8d8bef9SDimitry Andric StringRef PathPool::getPaths() const { return Paths.getBuffer(); }
38*e8d8bef9SDimitry Andric 
39*e8d8bef9SDimitry Andric SerializablePathCollection::SerializablePathCollection(
40*e8d8bef9SDimitry Andric     StringRef CurrentWorkDir, StringRef SysRoot, llvm::StringRef OutputFile)
41*e8d8bef9SDimitry Andric     : WorkDir(CurrentWorkDir),
42*e8d8bef9SDimitry Andric       SysRoot(llvm::sys::path::parent_path(SysRoot).empty() ? StringRef()
43*e8d8bef9SDimitry Andric                                                             : SysRoot),
44*e8d8bef9SDimitry Andric       WorkDirPath(Paths.addDirPath(WorkDir)),
45*e8d8bef9SDimitry Andric       SysRootPath(Paths.addDirPath(SysRoot)),
46*e8d8bef9SDimitry Andric       OutputFilePath(Paths.addDirPath(OutputFile)) {}
47*e8d8bef9SDimitry Andric 
48*e8d8bef9SDimitry Andric size_t SerializablePathCollection::tryStoreFilePath(const FileEntry &FE) {
49*e8d8bef9SDimitry Andric   auto FileIt = UniqueFiles.find(&FE);
50*e8d8bef9SDimitry Andric   if (FileIt != UniqueFiles.end())
51*e8d8bef9SDimitry Andric     return FileIt->second;
52*e8d8bef9SDimitry Andric 
53*e8d8bef9SDimitry Andric   const auto Dir = tryStoreDirPath(sys::path::parent_path(FE.getName()));
54*e8d8bef9SDimitry Andric   const auto FileIdx =
55*e8d8bef9SDimitry Andric       Paths.addFilePath(Dir.Root, Dir.Path, sys::path::filename(FE.getName()));
56*e8d8bef9SDimitry Andric 
57*e8d8bef9SDimitry Andric   UniqueFiles.try_emplace(&FE, FileIdx);
58*e8d8bef9SDimitry Andric   return FileIdx;
59*e8d8bef9SDimitry Andric }
60*e8d8bef9SDimitry Andric 
61*e8d8bef9SDimitry Andric PathPool::DirPath SerializablePathCollection::tryStoreDirPath(StringRef Dir) {
62*e8d8bef9SDimitry Andric   // We don't want to strip separator if Dir is "/" - so we check size > 1.
63*e8d8bef9SDimitry Andric   while (Dir.size() > 1 && llvm::sys::path::is_separator(Dir.back()))
64*e8d8bef9SDimitry Andric     Dir = Dir.drop_back();
65*e8d8bef9SDimitry Andric 
66*e8d8bef9SDimitry Andric   auto DirIt = UniqueDirs.find(Dir);
67*e8d8bef9SDimitry Andric   if (DirIt != UniqueDirs.end())
68*e8d8bef9SDimitry Andric     return DirIt->second;
69*e8d8bef9SDimitry Andric 
70*e8d8bef9SDimitry Andric   const std::string OrigDir = Dir.str();
71*e8d8bef9SDimitry Andric 
72*e8d8bef9SDimitry Andric   PathPool::RootDirKind Root = PathPool::RootDirKind::Regular;
73*e8d8bef9SDimitry Andric   if (!SysRoot.empty() && Dir.startswith(SysRoot) &&
74*e8d8bef9SDimitry Andric       llvm::sys::path::is_separator(Dir[SysRoot.size()])) {
75*e8d8bef9SDimitry Andric     Root = PathPool::RootDirKind::SysRoot;
76*e8d8bef9SDimitry Andric     Dir = Dir.drop_front(SysRoot.size());
77*e8d8bef9SDimitry Andric   } else if (!WorkDir.empty() && Dir.startswith(WorkDir) &&
78*e8d8bef9SDimitry Andric              llvm::sys::path::is_separator(Dir[WorkDir.size()])) {
79*e8d8bef9SDimitry Andric     Root = PathPool::RootDirKind::CurrentWorkDir;
80*e8d8bef9SDimitry Andric     Dir = Dir.drop_front(WorkDir.size());
81*e8d8bef9SDimitry Andric   }
82*e8d8bef9SDimitry Andric 
83*e8d8bef9SDimitry Andric   if (Root != PathPool::RootDirKind::Regular) {
84*e8d8bef9SDimitry Andric     while (!Dir.empty() && llvm::sys::path::is_separator(Dir.front()))
85*e8d8bef9SDimitry Andric       Dir = Dir.drop_front();
86*e8d8bef9SDimitry Andric   }
87*e8d8bef9SDimitry Andric 
88*e8d8bef9SDimitry Andric   PathPool::DirPath Result(Root, Paths.addDirPath(Dir));
89*e8d8bef9SDimitry Andric   UniqueDirs.try_emplace(OrigDir, Result);
90*e8d8bef9SDimitry Andric   return Result;
91*e8d8bef9SDimitry Andric }
92