[PATCH] D77311: clang-format: [JS] detect C++ keywords.

Krasimir Georgiev via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 2 10:17:41 PDT 2020


krasimir added a comment.

In D77311#1957367 <https://reviews.llvm.org/D77311#1957367>, @jfb wrote:

> Some of these are technically "future reserved keywords": https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#Keywords
>
> Wouldn't it be better to list all of JS's keywords at this point?


+1 to consider listing all of JS's keywords instead of listing all C++ keywords that are not JS keywords. With the current approach, any time a new C++ keyword token gets introduced, the switch in `IsJavaScriptIdentifier` would have to be updated.

But turning the implementation into something that lists JS keywords seems a bit tricky. Just a suggestion: maybe we can use the trick to #include TokenKinds.def <https://github.com/llvm/llvm-project/blob/master/clang/include/clang/Basic/TokenKinds.def> used in TokenKinds.h <https://github.com/llvm/llvm-project/blob/master/clang/include/clang/Basic/TokenKinds.h#L26>:

  bool IsJavaScriptIdentifier(const FormatToken &Tok) const {
  switch (Tok.Tok.getKind()) {
    // list all JS keywords defined as KEYWORD in TokenKinds.def
    case tok::kw_break:
    case tok::kw_case:
    ... 
    case tok::kw_return:
    return false;
  
    // All of the remaining C keywords are JS identifiers.
    #define KEYWORD(X,Y) case tok::kw_ ## X:
    #include "clang/Basic/TokenKinds.def"
    // #undef KEYWORD is not needed -- it's #undef-ed at the end of TokenKinds.def
    return true;
  
    // handle identifiers and everything else as in the patch.
    case tok::identifier:
    default: return false;
  }


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77311/new/

https://reviews.llvm.org/D77311





More information about the cfe-commits mailing list