AsmWriterTest.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //===- llvm/unittest/IR/AsmWriter.cpp - AsmWriter tests -------------------===//
  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. #include "llvm/BinaryFormat/Dwarf.h"
  9. #include "llvm/IR/DebugInfoMetadata.h"
  10. #include "llvm/IR/Function.h"
  11. #include "llvm/IR/IRBuilder.h"
  12. #include "llvm/IR/LLVMContext.h"
  13. #include "llvm/IR/MDBuilder.h"
  14. #include "llvm/IR/Module.h"
  15. #include "gtest/gtest.h"
  16. using namespace llvm;
  17. namespace {
  18. TEST(AsmWriterTest, DebugPrintDetachedInstruction) {
  19. // PR24852: Ensure that an instruction can be printed even when it
  20. // has metadata attached but no parent.
  21. LLVMContext Ctx;
  22. auto Ty = Type::getInt32Ty(Ctx);
  23. auto Undef = UndefValue::get(Ty);
  24. std::unique_ptr<BinaryOperator> Add(BinaryOperator::CreateAdd(Undef, Undef));
  25. Add->setMetadata(
  26. "", MDNode::get(Ctx, {ConstantAsMetadata::get(ConstantInt::get(Ty, 1))}));
  27. std::string S;
  28. raw_string_ostream OS(S);
  29. Add->print(OS);
  30. std::size_t r = OS.str().find("<badref> = add i32 undef, undef, !<empty");
  31. EXPECT_TRUE(r != std::string::npos);
  32. }
  33. TEST(AsmWriterTest, DebugPrintDetachedArgument) {
  34. LLVMContext Ctx;
  35. auto Ty = Type::getInt32Ty(Ctx);
  36. auto Arg = new Argument(Ty);
  37. std::string S;
  38. raw_string_ostream OS(S);
  39. Arg->print(OS);
  40. EXPECT_EQ(S, "i32 <badref>");
  41. delete Arg;
  42. }
  43. TEST(AsmWriterTest, DumpDIExpression) {
  44. LLVMContext Ctx;
  45. uint64_t Ops[] = {
  46. dwarf::DW_OP_constu, 4,
  47. dwarf::DW_OP_minus,
  48. dwarf::DW_OP_deref,
  49. };
  50. DIExpression *Expr = DIExpression::get(Ctx, Ops);
  51. std::string S;
  52. raw_string_ostream OS(S);
  53. Expr->print(OS);
  54. EXPECT_EQ("!DIExpression(DW_OP_constu, 4, DW_OP_minus, DW_OP_deref)",
  55. OS.str());
  56. }
  57. }