Coverage Report

Created: 2022-01-18 06:27

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