[llvm] [AsmParser] Fixed `assembler local symbol not defined` unable to locate the exact line number. (PR #219719)

via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 29 13:18:44 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-mc

Author: original-cooling-space (zhangweize9-cyber)

<details>
<summary>Changes</summary>

To be honest, when I was fixing this problem, my initial approach was limited by the `AsmParser.cpp` file. Later, I used the language server to locate the header file `llvm/include/llvm/MC/MCSymbol.h` based on the variable `isTemporary`, and found that it contained bit field variable declarations. Of course, I think the purpose of this design is to save memory.

During this period, I tried to save the parsed symbol names into the `Loc` variable within the `parsePrimaryExpr` function body or the `parseIdentifier` branch, but unfortunately, these methods all failed.

Later, when I used gdb to set breakpoints and analyze the stack trace of the function, I found that the `AsmParser.cpp` module called the `MCContext.h` and `MCExpr.cpp` modules. Since all referenced symbols must generate AST nodes in the `MCSymbolRefExpr::create()` function, I chose to intercept it directly at this entry point. The added logic is that whenever a symbol reference node is created, the `Loc` of the current expression is stored in the `Context`.

---
Full diff: https://github.com/llvm/llvm-project/pull/219719.diff


4 Files Affected:

- (modified) llvm/include/llvm/MC/MCContext.h (+14) 
- (modified) llvm/lib/MC/MCExpr.cpp (+2) 
- (modified) llvm/lib/MC/MCParser/AsmParser.cpp (+19-6) 
- (added) llvm/test/MC/AsmParser/undefined-local-symbol-location.s (+12) 


``````````diff
diff --git a/llvm/include/llvm/MC/MCContext.h b/llvm/include/llvm/MC/MCContext.h
index 7e76abe08f56f..9eca783571898 100644
--- a/llvm/include/llvm/MC/MCContext.h
+++ b/llvm/include/llvm/MC/MCContext.h
@@ -162,6 +162,9 @@ class MCContext {
   /// We have three labels represented by the pairs (1, 0), (2, 0) and (1, 1)
   DenseMap<std::pair<unsigned, unsigned>, MCSymbol *> LocalSymbols;
 
+  /// Recording SymbolLocs.
+  DenseMap<const MCSymbol *, SMLoc> SymbolLocs;
+
   /// Keeps track of labels that are used in inline assembly.
   StringMap<MCSymbol *, BumpPtrAllocator &> InlineAsmUsedLabelNames;
 
@@ -481,6 +484,17 @@ class MCContext {
   /// \param Name - The symbol name, which must be unique across all symbols.
   LLVM_ABI MCSymbol *getOrCreateSymbol(const Twine &Name);
 
+  /// Set the initial source location for a symbol.
+  void setSymbolLoc(const MCSymbol *Sym, SMLoc Loc) {
+    if (Sym && Loc.isValid() && !SymbolLocs.count(Sym))
+      SymbolLocs[Sym] = Loc;
+  }
+
+  /// Get the recorded source location for a symbol.
+  SMLoc getSymbolLoc(const MCSymbol *Sym) const {
+    return SymbolLocs.lookup(Sym);
+  }
+
   /// Variant of getOrCreateSymbol that handles backslash-escaped symbols.
   /// For example, parse "a\"b\\" as a"\.
   LLVM_ABI MCSymbol *parseSymbol(const Twine &Name);
diff --git a/llvm/lib/MC/MCExpr.cpp b/llvm/lib/MC/MCExpr.cpp
index c23dc3c119a01..d5ce3ad8d29f0 100644
--- a/llvm/lib/MC/MCExpr.cpp
+++ b/llvm/lib/MC/MCExpr.cpp
@@ -226,6 +226,8 @@ MCSymbolRefExpr::MCSymbolRefExpr(const MCSymbol *Symbol, Spec specifier,
 const MCSymbolRefExpr *MCSymbolRefExpr::create(const MCSymbol *Sym,
                                                uint16_t specifier,
                                                MCContext &Ctx, SMLoc Loc) {
+  if (Sym && Loc.isValid())
+    Ctx.setSymbolLoc(Sym, Loc);
   return new (Ctx) MCSymbolRefExpr(Sym, specifier, Loc);
 }
 
diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index 839dbca990578..8b446c2d2684a 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -1044,12 +1044,25 @@ bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
         // 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()) {
+          SMLoc ErrorLoc = getContext().getSymbolLoc(Sym);
+          if (!ErrorLoc.isValid())
+            ErrorLoc = getTok().getLoc();
+
+          /// Achieve underline annotation effect.
+          SMRange SymbolRange;
+          if (ErrorLoc.isValid()) {
+            const char *StartPtr = ErrorLoc.getPointer();
+            SMLoc EndLoc =
+                SMLoc::getFromPointer(StartPtr + Sym->getName().size());
+            SymbolRange = SMRange(ErrorLoc, EndLoc);
+          }
+
+          printError(ErrorLoc,
+                     "assembler local symbol '" + Sym->getName() +
+                         "' not defined",
+                     SymbolRange);
+        }
       }
     }
 
diff --git a/llvm/test/MC/AsmParser/undefined-local-symbol-location.s b/llvm/test/MC/AsmParser/undefined-local-symbol-location.s
new file mode 100644
index 0000000000000..e5529e7699ad0
--- /dev/null
+++ b/llvm/test/MC/AsmParser/undefined-local-symbol-location.s
@@ -0,0 +1,12 @@
+// NOTE: Assertions have been autogenerated by utils/update_mc_test_checks.py UTC_ARGS: --version 6
+// RUN: not llvm-mc -triple=x86_64-apple-macos %s 2>&1 | FileCheck %s
+.text
+
+jmp L_undefined_temporary_symbol
+// CHECK: :[[@LINE-1]]:5: error: assembler local symbol 'L_undefined_temporary_symbol' not defined
+movl %eax
+// CHECK: :[[@LINE-1]]:1: error: too few operands for instruction
+movl $0x1234567890, %eax
+// CHECK: :[[@LINE-1]]:6: error: unknown token in expression
+invalid_inst_name %eax
+// CHECK: :[[@LINE-1]]:1: error: invalid instruction mnemonic 'invalid_inst_name'

``````````

</details>


https://github.com/llvm/llvm-project/pull/219719


More information about the llvm-commits mailing list