Coverage Report

Created: 2022-01-18 06:27

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