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
, Align ByteAlignment
));
35 MOCK_METHOD5(emitZerofill
,
36 void(MCSection
*Section
, MCSymbol
*Symbol
, uint64_t Size
,
37 Align ByteAlignment
, SMLoc Loc
));
39 // The following are mock methods to be used in tests.
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
));
50 class TestAsmPrinter
{
51 std::unique_ptr
<MCContext
> MC
;
52 MockMCStreamer
*MS
= nullptr; // Owned by AsmPrinter
53 std::unique_ptr
<TargetMachine
> TM
;
54 std::unique_ptr
<AsmPrinter
> Asm
;
56 /// Private constructor; call TestAsmPrinter::create(...)
57 /// to create an instance.
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
);
65 /// Create an AsmPrinter and accompanied objects.
66 /// Returns ErrorSuccess() with an empty value if the requested target is not
67 /// 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
);
74 void setDwarfUsesRelocationsAcrossSections(bool Enable
);
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
; }
82 } // end namespace llvm
84 #endif // LLVM_UNITTESTS_CODEGEN_TESTASMPRINTER_H