1 //===--- unittests/CodeGen/TestAsmPrinter.h ---------------------*- C++ -*-===//
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-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_UNITTESTS_CODEGEN_TESTASMPRINTER_H
10 #define LLVM_UNITTESTS_CODEGEN_TESTASMPRINTER_H
12 #include "llvm/BinaryFormat/Dwarf.h"
13 #include "llvm/MC/MCStreamer.h"
14 #include "gmock/gmock.h"
24 class MockMCStreamer
: public MCStreamer
{
26 explicit MockMCStreamer(MCContext
*Ctx
);
29 // These methods are pure virtual in MCStreamer, thus, have to be overridden:
31 MOCK_METHOD2(emitSymbolAttribute
,
32 bool(MCSymbol
*Symbol
, MCSymbolAttr Attribute
));
33 MOCK_METHOD3(emitCommonSymbol
,
34 void(MCSymbol
*Symbol
, uint64_t Size
, unsigned ByteAlignment
));
35 MOCK_METHOD5(emitZerofill
,
36 void(MCSection
*Section
, MCSymbol
*Symbol
, uint64_t Size
,
37 unsigned ByteAlignment
, SMLoc Loc
));
39 // The following are mock methods to be used in tests.
41 MOCK_METHOD2(emitIntValue
, void(uint64_t Value
, unsigned Size
));
42 MOCK_METHOD3(emitValueImpl
,
43 void(const MCExpr
*Value
, unsigned Size
, SMLoc Loc
));
44 MOCK_METHOD3(emitAbsoluteSymbolDiff
,
45 void(const MCSymbol
*Hi
, const MCSymbol
*Lo
, unsigned Size
));
46 MOCK_METHOD2(EmitCOFFSecRel32
, void(MCSymbol
const *Symbol
, uint64_t Offset
));
49 class TestAsmPrinter
{
50 std::unique_ptr
<MCContext
> MC
;
51 MockMCStreamer
*MS
= nullptr; // Owned by AsmPrinter
52 std::unique_ptr
<TargetMachine
> TM
;
53 std::unique_ptr
<AsmPrinter
> Asm
;
55 /// Private constructor; call TestAsmPrinter::create(...)
56 /// to create an instance.
59 /// Initialize an AsmPrinter instance with a mocked MCStreamer.
60 llvm::Error
init(const Target
*TheTarget
, StringRef TripleStr
,
61 uint16_t DwarfVersion
, dwarf::DwarfFormat DwarfFormat
);
64 /// Create an AsmPrinter and accompanied objects.
65 /// Returns ErrorSuccess() with an empty value if the requested target is not
66 /// supported so that the corresponding test can be gracefully skipped.
67 static llvm::Expected
<std::unique_ptr
<TestAsmPrinter
>>
68 create(const std::string
&TripleStr
, uint16_t DwarfVersion
,
69 dwarf::DwarfFormat DwarfFormat
);
73 void setDwarfUsesRelocationsAcrossSections(bool Enable
);
75 AsmPrinter
*getAP() const { return Asm
.get(); }
76 MCContext
&getCtx() const { return *MC
; }
77 MockMCStreamer
&getMS() const { return *MS
; }
80 } // end namespace llvm
82 #endif // LLVM_UNITTESTS_CODEGEN_TESTASMPRINTER_H