|
3 | 3 | #include <clang-c/Index.h> |
4 | 4 | #include <algorithm> |
5 | 5 | #include <cctype> |
| 6 | +#include <cstdlib> |
6 | 7 | #include <string> |
7 | 8 | #include <vector> |
8 | 9 |
|
9 | 10 | namespace metagen { |
10 | 11 |
|
| 12 | +enum class AvailabilityPlatform { |
| 13 | + Unknown, |
| 14 | + IOS, |
| 15 | + MacOS, |
| 16 | + TvOS, |
| 17 | + WatchOS, |
| 18 | + XROS, |
| 19 | +}; |
| 20 | + |
| 21 | +inline AvailabilityPlatform gAvailabilityPlatform = AvailabilityPlatform::Unknown; |
| 22 | +inline int gAvailabilityTargetMajor = -1; |
| 23 | +inline int gAvailabilityTargetMinor = -1; |
| 24 | + |
| 25 | +inline AvailabilityPlatform platformFromTargetTriple(const std::string& targetTriple) { |
| 26 | + if (targetTriple.find("macosx") != std::string::npos) return AvailabilityPlatform::MacOS; |
| 27 | + if (targetTriple.find("ios") != std::string::npos) return AvailabilityPlatform::IOS; |
| 28 | + if (targetTriple.find("tvos") != std::string::npos) return AvailabilityPlatform::TvOS; |
| 29 | + if (targetTriple.find("watchos") != std::string::npos) return AvailabilityPlatform::WatchOS; |
| 30 | + if (targetTriple.find("xros") != std::string::npos) return AvailabilityPlatform::XROS; |
| 31 | + return AvailabilityPlatform::Unknown; |
| 32 | +} |
| 33 | + |
| 34 | +inline bool parsePlatformVersionFromTargetTriple(const std::string& targetTriple, int& major, |
| 35 | + int& minor) { |
| 36 | + size_t pos = std::string::npos; |
| 37 | + if ((pos = targetTriple.find("macosx")) != std::string::npos) { |
| 38 | + pos += 6; |
| 39 | + } else if ((pos = targetTriple.find("watchos")) != std::string::npos) { |
| 40 | + pos += 7; |
| 41 | + } else if ((pos = targetTriple.find("tvos")) != std::string::npos) { |
| 42 | + pos += 4; |
| 43 | + } else if ((pos = targetTriple.find("xros")) != std::string::npos) { |
| 44 | + pos += 4; |
| 45 | + } else if ((pos = targetTriple.find("ios")) != std::string::npos) { |
| 46 | + pos += 3; |
| 47 | + } else { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + if (pos >= targetTriple.size() || !std::isdigit(targetTriple[pos])) { |
| 52 | + return false; |
| 53 | + } |
| 54 | + |
| 55 | + size_t start = pos; |
| 56 | + while (pos < targetTriple.size() && std::isdigit(targetTriple[pos])) pos++; |
| 57 | + major = std::atoi(targetTriple.substr(start, pos - start).c_str()); |
| 58 | + minor = 0; |
| 59 | + |
| 60 | + if (pos < targetTriple.size() && targetTriple[pos] == '.') { |
| 61 | + pos++; |
| 62 | + size_t minorStart = pos; |
| 63 | + while (pos < targetTriple.size() && std::isdigit(targetTriple[pos])) pos++; |
| 64 | + if (minorStart < pos) { |
| 65 | + minor = std::atoi(targetTriple.substr(minorStart, pos - minorStart).c_str()); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return true; |
| 70 | +} |
| 71 | + |
| 72 | +inline void setAvailabilityTargetTriple(const std::string& targetTriple) { |
| 73 | + gAvailabilityPlatform = platformFromTargetTriple(targetTriple); |
| 74 | + gAvailabilityTargetMajor = -1; |
| 75 | + gAvailabilityTargetMinor = -1; |
| 76 | + |
| 77 | + int major = -1; |
| 78 | + int minor = -1; |
| 79 | + if (parsePlatformVersionFromTargetTriple(targetTriple, major, minor)) { |
| 80 | + gAvailabilityTargetMajor = major; |
| 81 | + gAvailabilityTargetMinor = minor; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +inline const char* platformNameForAvailability(AvailabilityPlatform platform) { |
| 86 | + switch (platform) { |
| 87 | + case AvailabilityPlatform::IOS: |
| 88 | + return "ios"; |
| 89 | + case AvailabilityPlatform::MacOS: |
| 90 | + return "macos"; |
| 91 | + case AvailabilityPlatform::TvOS: |
| 92 | + return "tvos"; |
| 93 | + case AvailabilityPlatform::WatchOS: |
| 94 | + return "watchos"; |
| 95 | + case AvailabilityPlatform::XROS: |
| 96 | + return "xros"; |
| 97 | + default: |
| 98 | + return nullptr; |
| 99 | + } |
| 100 | +} |
| 101 | + |
11 | 102 | inline std::string jsifySelector(const std::string& selector) { |
12 | 103 | std::string jsifiedSelector; |
13 | 104 | jsifiedSelector.reserve(selector.size()); |
@@ -118,9 +209,70 @@ inline std::string getFrameworkName(CXCursor cursor) { |
118 | 209 | } |
119 | 210 |
|
120 | 211 | inline bool isAvailable(CXCursor cursor) { |
121 | | - // Keep all declarations in metadata, except those that clang marks as |
122 | | - // inaccessible. |
123 | | - return clang_getCursorAvailability(cursor) != CXAvailability_NotAccessible; |
| 212 | + auto availability = clang_getCursorAvailability(cursor); |
| 213 | + if (availability != CXAvailability_Available && |
| 214 | + availability != CXAvailability_Deprecated) { |
| 215 | + return false; |
| 216 | + } |
| 217 | + |
| 218 | + if (gAvailabilityPlatform == AvailabilityPlatform::Unknown || |
| 219 | + gAvailabilityTargetMajor < 0) { |
| 220 | + return true; |
| 221 | + } |
| 222 | + |
| 223 | + int alwaysDeprecated = 0; |
| 224 | + int alwaysUnavailable = 0; |
| 225 | + CXString deprecatedMessage; |
| 226 | + CXString unavailableMessage; |
| 227 | + int availabilityCount = clang_getCursorPlatformAvailability( |
| 228 | + cursor, &alwaysDeprecated, &deprecatedMessage, &alwaysUnavailable, |
| 229 | + &unavailableMessage, nullptr, 0); |
| 230 | + clang_disposeString(deprecatedMessage); |
| 231 | + clang_disposeString(unavailableMessage); |
| 232 | + if (alwaysUnavailable) { |
| 233 | + return false; |
| 234 | + } |
| 235 | + |
| 236 | + if (availabilityCount <= 0) { |
| 237 | + return true; |
| 238 | + } |
| 239 | + |
| 240 | + std::vector<CXPlatformAvailability> platformAvailability( |
| 241 | + static_cast<size_t>(availabilityCount)); |
| 242 | + clang_getCursorPlatformAvailability( |
| 243 | + cursor, &alwaysDeprecated, &deprecatedMessage, &alwaysUnavailable, |
| 244 | + &unavailableMessage, platformAvailability.data(), availabilityCount); |
| 245 | + clang_disposeString(deprecatedMessage); |
| 246 | + clang_disposeString(unavailableMessage); |
| 247 | + |
| 248 | + const char* wantedPlatform = platformNameForAvailability(gAvailabilityPlatform); |
| 249 | + bool isCursorAvailableForTarget = true; |
| 250 | + for (auto& item : platformAvailability) { |
| 251 | + const char* platform = clang_getCString(item.Platform); |
| 252 | + if (wantedPlatform != nullptr && platform != nullptr && |
| 253 | + std::string(platform) == wantedPlatform) { |
| 254 | + if (item.Unavailable) { |
| 255 | + continue; |
| 256 | + } |
| 257 | + |
| 258 | + const int introducedMajor = item.Introduced.Major; |
| 259 | + const int introducedMinor = |
| 260 | + item.Introduced.Minor >= 0 ? item.Introduced.Minor : 0; |
| 261 | + if (introducedMajor >= 0 && |
| 262 | + (introducedMajor > gAvailabilityTargetMajor || |
| 263 | + (introducedMajor == gAvailabilityTargetMajor && |
| 264 | + introducedMinor > gAvailabilityTargetMinor))) { |
| 265 | + isCursorAvailableForTarget = false; |
| 266 | + break; |
| 267 | + } |
| 268 | + } |
| 269 | + } |
| 270 | + |
| 271 | + for (auto& item : platformAvailability) { |
| 272 | + clang_disposeCXPlatformAvailability(&item); |
| 273 | + } |
| 274 | + |
| 275 | + return isCursorAvailableForTarget; |
124 | 276 | } |
125 | 277 |
|
126 | 278 | inline bool isSelectorOwned(const std::string& selectorName) { |
|
0 commit comments