[llvm] [llvm-tblgen] Added keyword #undef to llvm-tblgen and fixed a small b… (PR #69135)

via llvm-commits llvm-commits at lists.llvm.org
Sun Oct 15 20:53:09 PDT 2023


https://github.com/whousemyname updated https://github.com/llvm/llvm-project/pull/69135

>From 4400cd237874a09c7d8b00740c4b028688af23c4 Mon Sep 17 00:00:00 2001
From: angryZ <lazytortoisezzzz at gmail.com>
Date: Mon, 16 Oct 2023 10:18:49 +0800
Subject: [PATCH] [llvm-tblgen] Added keyword #undef to llvm-tblgen and fixed a
 small bug for llvm-tblgen

---
 llvm/lib/TableGen/TGLexer.cpp | 32 +++++++++++++++++++++++---------
 llvm/lib/TableGen/TGLexer.h   |  1 +
 2 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/llvm/lib/TableGen/TGLexer.cpp b/llvm/lib/TableGen/TGLexer.cpp
index d5140e91fce9e94..256925b33a36c68 100644
--- a/llvm/lib/TableGen/TGLexer.cpp
+++ b/llvm/lib/TableGen/TGLexer.cpp
@@ -35,13 +35,9 @@ namespace {
 struct {
   tgtok::TokKind Kind;
   const char *Word;
-} PreprocessorDirs[] = {
-  { tgtok::Ifdef, "ifdef" },
-  { tgtok::Ifndef, "ifndef" },
-  { tgtok::Else, "else" },
-  { tgtok::Endif, "endif" },
-  { tgtok::Define, "define" }
-};
+} PreprocessorDirs[] = {{tgtok::Ifdef, "ifdef"},   {tgtok::Ifndef, "ifndef"},
+                        {tgtok::Else, "else"},     {tgtok::Endif, "endif"},
+                        {tgtok::Define, "define"}, {tgtok::Undef, "undef"}};
 } // end anonymous namespace
 
 TGLexer::TGLexer(SourceMgr &SM, ArrayRef<std::string> Macros) : SrcMgr(SM) {
@@ -664,7 +660,7 @@ tgtok::TokKind TGLexer::prepIsDirective() const {
           // It looks like TableGen does not support '\r' as the actual
           // carriage return, e.g. getNextChar() treats a single '\r'
           // as '\n'.  So we do the same here.
-          NextChar == '\r')
+          NextChar == '\r' || NextChar == '\0')
         return Kind;
 
       // Allow comments after some directives, e.g.:
@@ -834,6 +830,24 @@ tgtok::TokKind TGLexer::lexPreprocessor(
       return tgtok::Error;
     }
 
+    return LexToken();
+  } else if (Kind == tgtok::Undef) {
+    StringRef MacroName = prepLexMacroName();
+    if (MacroName.empty())
+      return ReturnError(TokStart, "Expected macor name after #undef");
+
+    if (!DefinedMacros.erase(MacroName))
+      return ReturnError(TokStart, "undefine(#undef) an undefined macro");
+
+    if (!prepSkipDirectiveEnd())
+      return ReturnError(CurPtr,
+                         "Only comments are supported after #undef NAME");
+
+    if (!ReturnNextLiveToken) {
+      PrintFatalError("#undef must be ignored during the lines skipping");
+      return tgtok::Error;
+    }
+
     return LexToken();
   }
 
@@ -867,7 +881,7 @@ bool TGLexer::prepSkipRegion(bool MustNeverBeFalse) {
     // If we did not find a preprocessing directive or it is #define,
     // then just skip to the next line.  We do not have to do anything
     // for #define in the line-skipping mode.
-    if (Kind == tgtok::Error || Kind == tgtok::Define)
+    if (Kind == tgtok::Error || Kind == tgtok::Define || Kind == tgtok::Undef)
       continue;
 
     tgtok::TokKind ProcessedKind = lexPreprocessor(Kind, false);
diff --git a/llvm/lib/TableGen/TGLexer.h b/llvm/lib/TableGen/TGLexer.h
index 4429c91b7c9cf76..02d141b629e1e04 100644
--- a/llvm/lib/TableGen/TGLexer.h
+++ b/llvm/lib/TableGen/TGLexer.h
@@ -72,6 +72,7 @@ enum TokKind {
   Else,
   Endif,
   Define,
+  Undef,
 
   // Reserved keywords. ('ElseKW' is named to distinguish it from the
   // existing 'Else' that means the preprocessor #else.)



More information about the llvm-commits mailing list