Skip to content

Commit 68fc606

Browse files
committed
[interop] NFC, add a method to print out identifier to clang syntax printer
1 parent e21abc1 commit 68fc606

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

lib/PrintAsClang/ClangSyntaxPrinter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ bool ClangSyntaxPrinter::isClangKeyword(Identifier name) {
3737
return ClangSyntaxPrinter::isClangKeyword(name.str());
3838
}
3939

40+
void ClangSyntaxPrinter::printIdentifier(StringRef name) {
41+
os << name;
42+
if (ClangSyntaxPrinter::isClangKeyword(name))
43+
os << '_';
44+
}
45+
4046
/// Print a C++ namespace declaration with the give name and body.
4147
void ClangSyntaxPrinter::printNamespace(
4248
llvm::function_ref<void(raw_ostream &OS)> namePrinter,

lib/PrintAsClang/ClangSyntaxPrinter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ class ClangSyntaxPrinter {
3333
public:
3434
ClangSyntaxPrinter(raw_ostream &os) : os(os) {}
3535

36+
/// Print a given identifier. If the identifer conflicts with a keyword, add a
37+
/// trailing underscore.
38+
void printIdentifier(StringRef name);
39+
3640
/// Print a C++ namespace declaration with the give name and body.
3741
void
3842
printNamespace(llvm::function_ref<void(raw_ostream &OS)> namePrinter,

lib/PrintAsClang/PrintClangFunction.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ void DeclAndTypeClangFunctionPrinter::printFunctionDeclAsCFunctionDecl(
9696
typePrinter.visit(ty, optionalKind);
9797

9898
if (!name.empty()) {
99-
os << ' ' << name;
100-
if (ClangSyntaxPrinter::isClangKeyword(name))
101-
os << '_';
99+
os << ' ';
100+
ClangSyntaxPrinter(os).printIdentifier(name);
102101
}
103102
};
104103

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend %s -typecheck -module-name Functions -clang-header-expose-public-decls -emit-clang-header-path %t/functions.h
3+
// RUN: %FileCheck %s < %t/functions.h
4+
5+
// CHECK: SWIFT_EXTERN void $s9Functions19testKeywordArgument8registerySi_tF(ptrdiff_t register_) SWIFT_NOEXCEPT SWIFT_CALL;
6+
7+
func testKeywordArgument(register: Int) { }

0 commit comments

Comments
 (0)