• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_UNITTESTS_CODEGEN_TESTASMPRINTER_H
10 #define LLVM_UNITTESTS_CODEGEN_TESTASMPRINTER_H
11 
12 #include "llvm/BinaryFormat/Dwarf.h"
13 #include "llvm/MC/MCStreamer.h"
14 #include "gmock/gmock.h"
15 
16 #include <memory>
17 
18 namespace llvm {
19 class AsmPrinter;
20 class MCContext;
21 class Target;
22 class TargetMachine;
23 
24 class MockMCStreamer : public MCStreamer {
25 public:
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, unsigned ByteAlignment));
35   MOCK_METHOD5(emitZerofill,
36                void(MCSection *Section, MCSymbol *Symbol, uint64_t Size,
37                     unsigned ByteAlignment, SMLoc Loc));
38 
39   // The following are mock methods to be used in tests.
40 
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));
47 };
48 
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;
54 
55   /// Private constructor; call TestAsmPrinter::create(...)
56   /// to create an instance.
57   TestAsmPrinter();
58 
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);
62 
63 public:
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);
70 
71   ~TestAsmPrinter();
72 
73   void setDwarfUsesRelocationsAcrossSections(bool Enable);
74 
getAP()75   AsmPrinter *getAP() const { return Asm.get(); }
releaseAP()76   AsmPrinter *releaseAP() { return Asm.release(); }
getCtx()77   MCContext &getCtx() const { return *MC; }
getMS()78   MockMCStreamer &getMS() const { return *MS; }
79 };
80 
81 } // end namespace llvm
82 
83 #endif // LLVM_UNITTESTS_CODEGEN_TESTASMPRINTER_H
84