[llvm] [llvm-mc][AsmParser] Fix Source location for undefined directional labels. (PR #214654)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 7 02:11:47 PDT 2026
https://github.com/zhangweize9-cyber updated https://github.com/llvm/llvm-project/pull/214654
>From 214583450a87415bb850eca691d91cb006fefab9 Mon Sep 17 00:00:00 2001
From: zhangweize9-cyber <zhangweize9 at gmail.com>
Date: Fri, 7 Aug 2026 15:06:41 +0800
Subject: [PATCH 1/2] [llvm-mc][AsmParser] Fix Source location for undefined
directional labels. Previously, when an error occurred, the corresponding
line of the error could not be correctly captured. To fix this issue, I
introduced a `DenseMap` mechanism into the `AsmParser` private variable group
to record the correct assembly syntax groups.
---
llvm/lib/MC/MCParser/AsmParser.cpp | 31 +++++++++++++++----
.../MC/AsmParser/undef-local-symbol-loc.s | 10 ++++++
2 files changed, 35 insertions(+), 6 deletions(-)
create mode 100644 llvm/test/MC/AsmParser/undef-local-symbol-loc.s
diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index c4c5b1da9aa91..f6d042dca14df 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -13,6 +13,7 @@
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallString.h"
@@ -181,6 +182,9 @@ class AsmParser : public MCAsmParser {
// Is alt macro mode enabled.
bool AltMacroMode = false;
+ // Retrieve all temporary symbols in assembly syntax.
+ DenseMap<const MCSymbol *, SMLoc> LocalSymbolLocs;
+
protected:
virtual bool parseStatement(ParseStatementInfo &Info,
MCAsmParserSemaCallback *SI);
@@ -1043,13 +1047,20 @@ bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
// Variable symbols may not be marked as defined, so check those
// explicitly. If we know it's a variable, we have a definition for
// the purposes of this check.
+
if (Sym && Sym->isTemporary() && !Sym->isVariable() &&
- !Sym->isDefined())
- // FIXME: We would really like to refer back to where the symbol was
- // first referenced for a source location. We need to add something
- // to track that. Currently, we just point to the end of the file.
- printError(getTok().getLoc(), "assembler local symbol '" +
- Sym->getName() + "' not defined");
+ !Sym->isDefined()) {
+ // To fix this issue, I introduced a `DenseMap` mechanism
+ // into the `AsmParser` private variable group to track the correct
+ // assembly syntax groups.
+ SMLoc ErrorLoc = getTok().getLoc();
+ auto It = LocalSymbolLocs.find(Sym);
+ if (It != LocalSymbolLocs.end())
+ ErrorLoc = It->second;
+
+ printError(ErrorLoc, "assembler local symbol '" + Sym->getName() +
+ "' not defined");
+ }
}
}
@@ -1243,6 +1254,14 @@ bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
Sym = getContext().parseSymbol(MAI.isHLASM() ? SymbolName.upper()
: SymbolName);
+ // Store in the mapping table.
+ LocalSymbolLocs.try_emplace(Sym, FirstTokenLoc);
+
+ // If it is a local symbol, record the location of its first reference.
+ if (Sym->isTemporary()) {
+ Sym = getContext().parseSymbol(MAI.isHLASM() ? SymbolName.upper() : SymbolName);
+ }
+
// If this is an absolute variable reference, substitute it now to preserve
// semantics in the face of reassignment.
if (Sym->isVariable()) {
diff --git a/llvm/test/MC/AsmParser/undef-local-symbol-loc.s b/llvm/test/MC/AsmParser/undef-local-symbol-loc.s
new file mode 100644
index 0000000000000..81a31c4ec57f9
--- /dev/null
+++ b/llvm/test/MC/AsmParser/undef-local-symbol-loc.s
@@ -0,0 +1,10 @@
+# RUN: not llvm-mc -triple=x86_64-apple-macos %s 2>&1 | FileCheck %s
+
+.text
+.globl _foo
+_foo:
+# CHECK: :[[#@LINE+2]]:9: error: assembler local symbol 'Ltmp_undefined' not defined
+# CHECK-NEXT: jmp Ltmp_undefined
+ jmp Ltmp_undefined
+ nop
+ jmp Ltmp_undefined
>From bc042c4deb9cde76f4c95de10fa459ec2715bdcc Mon Sep 17 00:00:00 2001
From: zhangweize9-cyber <zhangweize9 at gmail.com>
Date: Fri, 7 Aug 2026 15:45:05 +0800
Subject: [PATCH 2/2] fixed: Formatted AsmParser.
---
llvm/lib/MC/MCParser/AsmParser.cpp | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index f6d042dca14df..939dcc3d6cc39 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -1056,10 +1056,10 @@ bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
SMLoc ErrorLoc = getTok().getLoc();
auto It = LocalSymbolLocs.find(Sym);
if (It != LocalSymbolLocs.end())
- ErrorLoc = It->second;
+ ErrorLoc = It->second;
printError(ErrorLoc, "assembler local symbol '" + Sym->getName() +
- "' not defined");
+ "' not defined");
}
}
}
@@ -1259,7 +1259,8 @@ bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
// If it is a local symbol, record the location of its first reference.
if (Sym->isTemporary()) {
- Sym = getContext().parseSymbol(MAI.isHLASM() ? SymbolName.upper() : SymbolName);
+ Sym = getContext().parseSymbol(MAI.isHLASM() ? SymbolName.upper()
+ : SymbolName);
}
// If this is an absolute variable reference, substitute it now to preserve
More information about the llvm-commits
mailing list