[llvm] r230100 - AsmParser: Use StringRef for keyword comparisons, NFC

Duncan P. N. Exon Smith dexonsmith at apple.com
Fri Feb 20 16:18:41 PST 2015


Author: dexonsmith
Date: Fri Feb 20 18:18:40 2015
New Revision: 230100

URL: http://llvm.org/viewvc/llvm-project?rev=230100&view=rev
Log:
AsmParser: Use StringRef for keyword comparisons, NFC

Leverage `StringRef` inside keyword comparison macros.  There's no
reason to be so low-level here, and I'm about to add another
`startswith()` use, so let's make it easy to read.

Modified:
    llvm/trunk/lib/AsmParser/LLLexer.cpp

Modified: llvm/trunk/lib/AsmParser/LLLexer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/AsmParser/LLLexer.cpp?rev=230100&r1=230099&r2=230100&view=diff
==============================================================================
--- llvm/trunk/lib/AsmParser/LLLexer.cpp (original)
+++ llvm/trunk/lib/AsmParser/LLLexer.cpp Fri Feb 20 18:18:40 2015
@@ -486,11 +486,11 @@ lltok::Kind LLLexer::LexIdentifier() {
   if (!KeywordEnd) KeywordEnd = CurPtr;
   CurPtr = KeywordEnd;
   --StartChar;
-  unsigned Len = CurPtr-StartChar;
-#define KEYWORD(STR)                                                    \
-  do {                                                                  \
-    if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR)))  \
-      return lltok::kw_##STR;                                           \
+  StringRef Keyword(StartChar, CurPtr - StartChar);
+#define KEYWORD(STR)                                                           \
+  do {                                                                         \
+    if (Keyword == #STR)                                                       \
+      return lltok::kw_##STR;                                                  \
   } while (0)
 
   KEYWORD(true);    KEYWORD(false);
@@ -670,7 +670,7 @@ lltok::Kind LLLexer::LexIdentifier() {
   // Keywords for types.
 #define TYPEKEYWORD(STR, LLVMTY)                                               \
   do {                                                                         \
-    if (Len == strlen(STR) && !memcmp(StartChar, STR, strlen(STR))) {          \
+    if (Keyword == STR) {                                                      \
       TyVal = LLVMTY;                                                          \
       return lltok::Type;                                                      \
     }                                                                          \
@@ -690,7 +690,7 @@ lltok::Kind LLLexer::LexIdentifier() {
   // Keywords for instructions.
 #define INSTKEYWORD(STR, Enum)                                                 \
   do {                                                                         \
-    if (Len == strlen(#STR) && !memcmp(StartChar, #STR, strlen(#STR))) {       \
+    if (Keyword == #STR) {                                                     \
       UIntVal = Instruction::Enum;                                             \
       return lltok::kw_##STR;                                                  \
     }                                                                          \
@@ -748,9 +748,8 @@ lltok::Kind LLLexer::LexIdentifier() {
 
 #define DWKEYWORD(TYPE, TOKEN)                                                 \
   do {                                                                         \
-    if (Len >= strlen("DW_" #TYPE "_") &&                                      \
-        !memcmp(StartChar, "DW_" #TYPE "_", strlen("DW_" #TYPE "_"))) {        \
-      StrVal.assign(StartChar, CurPtr);                                        \
+    if (Keyword.startswith("DW_" #TYPE "_")) {                                 \
+      StrVal.assign(Keyword.begin(), Keyword.end());                           \
       return lltok::TOKEN;                                                     \
     }                                                                          \
   } while (false)





More information about the llvm-commits mailing list