Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FixBUG][llvm-tblgen] : The correct td file ending with #endif cannot be compiled #69411

Closed
wants to merge 1 commit into from

Conversation

whousemyname
Copy link

Brief introduction

The correct td file ending with #endif (there are no other characters after #endif, including newlines) still cannot be compiled. This PR is to solve this bug.
The test file in the submission can verify this situation, if you compile it with llvm-tblgen

Bug occurrence scenarios

td file ending with #endif (no characters after #endif, including newlines and comments), When you write a td file in vim, after wq saves it, the td file automatically uses the newline character as the last character, so you need to manually delete the last newline character. Afterwards, compilation errors will occur when using llvm-tblgen to compile.
Let me give you an example: If you open an IDE (vscode or the like) and create a new file test.td, enter the following content(Please make sure there are no characters after #endif):

#ifdef asdasdsasd
#endif

I can confirm that a compilation error will appear in this case:

tdtest.td:2:7: error: Reached EOF without matching #endif
#endif
      ^
tdtest.td:1:8: error: The latest preprocessor control is here
#ifdef asdasdsasd
       ^
tdtest.td:2:7: error: Unexpected token at top level
#endif

However, if Tablegen does not allow #endif as the end of the file, this is not a bug, so I checked the official documentation and found the following rules:

LineEnd                ::=  newline | return | EOF
PreEndif               ::=  LineBegin (WhiteSpaceOrCComment)*
                            "#endif" (WhiteSpaceOrAnyComment)* LineEnd

I'm not sure what return means, but I think it's a weird setting not to end with #endif.
Adding nextChar == '\0' can solve this compile, but there may be a more correct way to solve this problem.

@whousemyname whousemyname changed the title [llvm][Tablegen][BUG] : The correct td file ending with #endif (there… [BUG][llvm-tblgen] : The correct td file ending with #endif cannot be compiled Oct 18, 2023
@whousemyname whousemyname changed the title [BUG][llvm-tblgen] : The correct td file ending with #endif cannot be compiled [FixBUG][llvm-tblgen] : The correct td file ending with #endif cannot be compiled Oct 18, 2023
@whousemyname whousemyname force-pushed the patch-bug-in-tblgen branch 2 times, most recently from 0ba498a to 3cd1495 Compare October 19, 2023 07:45
@github-actions
Copy link

github-actions bot commented Oct 19, 2023

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff 9d10fbbb299eae4a63f1028274e2ffe92bf8e75a 27a10d3c75ec01595e005fd12e5b250fdc08952f -- llvm/lib/TableGen/TGLexer.cpp
View the diff from clang-format here.
diff --git a/llvm/lib/TableGen/TGLexer.cpp b/llvm/lib/TableGen/TGLexer.cpp
index 3bedbcd6307f..697441abf2cc 100644
--- a/llvm/lib/TableGen/TGLexer.cpp
+++ b/llvm/lib/TableGen/TGLexer.cpp
@@ -346,31 +346,31 @@ tgtok::TokKind TGLexer::LexIdentifier() {
   StringRef Str(IdentStart, CurPtr-IdentStart);
 
   tgtok::TokKind Kind = StringSwitch<tgtok::TokKind>(Str)
-    .Case("int", tgtok::Int)
-    .Case("bit", tgtok::Bit)
-    .Case("bits", tgtok::Bits)
-    .Case("string", tgtok::String)
-    .Case("list", tgtok::List)
-    .Case("code", tgtok::Code)
-    .Case("dag", tgtok::Dag)
-    .Case("class", tgtok::Class)
-    .Case("def", tgtok::Def)
-    .Case("true", tgtok::TrueVal)
-    .Case("false", tgtok::FalseVal)
-    .Case("foreach", tgtok::Foreach)
-    .Case("defm", tgtok::Defm)
-    .Case("defset", tgtok::Defset)
-    .Case("multiclass", tgtok::MultiClass)
-    .Case("field", tgtok::Field)
-    .Case("let", tgtok::Let)
-    .Case("in", tgtok::In)
-    .Case("defvar", tgtok::Defvar)
-    .Case("include", tgtok::Include)
-    .Case("if", tgtok::If)
-    .Case("then", tgtok::Then)
-    .Case("else", tgtok::ElseKW)
-    .Case("assert", tgtok::Assert)
-    .Default(tgtok::Id);
+                            .Case("int", tgtok::Int)
+                            .Case("bit", tgtok::Bit)
+                            .Case("bits", tgtok::Bits)
+                            .Case("string", tgtok::String)
+                            .Case("list", tgtok::List)
+                            .Case("code", tgtok::Code)
+                            .Case("dag", tgtok::Dag)
+                            .Case("class", tgtok::Class)
+                            .Case("def", tgtok::Def)
+                            .Case("true", tgtok::TrueVal)
+                            .Case("false", tgtok::FalseVal)
+                            .Case("foreach", tgtok::Foreach)
+                            .Case("defm", tgtok::Defm)
+                            .Case("defset", tgtok::Defset)
+                            .Case("multiclass", tgtok::MultiClass)
+                            .Case("field", tgtok::Field)
+                            .Case("let", tgtok::Let)
+                            .Case("in", tgtok::In)
+                            .Case("defvar", tgtok::Defvar)
+                            .Case("include", tgtok::Include)
+                            .Case("if", tgtok::If)
+                            .Case("then", tgtok::Then)
+                            .Case("else", tgtok::ElseKW)
+                            .Case("assert", tgtok::Assert)
+                            .Default(tgtok::Id);
 
   // A couple of tokens require special processing.
   switch (Kind) {

@whousemyname whousemyname force-pushed the patch-bug-in-tblgen branch 2 times, most recently from 6afc5d1 to 043d29c Compare October 20, 2023 01:38
… are no other characters after #endif, including newlines) still cannot be compiled. This PR is to solve this bug.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant