]> granicus.if.org Git - llvm/commitdiff
[JITLink] Add check to JITLink unit test to bail out for unsupported targets.
authorLang Hames <lhames@gmail.com>
Sat, 20 Apr 2019 18:30:17 +0000 (18:30 +0000)
committerLang Hames <lhames@gmail.com>
Sat, 20 Apr 2019 18:30:17 +0000 (18:30 +0000)
This should prevent spurious JITLink unit test failures for builds that do not
support the target(s) required by the tests.

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

unittests/ExecutionEngine/JITLink/JITLinkTestCommon.cpp
unittests/ExecutionEngine/JITLink/JITLinkTestCommon.h
unittests/ExecutionEngine/JITLink/JITLinkTest_MachO_x86_64_Tests.cpp

index 786b00e798584ce3fcfcc1c2394d348423f9c18b..b0c6d5b4481f2dd70541822a19e42096a1358f62 100644 (file)
 using namespace llvm::jitlink;
 namespace llvm {
 
-JITLinkTestCommon::TestResources::TestResources(StringRef AsmSrc,
-                                                StringRef TripleStr, bool PIC,
-                                                bool LargeCodeModel,
-                                                MCTargetOptions Options)
-    : ObjStream(ObjBuffer), Options(std::move(Options)) {
-  Triple TT(Triple::normalize(TripleStr));
-  initializeTripleSpecifics(TT);
-  initializeTestSpecifics(AsmSrc, TT, PIC, LargeCodeModel);
+Expected<std::unique_ptr<JITLinkTestCommon::TestResources>>
+JITLinkTestCommon::TestResources::Create(StringRef AsmSrc, StringRef TripleStr,
+                                         bool PIC, bool LargeCodeModel,
+                                         MCTargetOptions Options) {
+  Error Err = Error::success();
+  auto R = std::unique_ptr<TestResources>(new TestResources(
+      AsmSrc, TripleStr, PIC, LargeCodeModel, std::move(Options), Err));
+  if (Err)
+    return std::move(Err);
+  return std::move(R);
 }
 
 MemoryBufferRef
@@ -31,12 +33,27 @@ JITLinkTestCommon::TestResources::getTestObjectBufferRef() const {
                          "Test object");
 }
 
-void JITLinkTestCommon::TestResources::initializeTripleSpecifics(Triple &TT) {
-  std::string Error;
-  TheTarget = TargetRegistry::lookupTarget("", TT, Error);
+JITLinkTestCommon::TestResources::TestResources(StringRef AsmSrc,
+                                                StringRef TripleStr, bool PIC,
+                                                bool LargeCodeModel,
+                                                MCTargetOptions Options,
+                                                Error &Err)
+    : ObjStream(ObjBuffer), Options(std::move(Options)) {
+  ErrorAsOutParameter _(&Err);
+  Triple TT(Triple::normalize(TripleStr));
+  if (auto Err2 = initializeTripleSpecifics(TT)) {
+    Err = std::move(Err2);
+    return;
+  }
+  initializeTestSpecifics(AsmSrc, TT, PIC, LargeCodeModel);
+}
+
+Error JITLinkTestCommon::TestResources::initializeTripleSpecifics(Triple &TT) {
+  std::string ErrorMsg;
+  TheTarget = TargetRegistry::lookupTarget("", TT, ErrorMsg);
 
   if (!TheTarget)
-    report_fatal_error(Error);
+    return make_error<StringError>(ErrorMsg, inconvertibleErrorCode());
 
   MRI.reset(TheTarget->createMCRegInfo(TT.getTriple()));
   if (!MRI)
@@ -59,6 +76,8 @@ void JITLinkTestCommon::TestResources::initializeTripleSpecifics(Triple &TT) {
 
   if (!Dis)
     report_fatal_error("Could not build MCDisassembler");
+
+  return Error::success();
 }
 
 void JITLinkTestCommon::TestResources::initializeTestSpecifics(
index c19232e7bbc51f33138ef5964eab9b41108a4540..8e1273ed91198e08854edb6153dfe3a4d1dacb86 100644 (file)
@@ -40,15 +40,19 @@ public:
 
   class TestResources {
   public:
-    TestResources(StringRef AsmSrc, StringRef TripleStr, bool PIC,
-                  bool LargeCodeModel, MCTargetOptions Options);
+    static Expected<std::unique_ptr<TestResources>>
+    Create(StringRef AsmSrc, StringRef TripleStr, bool PIC, bool LargeCodeModel,
+           MCTargetOptions Options);
 
     MemoryBufferRef getTestObjectBufferRef() const;
 
     const MCDisassembler &getDisassembler() const { return *Dis; }
 
   private:
-    void initializeTripleSpecifics(Triple &TT);
+    TestResources(StringRef AsmSrc, StringRef TripleStr, bool PIC,
+                  bool LargeCodeModel, MCTargetOptions Options, Error &Err);
+
+    Error initializeTripleSpecifics(Triple &TT);
     void initializeTestSpecifics(StringRef AsmSource, const Triple &TT,
                                  bool PIC, bool LargeCodeModel);
 
@@ -123,11 +127,16 @@ public:
 
   JITLinkTestCommon();
 
-  std::unique_ptr<TestResources>
+  /// Get TestResources for this target/test.
+  ///
+  /// If this method fails it is likely because the target is not supported in
+  /// this build. The test should bail out without failing (possibly logging a
+  /// diagnostic).
+  Expected<std::unique_ptr<TestResources>>
   getTestResources(StringRef AsmSrc, StringRef Triple, bool PIC,
                    bool LargeCodeModel, MCTargetOptions Options) const {
-    return llvm::make_unique<TestResources>(AsmSrc, Triple, PIC, LargeCodeModel,
-                                            std::move(Options));
+    return TestResources::Create(AsmSrc, Triple, PIC, LargeCodeModel,
+                                 std::move(Options));
   }
 
   template <typename T>
index f8728c78a032631d2d540d8ffc7401e9ed877ba3..36e62a4f0ca78835ee5bdb9a17a43f629fd7e920 100644 (file)
@@ -33,9 +33,14 @@ public:
                                BasicVerifyGraphFunction RunGraphTest) {
     auto TR = getTestResources(AsmSrc, Triple, PIC, LargeCodeModel,
                                std::move(Options));
+    if (!TR) {
+      dbgs() << "Skipping JITLInk unit test: " << toString(TR.takeError())
+             << "\n";
+      return;
+    }
 
     auto JTCtx = llvm::make_unique<TestJITLinkContext>(
-        *TR, [&](AtomGraph &G) { RunGraphTest(G, TR->getDisassembler()); });
+        **TR, [&](AtomGraph &G) { RunGraphTest(G, (*TR)->getDisassembler()); });
 
     JTCtx->externals() = std::move(Externals);