[llvm] Added keyword #undef to llvm-tblgen and fixed a small bug for llvm-tb… (PR #69093)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 15 00:16:29 PDT 2023
https://github.com/whousemyname created https://github.com/llvm/llvm-project/pull/69093
### 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
/llvm/test/TableGen/prep-ifndef.td
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.
```
// 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 2f4529974677a075290f5038d0e7df4d897a34c8 Mon Sep 17 00:00:00 2001
From: angryZ <lazytortoisezzzz at gmail.com>
Date: Sun, 15 Oct 2023 14:53:03 +0800
Subject: [PATCH] Added keyword #undef to llvm-tblgen and fixed a small bug for
llvm-tblgen
---
llvm/lib/TableGen/TGLexer.cpp | 27 ++++++++++++++++++++++++---
llvm/lib/TableGen/TGLexer.h | 1 +
2 files changed, 25 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/TableGen/TGLexer.cpp b/llvm/lib/TableGen/TGLexer.cpp
index d5140e91fce9e94..87d3627f10ca7b0 100644
--- a/llvm/lib/TableGen/TGLexer.cpp
+++ b/llvm/lib/TableGen/TGLexer.cpp
@@ -13,6 +13,7 @@
#include "TGLexer.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringSwitch.h"
+#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Config/config.h" // for strtoull()/strtoll() define
#include "llvm/Support/Compiler.h"
@@ -40,7 +41,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 +666,8 @@ 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 +837,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 +888,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