aboutsummaryrefslogtreecommitdiff
path: root/lld/ELF/ScriptLexer.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2021-07-29 20:15:26 +0000
committerDimitry Andric <dim@FreeBSD.org>2021-07-29 20:15:26 +0000
commit344a3780b2e33f6ca763666c380202b18aab72a3 (patch)
treef0b203ee6eb71d7fdd792373e3c81eb18d6934dd /lld/ELF/ScriptLexer.cpp
parentb60736ec1405bb0a8dd40989f67ef4c93da068ab (diff)
downloadsrc-344a3780b2e33f6ca763666c380202b18aab72a3.tar.gz
src-344a3780b2e33f6ca763666c380202b18aab72a3.zip
the upstream release/13.x branch was created.
Diffstat (limited to 'lld/ELF/ScriptLexer.cpp')
-rw-r--r--lld/ELF/ScriptLexer.cpp20
1 files changed, 19 insertions, 1 deletions
diff --git a/lld/ELF/ScriptLexer.cpp b/lld/ELF/ScriptLexer.cpp
index 4b16974dd134..236a188324cd 100644
--- a/lld/ELF/ScriptLexer.cpp
+++ b/lld/ELF/ScriptLexer.cpp
@@ -56,7 +56,25 @@ size_t ScriptLexer::getLineNumber() {
return 1;
StringRef s = getCurrentMB().getBuffer();
StringRef tok = tokens[pos - 1];
- return s.substr(0, tok.data() - s.data()).count('\n') + 1;
+ const size_t tokOffset = tok.data() - s.data();
+
+ // For the first token, or when going backwards, start from the beginning of
+ // the buffer. If this token is after the previous token, start from the
+ // previous token.
+ size_t line = 1;
+ size_t start = 0;
+ if (lastLineNumberOffset > 0 && tokOffset >= lastLineNumberOffset) {
+ start = lastLineNumberOffset;
+ line = lastLineNumber;
+ }
+
+ line += s.substr(start, tokOffset - start).count('\n');
+
+ // Store the line number of this token for reuse.
+ lastLineNumberOffset = tokOffset;
+ lastLineNumber = line;
+
+ return line;
}
// Returns 0-based column number of the current token.