File: CMakeFileAPI.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (227 lines) | stat: -rw-r--r-- 8,562 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//===-- CMakeFileAPI.cpp - CMake File API ---------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "CMakeFileAPI.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"

using namespace llvm;

/// \returns true if \p Path is a nested directory in \p ParentPath.
/// \p Path should be absolute. Returns false if \p ParentPath is relative.
static bool isPathNestedIn(StringRef Path, StringRef ParentPath) {
  assert(sys::path::is_absolute(Path));
  if (Path.size() <= ParentPath.size())
    return false;
  if (!Path.startswith(ParentPath))
    return false;
  return sys::path::is_separator(Path.drop_front(ParentPath.size()).front());
}

/// Performs key lookups with \p KeyPath in the form of "key/subkey/anotherkey"
/// and returns the final nested value.
static Expected<const json::Value &> getValueFromPath(const json::Object &Root,
                                                      StringRef KeyPath,
                                                      StringRef ErrPrefix) {
  assert(!KeyPath.empty());
  StringRef Remaining = KeyPath;
  size_t KeyPathPos = 0;

  auto error = [&]() -> Error {
    return createStringError(llvm::inconvertibleErrorCode(),
                             ErrPrefix + ": missing '" +
                                 KeyPath.take_front(KeyPathPos - 1) +
                                 "' entry");
  };

  const json::Object *Obj = &Root;
  while (true) {
    StringRef Key;
    std::tie(Key, Remaining) = Remaining.split('/');
    KeyPathPos += Key.size() + 1;
    if (Remaining.empty()) {
      const json::Value *Val = Obj->get(Key);
      if (!Val)
        return error();
      return *Val;
    }
    Obj = Obj->getObject(Key);
    if (!Obj)
      return error();
  }
}

/// Performs key lookups with \p KeyPath in the form of "key/subkey/anotherkey"
/// and returns the final nested string.
static Expected<StringRef> getStringFromPath(const json::Object &Root,
                                             StringRef KeyPath,
                                             StringRef ErrPrefix) {
  auto Val = getValueFromPath(Root, KeyPath, ErrPrefix);
  if (!Val)
    return Val.takeError();
  std::optional<StringRef> Str = Val->getAsString();
  if (!Str)
    return createStringError(llvm::inconvertibleErrorCode(),
                             ErrPrefix + ": expected string '" + KeyPath +
                                 "' entry");

  return *Str;
}

Expected<cmake_file_api::Index>
cmake_file_api::Index::fromPath(StringRef CMakeBuildPath) {
  // Detect presence of index file like
  // '.cmake/api/v1/reply/index-2019-05-25T23-04-47-0494.json'
  SmallString<128> ReplyPath(CMakeBuildPath);
  sys::path::append(ReplyPath, ".cmake", "api", "v1", "reply");

  std::error_code EC;
  std::optional<std::string> RecentIndexPath;
  for (sys::fs::directory_iterator DirIt(ReplyPath, EC), DirE;
       !EC && DirIt != DirE; DirIt.increment(EC)) {
    StringRef Filename = sys::path::filename(DirIt->path());
    if (Filename.startswith("index-") && Filename.endswith(".json")) {
      if (!RecentIndexPath || sys::path::filename(*RecentIndexPath) < Filename)
        RecentIndexPath = DirIt->path();
    }
  }
  if (EC)
    return createStringError(EC, "failed reading contents of '" + ReplyPath +
                                     "': " + EC.message());

  if (!RecentIndexPath)
    return createStringError(llvm::inconvertibleErrorCode(),
                             "missing 'index' file");
  StringRef IndexPath = *RecentIndexPath;

  ErrorOr<std::unique_ptr<MemoryBuffer>> File =
      MemoryBuffer::getFile(IndexPath);
  if (!File)
    return createStringError(File.getError(),
                             "failed opening '" + IndexPath +
                                 "': " + File.getError().message());

  Expected<json::Value> Val = json::parse((*File)->getBuffer());
  if (!Val)
    return createStringError(llvm::inconvertibleErrorCode(),
                             "json error parsing '" + IndexPath +
                                 "': " + toString(Val.takeError()));
  json::Object *Obj = Val->getAsObject();
  if (!Obj)
    return createStringError(llvm::inconvertibleErrorCode(),
                             "expected json dictionary for 'index' file");
  return cmake_file_api::Index(std::move(*Obj), ReplyPath.str().str());
}

Expected<cmake_file_api::CodeModel>
cmake_file_api::Index::getCodeModel() const {
  Expected<StringRef> CodeModelPath =
      getStringFromPath(Obj, "reply/codemodel-v2/jsonFile", "index");
  if (!CodeModelPath)
    return CodeModelPath.takeError();

  SmallString<128> FullCodeModelPath(CMakeFileAPIPath);
  sys::path::append(FullCodeModelPath, *CodeModelPath);

  ErrorOr<std::unique_ptr<MemoryBuffer>> File =
      MemoryBuffer::getFile(FullCodeModelPath);
  if (!File)
    return createStringError(File.getError(),
                             "failed opening '" + FullCodeModelPath +
                                 "': " + File.getError().message());

  Expected<json::Value> Val = json::parse((*File)->getBuffer());
  if (!Val)
    return createStringError(llvm::inconvertibleErrorCode(),
                             "json error parsing '" + FullCodeModelPath +
                                 "': " + toString(Val.takeError()));
  json::Object *Obj = Val->getAsObject();
  if (!Obj)
    return createStringError(llvm::inconvertibleErrorCode(),
                             "expected json dictionary for 'codemodel' file");
  return cmake_file_api::CodeModel(std::move(*Obj));
}

Expected<StringRef> cmake_file_api::CodeModel::getBuildPath() const {
  Expected<StringRef> BuildPath =
      getStringFromPath(Obj, "paths/build", "codemodel");
  if (!BuildPath)
    return BuildPath.takeError();
  return *BuildPath;
}

Expected<StringRef> cmake_file_api::CodeModel::getSourcePath() const {
  Expected<StringRef> SourcePath =
      getStringFromPath(Obj, "paths/source", "codemodel");
  if (!SourcePath)
    return SourcePath.takeError();
  return *SourcePath;
}

Error cmake_file_api::CodeModel::getExtraTopLevelSourcePaths(
    SmallVectorImpl<StringRef> &TopLevelPaths) const {
  const json::Array *Configs = Obj.getArray("configurations");
  if (!Configs || Configs->empty())
    return createStringError(llvm::inconvertibleErrorCode(),
                             "codemodel: missing 'configurations' entry");
  const json::Object *Config = Configs->front().getAsObject();
  if (!Config)
    return createStringError(
        llvm::inconvertibleErrorCode(),
        "codemodel: expected 'configurations' as dictionary entries");
  const json::Array *Dirs = Config->getArray("directories");
  if (!Dirs)
    return createStringError(
        llvm::inconvertibleErrorCode(),
        "codemodel: missing 'configurations[0]/directories' entry");

  struct DirInfoTy {
    StringRef SourcePath;
    std::optional<int64_t> ParentIndex;
  };

  auto getDirInfo = [Dirs](unsigned Index) -> Expected<DirInfoTy> {
    const json::Object *DirObj = (*Dirs)[Index].getAsObject();
    if (!DirObj)
      return createStringError(
          llvm::inconvertibleErrorCode(),
          "codemodel: expected 'directories' as dictionary entries");
    std::optional<StringRef> SourcePath = DirObj->getString("source");
    if (!SourcePath)
      return createStringError(
          llvm::inconvertibleErrorCode(),
          "codemodel: missing 'configurations[0]/directories/source' entry");
    return DirInfoTy{*SourcePath, DirObj->getInteger("parentIndex")};
  };

  auto isTopLevelDir =
      [&getDirInfo](const DirInfoTy &DirInfo) -> Expected<bool> {
    if (sys::path::is_relative(DirInfo.SourcePath))
      return false;
    if (!DirInfo.ParentIndex)
      return true;
    auto Parent = getDirInfo(*DirInfo.ParentIndex);
    if (!Parent)
      return Parent.takeError();
    return !isPathNestedIn(DirInfo.SourcePath, Parent->SourcePath);
  };

  for (unsigned I = 0, E = Dirs->size(); I != E; ++I) {
    auto DirInfo = getDirInfo(I);
    if (!DirInfo)
      return DirInfo.takeError();
    Expected<bool> TopLevelCheck = isTopLevelDir(*DirInfo);
    if (!TopLevelCheck)
      return TopLevelCheck.takeError();
    if (*TopLevelCheck)
      TopLevelPaths.push_back(DirInfo->SourcePath);
  }

  return Error::success();
}