Coverage Report

Created: 2021-09-21 08:58

/Users/buildslave/jenkins/workspace/coverage/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
15
using namespace clang;
16
17
Optional<VersionTuple> DarwinSDKInfo::RelatedTargetVersionMapping::map(
18
    const VersionTuple &Key, const VersionTuple &MinimumValue,
19
57
    Optional<VersionTuple> MaximumValue) const {
20
57
  if (Key < MinimumKeyVersion)
21
21
    return MinimumValue;
22
36
  if (Key > MaximumKeyVersion)
23
6
    return MaximumValue;
24
30
  auto KV = Mapping.find(Key.normalize());
25
30
  if (KV != Mapping.end())
26
29
    return KV->getSecond();
27
  // If no exact entry found, try just the major key version. Only do so when
28
  // a minor version number is present, to avoid recursing indefinitely into
29
  // the major-only check.
30
1
  if (Key.getMinor())
31
1
    return map(VersionTuple(Key.getMajor()), MinimumValue, MaximumValue);
32
  // If this a major only key, return None for a missing entry.
33
0
  return None;
34
1
}
35
36
Optional<DarwinSDKInfo::RelatedTargetVersionMapping>
37
DarwinSDKInfo::RelatedTargetVersionMapping::parseJSON(
38
18.9k
    const llvm::json::Object &Obj, VersionTuple MaximumDeploymentTarget) {
39
18.9k
  VersionTuple Min = VersionTuple(std::numeric_limits<unsigned>::max());
40
18.9k
  VersionTuple Max = VersionTuple(0);
41
18.9k
  VersionTuple MinValue = Min;
42
18.9k
  llvm::DenseMap<VersionTuple, VersionTuple> Mapping;
43
94.8k
  for (const auto &KV : Obj) {
44
94.8k
    if (auto 
Val94.8k
= KV.getSecond().getAsString()) {
45
94.8k
      llvm::VersionTuple KeyVersion;
46
94.8k
      llvm::VersionTuple ValueVersion;
47
94.8k
      if (KeyVersion.tryParse(KV.getFirst()) || 
ValueVersion.tryParse(*Val)94.8k
)
48
0
        return None;
49
94.8k
      Mapping[KeyVersion.normalize()] = ValueVersion;
50
94.8k
      if (KeyVersion < Min)
51
75.9k
        Min = KeyVersion;
52
94.8k
      if (KeyVersion > Max)
53
28.4k
        Max = KeyVersion;
54
94.8k
      if (ValueVersion < MinValue)
55
75.9k
        MinValue = ValueVersion;
56
94.8k
    }
57
94.8k
  }
58
18.9k
  if (Mapping.empty())
59
0
    return None;
60
18.9k
  return RelatedTargetVersionMapping(
61
18.9k
      Min, Max, MinValue, MaximumDeploymentTarget, std::move(Mapping));
62
18.9k
}
63
64
static Optional<VersionTuple> getVersionKey(const llvm::json::Object &Obj,
65
19.0k
                                            StringRef Key) {
66
19.0k
  auto Value = Obj.getString(Key);
67
19.0k
  if (!Value)
68
3
    return None;
69
19.0k
  VersionTuple Version;
70
19.0k
  if (Version.tryParse(*Value))
71
0
    return None;
72
19.0k
  return Version;
73
19.0k
}
74
75
Optional<DarwinSDKInfo>
76
9.52k
DarwinSDKInfo::parseDarwinSDKSettingsJSON(const llvm::json::Object *Obj) {
77
9.52k
  auto Version = getVersionKey(*Obj, "Version");
78
9.52k
  if (!Version)
79
2
    return None;
80
9.52k
  auto MaximumDeploymentVersion =
81
9.52k
      getVersionKey(*Obj, "MaximumDeploymentTarget");
82
9.52k
  if (!MaximumDeploymentVersion)
83
1
    return None;
84
9.52k
  llvm::DenseMap<OSEnvPair::StorageType, Optional<RelatedTargetVersionMapping>>
85
9.52k
      VersionMappings;
86
9.52k
  if (const auto *VM = Obj->getObject("VersionMap")) {
87
9.49k
    if (const auto *Mapping = VM->getObject("macOS_iOSMac")) {
88
9.49k
      auto VersionMap = RelatedTargetVersionMapping::parseJSON(
89
9.49k
          *Mapping, *MaximumDeploymentVersion);
90
9.49k
      if (!VersionMap)
91
0
        return None;
92
9.49k
      VersionMappings[OSEnvPair::macOStoMacCatalystPair().Value] =
93
9.49k
          std::move(VersionMap);
94
9.49k
    }
95
9.49k
    if (const auto *Mapping = VM->getObject("iOSMac_macOS")) {
96
9.49k
      auto VersionMap = RelatedTargetVersionMapping::parseJSON(
97
9.49k
          *Mapping, *MaximumDeploymentVersion);
98
9.49k
      if (!VersionMap)
99
0
        return None;
100
9.49k
      VersionMappings[OSEnvPair::macCatalystToMacOSPair().Value] =
101
9.49k
          std::move(VersionMap);
102
9.49k
    }
103
9.49k
  }
104
105
9.52k
  return DarwinSDKInfo(std::move(*Version),
106
9.52k
                       std::move(*MaximumDeploymentVersion),
107
9.52k
                       std::move(VersionMappings));
108
9.52k
}
109
110
Expected<Optional<DarwinSDKInfo>>
111
9.62k
clang::parseDarwinSDKInfo(llvm::vfs::FileSystem &VFS, StringRef SDKRootPath) {
112
9.62k
  llvm::SmallString<256> Filepath = SDKRootPath;
113
9.62k
  llvm::sys::path::append(Filepath, "SDKSettings.json");
114
9.62k
  llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File =
115
9.62k
      VFS.getBufferForFile(Filepath);
116
9.62k
  if (!File) {
117
    // If the file couldn't be read, assume it just doesn't exist.
118
97
    return None;
119
97
  }
120
9.52k
  Expected<llvm::json::Value> Result =
121
9.52k
      llvm::json::parse(File.get()->getBuffer());
122
9.52k
  if (!Result)
123
1
    return Result.takeError();
124
125
9.52k
  if (const auto *Obj = Result->getAsObject()) {
126
9.52k
    if (auto SDKInfo = DarwinSDKInfo::parseDarwinSDKSettingsJSON(Obj))
127
9.52k
      return std::move(SDKInfo);
128
9.52k
  }
129
1
  return llvm::make_error<llvm::StringError>("invalid SDKSettings.json",
130
1
                                             llvm::inconvertibleErrorCode());
131
9.52k
}