blob: c7ec3a9333023e3c4f80978f80d08b540c8fba7e [file] [log] [blame]
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +00001//===- llvm/unittest/IR/DebugInfo.cpp - DebugInfo tests -------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +00006//
7//===----------------------------------------------------------------------===//
8
Vedant Kumar93507922020-04-30 13:22:22 -07009#include "llvm/IR/DebugInfo.h"
10#include "llvm/AsmParser/Parser.h"
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +000011#include "llvm/IR/DebugInfoMetadata.h"
Vedant Kumar93507922020-04-30 13:22:22 -070012#include "llvm/IR/LLVMContext.h"
13#include "llvm/IR/Module.h"
14#include "llvm/IR/Verifier.h"
15#include "llvm/Support/SourceMgr.h"
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000016#include "gtest/gtest.h"
17
18using namespace llvm;
19
Vedant Kumar93507922020-04-30 13:22:22 -070020static std::unique_ptr<Module> parseIR(LLVMContext &C, const char *IR) {
21 SMDiagnostic Err;
22 std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
23 if (!Mod)
24 Err.print("DebugInfoTest", errs());
25 return Mod;
26}
27
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000028namespace {
29
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000030TEST(DINodeTest, getFlag) {
Duncan P. N. Exon Smithc22a5c22015-02-21 00:43:09 +000031 // Some valid flags.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000032 EXPECT_EQ(DINode::FlagPublic, DINode::getFlag("DIFlagPublic"));
33 EXPECT_EQ(DINode::FlagProtected, DINode::getFlag("DIFlagProtected"));
34 EXPECT_EQ(DINode::FlagPrivate, DINode::getFlag("DIFlagPrivate"));
35 EXPECT_EQ(DINode::FlagVector, DINode::getFlag("DIFlagVector"));
36 EXPECT_EQ(DINode::FlagRValueReference,
37 DINode::getFlag("DIFlagRValueReference"));
Duncan P. N. Exon Smithc22a5c22015-02-21 00:43:09 +000038
39 // FlagAccessibility shouldn't work.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000040 EXPECT_EQ(0u, DINode::getFlag("DIFlagAccessibility"));
Duncan P. N. Exon Smithc22a5c22015-02-21 00:43:09 +000041
42 // Some other invalid strings.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000043 EXPECT_EQ(0u, DINode::getFlag("FlagVector"));
44 EXPECT_EQ(0u, DINode::getFlag("Vector"));
45 EXPECT_EQ(0u, DINode::getFlag("other things"));
46 EXPECT_EQ(0u, DINode::getFlag("DIFlagOther"));
Duncan P. N. Exon Smithc22a5c22015-02-21 00:43:09 +000047}
48
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000049TEST(DINodeTest, getFlagString) {
Duncan P. N. Exon Smithc22a5c22015-02-21 00:43:09 +000050 // Some valid flags.
51 EXPECT_EQ(StringRef("DIFlagPublic"),
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000052 DINode::getFlagString(DINode::FlagPublic));
Duncan P. N. Exon Smithc22a5c22015-02-21 00:43:09 +000053 EXPECT_EQ(StringRef("DIFlagProtected"),
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000054 DINode::getFlagString(DINode::FlagProtected));
Duncan P. N. Exon Smithc22a5c22015-02-21 00:43:09 +000055 EXPECT_EQ(StringRef("DIFlagPrivate"),
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000056 DINode::getFlagString(DINode::FlagPrivate));
Duncan P. N. Exon Smithc22a5c22015-02-21 00:43:09 +000057 EXPECT_EQ(StringRef("DIFlagVector"),
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000058 DINode::getFlagString(DINode::FlagVector));
Duncan P. N. Exon Smithc22a5c22015-02-21 00:43:09 +000059 EXPECT_EQ(StringRef("DIFlagRValueReference"),
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000060 DINode::getFlagString(DINode::FlagRValueReference));
Duncan P. N. Exon Smithc22a5c22015-02-21 00:43:09 +000061
62 // FlagAccessibility actually equals FlagPublic.
63 EXPECT_EQ(StringRef("DIFlagPublic"),
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000064 DINode::getFlagString(DINode::FlagAccessibility));
Duncan P. N. Exon Smithc22a5c22015-02-21 00:43:09 +000065
66 // Some other invalid flags.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000067 EXPECT_EQ(StringRef(),
68 DINode::getFlagString(DINode::FlagPublic | DINode::FlagVector));
69 EXPECT_EQ(StringRef(), DINode::getFlagString(DINode::FlagFwdDecl |
70 DINode::FlagArtificial));
Leny Kholodov5fcc4182016-09-06 10:46:28 +000071 EXPECT_EQ(StringRef(),
72 DINode::getFlagString(static_cast<DINode::DIFlags>(0xffff)));
Duncan P. N. Exon Smithc22a5c22015-02-21 00:43:09 +000073}
74
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000075TEST(DINodeTest, splitFlags) {
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +000076// Some valid flags.
Duncan P. N. Exon Smith269e38d2015-02-21 00:45:26 +000077#define CHECK_SPLIT(FLAGS, VECTOR, REMAINDER) \
78 { \
Leny Kholodov5fcc4182016-09-06 10:46:28 +000079 SmallVector<DINode::DIFlags, 8> V; \
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000080 EXPECT_EQ(REMAINDER, DINode::splitFlags(FLAGS, V)); \
Duncan P. N. Exon Smith5261e4b2015-04-07 01:21:40 +000081 EXPECT_TRUE(makeArrayRef(V).equals(VECTOR)); \
Duncan P. N. Exon Smith269e38d2015-02-21 00:45:26 +000082 }
Leny Kholodov5fcc4182016-09-06 10:46:28 +000083 CHECK_SPLIT(DINode::FlagPublic, {DINode::FlagPublic}, DINode::FlagZero);
84 CHECK_SPLIT(DINode::FlagProtected, {DINode::FlagProtected}, DINode::FlagZero);
85 CHECK_SPLIT(DINode::FlagPrivate, {DINode::FlagPrivate}, DINode::FlagZero);
86 CHECK_SPLIT(DINode::FlagVector, {DINode::FlagVector}, DINode::FlagZero);
Leny Kholodov40c62352016-09-06 17:03:02 +000087 CHECK_SPLIT(DINode::FlagRValueReference, {DINode::FlagRValueReference},
88 DINode::FlagZero);
Leny Kholodov5fcc4182016-09-06 10:46:28 +000089 DINode::DIFlags Flags[] = {DINode::FlagFwdDecl, DINode::FlagVector};
Leny Kholodov40c62352016-09-06 17:03:02 +000090 CHECK_SPLIT(DINode::FlagFwdDecl | DINode::FlagVector, Flags,
91 DINode::FlagZero);
Leny Kholodov5fcc4182016-09-06 10:46:28 +000092 CHECK_SPLIT(DINode::FlagZero, {}, DINode::FlagZero);
Duncan P. N. Exon Smith269e38d2015-02-21 00:45:26 +000093#undef CHECK_SPLIT
94}
95
Vedant Kumar93507922020-04-30 13:22:22 -070096TEST(StripTest, LoopMetadata) {
97 LLVMContext C;
98 std::unique_ptr<Module> M = parseIR(C, R"(
99 define void @f() !dbg !5 {
100 ret void, !dbg !10, !llvm.loop !11
101 }
102
103 !llvm.dbg.cu = !{!0}
104 !llvm.debugify = !{!3, !3}
105 !llvm.module.flags = !{!4}
106
107 !0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1, producer: "debugify", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
108 !1 = !DIFile(filename: "loop.ll", directory: "/")
109 !2 = !{}
110 !3 = !{i32 1}
111 !4 = !{i32 2, !"Debug Info Version", i32 3}
112 !5 = distinct !DISubprogram(name: "f", linkageName: "f", scope: null, file: !1, line: 1, type: !6, scopeLine: 1, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !7)
113 !6 = !DISubroutineType(types: !2)
114 !7 = !{!8}
115 !8 = !DILocalVariable(name: "1", scope: !5, file: !1, line: 1, type: !9)
116 !9 = !DIBasicType(name: "ty32", size: 32, encoding: DW_ATE_unsigned)
117 !10 = !DILocation(line: 1, column: 1, scope: !5)
118 !11 = distinct !{!11, !10, !10}
119)");
120
121 // Look up the debug info emission kind for the CU via the loop metadata
122 // attached to the terminator. If, when stripping non-line table debug info,
123 // we update the terminator's metadata correctly, we should be able to
124 // observe the change in emission kind for the CU.
125 auto getEmissionKind = [&]() {
126 Instruction &I = *M->getFunction("f")->getEntryBlock().getFirstNonPHI();
127 MDNode *LoopMD = I.getMetadata(LLVMContext::MD_loop);
128 return cast<DILocation>(LoopMD->getOperand(1))
129 ->getScope()
130 ->getSubprogram()
131 ->getUnit()
132 ->getEmissionKind();
133 };
134
135 EXPECT_EQ(getEmissionKind(), DICompileUnit::FullDebug);
136
137 bool Changed = stripNonLineTableDebugInfo(*M);
138 EXPECT_TRUE(Changed);
139
140 EXPECT_EQ(getEmissionKind(), DICompileUnit::LineTablesOnly);
141
142 bool BrokenDebugInfo = false;
143 bool HardError = verifyModule(*M, &errs(), &BrokenDebugInfo);
144 EXPECT_FALSE(HardError);
145 EXPECT_FALSE(BrokenDebugInfo);
146}
147
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000148} // end namespace