Skip to content

Commit 7c9b538

Browse files
committed
[LLVM][AsmParser] Add support for C style comments
Add support for C style comments in LLVM assembly.
1 parent 242ccd2 commit 7c9b538

File tree

5 files changed

+95
-3
lines changed

5 files changed

+95
-3
lines changed

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: 47 additions & 3 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
}
@@ -251,6 +259,10 @@ lltok::Kind LLLexer::LexToken() {
251259
case ',': return lltok::comma;
252260
case '*': return lltok::star;
253261
case '|': return lltok::bar;
262+
case '/':
263+
if (peekNextChar() == '*' && SkipCComment())
264+
return lltok::Error;
265+
continue;
254266
}
255267
}
256268
}
@@ -262,6 +274,38 @@ void LLLexer::SkipLineComment() {
262274
}
263275
}
264276

277+
/// SkipCComment - This skips C-style /**/ comments. The only difference from C
278+
/// is that we allow nesting.
279+
bool LLLexer::SkipCComment() {
280+
getNextChar(); // skip the star.
281+
unsigned CommentDepth = 1;
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+
break;
293+
294+
getNextChar(); // End the '/'.
295+
if (--CommentDepth == 0)
296+
return false;
297+
break;
298+
case '/':
299+
// Start of a nested comment?
300+
if (peekNextChar() != '*')
301+
break;
302+
getNextChar(); // Eat the '*'.
303+
++CommentDepth;
304+
break;
305+
}
306+
}
307+
}
308+
265309
/// Lex all tokens that start with an @ character.
266310
/// GlobalVar @\"[^\"]*\"
267311
/// GlobalVar @[-a-zA-Z$._][-a-zA-Z$._0-9]*
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
/* C style nested comment
20+
/* Nest
21+
/*
22+
* ; ignored
23+
*/
24+
*/
25+
*/
26+
27+
}
28+
29+
/* End of the assembly file */
30+
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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
7+
/* Unterminated comment with multiple nesting depths */
8+
/* /* ignored */ */
9+
/* /* /* ignored */ */ */
10+
* /

0 commit comments

Comments
 (0)