Skip to content

Commit 762720e

Browse files
authored
Merge pull request swiftlang#31400 from hlopko/pretty-print-static-vars-in-sil
[CxxInterop] Add C++ demangled name for globals/static member variables to SIL
2 parents 74b4e43 + 45509cd commit 762720e

File tree

6 files changed

+37
-1
lines changed

6 files changed

+37
-1
lines changed

lib/SIL/IR/SILPrinter.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
#include "swift/AST/ProtocolConformance.h"
3737
#include "swift/AST/Types.h"
3838
#include "swift/Basic/STLExtras.h"
39+
#include "clang/AST/ASTContext.h"
40+
#include "clang/AST/Decl.h"
3941
#include "llvm/ADT/APFloat.h"
4042
#include "llvm/ADT/APInt.h"
4143
#include "llvm/ADT/DenseMap.h"
@@ -2504,6 +2506,17 @@ static void printLinkage(llvm::raw_ostream &OS, SILLinkage linkage,
25042506
OS << getLinkageString(linkage);
25052507
}
25062508

2509+
static void printClangQualifiedNameCommentIfPresent(llvm::raw_ostream &OS,
2510+
const clang::Decl *decl) {
2511+
if (decl) {
2512+
if (auto namedDecl = dyn_cast_or_null<clang::NamedDecl>(decl)) {
2513+
OS << "// clang name: ";
2514+
namedDecl->printQualifiedName(OS);
2515+
OS << "\n";
2516+
}
2517+
}
2518+
}
2519+
25072520
/// Pretty-print the SILFunction to the designated stream.
25082521
void SILFunction::print(SILPrintContext &PrintCtx) const {
25092522
llvm::raw_ostream &OS = PrintCtx.OS();
@@ -2525,6 +2538,8 @@ void SILFunction::print(SILPrintContext &PrintCtx) const {
25252538
}
25262539

25272540
OS << "// " << demangleSymbol(getName()) << '\n';
2541+
printClangQualifiedNameCommentIfPresent(OS, getClangDecl());
2542+
25282543
OS << "sil ";
25292544
printLinkage(OS, getLinkage(), isDefinition());
25302545

@@ -2660,7 +2675,8 @@ void SILFunction::printName(raw_ostream &OS) const {
26602675
/// Pretty-print a global variable to the designated stream.
26612676
void SILGlobalVariable::print(llvm::raw_ostream &OS, bool Verbose) const {
26622677
OS << "// " << demangleSymbol(getName()) << '\n';
2663-
2678+
printClangQualifiedNameCommentIfPresent(OS, getClangDecl());
2679+
26642680
OS << "sil_global ";
26652681
printLinkage(OS, getLinkage(), isDefinition());
26662682

test/Interop/Cxx/extern-var/extern-var-silgen.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ func getCounter() -> CInt {
66
return counter
77
}
88

9+
// CHECK: // clang name: counter
910
// CHECK: sil_global @counter : $Int32
11+
// CHECK: // clang name: Namespaced::counter
1012
// CHECK: sil_global @{{_ZN10Namespaced7counterE|\?counter@Namespaced@@3HA}} : $Int32
1113

1214
// CHECK: sil hidden @$s4main10getCounters5Int32VyF : $@convention(thin) () -> Int32

test/Interop/Cxx/static/inline-static-member-var-silgen.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ func readStaticMember() -> CInt {
66
return WithInlineStaticMember.staticMember
77
}
88

9+
// CHECK: // clang name: WithInlineStaticMember::staticMember
910
// CHECK: sil_global @{{_ZN22WithInlineStaticMember12staticMemberE|\?staticMember@WithInlineStaticMember@@2HA}} : $Int32
1011

1112
// CHECK: sil hidden @$s4main16readStaticMembers5Int32VyF : $@convention(thin) () -> Int32

test/Interop/Cxx/static/static-member-func-silgen.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ func callStaticMemberFunc() -> CInt {
1111
// CHECK: [[VALUE:%.*]] = apply [[FUNC]]() : $@convention(c) () -> Int32
1212
// CHECK: return [[VALUE]] : $Int32
1313

14+
// CHECK: // clang name: WithStaticMemberFunc::staticMemberFunc
1415
// CHECK: sil [clang WithStaticMemberFunc.staticMemberFunc] @{{_ZN20WithStaticMemberFunc16staticMemberFuncEv|\?staticMemberFunc@WithStaticMemberFunc@@SAHXZ}} : $@convention(c) () -> Int32
1516

1617
func callStaticMemberFuncAddr() -> CInt {
@@ -21,4 +22,5 @@ func callStaticMemberFuncAddr() -> CInt {
2122
// CHECK: [[FUNC:%.*]] = function_ref @{{_ZN20WithStaticMemberFunc26getStaticMemberFuncAddressEv|\?getStaticMemberFuncAddress@WithStaticMemberFunc@@SAP6AHXZXZ}} : $@convention(c) () -> Optional<@convention(c) () -> Int32>
2223
// CHECK: [[VALUE:%.*]] = apply [[FUNC]]() : $@convention(c) () -> Optional<@convention(c) () -> Int32>
2324

25+
// CHECK: // clang name: WithStaticMemberFunc::getStaticMemberFuncAddress
2426
// CHECK: sil [clang WithStaticMemberFunc.getStaticMemberFuncAddress] @{{_ZN20WithStaticMemberFunc26getStaticMemberFuncAddressEv|\?getStaticMemberFuncAddress@WithStaticMemberFunc@@SAP6AHXZXZ}} : $@convention(c) () -> Optional<@convention(c) () -> Int32>

test/Interop/Cxx/static/static-member-var-silgen.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
// RUN: %target-swift-emit-sil -I %S/Inputs -enable-cxx-interop %s | %FileCheck %s
22

3+
// CHECK: // clang name: WithStaticMember::staticMember
34
// CHECK: sil_global @{{_ZN16WithStaticMember12staticMemberE|\?staticMember@WithStaticMember@@2HA}} : $Int32
5+
// CHECK: // clang name: WithIncompleteStaticMember::selfMember
46
// CHECK: sil_global @{{_ZN26WithIncompleteStaticMember10selfMemberE|\?selfMember@WithIncompleteStaticMember@@2V1@A}} : $WithIncompleteStaticMember
7+
// CHECK: // clang name: WithConstStaticMember::defined
58
// CHECK: sil_global [let] @{{_ZN21WithConstStaticMember7definedE|\?defined@WithConstStaticMember@@2HB}} : $Int32
9+
// CHECK: // clang name: WithConstStaticMember::definedOutOfLine
610
// CHECK: sil_global [let] @{{_ZN21WithConstStaticMember16definedOutOfLineE|\?definedOutOfLine@WithConstStaticMember@@2HB}} : $Int32
11+
// CHECK: // clang name: WithConstexprStaticMember::definedInline
712
// CHECK: sil_global [let] @{{_ZN25WithConstexprStaticMember13definedInlineE|\?definedInline@WithConstexprStaticMember@@2HB}} : $Int32
813

914
import StaticMemberVar

test/Interop/Cxx/static/static-var-silgen.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,25 @@ func initStaticVars() -> CInt {
88
+ staticConstexprNonTrivial.val
99
}
1010

11+
// CHECK: // clang name: staticVar
1112
// CHECK: sil_global @staticVar : $Int32
13+
// CHECK: // clang name: staticVarInit
1214
// CHECK: sil_global @staticVarInit : $Int32
15+
// CHECK: // clang name: staticVarInlineInit
1316
// CHECK: sil_global @staticVarInlineInit : $Int32
17+
// CHECK: // clang name: staticConst
1418
// CHECK: sil_global [let] @staticConst : $Int32
19+
// CHECK: // clang name: staticConstInit
1520
// CHECK: sil_global [let] @staticConstInit : $Int32
21+
// CHECK: // clang name: staticConstInlineInit
1622
// CHECK: sil_global [let] @staticConstInlineInit : $Int32
23+
// CHECK: // clang name: staticConstexpr
1724
// CHECK: sil_global [let] @staticConstexpr : $Int32
25+
// CHECK: // clang name: staticNonTrivial
1826
// CHECK: sil_global @staticNonTrivial : $NonTrivial
27+
// CHECK: // clang name: staticConstNonTrivial
1928
// CHECK: sil_global [let] @staticConstNonTrivial : $NonTrivial
29+
// CHECK: // clang name: staticConstexprNonTrivial
2030
// CHECK: sil_global [let] @staticConstexprNonTrivial : $NonTrivial
2131

2232
func readStaticVar() -> CInt {

0 commit comments

Comments
 (0)