[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 19:20:28 PDT 2023
https://github.com/whousemyname created https://github.com/llvm/llvm-project/pull/69135
### Motivation introduction
I want a td file to contain part of other td files instead of the entire file, so when using macros to implement it, I found that llvm-tblgen provides #define, #ifdef, #ifndef, #endif but does not provide #undef, so this PR The main purpose is to provide #undef support in llvm-tblgen, and when I was debugging, I found that if a td file ends with #endif, a td syntax error will occur, so I added a line to correct this bug. If there are any deficiencies, please point out
### bug in llvm-tblgen
If you remove a lot of spaces in the last line of the test file for testing the llvm-tblgen macro, you will find that a compilation error will occur.
ex: llvm/test/TableGen/prep-ifndef.td
```
// RUN: llvm-tblgen %s -DMACRO | FileCheck %s
// RUN: llvm-tblgen %s | FileCheck %s --check-prefix=CHECK-NOMACRO
#ifndef MACRO
// CHECK-NOMACRO: def nomacro
def nomacro;
#else
// CHECK: def macro
def macro;
#endif
```
>From 19fc7b3caca4a43fc1e7204d6957f5298d16c871 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 | 25 ++++++++++++++++++++++---
llvm/lib/TableGen/TGLexer.h | 1 +
2 files changed, 23 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/TableGen/TGLexer.cpp b/llvm/lib/TableGen/TGLexer.cpp
index d5140e91fce9e94..ab6c1b7fc704bdd 100644
--- a/llvm/lib/TableGen/TGLexer.cpp
+++ b/llvm/lib/TableGen/TGLexer.cpp
@@ -40,7 +40,8 @@ struct {
{ tgtok::Ifndef, "ifndef" },
{ tgtok::Else, "else" },
{ tgtok::Endif, "endif" },
- { tgtok::Define, "define" }
+ { tgtok::Define, "define" },
+ { tgtok::Undef, "undef" }
};
} // end anonymous namespace
@@ -664,7 +665,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 +835,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 +886,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..0b36249b4a57dd8 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