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