Skip to content

Commit e21abc1

Browse files
committed
[interop] NFC, extract out isClangKeyword into ClangSyntaxPrinter
1 parent fbbfb70 commit e21abc1

File tree

5 files changed

+25
-31
lines changed

5 files changed

+25
-31
lines changed

lib/PrintAsClang/ClangSyntaxPrinter.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@ using namespace cxx_synthesis;
1717

1818
StringRef cxx_synthesis::getCxxImplNamespaceName() { return "_impl"; }
1919

20+
bool ClangSyntaxPrinter::isClangKeyword(StringRef name) {
21+
static const llvm::DenseSet<StringRef> keywords = [] {
22+
llvm::DenseSet<StringRef> set;
23+
// FIXME: clang::IdentifierInfo /nearly/ has the API we need to do this
24+
// in a more principled way, but not quite.
25+
#define KEYWORD(SPELLING, FLAGS) set.insert(#SPELLING);
26+
#define CXX_KEYWORD_OPERATOR(SPELLING, TOK) set.insert(#SPELLING);
27+
#include "clang/Basic/TokenKinds.def"
28+
return set;
29+
}();
30+
31+
return keywords.contains(name);
32+
}
33+
34+
bool ClangSyntaxPrinter::isClangKeyword(Identifier name) {
35+
if (name.empty())
36+
return false;
37+
return ClangSyntaxPrinter::isClangKeyword(name.str());
38+
}
39+
2040
/// Print a C++ namespace declaration with the give name and body.
2141
void ClangSyntaxPrinter::printNamespace(
2242
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
@@ -53,6 +53,10 @@ class ClangSyntaxPrinter {
5353
Optional<OptionalTypeKind> kind,
5454
NullabilityPrintKind printKind = NullabilityPrintKind::After) const;
5555

56+
/// Returns true if \p name matches a keyword in any Clang language mode.
57+
static bool isClangKeyword(StringRef name);
58+
static bool isClangKeyword(Identifier name);
59+
5660
protected:
5761
raw_ostream &os;
5862
};

lib/PrintAsClang/DeclAndTypePrinter.cpp

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,33 +57,6 @@ static bool isAnyObjectOrAny(Type type) {
5757
return type->isAnyObject() || type->isAny();
5858
}
5959

60-
/// Returns true if \p name matches a keyword in any Clang language mode.
61-
static bool isClangKeyword(StringRef name) {
62-
static const llvm::DenseSet<StringRef> keywords = []{
63-
llvm::DenseSet<StringRef> set;
64-
// FIXME: clang::IdentifierInfo /nearly/ has the API we need to do this
65-
// in a more principled way, but not quite.
66-
#define KEYWORD(SPELLING, FLAGS) \
67-
set.insert(#SPELLING);
68-
#define CXX_KEYWORD_OPERATOR(SPELLING, TOK) \
69-
set.insert(#SPELLING);
70-
#include "clang/Basic/TokenKinds.def"
71-
return set;
72-
}();
73-
74-
return keywords.contains(name);
75-
}
76-
77-
static bool isClangKeyword(Identifier name) {
78-
if (name.empty())
79-
return false;
80-
return isClangKeyword(name.str());
81-
}
82-
83-
bool DeclAndTypePrinter::isStrClangKeyword(StringRef name) {
84-
return ::isClangKeyword(name);
85-
}
86-
8760
// For a given Decl and Type, if the type is not an optional return
8861
// the type and OTK_None as the optionality. If the type is
8962
// optional, return the underlying object type, and an optionality

lib/PrintAsClang/DeclAndTypePrinter.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@ class DeclAndTypePrinter {
9292

9393
static std::pair<Type, OptionalTypeKind>
9494
getObjectTypeAndOptionality(const ValueDecl *D, Type ty);
95-
96-
/// Returns true if \p name matches a keyword in any Clang language mode.
97-
static bool isStrClangKeyword(StringRef name);
9895
};
9996

10097
} // end namespace swift

lib/PrintAsClang/PrintClangFunction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ void DeclAndTypeClangFunctionPrinter::printFunctionDeclAsCFunctionDecl(
9797

9898
if (!name.empty()) {
9999
os << ' ' << name;
100-
if (DeclAndTypePrinter::isStrClangKeyword(name))
100+
if (ClangSyntaxPrinter::isClangKeyword(name))
101101
os << '_';
102102
}
103103
};

0 commit comments

Comments
 (0)