[PATCH] D87831: [clang] Expose helper function to turn PP keywords spelling into PPKeywordKind

Bruno Ricci via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 17 07:56:00 PDT 2020


riccibruno added inline comments.


================
Comment at: clang/include/clang/Basic/TokenKinds.h:99
 
+PPKeywordKind getPPKeywordFromSpelling(const std::string &Name);
+
----------------
A string is expensive here and unneeded. Why not a `StringRef`?


================
Comment at: clang/lib/Basic/IdentifierTable.cpp:274
 tok::PPKeywordKind IdentifierInfo::getPPKeywordID() const {
-  // We use a perfect hash function here involving the length of the keyword,
-  // the first and third character.  For preprocessor ID's there are no
-  // collisions (if there were, the switch below would complain about duplicate
-  // case values).  Note that this depends on 'if' being null terminated.
-
-#define HASH(LEN, FIRST, THIRD) \
-  (LEN << 5) + (((FIRST-'a') + (THIRD-'a')) & 31)
-#define CASE(LEN, FIRST, THIRD, NAME) \
-  case HASH(LEN, FIRST, THIRD): \
-    return memcmp(Name, #NAME, LEN) ? tok::pp_not_keyword : tok::pp_ ## NAME
-
-  unsigned Len = getLength();
-  if (Len < 2) return tok::pp_not_keyword;
-  const char *Name = getNameStart();
-  switch (HASH(Len, Name[0], Name[2])) {
-  default: return tok::pp_not_keyword;
-  CASE( 2, 'i', '\0', if);
-  CASE( 4, 'e', 'i', elif);
-  CASE( 4, 'e', 's', else);
-  CASE( 4, 'l', 'n', line);
-  CASE( 4, 's', 'c', sccs);
-  CASE( 5, 'e', 'd', endif);
-  CASE( 5, 'e', 'r', error);
-  CASE( 5, 'i', 'e', ident);
-  CASE( 5, 'i', 'd', ifdef);
-  CASE( 5, 'u', 'd', undef);
-
-  CASE( 6, 'a', 's', assert);
-  CASE( 6, 'd', 'f', define);
-  CASE( 6, 'i', 'n', ifndef);
-  CASE( 6, 'i', 'p', import);
-  CASE( 6, 'p', 'a', pragma);
-
-  CASE( 7, 'd', 'f', defined);
-  CASE( 7, 'i', 'c', include);
-  CASE( 7, 'w', 'r', warning);
-
-  CASE( 8, 'u', 'a', unassert);
-  CASE(12, 'i', 'c', include_next);
-
-  CASE(14, '_', 'p', __public_macro);
-
-  CASE(15, '_', 'p', __private_macro);
-
-  CASE(16, '_', 'i', __include_macros);
-#undef CASE
-#undef HASH
-  }
+  return tok::getPPKeywordFromSpelling(getNameStart());
 }
----------------
Better to use `IdentifierInfo::getName()` to avoid recomputing the length.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D87831



More information about the cfe-commits mailing list