[docs] Fix build-docs.sh
[llvm-project.git] / llvm / unittests / CodeGen / TestAsmPrinter.h
blob1414b8229ed3b018f3ac4795a981a2b68efa7825
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 //===----------------------------------------------------------------------===//
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"
16 #include <memory>
18 namespace llvm {
19 class AsmPrinter;
20 class MCContext;
21 class Target;
22 class TargetMachine;
24 class MockMCStreamer : public MCStreamer {
25 public:
26 explicit MockMCStreamer(MCContext *Ctx);
27 ~MockMCStreamer();
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(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.
58 TestAsmPrinter();
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);
64 public:
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);
72 ~TestAsmPrinter();
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