Skip to content

Commit

Permalink
[LLVM][AsmParser] Add support for C style comments (#111554)
Browse files Browse the repository at this point in the history
Add support for C style comments in LLVM assembly.

---------

Co-authored-by: Nikita Popov <[email protected]>
  • Loading branch information
jurahul and nikic authored Nov 5, 2024
1 parent 97982a8 commit b8ac87f
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 2 deletions.
3 changes: 2 additions & 1 deletion llvm/docs/LangRef.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,14 @@ And the hard way:
.. code-block:: llvm

%0 = add i32 %X, %X ; yields i32:%0
%1 = add i32 %0, %0 ; yields i32:%1
%1 = add i32 %0, %0 /* yields i32:%1 */
%result = add i32 %1, %1

This last way of multiplying ``%X`` by 8 illustrates several important
lexical features of LLVM:

#. Comments are delimited with a '``;``' and go until the end of line.
Alternatively, comments can start with ``/*`` and terminate with ``*/``.
#. Unnamed temporaries are created when the result of a computation is
not assigned to a named value.
#. By default, unnamed temporaries are numbered sequentially (using a
Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/AsmParser/LLLexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ namespace llvm {

int getNextChar();
void SkipLineComment();
bool SkipCComment();
lltok::Kind ReadString(lltok::Kind kind);
bool ReadVarName();

Expand Down
29 changes: 28 additions & 1 deletion llvm/lib/AsmParser/LLLexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ lltok::Kind LLLexer::LexToken() {
// Handle letters: [a-zA-Z_]
if (isalpha(static_cast<unsigned char>(CurChar)) || CurChar == '_')
return LexIdentifier();

return lltok::Error;
case EOF: return lltok::Eof;
case 0:
Expand Down Expand Up @@ -251,6 +250,12 @@ lltok::Kind LLLexer::LexToken() {
case ',': return lltok::comma;
case '*': return lltok::star;
case '|': return lltok::bar;
case '/':
if (getNextChar() != '*')
return lltok::Error;
if (SkipCComment())
return lltok::Error;
continue;
}
}
}
Expand All @@ -262,6 +267,28 @@ void LLLexer::SkipLineComment() {
}
}

/// This skips C-style /**/ comments. Returns true if there
/// was an error.
bool LLLexer::SkipCComment() {
while (true) {
int CurChar = getNextChar();
switch (CurChar) {
case EOF:
LexError("unterminated comment");
return true;
case '*':
// End of the comment?
CurChar = getNextChar();
if (CurChar == '/')
return false;
if (CurChar == EOF) {
LexError("unterminated comment");
return true;
}
}
}
}

/// Lex all tokens that start with an @ character.
/// GlobalVar @\"[^\"]*\"
/// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
Expand Down
22 changes: 22 additions & 0 deletions llvm/test/Assembler/c-style-comment.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
; RUN: llvm-as < %s | llvm-dis | FileCheck %s

/* Simple C style comment */

; CHECK: @B = external global i32
@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 */
}

/* End of the assembly file */

6 changes: 6 additions & 0 deletions llvm/test/Assembler/invalid-c-style-comment0.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
; RUN: not llvm-as --disable-output %s 2>&1 | FileCheck %s -DFILE=%s

@B = external global i32

; CHECK: [[FILE]]:[[@LINE+1]]:1: error: unterminated comment
/* End of the assembly file
8 changes: 8 additions & 0 deletions llvm/test/Assembler/invalid-c-style-comment1.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
; RUN: not llvm-as --disable-output %s 2>&1 | FileCheck %s -DFILE=%s

@B = external global i32

/* /* Nested comments not supported */

; CHECK: [[FILE]]:[[@LINE+1]]:1: error: redefinition of global '@B'
@B = external global i32
7 changes: 7 additions & 0 deletions llvm/test/Assembler/invalid-c-style-comment2.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
; RUN: not llvm-as --disable-output %s 2>&1 | FileCheck %s -DFILE=%s

@B = external global i32

; CHECK: [[FILE]]:[[@LINE+1]]:2: error: expected top-level entity
*/

6 changes: 6 additions & 0 deletions llvm/test/Assembler/invalid-c-style-comment3.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
; RUN: not llvm-as --disable-output %s 2>&1 | FileCheck %s -DFILE=%s

@B = external global i32

; CHECK: [[FILE]]:[[@LINE+1]]:1: error: unterminated comment
/* End of the assembly file *

0 comments on commit b8ac87f

Please sign in to comment.