[sanitizer] Improve FreeBSD ASLR detection
[llvm-project.git] / llvm / lib / TextAPI / Target.cpp
blobfeeb5c671a9cb3be88a53d1a659217cf98ba1e28
1 //===- Target.cpp -----------------------------------------------*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
9 #include "llvm/TextAPI/Target.h"
10 #include "llvm/ADT/SmallString.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/ADT/StringExtras.h"
13 #include "llvm/ADT/StringSwitch.h"
14 #include "llvm/Support/Format.h"
15 #include "llvm/Support/raw_ostream.h"
17 namespace llvm {
18 namespace MachO {
20 Expected<Target> Target::create(StringRef TargetValue) {
21 auto Result = TargetValue.split('-');
22 auto ArchitectureStr = Result.first;
23 auto Architecture = getArchitectureFromName(ArchitectureStr);
24 auto PlatformStr = Result.second;
25 PlatformType Platform;
26 Platform = StringSwitch<PlatformType>(PlatformStr)
27 .Case("macos", PLATFORM_MACOS)
28 .Case("ios", PLATFORM_IOS)
29 .Case("tvos", PLATFORM_TVOS)
30 .Case("watchos", PLATFORM_WATCHOS)
31 .Case("bridgeos", PLATFORM_BRIDGEOS)
32 .Case("maccatalyst", PLATFORM_MACCATALYST)
33 .Case("ios-simulator", PLATFORM_IOSSIMULATOR)
34 .Case("tvos-simulator", PLATFORM_TVOSSIMULATOR)
35 .Case("watchos-simulator", PLATFORM_WATCHOSSIMULATOR)
36 .Case("driverkit", PLATFORM_DRIVERKIT)
37 .Default(PLATFORM_UNKNOWN);
39 if (Platform == PLATFORM_UNKNOWN) {
40 if (PlatformStr.startswith("<") && PlatformStr.endswith(">")) {
41 PlatformStr = PlatformStr.drop_front().drop_back();
42 unsigned long long RawValue;
43 if (!PlatformStr.getAsInteger(10, RawValue))
44 Platform = (PlatformType)RawValue;
48 return Target{Architecture, Platform};
51 Target::operator std::string() const {
52 return (getArchitectureName(Arch) + " (" + getPlatformName(Platform) + ")")
53 .str();
56 raw_ostream &operator<<(raw_ostream &OS, const Target &Target) {
57 OS << std::string(Target);
58 return OS;
61 PlatformSet mapToPlatformSet(ArrayRef<Target> Targets) {
62 PlatformSet Result;
63 for (const auto &Target : Targets)
64 Result.insert(Target.Platform);
65 return Result;
68 ArchitectureSet mapToArchitectureSet(ArrayRef<Target> Targets) {
69 ArchitectureSet Result;
70 for (const auto &Target : Targets)
71 Result.set(Target.Arch);
72 return Result;
75 std::string getTargetTripleName(const Target &Targ) {
76 return (getArchitectureName(Targ.Arch) + "-apple-" +
77 getOSAndEnvironmentName(Targ.Platform))
78 .str();
81 } // end namespace MachO.
82 } // end namespace llvm.