reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
//===- YAML2ObjTest.cpp --------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/ObjectYAML/yaml2obj.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/YAMLTraits.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"

using namespace llvm;
using namespace object;
using namespace yaml;

TEST(yaml2ObjectFile, ELF) {
  bool ErrorReported = false;
  auto ErrHandler = [&](const Twine &Msg) { ErrorReported = true; };

  SmallString<0> Storage;
  std::unique_ptr<ObjectFile> Obj = yaml2ObjectFile(Storage, R"(
--- !ELF
FileHeader:
  Class:    ELFCLASS64
  Data:     ELFDATA2LSB
  Type:     ET_REL
  Machine:  EM_X86_64)", ErrHandler);

  ASSERT_FALSE(ErrorReported);
  ASSERT_TRUE(Obj);
  ASSERT_TRUE(Obj->isELF());
  ASSERT_TRUE(Obj->isRelocatableObject());
}

TEST(yaml2ObjectFile, Errors) {
  std::vector<std::string> Errors;
  auto ErrHandler = [&](const Twine &Msg) {
    Errors.push_back("ObjectYAML: " + Msg.str());
  };

  SmallString<0> Storage;
  StringRef Yaml = R"(
--- !ELF
FileHeader:
  Class:    ELFCLASS64
  Data:     ELFDATA2LSB
  Type:     ET_REL
  Machine:  EM_X86_64
Symbols:
  - Name: foo
  - Name: foo
  - Name: foo
)";

  // 1. Test yaml2ObjectFile().

  std::unique_ptr<ObjectFile> Obj = yaml2ObjectFile(Storage, Yaml, ErrHandler);

  ASSERT_FALSE(Obj);
  ASSERT_TRUE(Errors.size() == 2);
  ASSERT_TRUE(Errors[0] == "ObjectYAML: repeated symbol name: 'foo'");
  ASSERT_TRUE(Errors[1] == Errors[0]);

  // 2. Test convertYAML(). 

  Errors.clear();
  Storage.clear();
  raw_svector_ostream OS(Storage);

  yaml::Input YIn(Yaml);
  bool Res = convertYAML(YIn, OS, ErrHandler);
  ASSERT_FALSE(Res);
  ASSERT_TRUE(Errors.size() == 2);
  ASSERT_TRUE(Errors[0] == "ObjectYAML: repeated symbol name: 'foo'");
  ASSERT_TRUE(Errors[1] == Errors[0]);
}