[lld] [ELF] Add VersionNode lexer state for better version script parsing (PR #174530)
Peter Smith via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 6 10:02:48 PST 2026
================
@@ -124,29 +124,37 @@ void ScriptLexer::lex() {
return;
}
- // Some operators form separate tokens.
- if (s.starts_with("<<=") || s.starts_with(">>=")) {
- curTok = s.substr(0, 3);
- s = s.substr(3);
- return;
- }
- if (s.size() > 1 && (s[1] == '=' && strchr("+-*/!&^|", s[0]))) {
- curTok = s.substr(0, 2);
- s = s.substr(2);
- return;
- }
+ // In Script and Expr states, recognize compound assignment operators.
+ auto doAssign = [&]() -> std::optional<StringRef> {
+ if (s.starts_with("<<=") || s.starts_with(">>=")) {
+ curTok = s.substr(0, 3);
+ s = s.substr(3);
+ return s;
+ }
+ if (s.size() > 1 && (s[1] == '=' && strchr("+-*/!&^|", s[0]))) {
+ curTok = s.substr(0, 2);
+ s = s.substr(2);
+ return s;
+ }
+ return {};
+ };
// Unquoted token. The non-expression token is more relaxed than tokens in
// C-like languages, so that you can write "file-name.cpp" as one bare
// token.
size_t pos;
+ constexpr StringRef scriptChars =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
+ "0123456789_.$/\\~=+[]*?-!^:";
switch (lexState) {
case State::Script:
- pos = s.find_first_not_of(
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
- "0123456789_.$/\\~=+[]*?-!^:");
+ if (auto ret = doAssign())
+ return;
----------------
smithp35 wrote:
In both cases it is called the return value from `doAssign()` looks like it is only used as a true/false for the if statement.
Could doAssign be rewritten, I suggest renaming to `recognizeAssign` as:
```
auto recognizeAssign = [&]() -> bool {
if (s.starts_with("<<=") || s.starts_with(">>=")) {
curTok = s.substr(0, 3);
s = s.substr(3);
return true;
}
if (s.size() > 1 && (s[1] == '=' && strchr("+-*/!&^|", s[0]))) {
curTok = s.substr(0, 2);
s = s.substr(2);
return true;
}
return false;
};
```
Then at the call site:
```
if (recognizeAssign())
return;
```
https://github.com/llvm/llvm-project/pull/174530
More information about the llvm-commits
mailing list