Coverage Report

Created: 2024-01-17 10:31

/src/llvm-project/clang/lib/Basic/DarwinSDKInfo.cpp
Line
Count
Source (jump to first uncovered line)
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/Basic/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
#include <optional>
15
16
using namespace clang;
17
18
std::optional<VersionTuple> DarwinSDKInfo::RelatedTargetVersionMapping::map(
19
    const VersionTuple &Key, const VersionTuple &MinimumValue,
20
0
    std::optional<VersionTuple> MaximumValue) const {
21
0
  if (Key < MinimumKeyVersion)
22
0
    return MinimumValue;
23
0
  if (Key > MaximumKeyVersion)
24
0
    return MaximumValue;
25
0
  auto KV = Mapping.find(Key.normalize());
26
0
  if (KV != Mapping.end())
27
0
    return KV->getSecond();
28
  // If no exact entry found, try just the major key version. Only do so when
29
  // a minor version number is present, to avoid recursing indefinitely into
30
  // the major-only check.
31
0
  if (Key.getMinor())
32
0
    return map(VersionTuple(Key.getMajor()), MinimumValue, MaximumValue);
33
  // If this a major only key, return std::nullopt for a missing entry.
34
0
  return std::nullopt;
35
0
}
36
37
std::optional<DarwinSDKInfo::RelatedTargetVersionMapping>
38
DarwinSDKInfo::RelatedTargetVersionMapping::parseJSON(
39
0
    const llvm::json::Object &Obj, VersionTuple MaximumDeploymentTarget) {
40
0
  VersionTuple Min = VersionTuple(std::numeric_limits<unsigned>::max());
41
0
  VersionTuple Max = VersionTuple(0);
42
0
  VersionTuple MinValue = Min;
43
0
  llvm::DenseMap<VersionTuple, VersionTuple> Mapping;
44
0
  for (const auto &KV : Obj) {
45
0
    if (auto Val = KV.getSecond().getAsString()) {
46
0
      llvm::VersionTuple KeyVersion;
47
0
      llvm::VersionTuple ValueVersion;
48
0
      if (KeyVersion.tryParse(KV.getFirst()) || ValueVersion.tryParse(*Val))
49
0
        return std::nullopt;
50
0
      Mapping[KeyVersion.normalize()] = ValueVersion;
51
0
      if (KeyVersion < Min)
52
0
        Min = KeyVersion;
53
0
      if (KeyVersion > Max)
54
0
        Max = KeyVersion;
55
0
      if (ValueVersion < MinValue)
56
0
        MinValue = ValueVersion;
57
0
    }
58
0
  }
59
0
  if (Mapping.empty())
60
0
    return std::nullopt;
61
0
  return RelatedTargetVersionMapping(
62
0
      Min, Max, MinValue, MaximumDeploymentTarget, std::move(Mapping));
63
0
}
64
65
static std::optional<VersionTuple> getVersionKey(const llvm::json::Object &Obj,
66
0
                                                 StringRef Key) {
67
0
  auto Value = Obj.getString(Key);
68
0
  if (!Value)
69
0
    return std::nullopt;
70
0
  VersionTuple Version;
71
0
  if (Version.tryParse(*Value))
72
0
    return std::nullopt;
73
0
  return Version;
74
0
}
75
76
std::optional<DarwinSDKInfo>
77
0
DarwinSDKInfo::parseDarwinSDKSettingsJSON(const llvm::json::Object *Obj) {
78
0
  auto Version = getVersionKey(*Obj, "Version");
79
0
  if (!Version)
80
0
    return std::nullopt;
81
0
  auto MaximumDeploymentVersion =
82
0
      getVersionKey(*Obj, "MaximumDeploymentTarget");
83
0
  if (!MaximumDeploymentVersion)
84
0
    return std::nullopt;
85
0
  llvm::DenseMap<OSEnvPair::StorageType,
86
0
                 std::optional<RelatedTargetVersionMapping>>
87
0
      VersionMappings;
88
0
  if (const auto *VM = Obj->getObject("VersionMap")) {
89
    // FIXME: Generalize this out beyond iOS-deriving targets.
90
    // Look for ios_<targetos> version mapping for targets that derive from ios.
91
0
    for (const auto &KV : *VM) {
92
0
      auto Pair = StringRef(KV.getFirst()).split("_");
93
0
      if (Pair.first.compare_insensitive("ios") == 0) {
94
0
        llvm::Triple TT(llvm::Twine("--") + Pair.second.lower());
95
0
        if (TT.getOS() != llvm::Triple::UnknownOS) {
96
0
          auto Mapping = RelatedTargetVersionMapping::parseJSON(
97
0
              *KV.getSecond().getAsObject(), *MaximumDeploymentVersion);
98
0
          if (Mapping)
99
0
            VersionMappings[OSEnvPair(llvm::Triple::IOS,
100
0
                                      llvm::Triple::UnknownEnvironment,
101
0
                                      TT.getOS(),
102
0
                                      llvm::Triple::UnknownEnvironment)
103
0
                                .Value] = std::move(Mapping);
104
0
        }
105
0
      }
106
0
    }
107
108
0
    if (const auto *Mapping = VM->getObject("macOS_iOSMac")) {
109
0
      auto VersionMap = RelatedTargetVersionMapping::parseJSON(
110
0
          *Mapping, *MaximumDeploymentVersion);
111
0
      if (!VersionMap)
112
0
        return std::nullopt;
113
0
      VersionMappings[OSEnvPair::macOStoMacCatalystPair().Value] =
114
0
          std::move(VersionMap);
115
0
    }
116
0
    if (const auto *Mapping = VM->getObject("iOSMac_macOS")) {
117
0
      auto VersionMap = RelatedTargetVersionMapping::parseJSON(
118
0
          *Mapping, *MaximumDeploymentVersion);
119
0
      if (!VersionMap)
120
0
        return std::nullopt;
121
0
      VersionMappings[OSEnvPair::macCatalystToMacOSPair().Value] =
122
0
          std::move(VersionMap);
123
0
    }
124
0
  }
125
126
0
  return DarwinSDKInfo(std::move(*Version),
127
0
                       std::move(*MaximumDeploymentVersion),
128
0
                       std::move(VersionMappings));
129
0
}
130
131
Expected<std::optional<DarwinSDKInfo>>
132
0
clang::parseDarwinSDKInfo(llvm::vfs::FileSystem &VFS, StringRef SDKRootPath) {
133
0
  llvm::SmallString<256> Filepath = SDKRootPath;
134
0
  llvm::sys::path::append(Filepath, "SDKSettings.json");
135
0
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
136
0
      VFS.getBufferForFile(Filepath);
137
0
  if (!File) {
138
    // If the file couldn't be read, assume it just doesn't exist.
139
0
    return std::nullopt;
140
0
  }
141
0
  Expected<llvm::json::Value> Result =
142
0
      llvm::json::parse(File.get()->getBuffer());
143
0
  if (!Result)
144
0
    return Result.takeError();
145
146
0
  if (const auto *Obj = Result->getAsObject()) {
147
0
    if (auto SDKInfo = DarwinSDKInfo::parseDarwinSDKSettingsJSON(Obj))
148
0
      return std::move(SDKInfo);
149
0
  }
150
0
  return llvm::make_error<llvm::StringError>("invalid SDKSettings.json",
151
0
                                             llvm::inconvertibleErrorCode());
152
0
}