Path: blob/main/contrib/llvm-project/clang/lib/IndexSerialization/SerializablePathCollection.cpp
35233 views
//===--- SerializablePathCollection.cpp -- Index of paths -------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "clang/IndexSerialization/SerializablePathCollection.h"9#include "llvm/Support/Path.h"1011using namespace llvm;12using namespace clang;13using namespace clang::index;1415StringPool::StringOffsetSize StringPool::add(StringRef Str) {16const std::size_t Offset = Buffer.size();17Buffer += Str;18return StringPool::StringOffsetSize(Offset, Str.size());19}2021size_t PathPool::addFilePath(RootDirKind Root,22const StringPool::StringOffsetSize &Dir,23StringRef Filename) {24FilePaths.emplace_back(DirPath(Root, Dir), Paths.add(Filename));25return FilePaths.size() - 1;26}2728StringPool::StringOffsetSize PathPool::addDirPath(StringRef Dir) {29return Paths.add(Dir);30}3132llvm::ArrayRef<PathPool::FilePath> PathPool::getFilePaths() const {33return FilePaths;34}3536StringRef PathPool::getPaths() const { return Paths.getBuffer(); }3738SerializablePathCollection::SerializablePathCollection(39StringRef CurrentWorkDir, StringRef SysRoot, llvm::StringRef OutputFile)40: WorkDir(CurrentWorkDir),41SysRoot(llvm::sys::path::parent_path(SysRoot).empty() ? StringRef()42: SysRoot),43WorkDirPath(Paths.addDirPath(WorkDir)),44SysRootPath(Paths.addDirPath(SysRoot)),45OutputFilePath(Paths.addDirPath(OutputFile)) {}4647size_t SerializablePathCollection::tryStoreFilePath(FileEntryRef FE) {48auto FileIt = UniqueFiles.find(FE);49if (FileIt != UniqueFiles.end())50return FileIt->second;5152const auto Dir = tryStoreDirPath(sys::path::parent_path(FE.getName()));53const auto FileIdx =54Paths.addFilePath(Dir.Root, Dir.Path, sys::path::filename(FE.getName()));5556UniqueFiles.try_emplace(FE, FileIdx);57return FileIdx;58}5960PathPool::DirPath SerializablePathCollection::tryStoreDirPath(StringRef Dir) {61// We don't want to strip separator if Dir is "/" - so we check size > 1.62while (Dir.size() > 1 && llvm::sys::path::is_separator(Dir.back()))63Dir = Dir.drop_back();6465auto DirIt = UniqueDirs.find(Dir);66if (DirIt != UniqueDirs.end())67return DirIt->second;6869const std::string OrigDir = Dir.str();7071PathPool::RootDirKind Root = PathPool::RootDirKind::Regular;72if (!SysRoot.empty() && Dir.starts_with(SysRoot) &&73llvm::sys::path::is_separator(Dir[SysRoot.size()])) {74Root = PathPool::RootDirKind::SysRoot;75Dir = Dir.drop_front(SysRoot.size());76} else if (!WorkDir.empty() && Dir.starts_with(WorkDir) &&77llvm::sys::path::is_separator(Dir[WorkDir.size()])) {78Root = PathPool::RootDirKind::CurrentWorkDir;79Dir = Dir.drop_front(WorkDir.size());80}8182if (Root != PathPool::RootDirKind::Regular) {83while (!Dir.empty() && llvm::sys::path::is_separator(Dir.front()))84Dir = Dir.drop_front();85}8687PathPool::DirPath Result(Root, Paths.addDirPath(Dir));88UniqueDirs.try_emplace(OrigDir, Result);89return Result;90}919293