Skip to content

Commit 43e3a01

Browse files
committed
[LLVM][AsmParser] Add support for C style comments
Add support for C style comments in LLVM assembly.
1 parent b77e402 commit 43e3a01

File tree

7 files changed

+85
-5
lines changed

7 files changed

+85
-5
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ namespace llvm {
9494
lltok::Kind LexToken();
9595

9696
int getNextChar();
97+
int peekNextChar() const;
9798
void SkipLineComment();
99+
bool SkipCComment();
98100
lltok::Kind ReadString(lltok::Kind kind);
99101
bool ReadVarName();
100102

llvm/lib/AsmParser/LLLexer.cpp

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,25 @@ LLLexer::LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &Err,
175175
}
176176

177177
int LLLexer::getNextChar() {
178-
char CurChar = *CurPtr++;
178+
int NextChar = peekNextChar();
179+
// Keeping CurPtr unchanged at EOF, so that another call to `getNextChar`
180+
// returns EOF again.
181+
if (NextChar != EOF)
182+
++CurPtr;
183+
return NextChar;
184+
}
185+
186+
int LLLexer::peekNextChar() const {
187+
char CurChar = *CurPtr;
179188
switch (CurChar) {
180189
default: return (unsigned char)CurChar;
181190
case 0:
182191
// A nul character in the stream is either the end of the current buffer or
183192
// a random nul in the file. Disambiguate that here.
184-
if (CurPtr-1 != CurBuf.end())
193+
if (CurPtr != CurBuf.end())
185194
return 0; // Just whitespace.
186195

187196
// Otherwise, return end of file.
188-
--CurPtr; // Another call to lex will return EOF again.
189197
return EOF;
190198
}
191199
}
@@ -200,7 +208,6 @@ lltok::Kind LLLexer::LexToken() {
200208
// Handle letters: [a-zA-Z_]
201209
if (isalpha(static_cast<unsigned char>(CurChar)) || CurChar == '_')
202210
return LexIdentifier();
203-
204211
return lltok::Error;
205212
case EOF: return lltok::Eof;
206213
case 0:
@@ -251,6 +258,12 @@ lltok::Kind LLLexer::LexToken() {
251258
case ',': return lltok::comma;
252259
case '*': return lltok::star;
253260
case '|': return lltok::bar;
261+
case '/':
262+
if (peekNextChar() != '*')
263+
return lltok::Error;
264+
if (SkipCComment())
265+
return lltok::Error;
266+
continue;
254267
}
255268
}
256269
}
@@ -262,6 +275,27 @@ void LLLexer::SkipLineComment() {
262275
}
263276
}
264277

278+
/// SkipCComment - This skips C-style /**/ comments. Returns true if there
279+
/// was an error.
280+
bool LLLexer::SkipCComment() {
281+
getNextChar(); // skip the star.
282+
283+
while (true) {
284+
int CurChar = getNextChar();
285+
switch (CurChar) {
286+
case EOF:
287+
LexError("unterminated comment");
288+
return true;
289+
case '*':
290+
// End of the comment?
291+
if (peekNextChar() == '/') {
292+
getNextChar(); // Eat the '/'.
293+
return false;
294+
}
295+
}
296+
}
297+
}
298+
265299
/// Lex all tokens that start with an @ character.
266300
/// GlobalVar @\"[^\"]*\"
267301
/// 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+

0 commit comments

Comments
 (0)