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