85 lines · c
1//===--- unittests/CodeGen/TestAsmPrinter.h ---------------------*- 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#ifndef LLVM_UNITTESTS_CODEGEN_TESTASMPRINTER_H10#define LLVM_UNITTESTS_CODEGEN_TESTASMPRINTER_H11 12#include "llvm/BinaryFormat/Dwarf.h"13#include "llvm/MC/MCStreamer.h"14#include "gmock/gmock.h"15 16#include <memory>17 18namespace llvm {19class AsmPrinter;20class MCContext;21class Target;22class TargetMachine;23 24class MockMCStreamer : public MCStreamer {25public:26 explicit MockMCStreamer(MCContext *Ctx);27 ~MockMCStreamer();28 29 // These methods are pure virtual in MCStreamer, thus, have to be overridden:30 31 MOCK_METHOD2(emitSymbolAttribute,32 bool(MCSymbol *Symbol, MCSymbolAttr Attribute));33 MOCK_METHOD3(emitCommonSymbol,34 void(MCSymbol *Symbol, uint64_t Size, Align ByteAlignment));35 MOCK_METHOD5(emitZerofill,36 void(MCSection *Section, MCSymbol *Symbol, uint64_t Size,37 Align ByteAlignment, SMLoc Loc));38 39 // The following are mock methods to be used in tests.40 41 MOCK_METHOD2(emitLabel, void(MCSymbol *Symbol, SMLoc Loc));42 MOCK_METHOD2(emitIntValue, void(uint64_t Value, unsigned Size));43 MOCK_METHOD3(emitValueImpl,44 void(const MCExpr *Value, unsigned Size, SMLoc Loc));45 MOCK_METHOD3(emitAbsoluteSymbolDiff,46 void(const MCSymbol *Hi, const MCSymbol *Lo, unsigned Size));47 MOCK_METHOD2(emitCOFFSecRel32, void(MCSymbol const *Symbol, uint64_t Offset));48};49 50class TestAsmPrinter {51 std::unique_ptr<MCContext> MC;52 MockMCStreamer *MS = nullptr; // Owned by AsmPrinter53 std::unique_ptr<TargetMachine> TM;54 std::unique_ptr<AsmPrinter> Asm;55 56 /// Private constructor; call TestAsmPrinter::create(...)57 /// to create an instance.58 TestAsmPrinter();59 60 /// Initialize an AsmPrinter instance with a mocked MCStreamer.61 llvm::Error init(const Target *TheTarget, StringRef TripleStr,62 uint16_t DwarfVersion, dwarf::DwarfFormat DwarfFormat);63 64public:65 /// Create an AsmPrinter and accompanied objects.66 /// Returns ErrorSuccess() with an empty value if the requested target is not67 /// supported so that the corresponding test can be gracefully skipped.68 static llvm::Expected<std::unique_ptr<TestAsmPrinter>>69 create(const std::string &TripleStr, uint16_t DwarfVersion,70 dwarf::DwarfFormat DwarfFormat);71 72 ~TestAsmPrinter();73 74 void setDwarfUsesRelocationsAcrossSections(bool Enable);75 76 AsmPrinter *getAP() const { return Asm.get(); }77 AsmPrinter *releaseAP() { return Asm.release(); }78 MCContext &getCtx() const { return *MC; }79 MockMCStreamer &getMS() const { return *MS; }80};81 82} // end namespace llvm83 84#endif // LLVM_UNITTESTS_CODEGEN_TESTASMPRINTER_H85