Skip to content

Commit 35ca1a5

Browse files
committed
Use SourceMgr to resolve Line:Column position
1 parent 416514e commit 35ca1a5

File tree

2 files changed

+12
-41
lines changed

2 files changed

+12
-41
lines changed

llvm/include/llvm/AsmParser/LLLexer.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ namespace llvm {
2929
const char *CurPtr;
3030
StringRef CurBuf;
3131

32-
// The line number at `CurPtr-1`, zero-indexed
33-
unsigned CurLineNum = 0;
34-
// The column number at `CurPtr-1`, zero-indexed
35-
unsigned CurColNum = -1;
3632
// The line number of the start of the current token, zero-indexed
3733
unsigned CurTokLineNum = 0;
3834
// The column number of the start of the current token, zero-indexed
@@ -91,10 +87,6 @@ namespace llvm {
9187
IgnoreColonInIdentifiers = val;
9288
}
9389

94-
// Get the current line number, zero-indexed
95-
unsigned getLineNum() { return CurLineNum; }
96-
// Get the current column number, zero-indexed
97-
unsigned getColNum() { return CurColNum; }
9890
// Get the line number of the start of the current token, zero-indexed
9991
unsigned getTokLineNum() { return CurTokLineNum; }
10092
// Get the column number of the start of the current token, zero-indexed

llvm/lib/AsmParser/LLLexer.cpp

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -180,11 +180,6 @@ int LLLexer::getNextChar() {
180180
if (CurPtr == CurBuf.end())
181181
return EOF;
182182
// Increment line number if this is the first character after a newline
183-
if (CurPtr > CurBuf.begin() && *(CurPtr - 1) == '\n') {
184-
CurLineNum++;
185-
CurColNum = 0;
186-
} else
187-
CurColNum++;
188183
return *CurPtr++;
189184
}
190185

@@ -195,44 +190,28 @@ const char *LLLexer::skipNChars(unsigned N) {
195190
}
196191

197192
void LLLexer::advancePositionTo(const char *Ptr) {
198-
bool RecalculateColumn = false;
199-
while (CurPtr != Ptr) {
200-
if (CurPtr > Ptr) {
201-
--CurPtr;
202-
--CurColNum;
203-
// Since CurPtr is one char ahead of the stored position, check if the
204-
// previous char is not a newline
205-
if (CurPtr != CurBuf.begin() && *(CurPtr - 1) == '\n') {
206-
--CurLineNum;
207-
RecalculateColumn = true;
208-
}
209-
} else
210-
getNextChar();
193+
if (CurBuf.begin() > Ptr) {
194+
CurPtr = CurBuf.begin();
195+
return;
211196
}
212-
if (RecalculateColumn) {
213-
CurColNum = 0;
214-
// Count the number of chars to the previous newline or start of buffer
215-
for (const char *Ptr = CurPtr; Ptr != CurBuf.begin() && *(Ptr - 1) != '\n';
216-
--Ptr, ++CurColNum)
217-
;
197+
if (CurBuf.end() < Ptr) {
198+
CurPtr = CurBuf.end();
199+
return;
218200
}
201+
202+
CurPtr = Ptr;
219203
}
220204

221205
lltok::Kind LLLexer::LexToken() {
222206
// Set token end to next location, since the end is
223207
// exclusive
224-
if (CurPtr != CurBuf.begin() && *(CurPtr - 1) == '\n') {
225-
PrevTokEndLineNum = CurLineNum + 1;
226-
PrevTokEndColNum = 0;
227-
} else {
228-
PrevTokEndLineNum = CurLineNum;
229-
PrevTokEndColNum = CurColNum + 1;
230-
}
208+
std::tie(PrevTokEndLineNum, PrevTokEndColNum) =
209+
SM.getLineAndColumn(SMLoc::getFromPointer(CurPtr));
231210
while (true) {
232211
TokStart = CurPtr;
212+
std::tie(CurTokLineNum, CurTokColNum) =
213+
SM.getLineAndColumn(SMLoc::getFromPointer(CurPtr));
233214
int CurChar = getNextChar();
234-
CurTokColNum = CurColNum;
235-
CurTokLineNum = CurLineNum;
236215

237216
switch (CurChar) {
238217
default:

0 commit comments

Comments
 (0)