Skip to content

Commit 23dcc6b

Browse files
committed
Replace Line:Column storage with Poiters and on demand conversion
1 parent b3d8254 commit 23dcc6b

File tree

4 files changed

+27
-37
lines changed

4 files changed

+27
-37
lines changed

llvm/include/llvm/AsmParser/LLLexer.h

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,20 @@
1717
#include "llvm/ADT/APFloat.h"
1818
#include "llvm/ADT/APSInt.h"
1919
#include "llvm/Support/SMLoc.h"
20+
#include "llvm/Support/SourceMgr.h"
2021
#include <string>
2122

2223
namespace llvm {
2324
class Type;
2425
class SMDiagnostic;
25-
class SourceMgr;
2626
class LLVMContext;
2727

2828
class LLLexer {
2929
const char *CurPtr;
3030
StringRef CurBuf;
3131

32-
// The line number of the start of the current token, zero-indexed
33-
unsigned CurTokLineNum = 0;
34-
// The column number of the start of the current token, zero-indexed
35-
unsigned CurTokColNum = 0;
36-
// The line number of the end of the current token, zero-indexed
37-
unsigned PrevTokEndLineNum = -1;
38-
// The column number of the end (exclusive) of the current token,
39-
// zero-indexed
40-
unsigned PrevTokEndColNum = -1;
32+
// The the end (exclusive) of the current token
33+
const char *PrevTokEnd = nullptr;
4134

4235
enum class ErrorPriority {
4336
None, // No error message present.
@@ -87,16 +80,22 @@ namespace llvm {
8780
IgnoreColonInIdentifiers = val;
8881
}
8982

90-
// Get the line number of the start of the current token, zero-indexed
91-
unsigned getTokLineNum() { return CurTokLineNum; }
92-
// Get the column number of the start of the current token, zero-indexed
93-
unsigned getTokColNum() { return CurTokColNum; }
94-
// Get the line number of the end of the previous token, zero-indexed,
95-
// exclusive
96-
unsigned getPrevTokEndLineNum() { return PrevTokEndLineNum; }
97-
// Get the column number of the end of the previous token, zero-indexed,
98-
// exclusive
99-
unsigned getPrevTokEndColNum() { return PrevTokEndColNum; }
83+
// Get the line, column position of the start of the current token,
84+
// zero-indexed
85+
std::pair<unsigned, unsigned> getTokLineColumnPos() {
86+
auto LC = SM.getLineAndColumn(SMLoc::getFromPointer(TokStart));
87+
--LC.first;
88+
--LC.second;
89+
return LC;
90+
}
91+
// Get the line, column position of the end of the previous token,
92+
// zero-indexed exclusive
93+
std::pair<unsigned, unsigned> getPrevTokEndLineColumnPos() {
94+
auto LC = SM.getLineAndColumn(SMLoc::getFromPointer(PrevTokEnd));
95+
--LC.first;
96+
--LC.second;
97+
return LC;
98+
}
10099

101100
// This returns true as a convenience for the parser functions that return
102101
// true on error.

llvm/include/llvm/IR/Value.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ struct FileLoc {
6868
}
6969

7070
FileLoc(unsigned L, unsigned C) : Line(L), Col(C) {}
71+
FileLoc(std::pair<unsigned, unsigned> LC) : Line(LC.first), Col(LC.second) {}
7172
};
7273

7374
struct FileLocRange {

llvm/lib/AsmParser/LLLexer.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,9 @@ void LLLexer::advancePositionTo(const char *Ptr) {
205205
lltok::Kind LLLexer::LexToken() {
206206
// Set token end to next location, since the end is
207207
// exclusive
208-
std::tie(PrevTokEndLineNum, PrevTokEndColNum) =
209-
SM.getLineAndColumn(SMLoc::getFromPointer(CurPtr));
210-
--PrevTokEndLineNum;
211-
--PrevTokEndColNum;
208+
PrevTokEnd = CurPtr;
212209
while (true) {
213210
TokStart = CurPtr;
214-
std::tie(CurTokLineNum, CurTokColNum) =
215-
SM.getLineAndColumn(SMLoc::getFromPointer(CurPtr));
216-
--CurTokLineNum;
217-
--CurTokColNum;
218211
int CurChar = getNextChar();
219212

220213
switch (CurChar) {

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ bool LLParser::parseDeclare() {
740740
/// ::= 'define' FunctionHeader (!dbg !56)* '{' ...
741741
bool LLParser::parseDefine() {
742742
assert(Lex.getKind() == lltok::kw_define);
743-
FileLoc FunctionStart(Lex.getTokLineNum(), Lex.getTokColNum());
743+
FileLoc FunctionStart(Lex.getTokLineColumnPos());
744744
Lex.Lex();
745745

746746
Function *F;
@@ -752,8 +752,7 @@ bool LLParser::parseDefine() {
752752
parseFunctionBody(*F, FunctionNumber, UnnamedArgNums);
753753
if (ParserContext)
754754
ParserContext->addFunctionLocation(
755-
F, FileLocRange(FunctionStart, {Lex.getPrevTokEndLineNum(),
756-
Lex.getPrevTokEndColNum()}));
755+
F, FileLocRange(FunctionStart, Lex.getPrevTokEndLineColumnPos()));
757756

758757
return RetValue;
759758
}
@@ -6959,7 +6958,7 @@ bool LLParser::parseFunctionBody(Function &Fn, unsigned FunctionNumber,
69596958
/// parseBasicBlock
69606959
/// ::= (LabelStr|LabelID)? Instruction*
69616960
bool LLParser::parseBasicBlock(PerFunctionState &PFS) {
6962-
FileLoc BBStart(Lex.getTokLineNum(), Lex.getTokColNum());
6961+
FileLoc BBStart(Lex.getTokLineColumnPos());
69636962

69646963
// If this basic block starts out with a name, remember it.
69656964
std::string Name;
@@ -7002,7 +7001,7 @@ bool LLParser::parseBasicBlock(PerFunctionState &PFS) {
70027001
TrailingDbgRecord.emplace_back(DR, DeleteDbgRecord);
70037002
}
70047003

7005-
FileLoc InstStart(Lex.getTokLineNum(), Lex.getTokColNum());
7004+
FileLoc InstStart(Lex.getTokLineColumnPos());
70067005
// This instruction may have three possibilities for a name: a) none
70077006
// specified, b) name specified "%foo =", c) number specified: "%4 =".
70087007
LocTy NameLoc = Lex.getLoc();
@@ -7054,15 +7053,13 @@ bool LLParser::parseBasicBlock(PerFunctionState &PFS) {
70547053
TrailingDbgRecord.clear();
70557054
if (ParserContext) {
70567055
ParserContext->addInstructionLocation(
7057-
Inst, FileLocRange(InstStart, {Lex.getPrevTokEndLineNum(),
7058-
Lex.getPrevTokEndColNum()}));
7056+
Inst, FileLocRange(InstStart, Lex.getPrevTokEndLineColumnPos()));
70597057
}
70607058
} while (!Inst->isTerminator());
70617059

70627060
if (ParserContext)
70637061
ParserContext->addBlockLocation(
7064-
BB, FileLocRange(BBStart, {Lex.getPrevTokEndLineNum(),
7065-
Lex.getPrevTokEndColNum()}));
7062+
BB, FileLocRange(BBStart, Lex.getPrevTokEndLineColumnPos()));
70667063

70677064
assert(TrailingDbgRecord.empty() &&
70687065
"All debug values should have been attached to an instruction.");

0 commit comments

Comments
 (0)