Skip to content

Commit b8ac87f

Browse files
jurahulnikic
andauthored
[LLVM][AsmParser] Add support for C style comments (#111554)
Add support for C style comments in LLVM assembly. --------- Co-authored-by: Nikita Popov <[email protected]>
1 parent 97982a8 commit b8ac87f

File tree

8 files changed

+80
-2
lines changed

8 files changed

+80
-2
lines changed

llvm/docs/LangRef.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,14 @@ And the hard way:
122122
.. code-block:: llvm
123123

124124
%0 = add i32 %X, %X ; yields i32:%0
125-
%1 = add i32 %0, %0 ; yields i32:%1
125+
%1 = add i32 %0, %0 /* yields i32:%1 */
126126
%result = add i32 %1, %1
127127

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

131131
#. Comments are delimited with a '``;``' and go until the end of line.
132+
Alternatively, comments can start with ``/*`` and terminate with ``*/``.
132133
#. Unnamed temporaries are created when the result of a computation is
133134
not assigned to a named value.
134135
#. By default, unnamed temporaries are numbered sequentially (using a

llvm/include/llvm/AsmParser/LLLexer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ namespace llvm {
9595

9696
int getNextChar();
9797
void SkipLineComment();
98+
bool SkipCComment();
9899
lltok::Kind ReadString(lltok::Kind kind);
99100
bool ReadVarName();
100101

llvm/lib/AsmParser/LLLexer.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ lltok::Kind LLLexer::LexToken() {
200200
// Handle letters: [a-zA-Z_]
201201
if (isalpha(static_cast<unsigned char>(CurChar)) || CurChar == '_')
202202
return LexIdentifier();
203-
204203
return lltok::Error;
205204
case EOF: return lltok::Eof;
206205
case 0:
@@ -251,6 +250,12 @@ lltok::Kind LLLexer::LexToken() {
251250
case ',': return lltok::comma;
252251
case '*': return lltok::star;
253252
case '|': return lltok::bar;
253+
case '/':
254+
if (getNextChar() != '*')
255+
return lltok::Error;
256+
if (SkipCComment())
257+
return lltok::Error;
258+
continue;
254259
}
255260
}
256261
}
@@ -262,6 +267,28 @@ void LLLexer::SkipLineComment() {
262267
}
263268
}
264269

270+
/// This skips C-style /**/ comments. Returns true if there
271+
/// was an error.
272+
bool LLLexer::SkipCComment() {
273+
while (true) {
274+
int CurChar = getNextChar();
275+
switch (CurChar) {
276+
case EOF:
277+
LexError("unterminated comment");
278+
return true;
279+
case '*':
280+
// End of the comment?
281+
CurChar = getNextChar();
282+
if (CurChar == '/')
283+
return false;
284+
if (CurChar == EOF) {
285+
LexError("unterminated comment");
286+
return true;
287+
}
288+
}
289+
}
290+
}
291+
265292
/// Lex all tokens that start with an @ character.
266293
/// GlobalVar @\"[^\"]*\"
267294
/// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
; RUN: llvm-as < %s | llvm-dis | FileCheck %s
2+
3+
/* Simple C style comment */
4+
5+
; CHECK: @B = external global i32
6+
@B = external global i32
7+
8+
/* multiline C ctyle comment at "top-level"
9+
* This is the second line
10+
* and this is third
11+
*/
12+
13+
14+
; CHECK: @foo
15+
define <4 x i1> @foo(<4 x float> %a, <4 x float> %b) nounwind {
16+
entry: /* inline comment */
17+
%cmp = fcmp olt <4 x float> %a, /* to be ignored */ %b
18+
ret <4 x i1> %cmp /* ignore */
19+
}
20+
21+
/* End of the assembly file */
22+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
; RUN: not llvm-as --disable-output %s 2>&1 | FileCheck %s -DFILE=%s
2+
3+
@B = external global i32
4+
5+
; CHECK: [[FILE]]:[[@LINE+1]]:1: error: unterminated comment
6+
/* End of the assembly file
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
; RUN: not llvm-as --disable-output %s 2>&1 | FileCheck %s -DFILE=%s
2+
3+
@B = external global i32
4+
5+
/* /* Nested comments not supported */
6+
7+
; CHECK: [[FILE]]:[[@LINE+1]]:1: error: redefinition of global '@B'
8+
@B = external global i32
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
; RUN: not llvm-as --disable-output %s 2>&1 | FileCheck %s -DFILE=%s
2+
3+
@B = external global i32
4+
5+
; CHECK: [[FILE]]:[[@LINE+1]]:2: error: expected top-level entity
6+
*/
7+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
; RUN: not llvm-as --disable-output %s 2>&1 | FileCheck %s -DFILE=%s
2+
3+
@B = external global i32
4+
5+
; CHECK: [[FILE]]:[[@LINE+1]]:1: error: unterminated comment
6+
/* End of the assembly file *

0 commit comments

Comments
 (0)