[llvm] [LLVM][AsmParser] Add support for C style comments (PR #111554)

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 25 18:01:33 PDT 2024


https://github.com/jurahul updated https://github.com/llvm/llvm-project/pull/111554

>From 7c9b538053df7a9f7084c82194663ee634153302 Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Thu, 3 Oct 2024 15:44:17 -0700
Subject: [PATCH] [LLVM][AsmParser] Add support for C style comments

Add support for C style comments in LLVM assembly.
---
 llvm/include/llvm/AsmParser/LLLexer.h         |  2 +
 llvm/lib/AsmParser/LLLexer.cpp                | 50 +++++++++++++++++--
 llvm/test/Assembler/c-style-comment.ll        | 30 +++++++++++
 .../Assembler/invalid-c-style-comment0.ll     |  6 +++
 .../Assembler/invalid-c-style-comment1.ll     | 10 ++++
 5 files changed, 95 insertions(+), 3 deletions(-)
 create mode 100644 llvm/test/Assembler/c-style-comment.ll
 create mode 100644 llvm/test/Assembler/invalid-c-style-comment0.ll
 create mode 100644 llvm/test/Assembler/invalid-c-style-comment1.ll

diff --git a/llvm/include/llvm/AsmParser/LLLexer.h b/llvm/include/llvm/AsmParser/LLLexer.h
index a9f51fb925f5d5..8e0c5638eef37d 100644
--- a/llvm/include/llvm/AsmParser/LLLexer.h
+++ b/llvm/include/llvm/AsmParser/LLLexer.h
@@ -94,7 +94,9 @@ namespace llvm {
     lltok::Kind LexToken();
 
     int getNextChar();
+    int peekNextChar() const;
     void SkipLineComment();
+    bool SkipCComment();
     lltok::Kind ReadString(lltok::Kind kind);
     bool ReadVarName();
 
diff --git a/llvm/lib/AsmParser/LLLexer.cpp b/llvm/lib/AsmParser/LLLexer.cpp
index 759db6db60774c..105668d6d89d44 100644
--- a/llvm/lib/AsmParser/LLLexer.cpp
+++ b/llvm/lib/AsmParser/LLLexer.cpp
@@ -175,17 +175,25 @@ LLLexer::LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &Err,
 }
 
 int LLLexer::getNextChar() {
-  char CurChar = *CurPtr++;
+  int NextChar = peekNextChar();
+  // Keeping CurPtr unchanged at EOF, so that another call to `getNextChar`
+  // returns EOF again.
+  if (NextChar != EOF)
+    ++CurPtr;
+  return NextChar;
+}
+
+int LLLexer::peekNextChar() const {
+  char CurChar = *CurPtr;
   switch (CurChar) {
   default: return (unsigned char)CurChar;
   case 0:
     // A nul character in the stream is either the end of the current buffer or
     // a random nul in the file.  Disambiguate that here.
-    if (CurPtr-1 != CurBuf.end())
+    if (CurPtr != CurBuf.end())
       return 0;  // Just whitespace.
 
     // Otherwise, return end of file.
-    --CurPtr;  // Another call to lex will return EOF again.
     return EOF;
   }
 }
@@ -251,6 +259,10 @@ lltok::Kind LLLexer::LexToken() {
     case ',': return lltok::comma;
     case '*': return lltok::star;
     case '|': return lltok::bar;
+    case '/':
+      if (peekNextChar() == '*' && SkipCComment())
+        return lltok::Error;
+      continue;
     }
   }
 }
@@ -262,6 +274,38 @@ void LLLexer::SkipLineComment() {
   }
 }
 
+/// SkipCComment - This skips C-style /**/ comments.  The only difference from C
+/// is that we allow nesting.
+bool LLLexer::SkipCComment() {
+  getNextChar(); // skip the star.
+  unsigned CommentDepth = 1;
+
+  while (true) {
+    int CurChar = getNextChar();
+    switch (CurChar) {
+    case EOF:
+      LexError("unterminated comment");
+      return true;
+    case '*':
+      // End of the comment?
+      if (peekNextChar() != '/')
+        break;
+
+      getNextChar(); // End the '/'.
+      if (--CommentDepth == 0)
+        return false;
+      break;
+    case '/':
+      // Start of a nested comment?
+      if (peekNextChar() != '*')
+        break;
+      getNextChar(); // Eat the '*'.
+      ++CommentDepth;
+      break;
+    }
+  }
+}
+
 /// Lex all tokens that start with an @ character.
 ///   GlobalVar   @\"[^\"]*\"
 ///   GlobalVar   @[-a-zA-Z$._][-a-zA-Z$._0-9]*
diff --git a/llvm/test/Assembler/c-style-comment.ll b/llvm/test/Assembler/c-style-comment.ll
new file mode 100644
index 00000000000000..b24a3e560e0e90
--- /dev/null
+++ b/llvm/test/Assembler/c-style-comment.ll
@@ -0,0 +1,30 @@
+; RUN: llvm-as < %s | llvm-dis | FileCheck %s 
+
+/* Simple C style comment */
+
+; CHECK: @B = external global i32
+ at B = external global i32
+
+/* multiline C ctyle comment at "top-level"
+ * This is the second line
+ * and this is third
+ */
+
+
+; CHECK: @foo
+define <4 x i1> @foo(<4 x float> %a, <4 x float> %b) nounwind {
+entry: /* inline comment */
+  %cmp = fcmp olt <4 x float> %a, /* to be ignored */ %b
+  ret <4 x i1> %cmp /* ignore */
+ /* C style nested comment
+    /* Nest
+       /*
+        * ; ignored
+	*/
+    */
+ */
+
+}
+
+/* End of the assembly file */
+
diff --git a/llvm/test/Assembler/invalid-c-style-comment0.ll b/llvm/test/Assembler/invalid-c-style-comment0.ll
new file mode 100644
index 00000000000000..f042cdd151af92
--- /dev/null
+++ b/llvm/test/Assembler/invalid-c-style-comment0.ll
@@ -0,0 +1,6 @@
+; RUN: not llvm-as --disable-output %s 2>&1 | FileCheck %s -DFILE=%s
+
+ at B = external global i32
+
+; CHECK: [[FILE]]:[[@LINE+1]]:1: error: unterminated comment
+/* End of the assembly file
diff --git a/llvm/test/Assembler/invalid-c-style-comment1.ll b/llvm/test/Assembler/invalid-c-style-comment1.ll
new file mode 100644
index 00000000000000..be7c4f9b7358fb
--- /dev/null
+++ b/llvm/test/Assembler/invalid-c-style-comment1.ll
@@ -0,0 +1,10 @@
+; RUN: not llvm-as --disable-output %s 2>&1 | FileCheck %s -DFILE=%s
+
+ at B = external global i32
+
+; CHECK: [[FILE]]:[[@LINE+1]]:1: error: unterminated comment
+/* End of the assembly file
+   /* Unterminated comment with multiple nesting depths */
+   /* /* ignored */ */
+   /* /* /* ignored */ */ */
+* /   



More information about the llvm-commits mailing list