|
| 1 | +//===- llvm/unittest/MC/MCStreamer.cpp ------------------------------------===// |
| 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 | +#include "llvm/MC/MCStreamer.h" |
| 10 | +#include "llvm/MC/MCAsmBackend.h" |
| 11 | +#include "llvm/MC/MCAsmInfo.h" |
| 12 | +#include "llvm/MC/MCCodeEmitter.h" |
| 13 | +#include "llvm/MC/MCContext.h" |
| 14 | +#include "llvm/MC/MCInstPrinter.h" |
| 15 | +#include "llvm/MC/MCInstrInfo.h" |
| 16 | +#include "llvm/MC/MCRegisterInfo.h" |
| 17 | +#include "llvm/MC/MCSectionELF.h" |
| 18 | +#include "llvm/MC/MCSubtargetInfo.h" |
| 19 | +#include "llvm/MC/TargetRegistry.h" |
| 20 | +#include "llvm/Support/TargetSelect.h" |
| 21 | +#include "gtest/gtest.h" |
| 22 | + |
| 23 | +using namespace llvm; |
| 24 | + |
| 25 | +namespace { |
| 26 | + |
| 27 | +class MCStreamerTest : public ::testing::Test { |
| 28 | +public: |
| 29 | + static constexpr char TripleName[] = "x86_64-pc-linux"; |
| 30 | + std::unique_ptr<MCRegisterInfo> MRI; |
| 31 | + std::unique_ptr<MCAsmInfo> MAI; |
| 32 | + std::unique_ptr<const MCSubtargetInfo> STI; |
| 33 | + const Target *TheTarget; |
| 34 | + |
| 35 | + struct StreamerContext { |
| 36 | + std::unique_ptr<MCContext> Ctx; |
| 37 | + std::unique_ptr<const MCInstrInfo> MII; |
| 38 | + std::unique_ptr<MCInstPrinter> Printer; |
| 39 | + std::unique_ptr<MCStreamer> Streamer; |
| 40 | + }; |
| 41 | + |
| 42 | + MCStreamerTest() { |
| 43 | + llvm::InitializeAllTargetInfos(); |
| 44 | + llvm::InitializeAllTargetMCs(); |
| 45 | + llvm::InitializeAllDisassemblers(); |
| 46 | + |
| 47 | + // If we didn't build the target, do not run the test. |
| 48 | + std::string Error; |
| 49 | + TheTarget = TargetRegistry::lookupTarget(TripleName, Error); |
| 50 | + if (!TheTarget) |
| 51 | + return; |
| 52 | + |
| 53 | + MRI.reset(TheTarget->createMCRegInfo(TripleName)); |
| 54 | + MCTargetOptions MCOptions; |
| 55 | + MAI.reset(TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions)); |
| 56 | + STI.reset(TheTarget->createMCSubtargetInfo(TripleName, "", "")); |
| 57 | + } |
| 58 | + |
| 59 | + /// Create all data structures necessary to operate an assembler. |
| 60 | + StreamerContext createStreamer(raw_pwrite_stream &OS) { |
| 61 | + StreamerContext Res; |
| 62 | + Res.Ctx = |
| 63 | + std::make_unique<MCContext>(Triple(TripleName), MAI.get(), MRI.get(), |
| 64 | + /*MSTI=*/nullptr); |
| 65 | + Res.MII.reset(TheTarget->createMCInstrInfo()); |
| 66 | + |
| 67 | + Res.Printer.reset(TheTarget->createMCInstPrinter( |
| 68 | + Triple(TripleName), MAI->getAssemblerDialect(), *MAI, *Res.MII, *MRI)); |
| 69 | + |
| 70 | + MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*Res.MII, *Res.Ctx); |
| 71 | + MCAsmBackend *MAB = |
| 72 | + TheTarget->createMCAsmBackend(*STI, *MRI, MCTargetOptions()); |
| 73 | + std::unique_ptr<formatted_raw_ostream> Out(new formatted_raw_ostream(OS)); |
| 74 | + Res.Streamer.reset(TheTarget->createAsmStreamer( |
| 75 | + *Res.Ctx, std::move(Out), Res.Printer.get(), |
| 76 | + std::unique_ptr<MCCodeEmitter>(MCE), |
| 77 | + std::unique_ptr<MCAsmBackend>(MAB))); |
| 78 | + return Res; |
| 79 | + } |
| 80 | +}; |
| 81 | +} // namespace |
| 82 | + |
| 83 | +TEST_F(MCStreamerTest, AnonymousGroup) { |
| 84 | + if (!MRI) |
| 85 | + GTEST_SKIP(); |
| 86 | + |
| 87 | + SmallString<0> Out; |
| 88 | + raw_svector_ostream S(Out); |
| 89 | + StreamerContext C = createStreamer(S); |
| 90 | + |
| 91 | + // Test that switching to a group section with no associated signature name |
| 92 | + // doesn't crash. |
| 93 | + C.Streamer->switchSection( |
| 94 | + C.Ctx->getELFSection("foo", ELF::SHT_PROGBITS, ELF::SHF_GROUP)); |
| 95 | + C.Streamer->finish(); |
| 96 | +} |
0 commit comments