Skip to content

Commit 45032fd

Browse files
authored
Merge pull request #79223 from compnerd/sizeof-void-star
Basic: query the target pointer width from clang
2 parents edc81e8 + a04c75a commit 45032fd

File tree

4 files changed

+52
-16
lines changed

4 files changed

+52
-16
lines changed

include/swift/Basic/TargetInfo.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ namespace targetinfo {
3333
void printTargetInfo(const CompilerInvocation &invocation,
3434
llvm::raw_ostream &out);
3535

36-
void printTripleInfo(const llvm::Triple &triple,
36+
void printTripleInfo(const CompilerInvocation &invocation,
37+
const llvm::Triple &triple,
3738
std::optional<llvm::VersionTuple> runtimeVersion,
3839
llvm::raw_ostream &out);
3940
} // namespace targetinfo

lib/Basic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ _swift_gyb_target_sources(swiftBasic PRIVATE
9797
UnicodeExtendedGraphemeClusters.cpp.gyb)
9898

9999
target_include_directories(swiftBasic PRIVATE
100+
clangBasic
100101
${UUID_INCLUDE})
101102

102103
target_link_libraries(swiftBasic PUBLIC

lib/Basic/TargetInfo.cpp

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/Basic/StringExtras.h"
1717
#include "swift/Frontend/Frontend.h"
1818

19+
#include "clang/Basic/TargetInfo.h"
1920
#include "llvm/Support/raw_ostream.h"
2021

2122
using namespace swift;
@@ -52,9 +53,11 @@ static void printCompatibilityLibrary(
5253
printedAny = true;
5354
}
5455

56+
namespace swift {
57+
namespace targetinfo {
5558
/// Print information about the selected target in JSON.
56-
void targetinfo::printTargetInfo(const CompilerInvocation &invocation,
57-
llvm::raw_ostream &out) {
59+
void printTargetInfo(const CompilerInvocation &invocation,
60+
llvm::raw_ostream &out) {
5861
out << "{\n";
5962

6063
// Compiler version, as produced by --version.
@@ -67,12 +70,12 @@ void targetinfo::printTargetInfo(const CompilerInvocation &invocation,
6770
invocation.getIRGenOptions().AutolinkRuntimeCompatibilityLibraryVersion;
6871
auto &langOpts = invocation.getLangOptions();
6972
out << " \"target\": ";
70-
printTripleInfo(langOpts.Target, runtimeVersion, out);
73+
printTripleInfo(invocation, langOpts.Target, runtimeVersion, out);
7174
out << ",\n";
7275

7376
if (auto &variant = langOpts.TargetVariant) {
7477
out << " \"targetVariant\": ";
75-
printTripleInfo(*variant, runtimeVersion, out);
78+
printTripleInfo(invocation, *variant, runtimeVersion, out);
7679
out << ",\n";
7780
}
7881

@@ -112,9 +115,10 @@ void targetinfo::printTargetInfo(const CompilerInvocation &invocation,
112115
}
113116

114117
// Print information about the target triple in JSON.
115-
void targetinfo::printTripleInfo(
116-
const llvm::Triple &triple,
117-
std::optional<llvm::VersionTuple> runtimeVersion, llvm::raw_ostream &out) {
118+
void printTripleInfo(const CompilerInvocation &invocation,
119+
const llvm::Triple &triple,
120+
std::optional<llvm::VersionTuple> runtimeVersion,
121+
llvm::raw_ostream &out) {
118122
out << "{\n";
119123

120124
out << " \"triple\": \"";
@@ -130,7 +134,21 @@ void targetinfo::printTripleInfo(
130134
out << "\",\n";
131135

132136
out << " \"platform\": \"" << getPlatformNameForTriple(triple) << "\",\n";
133-
out << " \"arch\": \"" << swift::getMajorArchitectureName(triple) << "\",\n";
137+
out << " \"arch\": \"" << swift::getMajorArchitectureName(triple)
138+
<< "\",\n";
139+
140+
clang::DiagnosticsEngine DE{new clang::DiagnosticIDs(),
141+
new clang::DiagnosticOptions(),
142+
new clang::IgnoringDiagConsumer()};
143+
std::shared_ptr<clang::TargetOptions> TO =
144+
std::make_shared<clang::TargetOptions>();
145+
TO->Triple = triple.str();
146+
clang::TargetInfo *TI = clang::TargetInfo::CreateTargetInfo(DE, TO);
147+
out << " \"pointerWidthInBits\": "
148+
<< TI->getPointerWidth(clang::LangAS::Default) << ",\n";
149+
out << " \"pointerWidthInBytes\": "
150+
<< TI->getPointerWidth(clang::LangAS::Default) / TI->getCharWidth()
151+
<< ",\n";
134152

135153
if (runtimeVersion) {
136154
out << " \"swiftRuntimeCompatibilityVersion\": \"";
@@ -140,15 +158,14 @@ void targetinfo::printTripleInfo(
140158
// Compatibility libraries that need to be linked.
141159
out << " \"compatibilityLibraries\": [";
142160
bool printedAnyCompatibilityLibrary = false;
143-
#define BACK_DEPLOYMENT_LIB(Version, Filter, LibraryName, ForceLoad) \
144-
printCompatibilityLibrary( \
145-
*runtimeVersion, llvm::VersionTuple Version, #Filter, LibraryName, \
146-
ForceLoad, printedAnyCompatibilityLibrary, out);
147-
#include "swift/Frontend/BackDeploymentLibs.def"
161+
#define BACK_DEPLOYMENT_LIB(Version, Filter, LibraryName, ForceLoad) \
162+
printCompatibilityLibrary(*runtimeVersion, llvm::VersionTuple Version, \
163+
#Filter, LibraryName, ForceLoad, \
164+
printedAnyCompatibilityLibrary, out);
165+
#include "swift/Frontend/BackDeploymentLibs.def"
148166

149-
if (printedAnyCompatibilityLibrary) {
167+
if (printedAnyCompatibilityLibrary)
150168
out << "\n ";
151-
}
152169
out << " ],\n";
153170
} else {
154171
out << " \"compatibilityLibraries\": [ ],\n";
@@ -160,3 +177,5 @@ void targetinfo::printTripleInfo(
160177

161178
out << " }";
162179
}
180+
} // namespace targetinfo
181+
} // namespace swift

test/Driver/print_target_info.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
// RUN: %swift_driver -print-target-info -target x86_64-apple-ios12.0 | %FileCheck -check-prefix CHECK-IOS-SIM %s
1818

19+
// RUN: %swift_frontend_plain -target thumbv7-unknown-windows-msvc -print-target-info | %FileCheck -check-prefix CHECK-PTR-SIZE-32 %s
20+
// RUN: %swift_frontend_plain -target aarch64-unknown-windows-msvc -print-target-info | %FileCheck -check-prefix CHECK-PTR-SIZE-64 %s
21+
1922
// CHECK-IOS: "compilerVersion": "{{.*}}Swift version
2023

2124
// CHECK-IOS: "target": {
@@ -107,3 +110,15 @@
107110
// CHECK-IOS-SIM: "swiftRuntimeCompatibilityVersion": "5.0",
108111
// CHECK-IOS-SIM: "librariesRequireRPath": true
109112
// CHECK-IOS-SIM: }
113+
114+
// CHECK-PTR-SIZE-32: "target": {
115+
// CHECK-PTR-SIZE-32: "triple": "thumbv7-unknown-windows-msvc",
116+
// CHECK-PTR-SIZE-32: "pointerWidthInBits": 32,
117+
// CHECK-PTR-SIZE-32: "pointerWidthInBytes": 4,
118+
// CHECK-PTR-SIZE-32: }
119+
120+
// CHECK-PTR-SIZE-64: "target": {
121+
// CHECK-PTR-SIZE-64: "triple": "aarch64-unknown-windows-msvc",
122+
// CHECK-PTR-SIZE-64: "pointerWidthInBits": 64,
123+
// CHECK-PTR-SIZE-64: "pointerWidthInBytes": 8,
124+
// CHECK-PTR-SIZE-64: }

0 commit comments

Comments
 (0)