Clang Project

clang_source_code/lib/Driver/DarwinSDKInfo.cpp
1//===--- DarwinSDKInfo.cpp - SDK Information parser for darwin - ----------===//
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/Driver/DarwinSDKInfo.h"
10#include "llvm/Support/ErrorOr.h"
11#include "llvm/Support/JSON.h"
12#include "llvm/Support/MemoryBuffer.h"
13#include "llvm/Support/Path.h"
14
15using namespace clang::driver;
16using namespace clang;
17
18Expected<Optional<DarwinSDKInfo>>
19driver::parseDarwinSDKInfo(llvm::vfs::FileSystem &VFSStringRef SDKRootPath) {
20  llvm::SmallString<256Filepath = SDKRootPath;
21  llvm::sys::path::append(Filepath, "SDKSettings.json");
22  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
23      VFS.getBufferForFile(Filepath);
24  if (!File) {
25    // If the file couldn't be read, assume it just doesn't exist.
26    return None;
27  }
28  Expected<llvm::json::Value> Result =
29      llvm::json::parse(File.get()->getBuffer());
30  if (!Result)
31    return Result.takeError();
32
33  if (const auto *Obj = Result->getAsObject()) {
34    auto VersionString = Obj->getString("Version");
35    if (VersionString) {
36      VersionTuple Version;
37      if (!Version.tryParse(*VersionString))
38        return DarwinSDKInfo(Version);
39    }
40  }
41  return llvm::make_error<llvm::StringError>("invalid SDKSettings.json",
42                                             llvm::inconvertibleErrorCode());
43}
44