Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/swift/AST/ASTPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,9 @@ bool printRequirementStub(ValueDecl *Requirement, DeclContext *Adopter,
/// Print a keyword or punctuator directly by its kind.
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, tok keyword);

/// Get the length of a keyword or punctuator by its kind.
uint8_t getKeywordLen(tok keyword);

} // namespace swift

#endif // LLVM_SWIFT_AST_ASTPRINTER_H
11 changes: 11 additions & 0 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,17 @@ operator<<(llvm::raw_ostream &OS, tok keyword) {
return OS;
}

uint8_t swift::getKeywordLen(tok keyword) {
switch (keyword) {
#define KEYWORD(KW) case tok::kw_##KW: return StringRef(#KW).size();
Copy link
Member

@rintaro rintaro Feb 22, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about sizeof(#KW) - 1; or strlen(#KW)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, i think that would work too : )

#define POUND_KEYWORD(KW) case tok::pound_##KW: return StringRef("#"#KW).size();
#define PUNCTUATOR(PUN, TEXT) case tok::PUN: return StringRef(TEXT).size();
#include "swift/Syntax/TokenKinds.def"
default:
llvm_unreachable("unexpected keyword or punctuator kind");
}
}

ASTPrinter &operator<<(ASTPrinter &printer, tok keyword) {
SmallString<16> Buffer;
llvm::raw_svector_ostream OS(Buffer);
Expand Down