xref: /llvm-project/llvm/unittests/CodeGen/TestAsmPrinter.h (revision 5bf8e14350105b19d3815a3e357fbe7b964cbd45)
1380e746bSIgor Kudrin //===--- unittests/CodeGen/TestAsmPrinter.h ---------------------*- C++ -*-===//
2380e746bSIgor Kudrin //
3380e746bSIgor Kudrin // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4380e746bSIgor Kudrin // See https://llvm.org/LICENSE.txt for license information.
5380e746bSIgor Kudrin // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6380e746bSIgor Kudrin //
7380e746bSIgor Kudrin //===----------------------------------------------------------------------===//
8380e746bSIgor Kudrin 
9380e746bSIgor Kudrin #ifndef LLVM_UNITTESTS_CODEGEN_TESTASMPRINTER_H
10380e746bSIgor Kudrin #define LLVM_UNITTESTS_CODEGEN_TESTASMPRINTER_H
11380e746bSIgor Kudrin 
12380e746bSIgor Kudrin #include "llvm/BinaryFormat/Dwarf.h"
13380e746bSIgor Kudrin #include "llvm/MC/MCStreamer.h"
14380e746bSIgor Kudrin #include "gmock/gmock.h"
15380e746bSIgor Kudrin 
16380e746bSIgor Kudrin #include <memory>
17380e746bSIgor Kudrin 
18380e746bSIgor Kudrin namespace llvm {
19380e746bSIgor Kudrin class AsmPrinter;
20380e746bSIgor Kudrin class MCContext;
21380e746bSIgor Kudrin class Target;
22380e746bSIgor Kudrin class TargetMachine;
23380e746bSIgor Kudrin 
24380e746bSIgor Kudrin class MockMCStreamer : public MCStreamer {
25380e746bSIgor Kudrin public:
26380e746bSIgor Kudrin   explicit MockMCStreamer(MCContext *Ctx);
27380e746bSIgor Kudrin   ~MockMCStreamer();
28380e746bSIgor Kudrin 
29380e746bSIgor Kudrin   // These methods are pure virtual in MCStreamer, thus, have to be overridden:
30380e746bSIgor Kudrin 
31395c8bedSFangrui Song   MOCK_METHOD2(emitSymbolAttribute,
32395c8bedSFangrui Song                bool(MCSymbol *Symbol, MCSymbolAttr Attribute));
33380e746bSIgor Kudrin   MOCK_METHOD3(emitCommonSymbol,
34*5bf8e143SGuillaume Chatelet                void(MCSymbol *Symbol, uint64_t Size, Align ByteAlignment));
35380e746bSIgor Kudrin   MOCK_METHOD5(emitZerofill,
36380e746bSIgor Kudrin                void(MCSection *Section, MCSymbol *Symbol, uint64_t Size,
377203a861SGuillaume Chatelet                     Align ByteAlignment, SMLoc Loc));
38380e746bSIgor Kudrin 
39380e746bSIgor Kudrin   // The following are mock methods to be used in tests.
40380e746bSIgor Kudrin 
41d39bc36bSChen Zheng   MOCK_METHOD2(emitLabel, void(MCSymbol *Symbol, SMLoc Loc));
42380e746bSIgor Kudrin   MOCK_METHOD2(emitIntValue, void(uint64_t Value, unsigned Size));
43380e746bSIgor Kudrin   MOCK_METHOD3(emitValueImpl,
44380e746bSIgor Kudrin                void(const MCExpr *Value, unsigned Size, SMLoc Loc));
45380e746bSIgor Kudrin   MOCK_METHOD3(emitAbsoluteSymbolDiff,
46380e746bSIgor Kudrin                void(const MCSymbol *Hi, const MCSymbol *Lo, unsigned Size));
479ee15bbaSFangrui Song   MOCK_METHOD2(emitCOFFSecRel32, void(MCSymbol const *Symbol, uint64_t Offset));
48380e746bSIgor Kudrin };
49380e746bSIgor Kudrin 
50380e746bSIgor Kudrin class TestAsmPrinter {
51380e746bSIgor Kudrin   std::unique_ptr<MCContext> MC;
52380e746bSIgor Kudrin   MockMCStreamer *MS = nullptr; // Owned by AsmPrinter
53380e746bSIgor Kudrin   std::unique_ptr<TargetMachine> TM;
54380e746bSIgor Kudrin   std::unique_ptr<AsmPrinter> Asm;
55380e746bSIgor Kudrin 
56380e746bSIgor Kudrin   /// Private constructor; call TestAsmPrinter::create(...)
57380e746bSIgor Kudrin   /// to create an instance.
58380e746bSIgor Kudrin   TestAsmPrinter();
59380e746bSIgor Kudrin 
60380e746bSIgor Kudrin   /// Initialize an AsmPrinter instance with a mocked MCStreamer.
61380e746bSIgor Kudrin   llvm::Error init(const Target *TheTarget, StringRef TripleStr,
62380e746bSIgor Kudrin                    uint16_t DwarfVersion, dwarf::DwarfFormat DwarfFormat);
63380e746bSIgor Kudrin 
64380e746bSIgor Kudrin public:
65380e746bSIgor Kudrin   /// Create an AsmPrinter and accompanied objects.
66380e746bSIgor Kudrin   /// Returns ErrorSuccess() with an empty value if the requested target is not
67380e746bSIgor Kudrin   /// supported so that the corresponding test can be gracefully skipped.
68380e746bSIgor Kudrin   static llvm::Expected<std::unique_ptr<TestAsmPrinter>>
69380e746bSIgor Kudrin   create(const std::string &TripleStr, uint16_t DwarfVersion,
70380e746bSIgor Kudrin          dwarf::DwarfFormat DwarfFormat);
71380e746bSIgor Kudrin 
72380e746bSIgor Kudrin   ~TestAsmPrinter();
73380e746bSIgor Kudrin 
74380e746bSIgor Kudrin   void setDwarfUsesRelocationsAcrossSections(bool Enable);
75380e746bSIgor Kudrin 
getAP()76380e746bSIgor Kudrin   AsmPrinter *getAP() const { return Asm.get(); }
releaseAP()77a0ad066cSJameson Nash   AsmPrinter *releaseAP() { return Asm.release(); }
getCtx()78380e746bSIgor Kudrin   MCContext &getCtx() const { return *MC; }
getMS()79380e746bSIgor Kudrin   MockMCStreamer &getMS() const { return *MS; }
80380e746bSIgor Kudrin };
81380e746bSIgor Kudrin 
82380e746bSIgor Kudrin } // end namespace llvm
83380e746bSIgor Kudrin 
84380e746bSIgor Kudrin #endif // LLVM_UNITTESTS_CODEGEN_TESTASMPRINTER_H
85