Skip to content

Commit 1fdf13c

Browse files
committed
Make isLabelTail more safe and rename it to better show what it does
1 parent 06926e9 commit 1fdf13c

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

llvm/include/llvm/AsmParser/LLLexer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ namespace llvm {
9393
private:
9494
lltok::Kind LexToken();
9595

96+
// Return closes pointer after `Ptr` that is an end of a label.
97+
// Returns nullptr if `Ptr` doesn't point into a label.
98+
const char *getLabelTail(const char *Ptr);
9699
int getNextChar();
97100
const char *skipNChars(unsigned N);
98101
void advancePositionTo(const char *Ptr);

llvm/lib/AsmParser/LLLexer.cpp

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,6 @@ static bool isLabelChar(char C) {
155155
C == '.' || C == '_';
156156
}
157157

158-
/// isLabelTail - Return true if this pointer points to a valid end of a label.
159-
static const char *isLabelTail(const char *CurPtr) {
160-
while (true) {
161-
if (CurPtr[0] == ':') return CurPtr+1;
162-
if (!isLabelChar(CurPtr[0])) return nullptr;
163-
++CurPtr;
164-
}
165-
}
166-
167158
//===----------------------------------------------------------------------===//
168159
// Lexer definition.
169160
//===----------------------------------------------------------------------===//
@@ -174,20 +165,22 @@ LLLexer::LLLexer(StringRef StartBuf, SourceMgr &SM, SMDiagnostic &Err,
174165
CurPtr = CurBuf.begin();
175166
}
176167

168+
/// getLabelTail - Return true if this pointer points to a valid end of a label.
169+
const char *LLLexer::getLabelTail(const char *Ptr) {
170+
while (Ptr != CurBuf.end()) {
171+
if (Ptr[0] == ':')
172+
return Ptr + 1;
173+
if (!isLabelChar(Ptr[0]))
174+
return nullptr;
175+
++Ptr;
176+
}
177+
return nullptr;
178+
}
179+
177180
int LLLexer::getNextChar() {
178-
char CurChar = *CurPtr++;
179-
switch (CurChar) {
180-
default: return (unsigned char)CurChar;
181-
case 0:
182-
// A nul character in the stream is either the end of the current buffer or
183-
// a random nul in the file. Disambiguate that here.
184-
if (CurPtr-1 != CurBuf.end())
185-
return 0; // Just whitespace.
186-
187-
// Otherwise, return end of file.
188-
--CurPtr; // Another call to lex will return EOF again.
181+
if (CurPtr == CurBuf.end())
189182
return EOF;
190-
}
183+
return *CurPtr++;
191184
}
192185

193186
const char *LLLexer::skipNChars(unsigned N) {
@@ -230,7 +223,7 @@ lltok::Kind LLLexer::LexToken() {
230223
case '%': return LexPercent();
231224
case '"': return LexQuote();
232225
case '.':
233-
if (const char *Ptr = isLabelTail(CurPtr)) {
226+
if (const char *Ptr = getLabelTail(CurPtr)) {
234227
advancePositionTo(Ptr);
235228
StrVal.assign(TokStart, CurPtr-1);
236229
return lltok::LabelStr;
@@ -313,7 +306,7 @@ lltok::Kind LLLexer::LexAt() {
313306
}
314307

315308
lltok::Kind LLLexer::LexDollar() {
316-
if (const char *Ptr = isLabelTail(TokStart)) {
309+
if (const char *Ptr = getLabelTail(TokStart)) {
317310
advancePositionTo(Ptr);
318311
StrVal.assign(TokStart, CurPtr - 1);
319312
return lltok::LabelStr;
@@ -1163,7 +1156,7 @@ lltok::Kind LLLexer::LexDigitOrNegative() {
11631156
if (!isdigit(static_cast<unsigned char>(TokStart[0])) &&
11641157
!isdigit(static_cast<unsigned char>(CurPtr[0]))) {
11651158
// Okay, this is not a number after the -, it's probably a label.
1166-
if (const char *End = isLabelTail(CurPtr)) {
1159+
if (const char *End = getLabelTail(CurPtr)) {
11671160
StrVal.assign(TokStart, End-1);
11681161
advancePositionTo(End);
11691162
return lltok::LabelStr;
@@ -1190,7 +1183,7 @@ lltok::Kind LLLexer::LexDigitOrNegative() {
11901183

11911184
// Check to see if this really is a string label, e.g. "-1:".
11921185
if (isLabelChar(CurPtr[0]) || CurPtr[0] == ':') {
1193-
if (const char *End = isLabelTail(CurPtr)) {
1186+
if (const char *End = getLabelTail(CurPtr)) {
11941187
StrVal.assign(TokStart, End-1);
11951188
advancePositionTo(End);
11961189
return lltok::LabelStr;

0 commit comments

Comments
 (0)