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