86 lines · cpp
1//===--- unittests/CodeGen/TestAsmPrinter.cpp -------------------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "TestAsmPrinter.h"10#include "llvm/CodeGen/AsmPrinter.h"11#include "llvm/MC/MCAsmInfo.h"12#include "llvm/MC/MCContext.h"13#include "llvm/MC/TargetRegistry.h"14#include "llvm/Target/TargetLoweringObjectFile.h"15#include "llvm/Target/TargetMachine.h"16#include "llvm/TargetParser/Triple.h"17 18using namespace llvm;19using ::testing::StrictMock;20 21// Note: a non-const reference argument cannot be passed through22// testing::StrictMock, thus, we pass a pointer and dereference it here.23MockMCStreamer::MockMCStreamer(MCContext *Ctx) : MCStreamer(*Ctx) {}24 25MockMCStreamer::~MockMCStreamer() = default;26 27TestAsmPrinter::TestAsmPrinter() = default;28 29TestAsmPrinter::~TestAsmPrinter() = default;30 31llvm::Expected<std::unique_ptr<TestAsmPrinter>>32TestAsmPrinter::create(const std::string &TripleStr, uint16_t DwarfVersion,33 dwarf::DwarfFormat DwarfFormat) {34 std::string ErrorStr;35 Triple TT(TripleStr);36 const Target *TheTarget = TargetRegistry::lookupTarget(TT, ErrorStr);37 if (!TheTarget)38 return std::unique_ptr<TestAsmPrinter>();39 40 std::unique_ptr<TestAsmPrinter> TestPrinter(new TestAsmPrinter);41 if (llvm::Error E =42 TestPrinter->init(TheTarget, TripleStr, DwarfVersion, DwarfFormat))43 return std::move(E);44 45 return std::move(TestPrinter);46}47 48// Note:: based on dwarfgen::Generator::init() from49// llvm/unittests/DebugInfo/DWARF/DwarfGenerator.cpp50llvm::Error TestAsmPrinter::init(const Target *TheTarget, StringRef TripleName,51 uint16_t DwarfVersion,52 dwarf::DwarfFormat DwarfFormat) {53 Triple TheTriple(TripleName);54 TM.reset(TheTarget->createTargetMachine(TheTriple, "", "", TargetOptions(),55 std::nullopt));56 if (!TM)57 return make_error<StringError>("no target machine for target " + TripleName,58 inconvertibleErrorCode());59 60 MC.reset(new MCContext(TheTriple, TM->getMCAsmInfo(), TM->getMCRegisterInfo(),61 TM->getMCSubtargetInfo()));62 TM->getObjFileLowering()->Initialize(*MC, *TM);63 MC->setObjectFileInfo(TM->getObjFileLowering());64 65 MS = new StrictMock<MockMCStreamer>(MC.get());66 67 Asm.reset(68 TheTarget->createAsmPrinter(*TM, std::unique_ptr<MockMCStreamer>(MS)));69 if (!Asm)70 return make_error<StringError>("no asm printer for target " + TripleName,71 inconvertibleErrorCode());72 73 // Set the DWARF version correctly on all classes that we use.74 MC->setDwarfVersion(DwarfVersion);75 Asm->setDwarfVersion(DwarfVersion);76 77 // Set the DWARF format.78 MC->setDwarfFormat(DwarfFormat);79 80 return Error::success();81}82 83void TestAsmPrinter::setDwarfUsesRelocationsAcrossSections(bool Enable) {84 Asm->setDwarfUsesRelocationsAcrossSections(Enable);85}86