MC: formalise some assertions into proper errors
authorSaleem Abdulrasool <compnerd@compnerd.org>
Thu, 22 May 2014 02:18:10 +0000 (02:18 +0000)
committerSaleem Abdulrasool <compnerd@compnerd.org>
Thu, 22 May 2014 02:18:10 +0000 (02:18 +0000)
Now that clang can be used as an assembler via the IAS, invalid assembler inputs
would cause the assertions to trigger.  Although we cannot recover from the
errors here, nor provide caret diagnostics, attempt to handle them slightly more
gracefully by reporting a fatal error.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@209387 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/MC/MCWinCOFFStreamer.h
lib/MC/WinCOFFStreamer.cpp
test/MC/COFF/invalid-def.s [new file with mode: 0644]
test/MC/COFF/invalid-endef.s [new file with mode: 0644]
test/MC/COFF/invalid-scl-range.s [new file with mode: 0644]
test/MC/COFF/invalid-scl.s [new file with mode: 0644]
test/MC/COFF/invalid-type-range.s [new file with mode: 0644]
test/MC/COFF/invalid-type.s [new file with mode: 0644]

index b0a27cdbd75ba62c5971c9aaccba701938de97aa..34e39bb0a63625b153528985d9ffe514f1a3c580 100644 (file)
@@ -65,6 +65,9 @@ public:
 protected:
   const MCSymbol *CurSymbol;
   void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) override;
+
+private:
+  LLVM_ATTRIBUTE_NORETURN void FatalError(const Twine &Msg) const;
 };
 }
 
index 40b8dd944bd94c777516d11f55e57558accee9fb..e6df4651a53615ae8b152afe5573cbf3eaf22c13 100644 (file)
@@ -11,6 +11,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/ADT/StringExtras.h"
 #include "llvm/MC/MCStreamer.h"
 #include "llvm/MC/MCAsmBackend.h"
 #include "llvm/MC/MCAsmLayout.h"
@@ -125,30 +126,39 @@ void MCWinCOFFStreamer::BeginCOFFSymbolDef(MCSymbol const *Symbol) {
   assert((!Symbol->isInSection() ||
           Symbol->getSection().getVariant() == MCSection::SV_COFF) &&
          "Got non-COFF section in the COFF backend!");
-  assert(!CurSymbol && "starting new symbol definition in a symbol definition");
+
+  if (CurSymbol)
+    FatalError("starting a new symbol definition without completing the "
+               "previous one");
   CurSymbol = Symbol;
 }
 
 void MCWinCOFFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
-  assert(CurSymbol && "StorageClass specified outside of symbol definition");
-  assert((StorageClass & ~0xFF) == 0 &&
-         "StorageClass must only have data in the first byte!");
+  if (!CurSymbol)
+    FatalError("storage class specified outside of symbol definition");
+
+  if (StorageClass & ~0xff)
+    FatalError(Twine("storage class value '") + itostr(StorageClass) +
+               "' out of range");
 
   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*CurSymbol);
   SD.modifyFlags(StorageClass << COFF::SF_ClassShift, COFF::SF_ClassMask);
 }
 
 void MCWinCOFFStreamer::EmitCOFFSymbolType(int Type) {
-  assert(CurSymbol && "SymbolType specified outside of a symbol definition");
-  assert((Type & ~0xFFFF) == 0 &&
-         "Type must only have data in the first 2 bytes");
+  if (!CurSymbol)
+    FatalError("symbol type specified outside of a symbol definition");
+
+  if (Type & ~0xffff)
+    FatalError(Twine("type value '") + itostr(Type) + "' out of range");
 
   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*CurSymbol);
   SD.modifyFlags(Type << COFF::SF_TypeShift, COFF::SF_TypeMask);
 }
 
 void MCWinCOFFStreamer::EndCOFFSymbolDef() {
-  assert(CurSymbol && "ending symbol definition without beginning one");
+  if (!CurSymbol)
+    FatalError("ending symbol definition without starting one");
   CurSymbol = nullptr;
 }
 
@@ -239,5 +249,10 @@ void MCWinCOFFStreamer::EmitWin64EHHandlerData() {
 void MCWinCOFFStreamer::FinishImpl() {
   MCObjectStreamer::FinishImpl();
 }
+
+LLVM_ATTRIBUTE_NORETURN
+void MCWinCOFFStreamer::FatalError(const Twine &Msg) const {
+  getContext().FatalError(SMLoc(), Msg);
+}
 }
 
diff --git a/test/MC/COFF/invalid-def.s b/test/MC/COFF/invalid-def.s
new file mode 100644 (file)
index 0000000..bfa1a54
--- /dev/null
@@ -0,0 +1,6 @@
+# RUN: not llvm-mc -triple i686-windows -filetype obj -o /dev/null %s
+# REQUIRES: asserts
+
+       .def first
+       .def second
+
diff --git a/test/MC/COFF/invalid-endef.s b/test/MC/COFF/invalid-endef.s
new file mode 100644 (file)
index 0000000..543685a
--- /dev/null
@@ -0,0 +1,5 @@
+# RUN: not llvm-mc -triple i686-windows -filetype obj -o /dev/null %s
+# REQUIRES: asserts
+
+       .endef
+
diff --git a/test/MC/COFF/invalid-scl-range.s b/test/MC/COFF/invalid-scl-range.s
new file mode 100644 (file)
index 0000000..ec0c2bb
--- /dev/null
@@ -0,0 +1,7 @@
+# RUN: not llvm-mc -triple i686-windows -filetype obj -o /dev/null %s
+# REQUIRES: asserts
+
+       .def storage_class_range
+               .scl 1337
+       .endef
+
diff --git a/test/MC/COFF/invalid-scl.s b/test/MC/COFF/invalid-scl.s
new file mode 100644 (file)
index 0000000..0d62497
--- /dev/null
@@ -0,0 +1,5 @@
+# RUN: not llvm-mc -triple i686-windows -filetype obj -o /dev/null %s
+# REQUIRES: asserts
+
+       .scl 1337
+
diff --git a/test/MC/COFF/invalid-type-range.s b/test/MC/COFF/invalid-type-range.s
new file mode 100644 (file)
index 0000000..9397cc5
--- /dev/null
@@ -0,0 +1,7 @@
+# RUN: not llvm-mc -triple i686-windows -filetype obj -o /dev/null %s
+# REQUIRES: asserts
+
+       .def invalid_type_range
+               .type 65536
+       .endef
+
diff --git a/test/MC/COFF/invalid-type.s b/test/MC/COFF/invalid-type.s
new file mode 100644 (file)
index 0000000..a5c61f4
--- /dev/null
@@ -0,0 +1,5 @@
+# RUN: not llvm-mc -triple i686-windows -filetype obj -o /dev/null %s
+# REQUIRES: asserts
+
+       .type 65536
+