[llvm] r265984 - MCParser: diagnose missing directional labels more clearly.

Tim Northover via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 11 12:50:47 PDT 2016


Author: tnorthover
Date: Mon Apr 11 14:50:46 2016
New Revision: 265984

URL: http://llvm.org/viewvc/llvm-project?rev=265984&view=rev
Log:
MCParser: diagnose missing directional labels more clearly.

Before, ELF at least managed a diagnostic but it was a completely untraceable
"undefined symbol" error. MachO had a variety of even worse behaviours: crash,
emit corrupt file, or an equally bad message.

Added:
    llvm/trunk/test/MC/ELF/undefined-directional.s
    llvm/trunk/test/MC/MachO/undefined-directional.s
Modified:
    llvm/trunk/lib/MC/MCParser/AsmParser.cpp
    llvm/trunk/test/MC/AArch64/error-location.s
    llvm/trunk/test/MC/ARM/error-location.s

Modified: llvm/trunk/lib/MC/MCParser/AsmParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCParser/AsmParser.cpp?rev=265984&r1=265983&r2=265984&view=diff
==============================================================================
--- llvm/trunk/lib/MC/MCParser/AsmParser.cpp (original)
+++ llvm/trunk/lib/MC/MCParser/AsmParser.cpp Mon Apr 11 14:50:46 2016
@@ -146,6 +146,9 @@ private:
   /// \brief List of bodies of anonymous macros.
   std::deque<MCAsmMacro> MacroLikeBodies;
 
+  /// \brief List of forward directional labels for diagnosis at the end.
+  SmallVector<std::pair<SMLoc, MCSymbol *>, 4> DirectionalLabels;
+
   /// Boolean tracking whether macro substitution is enabled.
   unsigned MacrosEnabledFlag : 1;
 
@@ -696,18 +699,28 @@ bool AsmParser::Run(bool NoInitialTextSe
   // Targets that don't do subsections via symbols may not want this, though,
   // so conservatively exclude them. Only do this if we're finalizing, though,
   // as otherwise we won't necessarilly have seen everything yet.
-  if (!NoFinalize && MAI.hasSubsectionsViaSymbols()) {
-    for (const auto &TableEntry : getContext().getSymbols()) {
-      MCSymbol *Sym = TableEntry.getValue();
-      // 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->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.
-        return Error(getLexer().getLoc(), "assembler local symbol '" +
-                                              Sym->getName() + "' not defined");
+  if (!NoFinalize) {
+    if (MAI.hasSubsectionsViaSymbols()) {
+      for (const auto &TableEntry : getContext().getSymbols()) {
+        MCSymbol *Sym = TableEntry.getValue();
+        // 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->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.
+          HadError |=
+              Error(getLexer().getLoc(), "assembler local symbol '" +
+                                             Sym->getName() + "' not defined");
+      }
+    }
+
+    // Temporary symbols like the ones for directional jumps don't go in the
+    // symbol table. They also need to be diagnosed in all (final) cases.
+    for (std::pair<SMLoc, MCSymbol *> &LocSym : DirectionalLabels) {
+      if (LocSym.second->isUndefined())
+        HadError |= Error(LocSym.first, "directional label undefined");
     }
   }
 
@@ -917,7 +930,8 @@ bool AsmParser::parsePrimaryExpr(const M
             Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b");
         Res = MCSymbolRefExpr::create(Sym, Variant, getContext());
         if (IDVal == "b" && Sym->isUndefined())
-          return Error(Loc, "invalid reference to undefined symbol");
+          return Error(Loc, "directional label undefined");
+        DirectionalLabels.push_back(std::make_pair(Loc, Sym));
         EndLoc = Lexer.getTok().getEndLoc();
         Lex(); // Eat identifier.
       }

Modified: llvm/trunk/test/MC/AArch64/error-location.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/AArch64/error-location.s?rev=265984&r1=265983&r2=265984&view=diff
==============================================================================
--- llvm/trunk/test/MC/AArch64/error-location.s (original)
+++ llvm/trunk/test/MC/AArch64/error-location.s Mon Apr 11 14:50:46 2016
@@ -41,9 +41,6 @@
 // CHECK: <unknown>:0: error: Common symbol 'common' cannot be used in assignment expr
   .set v3, common
 
-// CHECK: <unknown>:0: error: Undefined temporary symbol
-  .word 5f
-
 // CHECK: <unknown>:0: error: symbol 'undef' could not be evaluated in a subtraction expression
   .set v2, a-undef
 

Modified: llvm/trunk/test/MC/ARM/error-location.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ARM/error-location.s?rev=265984&r1=265983&r2=265984&view=diff
==============================================================================
--- llvm/trunk/test/MC/ARM/error-location.s (original)
+++ llvm/trunk/test/MC/ARM/error-location.s Mon Apr 11 14:50:46 2016
@@ -23,9 +23,6 @@
 @ CHECK: <unknown>:0: error: Common symbol 'common' cannot be used in assignment expr
   .set v3, common
 
-@ CHECK: <unknown>:0: error: Undefined temporary symbol
-  .word 5f
-
 @ CHECK: <unknown>:0: error: symbol 'undef' could not be evaluated in a subtraction expression
   .set v2, a-undef
 

Added: llvm/trunk/test/MC/ELF/undefined-directional.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/ELF/undefined-directional.s?rev=265984&view=auto
==============================================================================
--- llvm/trunk/test/MC/ELF/undefined-directional.s (added)
+++ llvm/trunk/test/MC/ELF/undefined-directional.s Mon Apr 11 14:50:46 2016
@@ -0,0 +1,9 @@
+// RUN: not llvm-mc -triple x86_64-linux-gnu -filetype=obj -o /dev/null %s 2>&1 | FileCheck  %s
+
+// CHECK: [[@LINE+1]]:{{[0-9]+}}: error: directional label undefined
+        jmp 1b
+// CHECK: [[@LINE+1]]:{{[0-9]+}}: error: directional label undefined
+        jmp 1f
+// CHECK: [[@LINE+1]]:{{[0-9]+}}: error: directional label undefined
+        jmp 2f
+

Added: llvm/trunk/test/MC/MachO/undefined-directional.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/MachO/undefined-directional.s?rev=265984&view=auto
==============================================================================
--- llvm/trunk/test/MC/MachO/undefined-directional.s (added)
+++ llvm/trunk/test/MC/MachO/undefined-directional.s Mon Apr 11 14:50:46 2016
@@ -0,0 +1,9 @@
+// RUN: not llvm-mc -triple x86_64-apple-macosx -filetype=obj -o /dev/null %s 2>&1 | FileCheck  %s
+
+// CHECK: [[@LINE+1]]:{{[0-9]+}}: error: directional label undefined
+        jmp 1b
+// CHECK: [[@LINE+1]]:{{[0-9]+}}: error: directional label undefined
+        jmp 1f
+// CHECK: [[@LINE+1]]:{{[0-9]+}}: error: directional label undefined
+        jmp 2f
+




More information about the llvm-commits mailing list