MetadataTest.cpp 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857
  1. //===- unittests/IR/MetadataTest.cpp - Metadata unit 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/IR/Metadata.h"
  9. #include "llvm/ADT/STLExtras.h"
  10. #include "llvm/IR/Constants.h"
  11. #include "llvm/IR/DebugInfo.h"
  12. #include "llvm/IR/DebugInfoMetadata.h"
  13. #include "llvm/IR/Function.h"
  14. #include "llvm/IR/Instructions.h"
  15. #include "llvm/IR/LLVMContext.h"
  16. #include "llvm/IR/Module.h"
  17. #include "llvm/IR/ModuleSlotTracker.h"
  18. #include "llvm/IR/Type.h"
  19. #include "llvm/IR/Verifier.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. #include "gtest/gtest.h"
  22. using namespace llvm;
  23. namespace {
  24. TEST(ContextAndReplaceableUsesTest, FromContext) {
  25. LLVMContext Context;
  26. ContextAndReplaceableUses CRU(Context);
  27. EXPECT_EQ(&Context, &CRU.getContext());
  28. EXPECT_FALSE(CRU.hasReplaceableUses());
  29. EXPECT_FALSE(CRU.getReplaceableUses());
  30. }
  31. TEST(ContextAndReplaceableUsesTest, FromReplaceableUses) {
  32. LLVMContext Context;
  33. ContextAndReplaceableUses CRU(std::make_unique<ReplaceableMetadataImpl>(Context));
  34. EXPECT_EQ(&Context, &CRU.getContext());
  35. EXPECT_TRUE(CRU.hasReplaceableUses());
  36. EXPECT_TRUE(CRU.getReplaceableUses());
  37. }
  38. TEST(ContextAndReplaceableUsesTest, makeReplaceable) {
  39. LLVMContext Context;
  40. ContextAndReplaceableUses CRU(Context);
  41. CRU.makeReplaceable(std::make_unique<ReplaceableMetadataImpl>(Context));
  42. EXPECT_EQ(&Context, &CRU.getContext());
  43. EXPECT_TRUE(CRU.hasReplaceableUses());
  44. EXPECT_TRUE(CRU.getReplaceableUses());
  45. }
  46. TEST(ContextAndReplaceableUsesTest, takeReplaceableUses) {
  47. LLVMContext Context;
  48. auto ReplaceableUses = std::make_unique<ReplaceableMetadataImpl>(Context);
  49. auto *Ptr = ReplaceableUses.get();
  50. ContextAndReplaceableUses CRU(std::move(ReplaceableUses));
  51. ReplaceableUses = CRU.takeReplaceableUses();
  52. EXPECT_EQ(&Context, &CRU.getContext());
  53. EXPECT_FALSE(CRU.hasReplaceableUses());
  54. EXPECT_FALSE(CRU.getReplaceableUses());
  55. EXPECT_EQ(Ptr, ReplaceableUses.get());
  56. }
  57. class MetadataTest : public testing::Test {
  58. public:
  59. MetadataTest() : M("test", Context), Counter(0) {}
  60. protected:
  61. LLVMContext Context;
  62. Module M;
  63. int Counter;
  64. MDNode *getNode() { return MDNode::get(Context, None); }
  65. MDNode *getNode(Metadata *MD) { return MDNode::get(Context, MD); }
  66. MDNode *getNode(Metadata *MD1, Metadata *MD2) {
  67. Metadata *MDs[] = {MD1, MD2};
  68. return MDNode::get(Context, MDs);
  69. }
  70. MDTuple *getTuple() { return MDTuple::getDistinct(Context, None); }
  71. DISubroutineType *getSubroutineType() {
  72. return DISubroutineType::getDistinct(Context, DINode::FlagZero, 0,
  73. getNode(nullptr));
  74. }
  75. DISubprogram *getSubprogram() {
  76. return DISubprogram::getDistinct(
  77. Context, nullptr, "", "", nullptr, 0, nullptr, 0, nullptr, 0, 0,
  78. DINode::FlagZero, DISubprogram::SPFlagZero, nullptr);
  79. }
  80. DIFile *getFile() {
  81. return DIFile::getDistinct(Context, "file.c", "/path/to/dir");
  82. }
  83. DICompileUnit *getUnit() {
  84. return DICompileUnit::getDistinct(
  85. Context, 1, getFile(), "clang", false, "-g", 2, "",
  86. DICompileUnit::FullDebug, getTuple(), getTuple(), getTuple(),
  87. getTuple(), getTuple(), 0, true, false,
  88. DICompileUnit::DebugNameTableKind::Default, false);
  89. }
  90. DIType *getBasicType(StringRef Name) {
  91. return DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, Name);
  92. }
  93. DIType *getDerivedType() {
  94. return DIDerivedType::getDistinct(
  95. Context, dwarf::DW_TAG_pointer_type, "", nullptr, 0, nullptr,
  96. getBasicType("basictype"), 1, 2, 0, None, DINode::FlagZero);
  97. }
  98. Constant *getConstant() {
  99. return ConstantInt::get(Type::getInt32Ty(Context), Counter++);
  100. }
  101. ConstantAsMetadata *getConstantAsMetadata() {
  102. return ConstantAsMetadata::get(getConstant());
  103. }
  104. DIType *getCompositeType() {
  105. return DICompositeType::getDistinct(
  106. Context, dwarf::DW_TAG_structure_type, "", nullptr, 0, nullptr, nullptr,
  107. 32, 32, 0, DINode::FlagZero, nullptr, 0, nullptr, nullptr, "");
  108. }
  109. Function *getFunction(StringRef Name) {
  110. return Function::Create(
  111. FunctionType::get(Type::getVoidTy(Context), None, false),
  112. Function::ExternalLinkage, Name, M);
  113. }
  114. };
  115. typedef MetadataTest MDStringTest;
  116. // Test that construction of MDString with different value produces different
  117. // MDString objects, even with the same string pointer and nulls in the string.
  118. TEST_F(MDStringTest, CreateDifferent) {
  119. char x[3] = { 'f', 0, 'A' };
  120. MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
  121. x[2] = 'B';
  122. MDString *s2 = MDString::get(Context, StringRef(&x[0], 3));
  123. EXPECT_NE(s1, s2);
  124. }
  125. // Test that creation of MDStrings with the same string contents produces the
  126. // same MDString object, even with different pointers.
  127. TEST_F(MDStringTest, CreateSame) {
  128. char x[4] = { 'a', 'b', 'c', 'X' };
  129. char y[4] = { 'a', 'b', 'c', 'Y' };
  130. MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
  131. MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
  132. EXPECT_EQ(s1, s2);
  133. }
  134. // Test that MDString prints out the string we fed it.
  135. TEST_F(MDStringTest, PrintingSimple) {
  136. char str[14] = "testing 1 2 3";
  137. MDString *s = MDString::get(Context, StringRef(&str[0], 13));
  138. strncpy(str, "aaaaaaaaaaaaa", 14);
  139. std::string Str;
  140. raw_string_ostream oss(Str);
  141. s->print(oss);
  142. EXPECT_STREQ("!\"testing 1 2 3\"", oss.str().c_str());
  143. }
  144. // Test printing of MDString with non-printable characters.
  145. TEST_F(MDStringTest, PrintingComplex) {
  146. char str[5] = {0, '\n', '"', '\\', (char)-1};
  147. MDString *s = MDString::get(Context, StringRef(str+0, 5));
  148. std::string Str;
  149. raw_string_ostream oss(Str);
  150. s->print(oss);
  151. EXPECT_STREQ("!\"\\00\\0A\\22\\\\\\FF\"", oss.str().c_str());
  152. }
  153. typedef MetadataTest MDNodeTest;
  154. // Test the two constructors, and containing other Constants.
  155. TEST_F(MDNodeTest, Simple) {
  156. char x[3] = { 'a', 'b', 'c' };
  157. char y[3] = { '1', '2', '3' };
  158. MDString *s1 = MDString::get(Context, StringRef(&x[0], 3));
  159. MDString *s2 = MDString::get(Context, StringRef(&y[0], 3));
  160. ConstantAsMetadata *CI =
  161. ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
  162. std::vector<Metadata *> V;
  163. V.push_back(s1);
  164. V.push_back(CI);
  165. V.push_back(s2);
  166. MDNode *n1 = MDNode::get(Context, V);
  167. Metadata *const c1 = n1;
  168. MDNode *n2 = MDNode::get(Context, c1);
  169. Metadata *const c2 = n2;
  170. MDNode *n3 = MDNode::get(Context, V);
  171. MDNode *n4 = MDNode::getIfExists(Context, V);
  172. MDNode *n5 = MDNode::getIfExists(Context, c1);
  173. MDNode *n6 = MDNode::getIfExists(Context, c2);
  174. EXPECT_NE(n1, n2);
  175. EXPECT_EQ(n1, n3);
  176. EXPECT_EQ(n4, n1);
  177. EXPECT_EQ(n5, n2);
  178. EXPECT_EQ(n6, (Metadata *)nullptr);
  179. EXPECT_EQ(3u, n1->getNumOperands());
  180. EXPECT_EQ(s1, n1->getOperand(0));
  181. EXPECT_EQ(CI, n1->getOperand(1));
  182. EXPECT_EQ(s2, n1->getOperand(2));
  183. EXPECT_EQ(1u, n2->getNumOperands());
  184. EXPECT_EQ(n1, n2->getOperand(0));
  185. }
  186. TEST_F(MDNodeTest, Delete) {
  187. Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 1);
  188. Instruction *I = new BitCastInst(C, Type::getInt32Ty(Context));
  189. Metadata *const V = LocalAsMetadata::get(I);
  190. MDNode *n = MDNode::get(Context, V);
  191. TrackingMDRef wvh(n);
  192. EXPECT_EQ(n, wvh);
  193. I->deleteValue();
  194. }
  195. TEST_F(MDNodeTest, SelfReference) {
  196. // !0 = !{!0}
  197. // !1 = !{!0}
  198. {
  199. auto Temp = MDNode::getTemporary(Context, None);
  200. Metadata *Args[] = {Temp.get()};
  201. MDNode *Self = MDNode::get(Context, Args);
  202. Self->replaceOperandWith(0, Self);
  203. ASSERT_EQ(Self, Self->getOperand(0));
  204. // Self-references should be distinct, so MDNode::get() should grab a
  205. // uniqued node that references Self, not Self.
  206. Args[0] = Self;
  207. MDNode *Ref1 = MDNode::get(Context, Args);
  208. MDNode *Ref2 = MDNode::get(Context, Args);
  209. EXPECT_NE(Self, Ref1);
  210. EXPECT_EQ(Ref1, Ref2);
  211. }
  212. // !0 = !{!0, !{}}
  213. // !1 = !{!0, !{}}
  214. {
  215. auto Temp = MDNode::getTemporary(Context, None);
  216. Metadata *Args[] = {Temp.get(), MDNode::get(Context, None)};
  217. MDNode *Self = MDNode::get(Context, Args);
  218. Self->replaceOperandWith(0, Self);
  219. ASSERT_EQ(Self, Self->getOperand(0));
  220. // Self-references should be distinct, so MDNode::get() should grab a
  221. // uniqued node that references Self, not Self itself.
  222. Args[0] = Self;
  223. MDNode *Ref1 = MDNode::get(Context, Args);
  224. MDNode *Ref2 = MDNode::get(Context, Args);
  225. EXPECT_NE(Self, Ref1);
  226. EXPECT_EQ(Ref1, Ref2);
  227. }
  228. }
  229. TEST_F(MDNodeTest, Print) {
  230. Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
  231. MDString *S = MDString::get(Context, "foo");
  232. MDNode *N0 = getNode();
  233. MDNode *N1 = getNode(N0);
  234. MDNode *N2 = getNode(N0, N1);
  235. Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
  236. MDNode *N = MDNode::get(Context, Args);
  237. std::string Expected;
  238. {
  239. raw_string_ostream OS(Expected);
  240. OS << "<" << (void *)N << "> = !{";
  241. C->printAsOperand(OS);
  242. OS << ", ";
  243. S->printAsOperand(OS);
  244. OS << ", null";
  245. MDNode *Nodes[] = {N0, N1, N2};
  246. for (auto *Node : Nodes)
  247. OS << ", <" << (void *)Node << ">";
  248. OS << "}";
  249. }
  250. std::string Actual;
  251. {
  252. raw_string_ostream OS(Actual);
  253. N->print(OS);
  254. }
  255. EXPECT_EQ(Expected, Actual);
  256. }
  257. #define EXPECT_PRINTER_EQ(EXPECTED, PRINT) \
  258. do { \
  259. std::string Actual_; \
  260. raw_string_ostream OS(Actual_); \
  261. PRINT; \
  262. OS.flush(); \
  263. std::string Expected_(EXPECTED); \
  264. EXPECT_EQ(Expected_, Actual_); \
  265. } while (false)
  266. TEST_F(MDNodeTest, PrintTemporary) {
  267. MDNode *Arg = getNode();
  268. TempMDNode Temp = MDNode::getTemporary(Context, Arg);
  269. MDNode *N = getNode(Temp.get());
  270. Module M("test", Context);
  271. NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
  272. NMD->addOperand(N);
  273. EXPECT_PRINTER_EQ("!0 = !{!1}", N->print(OS, &M));
  274. EXPECT_PRINTER_EQ("!1 = <temporary!> !{!2}", Temp->print(OS, &M));
  275. EXPECT_PRINTER_EQ("!2 = !{}", Arg->print(OS, &M));
  276. // Cleanup.
  277. Temp->replaceAllUsesWith(Arg);
  278. }
  279. TEST_F(MDNodeTest, PrintFromModule) {
  280. Constant *C = ConstantInt::get(Type::getInt32Ty(Context), 7);
  281. MDString *S = MDString::get(Context, "foo");
  282. MDNode *N0 = getNode();
  283. MDNode *N1 = getNode(N0);
  284. MDNode *N2 = getNode(N0, N1);
  285. Metadata *Args[] = {ConstantAsMetadata::get(C), S, nullptr, N0, N1, N2};
  286. MDNode *N = MDNode::get(Context, Args);
  287. Module M("test", Context);
  288. NamedMDNode *NMD = M.getOrInsertNamedMetadata("named");
  289. NMD->addOperand(N);
  290. std::string Expected;
  291. {
  292. raw_string_ostream OS(Expected);
  293. OS << "!0 = !{";
  294. C->printAsOperand(OS);
  295. OS << ", ";
  296. S->printAsOperand(OS);
  297. OS << ", null, !1, !2, !3}";
  298. }
  299. EXPECT_PRINTER_EQ(Expected, N->print(OS, &M));
  300. }
  301. TEST_F(MDNodeTest, PrintFromFunction) {
  302. Module M("test", Context);
  303. auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
  304. auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
  305. auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
  306. auto *BB0 = BasicBlock::Create(Context, "entry", F0);
  307. auto *BB1 = BasicBlock::Create(Context, "entry", F1);
  308. auto *R0 = ReturnInst::Create(Context, BB0);
  309. auto *R1 = ReturnInst::Create(Context, BB1);
  310. auto *N0 = MDNode::getDistinct(Context, None);
  311. auto *N1 = MDNode::getDistinct(Context, None);
  312. R0->setMetadata("md", N0);
  313. R1->setMetadata("md", N1);
  314. EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, &M));
  315. EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, &M));
  316. ModuleSlotTracker MST(&M);
  317. EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, MST));
  318. EXPECT_PRINTER_EQ("!1 = distinct !{}", N1->print(OS, MST));
  319. }
  320. TEST_F(MDNodeTest, PrintFromMetadataAsValue) {
  321. Module M("test", Context);
  322. auto *Intrinsic =
  323. Function::Create(FunctionType::get(Type::getVoidTy(Context),
  324. Type::getMetadataTy(Context), false),
  325. GlobalValue::ExternalLinkage, "llvm.intrinsic", &M);
  326. auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
  327. auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
  328. auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
  329. auto *BB0 = BasicBlock::Create(Context, "entry", F0);
  330. auto *BB1 = BasicBlock::Create(Context, "entry", F1);
  331. auto *N0 = MDNode::getDistinct(Context, None);
  332. auto *N1 = MDNode::getDistinct(Context, None);
  333. auto *MAV0 = MetadataAsValue::get(Context, N0);
  334. auto *MAV1 = MetadataAsValue::get(Context, N1);
  335. CallInst::Create(Intrinsic, MAV0, "", BB0);
  336. CallInst::Create(Intrinsic, MAV1, "", BB1);
  337. EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS));
  338. EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS));
  339. EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false));
  340. EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false));
  341. EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true));
  342. EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true));
  343. ModuleSlotTracker MST(&M);
  344. EXPECT_PRINTER_EQ("!0 = distinct !{}", MAV0->print(OS, MST));
  345. EXPECT_PRINTER_EQ("!1 = distinct !{}", MAV1->print(OS, MST));
  346. EXPECT_PRINTER_EQ("!0", MAV0->printAsOperand(OS, false, MST));
  347. EXPECT_PRINTER_EQ("!1", MAV1->printAsOperand(OS, false, MST));
  348. EXPECT_PRINTER_EQ("metadata !0", MAV0->printAsOperand(OS, true, MST));
  349. EXPECT_PRINTER_EQ("metadata !1", MAV1->printAsOperand(OS, true, MST));
  350. }
  351. TEST_F(MDNodeTest, PrintWithDroppedCallOperand) {
  352. Module M("test", Context);
  353. auto *FTy = FunctionType::get(Type::getVoidTy(Context), false);
  354. auto *F0 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F0", &M);
  355. auto *F1 = Function::Create(FTy, GlobalValue::ExternalLinkage, "F1", &M);
  356. auto *BB0 = BasicBlock::Create(Context, "entry", F0);
  357. CallInst *CI0 = CallInst::Create(F1, "", BB0);
  358. CI0->dropAllReferences();
  359. auto *R0 = ReturnInst::Create(Context, BB0);
  360. auto *N0 = MDNode::getDistinct(Context, None);
  361. R0->setMetadata("md", N0);
  362. // Printing the metadata node would previously result in a failed assertion
  363. // due to the call instruction's dropped function operand.
  364. ModuleSlotTracker MST(&M);
  365. EXPECT_PRINTER_EQ("!0 = distinct !{}", N0->print(OS, MST));
  366. }
  367. #undef EXPECT_PRINTER_EQ
  368. TEST_F(MDNodeTest, NullOperand) {
  369. // metadata !{}
  370. MDNode *Empty = MDNode::get(Context, None);
  371. // metadata !{metadata !{}}
  372. Metadata *Ops[] = {Empty};
  373. MDNode *N = MDNode::get(Context, Ops);
  374. ASSERT_EQ(Empty, N->getOperand(0));
  375. // metadata !{metadata !{}} => metadata !{null}
  376. N->replaceOperandWith(0, nullptr);
  377. ASSERT_EQ(nullptr, N->getOperand(0));
  378. // metadata !{null}
  379. Ops[0] = nullptr;
  380. MDNode *NullOp = MDNode::get(Context, Ops);
  381. ASSERT_EQ(nullptr, NullOp->getOperand(0));
  382. EXPECT_EQ(N, NullOp);
  383. }
  384. TEST_F(MDNodeTest, DistinctOnUniquingCollision) {
  385. // !{}
  386. MDNode *Empty = MDNode::get(Context, None);
  387. ASSERT_TRUE(Empty->isResolved());
  388. EXPECT_FALSE(Empty->isDistinct());
  389. // !{!{}}
  390. Metadata *Wrapped1Ops[] = {Empty};
  391. MDNode *Wrapped1 = MDNode::get(Context, Wrapped1Ops);
  392. ASSERT_EQ(Empty, Wrapped1->getOperand(0));
  393. ASSERT_TRUE(Wrapped1->isResolved());
  394. EXPECT_FALSE(Wrapped1->isDistinct());
  395. // !{!{!{}}}
  396. Metadata *Wrapped2Ops[] = {Wrapped1};
  397. MDNode *Wrapped2 = MDNode::get(Context, Wrapped2Ops);
  398. ASSERT_EQ(Wrapped1, Wrapped2->getOperand(0));
  399. ASSERT_TRUE(Wrapped2->isResolved());
  400. EXPECT_FALSE(Wrapped2->isDistinct());
  401. // !{!{!{}}} => !{!{}}
  402. Wrapped2->replaceOperandWith(0, Empty);
  403. ASSERT_EQ(Empty, Wrapped2->getOperand(0));
  404. EXPECT_TRUE(Wrapped2->isDistinct());
  405. EXPECT_FALSE(Wrapped1->isDistinct());
  406. }
  407. TEST_F(MDNodeTest, UniquedOnDeletedOperand) {
  408. // temp !{}
  409. TempMDTuple T = MDTuple::getTemporary(Context, None);
  410. // !{temp !{}}
  411. Metadata *Ops[] = {T.get()};
  412. MDTuple *N = MDTuple::get(Context, Ops);
  413. // !{temp !{}} => !{null}
  414. T.reset();
  415. ASSERT_TRUE(N->isUniqued());
  416. Metadata *NullOps[] = {nullptr};
  417. ASSERT_EQ(N, MDTuple::get(Context, NullOps));
  418. }
  419. TEST_F(MDNodeTest, DistinctOnDeletedValueOperand) {
  420. // i1* @GV
  421. Type *Ty = Type::getInt1PtrTy(Context);
  422. std::unique_ptr<GlobalVariable> GV(
  423. new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
  424. ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
  425. // !{i1* @GV}
  426. Metadata *Ops[] = {Op};
  427. MDTuple *N = MDTuple::get(Context, Ops);
  428. // !{i1* @GV} => !{null}
  429. GV.reset();
  430. ASSERT_TRUE(N->isDistinct());
  431. ASSERT_EQ(nullptr, N->getOperand(0));
  432. Metadata *NullOps[] = {nullptr};
  433. ASSERT_NE(N, MDTuple::get(Context, NullOps));
  434. }
  435. TEST_F(MDNodeTest, getDistinct) {
  436. // !{}
  437. MDNode *Empty = MDNode::get(Context, None);
  438. ASSERT_TRUE(Empty->isResolved());
  439. ASSERT_FALSE(Empty->isDistinct());
  440. ASSERT_EQ(Empty, MDNode::get(Context, None));
  441. // distinct !{}
  442. MDNode *Distinct1 = MDNode::getDistinct(Context, None);
  443. MDNode *Distinct2 = MDNode::getDistinct(Context, None);
  444. EXPECT_TRUE(Distinct1->isResolved());
  445. EXPECT_TRUE(Distinct2->isDistinct());
  446. EXPECT_NE(Empty, Distinct1);
  447. EXPECT_NE(Empty, Distinct2);
  448. EXPECT_NE(Distinct1, Distinct2);
  449. // !{}
  450. ASSERT_EQ(Empty, MDNode::get(Context, None));
  451. }
  452. TEST_F(MDNodeTest, isUniqued) {
  453. MDNode *U = MDTuple::get(Context, None);
  454. MDNode *D = MDTuple::getDistinct(Context, None);
  455. auto T = MDTuple::getTemporary(Context, None);
  456. EXPECT_TRUE(U->isUniqued());
  457. EXPECT_FALSE(D->isUniqued());
  458. EXPECT_FALSE(T->isUniqued());
  459. }
  460. TEST_F(MDNodeTest, isDistinct) {
  461. MDNode *U = MDTuple::get(Context, None);
  462. MDNode *D = MDTuple::getDistinct(Context, None);
  463. auto T = MDTuple::getTemporary(Context, None);
  464. EXPECT_FALSE(U->isDistinct());
  465. EXPECT_TRUE(D->isDistinct());
  466. EXPECT_FALSE(T->isDistinct());
  467. }
  468. TEST_F(MDNodeTest, isTemporary) {
  469. MDNode *U = MDTuple::get(Context, None);
  470. MDNode *D = MDTuple::getDistinct(Context, None);
  471. auto T = MDTuple::getTemporary(Context, None);
  472. EXPECT_FALSE(U->isTemporary());
  473. EXPECT_FALSE(D->isTemporary());
  474. EXPECT_TRUE(T->isTemporary());
  475. }
  476. TEST_F(MDNodeTest, getDistinctWithUnresolvedOperands) {
  477. // temporary !{}
  478. auto Temp = MDTuple::getTemporary(Context, None);
  479. ASSERT_FALSE(Temp->isResolved());
  480. // distinct !{temporary !{}}
  481. Metadata *Ops[] = {Temp.get()};
  482. MDNode *Distinct = MDNode::getDistinct(Context, Ops);
  483. EXPECT_TRUE(Distinct->isResolved());
  484. EXPECT_EQ(Temp.get(), Distinct->getOperand(0));
  485. // temporary !{} => !{}
  486. MDNode *Empty = MDNode::get(Context, None);
  487. Temp->replaceAllUsesWith(Empty);
  488. EXPECT_EQ(Empty, Distinct->getOperand(0));
  489. }
  490. TEST_F(MDNodeTest, handleChangedOperandRecursion) {
  491. // !0 = !{}
  492. MDNode *N0 = MDNode::get(Context, None);
  493. // !1 = !{!3, null}
  494. auto Temp3 = MDTuple::getTemporary(Context, None);
  495. Metadata *Ops1[] = {Temp3.get(), nullptr};
  496. MDNode *N1 = MDNode::get(Context, Ops1);
  497. // !2 = !{!3, !0}
  498. Metadata *Ops2[] = {Temp3.get(), N0};
  499. MDNode *N2 = MDNode::get(Context, Ops2);
  500. // !3 = !{!2}
  501. Metadata *Ops3[] = {N2};
  502. MDNode *N3 = MDNode::get(Context, Ops3);
  503. Temp3->replaceAllUsesWith(N3);
  504. // !4 = !{!1}
  505. Metadata *Ops4[] = {N1};
  506. MDNode *N4 = MDNode::get(Context, Ops4);
  507. // Confirm that the cycle prevented RAUW from getting dropped.
  508. EXPECT_TRUE(N0->isResolved());
  509. EXPECT_FALSE(N1->isResolved());
  510. EXPECT_FALSE(N2->isResolved());
  511. EXPECT_FALSE(N3->isResolved());
  512. EXPECT_FALSE(N4->isResolved());
  513. // Create a couple of distinct nodes to observe what's going on.
  514. //
  515. // !5 = distinct !{!2}
  516. // !6 = distinct !{!3}
  517. Metadata *Ops5[] = {N2};
  518. MDNode *N5 = MDNode::getDistinct(Context, Ops5);
  519. Metadata *Ops6[] = {N3};
  520. MDNode *N6 = MDNode::getDistinct(Context, Ops6);
  521. // Mutate !2 to look like !1, causing a uniquing collision (and an RAUW).
  522. // This will ripple up, with !3 colliding with !4, and RAUWing. Since !2
  523. // references !3, this can cause a re-entry of handleChangedOperand() when !3
  524. // is not ready for it.
  525. //
  526. // !2->replaceOperandWith(1, nullptr)
  527. // !2: !{!3, !0} => !{!3, null}
  528. // !2->replaceAllUsesWith(!1)
  529. // !3: !{!2] => !{!1}
  530. // !3->replaceAllUsesWith(!4)
  531. N2->replaceOperandWith(1, nullptr);
  532. // If all has gone well, N2 and N3 will have been RAUW'ed and deleted from
  533. // under us. Just check that the other nodes are sane.
  534. //
  535. // !1 = !{!4, null}
  536. // !4 = !{!1}
  537. // !5 = distinct !{!1}
  538. // !6 = distinct !{!4}
  539. EXPECT_EQ(N4, N1->getOperand(0));
  540. EXPECT_EQ(N1, N4->getOperand(0));
  541. EXPECT_EQ(N1, N5->getOperand(0));
  542. EXPECT_EQ(N4, N6->getOperand(0));
  543. }
  544. TEST_F(MDNodeTest, replaceResolvedOperand) {
  545. // Check code for replacing one resolved operand with another. If doing this
  546. // directly (via replaceOperandWith()) becomes illegal, change the operand to
  547. // a global value that gets RAUW'ed.
  548. //
  549. // Use a temporary node to keep N from being resolved.
  550. auto Temp = MDTuple::getTemporary(Context, None);
  551. Metadata *Ops[] = {nullptr, Temp.get()};
  552. MDNode *Empty = MDTuple::get(Context, ArrayRef<Metadata *>());
  553. MDNode *N = MDTuple::get(Context, Ops);
  554. EXPECT_EQ(nullptr, N->getOperand(0));
  555. ASSERT_FALSE(N->isResolved());
  556. // Check code for replacing resolved nodes.
  557. N->replaceOperandWith(0, Empty);
  558. EXPECT_EQ(Empty, N->getOperand(0));
  559. // Check code for adding another unresolved operand.
  560. N->replaceOperandWith(0, Temp.get());
  561. EXPECT_EQ(Temp.get(), N->getOperand(0));
  562. // Remove the references to Temp; required for teardown.
  563. Temp->replaceAllUsesWith(nullptr);
  564. }
  565. TEST_F(MDNodeTest, replaceWithUniqued) {
  566. auto *Empty = MDTuple::get(Context, None);
  567. MDTuple *FirstUniqued;
  568. {
  569. Metadata *Ops[] = {Empty};
  570. auto Temp = MDTuple::getTemporary(Context, Ops);
  571. EXPECT_TRUE(Temp->isTemporary());
  572. // Don't expect a collision.
  573. auto *Current = Temp.get();
  574. FirstUniqued = MDNode::replaceWithUniqued(std::move(Temp));
  575. EXPECT_TRUE(FirstUniqued->isUniqued());
  576. EXPECT_TRUE(FirstUniqued->isResolved());
  577. EXPECT_EQ(Current, FirstUniqued);
  578. }
  579. {
  580. Metadata *Ops[] = {Empty};
  581. auto Temp = MDTuple::getTemporary(Context, Ops);
  582. EXPECT_TRUE(Temp->isTemporary());
  583. // Should collide with Uniqued above this time.
  584. auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
  585. EXPECT_TRUE(Uniqued->isUniqued());
  586. EXPECT_TRUE(Uniqued->isResolved());
  587. EXPECT_EQ(FirstUniqued, Uniqued);
  588. }
  589. {
  590. auto Unresolved = MDTuple::getTemporary(Context, None);
  591. Metadata *Ops[] = {Unresolved.get()};
  592. auto Temp = MDTuple::getTemporary(Context, Ops);
  593. EXPECT_TRUE(Temp->isTemporary());
  594. // Shouldn't be resolved.
  595. auto *Uniqued = MDNode::replaceWithUniqued(std::move(Temp));
  596. EXPECT_TRUE(Uniqued->isUniqued());
  597. EXPECT_FALSE(Uniqued->isResolved());
  598. // Should be a different node.
  599. EXPECT_NE(FirstUniqued, Uniqued);
  600. // Should resolve when we update its node (note: be careful to avoid a
  601. // collision with any other nodes above).
  602. Uniqued->replaceOperandWith(0, nullptr);
  603. EXPECT_TRUE(Uniqued->isResolved());
  604. }
  605. }
  606. TEST_F(MDNodeTest, replaceWithUniquedResolvingOperand) {
  607. // temp !{}
  608. MDTuple *Op = MDTuple::getTemporary(Context, None).release();
  609. EXPECT_FALSE(Op->isResolved());
  610. // temp !{temp !{}}
  611. Metadata *Ops[] = {Op};
  612. MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
  613. EXPECT_FALSE(N->isResolved());
  614. // temp !{temp !{}} => !{temp !{}}
  615. ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
  616. EXPECT_FALSE(N->isResolved());
  617. // !{temp !{}} => !{!{}}
  618. ASSERT_EQ(Op, MDNode::replaceWithUniqued(TempMDTuple(Op)));
  619. EXPECT_TRUE(Op->isResolved());
  620. EXPECT_TRUE(N->isResolved());
  621. }
  622. TEST_F(MDNodeTest, replaceWithUniquedDeletedOperand) {
  623. // i1* @GV
  624. Type *Ty = Type::getInt1PtrTy(Context);
  625. std::unique_ptr<GlobalVariable> GV(
  626. new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
  627. ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
  628. // temp !{i1* @GV}
  629. Metadata *Ops[] = {Op};
  630. MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
  631. // temp !{i1* @GV} => !{i1* @GV}
  632. ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
  633. ASSERT_TRUE(N->isUniqued());
  634. // !{i1* @GV} => !{null}
  635. GV.reset();
  636. ASSERT_TRUE(N->isDistinct());
  637. ASSERT_EQ(nullptr, N->getOperand(0));
  638. Metadata *NullOps[] = {nullptr};
  639. ASSERT_NE(N, MDTuple::get(Context, NullOps));
  640. }
  641. TEST_F(MDNodeTest, replaceWithUniquedChangedOperand) {
  642. // i1* @GV
  643. Type *Ty = Type::getInt1PtrTy(Context);
  644. std::unique_ptr<GlobalVariable> GV(
  645. new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
  646. ConstantAsMetadata *Op = ConstantAsMetadata::get(GV.get());
  647. // temp !{i1* @GV}
  648. Metadata *Ops[] = {Op};
  649. MDTuple *N = MDTuple::getTemporary(Context, Ops).release();
  650. // temp !{i1* @GV} => !{i1* @GV}
  651. ASSERT_EQ(N, MDNode::replaceWithUniqued(TempMDTuple(N)));
  652. ASSERT_TRUE(N->isUniqued());
  653. // !{i1* @GV} => !{i1* @GV2}
  654. std::unique_ptr<GlobalVariable> GV2(
  655. new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
  656. GV->replaceAllUsesWith(GV2.get());
  657. ASSERT_TRUE(N->isUniqued());
  658. Metadata *NullOps[] = {ConstantAsMetadata::get(GV2.get())};
  659. ASSERT_EQ(N, MDTuple::get(Context, NullOps));
  660. }
  661. TEST_F(MDNodeTest, replaceWithDistinct) {
  662. {
  663. auto *Empty = MDTuple::get(Context, None);
  664. Metadata *Ops[] = {Empty};
  665. auto Temp = MDTuple::getTemporary(Context, Ops);
  666. EXPECT_TRUE(Temp->isTemporary());
  667. // Don't expect a collision.
  668. auto *Current = Temp.get();
  669. auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
  670. EXPECT_TRUE(Distinct->isDistinct());
  671. EXPECT_TRUE(Distinct->isResolved());
  672. EXPECT_EQ(Current, Distinct);
  673. }
  674. {
  675. auto Unresolved = MDTuple::getTemporary(Context, None);
  676. Metadata *Ops[] = {Unresolved.get()};
  677. auto Temp = MDTuple::getTemporary(Context, Ops);
  678. EXPECT_TRUE(Temp->isTemporary());
  679. // Don't expect a collision.
  680. auto *Current = Temp.get();
  681. auto *Distinct = MDNode::replaceWithDistinct(std::move(Temp));
  682. EXPECT_TRUE(Distinct->isDistinct());
  683. EXPECT_TRUE(Distinct->isResolved());
  684. EXPECT_EQ(Current, Distinct);
  685. // Cleanup; required for teardown.
  686. Unresolved->replaceAllUsesWith(nullptr);
  687. }
  688. }
  689. TEST_F(MDNodeTest, replaceWithPermanent) {
  690. Metadata *Ops[] = {nullptr};
  691. auto Temp = MDTuple::getTemporary(Context, Ops);
  692. auto *T = Temp.get();
  693. // U is a normal, uniqued node that references T.
  694. auto *U = MDTuple::get(Context, T);
  695. EXPECT_TRUE(U->isUniqued());
  696. // Make Temp self-referencing.
  697. Temp->replaceOperandWith(0, T);
  698. // Try to uniquify Temp. This should, despite the name in the API, give a
  699. // 'distinct' node, since self-references aren't allowed to be uniqued.
  700. //
  701. // Since it's distinct, N should have the same address as when it was a
  702. // temporary (i.e., be equal to T not U).
  703. auto *N = MDNode::replaceWithPermanent(std::move(Temp));
  704. EXPECT_EQ(N, T);
  705. EXPECT_TRUE(N->isDistinct());
  706. // U should be the canonical unique node with N as the argument.
  707. EXPECT_EQ(U, MDTuple::get(Context, N));
  708. EXPECT_TRUE(U->isUniqued());
  709. // This temporary should collide with U when replaced, but it should still be
  710. // uniqued.
  711. EXPECT_EQ(U, MDNode::replaceWithPermanent(MDTuple::getTemporary(Context, N)));
  712. EXPECT_TRUE(U->isUniqued());
  713. // This temporary should become a new uniqued node.
  714. auto Temp2 = MDTuple::getTemporary(Context, U);
  715. auto *V = Temp2.get();
  716. EXPECT_EQ(V, MDNode::replaceWithPermanent(std::move(Temp2)));
  717. EXPECT_TRUE(V->isUniqued());
  718. EXPECT_EQ(U, V->getOperand(0));
  719. }
  720. TEST_F(MDNodeTest, deleteTemporaryWithTrackingRef) {
  721. TrackingMDRef Ref;
  722. EXPECT_EQ(nullptr, Ref.get());
  723. {
  724. auto Temp = MDTuple::getTemporary(Context, None);
  725. Ref.reset(Temp.get());
  726. EXPECT_EQ(Temp.get(), Ref.get());
  727. }
  728. EXPECT_EQ(nullptr, Ref.get());
  729. }
  730. typedef MetadataTest DILocationTest;
  731. TEST_F(DILocationTest, Overflow) {
  732. DISubprogram *N = getSubprogram();
  733. {
  734. DILocation *L = DILocation::get(Context, 2, 7, N);
  735. EXPECT_EQ(2u, L->getLine());
  736. EXPECT_EQ(7u, L->getColumn());
  737. }
  738. unsigned U16 = 1u << 16;
  739. {
  740. DILocation *L = DILocation::get(Context, UINT32_MAX, U16 - 1, N);
  741. EXPECT_EQ(UINT32_MAX, L->getLine());
  742. EXPECT_EQ(U16 - 1, L->getColumn());
  743. }
  744. {
  745. DILocation *L = DILocation::get(Context, UINT32_MAX, U16, N);
  746. EXPECT_EQ(UINT32_MAX, L->getLine());
  747. EXPECT_EQ(0u, L->getColumn());
  748. }
  749. {
  750. DILocation *L = DILocation::get(Context, UINT32_MAX, U16 + 1, N);
  751. EXPECT_EQ(UINT32_MAX, L->getLine());
  752. EXPECT_EQ(0u, L->getColumn());
  753. }
  754. }
  755. TEST_F(DILocationTest, Merge) {
  756. DISubprogram *N = getSubprogram();
  757. DIScope *S = DILexicalBlock::get(Context, N, getFile(), 3, 4);
  758. {
  759. // Identical.
  760. auto *A = DILocation::get(Context, 2, 7, N);
  761. auto *B = DILocation::get(Context, 2, 7, N);
  762. auto *M = DILocation::getMergedLocation(A, B);
  763. EXPECT_EQ(2u, M->getLine());
  764. EXPECT_EQ(7u, M->getColumn());
  765. EXPECT_EQ(N, M->getScope());
  766. }
  767. {
  768. // Identical, different scopes.
  769. auto *A = DILocation::get(Context, 2, 7, N);
  770. auto *B = DILocation::get(Context, 2, 7, S);
  771. auto *M = DILocation::getMergedLocation(A, B);
  772. EXPECT_EQ(0u, M->getLine()); // FIXME: Should this be 2?
  773. EXPECT_EQ(0u, M->getColumn()); // FIXME: Should this be 7?
  774. EXPECT_EQ(N, M->getScope());
  775. }
  776. {
  777. // Different lines, same scopes.
  778. auto *A = DILocation::get(Context, 1, 6, N);
  779. auto *B = DILocation::get(Context, 2, 7, N);
  780. auto *M = DILocation::getMergedLocation(A, B);
  781. EXPECT_EQ(0u, M->getLine());
  782. EXPECT_EQ(0u, M->getColumn());
  783. EXPECT_EQ(N, M->getScope());
  784. }
  785. {
  786. // Twisty locations, all different, same function.
  787. auto *A = DILocation::get(Context, 1, 6, N);
  788. auto *B = DILocation::get(Context, 2, 7, S);
  789. auto *M = DILocation::getMergedLocation(A, B);
  790. EXPECT_EQ(0u, M->getLine());
  791. EXPECT_EQ(0u, M->getColumn());
  792. EXPECT_EQ(N, M->getScope());
  793. }
  794. {
  795. // Different function, same inlined-at.
  796. auto *F = getFile();
  797. auto *SP1 = DISubprogram::getDistinct(Context, F, "a", "a", F, 0, nullptr,
  798. 0, nullptr, 0, 0, DINode::FlagZero,
  799. DISubprogram::SPFlagZero, nullptr);
  800. auto *SP2 = DISubprogram::getDistinct(Context, F, "b", "b", F, 0, nullptr,
  801. 0, nullptr, 0, 0, DINode::FlagZero,
  802. DISubprogram::SPFlagZero, nullptr);
  803. auto *I = DILocation::get(Context, 2, 7, N);
  804. auto *A = DILocation::get(Context, 1, 6, SP1, I);
  805. auto *B = DILocation::get(Context, 2, 7, SP2, I);
  806. auto *M = DILocation::getMergedLocation(A, B);
  807. EXPECT_EQ(0u, M->getLine());
  808. EXPECT_EQ(0u, M->getColumn());
  809. EXPECT_TRUE(isa<DILocalScope>(M->getScope()));
  810. EXPECT_EQ(I, M->getInlinedAt());
  811. }
  812. {
  813. // Completely different.
  814. auto *I = DILocation::get(Context, 2, 7, N);
  815. auto *A = DILocation::get(Context, 1, 6, S, I);
  816. auto *B = DILocation::get(Context, 2, 7, getSubprogram());
  817. auto *M = DILocation::getMergedLocation(A, B);
  818. EXPECT_EQ(0u, M->getLine());
  819. EXPECT_EQ(0u, M->getColumn());
  820. EXPECT_TRUE(isa<DILocalScope>(M->getScope()));
  821. EXPECT_EQ(S, M->getScope());
  822. EXPECT_EQ(nullptr, M->getInlinedAt());
  823. }
  824. }
  825. TEST_F(DILocationTest, getDistinct) {
  826. MDNode *N = getSubprogram();
  827. DILocation *L0 = DILocation::getDistinct(Context, 2, 7, N);
  828. EXPECT_TRUE(L0->isDistinct());
  829. DILocation *L1 = DILocation::get(Context, 2, 7, N);
  830. EXPECT_FALSE(L1->isDistinct());
  831. EXPECT_EQ(L1, DILocation::get(Context, 2, 7, N));
  832. }
  833. TEST_F(DILocationTest, getTemporary) {
  834. MDNode *N = MDNode::get(Context, None);
  835. auto L = DILocation::getTemporary(Context, 2, 7, N);
  836. EXPECT_TRUE(L->isTemporary());
  837. EXPECT_FALSE(L->isResolved());
  838. }
  839. TEST_F(DILocationTest, cloneTemporary) {
  840. MDNode *N = MDNode::get(Context, None);
  841. auto L = DILocation::getTemporary(Context, 2, 7, N);
  842. EXPECT_TRUE(L->isTemporary());
  843. auto L2 = L->clone();
  844. EXPECT_TRUE(L2->isTemporary());
  845. }
  846. TEST_F(DILocationTest, discriminatorEncoding) {
  847. EXPECT_EQ(0U, DILocation::encodeDiscriminator(0, 0, 0).getValue());
  848. // Encode base discriminator as a component: lsb is 0, then the value.
  849. // The other components are all absent, so we leave all the other bits 0.
  850. EXPECT_EQ(2U, DILocation::encodeDiscriminator(1, 0, 0).getValue());
  851. // Base discriminator component is empty, so lsb is 1. Next component is not
  852. // empty, so its lsb is 0, then its value (1). Next component is empty.
  853. // So the bit pattern is 101.
  854. EXPECT_EQ(5U, DILocation::encodeDiscriminator(0, 1, 0).getValue());
  855. // First 2 components are empty, so the bit pattern is 11. Then the
  856. // next component - ending up with 1011.
  857. EXPECT_EQ(0xbU, DILocation::encodeDiscriminator(0, 0, 1).getValue());
  858. // The bit pattern for the first 2 components is 11. The next bit is 0,
  859. // because the last component is not empty. We have 29 bits usable for
  860. // encoding, but we cap it at 12 bits uniformously for all components. We
  861. // encode the last component over 14 bits.
  862. EXPECT_EQ(0xfffbU, DILocation::encodeDiscriminator(0, 0, 0xfff).getValue());
  863. EXPECT_EQ(0x102U, DILocation::encodeDiscriminator(1, 1, 0).getValue());
  864. EXPECT_EQ(0x13eU, DILocation::encodeDiscriminator(0x1f, 1, 0).getValue());
  865. EXPECT_EQ(0x87feU, DILocation::encodeDiscriminator(0x1ff, 1, 0).getValue());
  866. EXPECT_EQ(0x1f3eU, DILocation::encodeDiscriminator(0x1f, 0x1f, 0).getValue());
  867. EXPECT_EQ(0x3ff3eU,
  868. DILocation::encodeDiscriminator(0x1f, 0x1ff, 0).getValue());
  869. EXPECT_EQ(0x1ff87feU,
  870. DILocation::encodeDiscriminator(0x1ff, 0x1ff, 0).getValue());
  871. EXPECT_EQ(0xfff9f3eU,
  872. DILocation::encodeDiscriminator(0x1f, 0x1f, 0xfff).getValue());
  873. EXPECT_EQ(0xffc3ff3eU,
  874. DILocation::encodeDiscriminator(0x1f, 0x1ff, 0x1ff).getValue());
  875. EXPECT_EQ(0xffcf87feU,
  876. DILocation::encodeDiscriminator(0x1ff, 0x1f, 0x1ff).getValue());
  877. EXPECT_EQ(0xe1ff87feU,
  878. DILocation::encodeDiscriminator(0x1ff, 0x1ff, 7).getValue());
  879. }
  880. TEST_F(DILocationTest, discriminatorEncodingNegativeTests) {
  881. EXPECT_EQ(None, DILocation::encodeDiscriminator(0, 0, 0x1000));
  882. EXPECT_EQ(None, DILocation::encodeDiscriminator(0x1000, 0, 0));
  883. EXPECT_EQ(None, DILocation::encodeDiscriminator(0, 0x1000, 0));
  884. EXPECT_EQ(None, DILocation::encodeDiscriminator(0, 0, 0x1000));
  885. EXPECT_EQ(None, DILocation::encodeDiscriminator(0x1ff, 0x1ff, 8));
  886. EXPECT_EQ(None,
  887. DILocation::encodeDiscriminator(std::numeric_limits<uint32_t>::max(),
  888. std::numeric_limits<uint32_t>::max(),
  889. 0));
  890. }
  891. TEST_F(DILocationTest, discriminatorSpecialCases) {
  892. // We don't test getCopyIdentifier here because the only way
  893. // to set it is by constructing an encoded discriminator using
  894. // encodeDiscriminator, which is already tested.
  895. auto L1 = DILocation::get(Context, 1, 2, getSubprogram());
  896. EXPECT_EQ(0U, L1->getBaseDiscriminator());
  897. EXPECT_EQ(1U, L1->getDuplicationFactor());
  898. EXPECT_EQ(L1, L1->cloneWithBaseDiscriminator(0).getValue());
  899. EXPECT_EQ(L1, L1->cloneByMultiplyingDuplicationFactor(0).getValue());
  900. EXPECT_EQ(L1, L1->cloneByMultiplyingDuplicationFactor(1).getValue());
  901. auto L2 = L1->cloneWithBaseDiscriminator(1).getValue();
  902. EXPECT_EQ(0U, L1->getBaseDiscriminator());
  903. EXPECT_EQ(1U, L1->getDuplicationFactor());
  904. EXPECT_EQ(1U, L2->getBaseDiscriminator());
  905. EXPECT_EQ(1U, L2->getDuplicationFactor());
  906. auto L3 = L2->cloneByMultiplyingDuplicationFactor(2).getValue();
  907. EXPECT_EQ(1U, L3->getBaseDiscriminator());
  908. EXPECT_EQ(2U, L3->getDuplicationFactor());
  909. EXPECT_EQ(L2, L2->cloneByMultiplyingDuplicationFactor(1).getValue());
  910. auto L4 = L3->cloneByMultiplyingDuplicationFactor(4).getValue();
  911. EXPECT_EQ(1U, L4->getBaseDiscriminator());
  912. EXPECT_EQ(8U, L4->getDuplicationFactor());
  913. auto L5 = L4->cloneWithBaseDiscriminator(2).getValue();
  914. EXPECT_EQ(2U, L5->getBaseDiscriminator());
  915. EXPECT_EQ(8U, L5->getDuplicationFactor());
  916. // Check extreme cases
  917. auto L6 = L1->cloneWithBaseDiscriminator(0xfff).getValue();
  918. EXPECT_EQ(0xfffU, L6->getBaseDiscriminator());
  919. EXPECT_EQ(0xfffU, L6->cloneByMultiplyingDuplicationFactor(0xfff)
  920. .getValue()
  921. ->getDuplicationFactor());
  922. // Check we return None for unencodable cases.
  923. EXPECT_EQ(None, L4->cloneWithBaseDiscriminator(0x1000));
  924. EXPECT_EQ(None, L4->cloneByMultiplyingDuplicationFactor(0x1000));
  925. }
  926. typedef MetadataTest GenericDINodeTest;
  927. TEST_F(GenericDINodeTest, get) {
  928. StringRef Header = "header";
  929. auto *Empty = MDNode::get(Context, None);
  930. Metadata *Ops1[] = {Empty};
  931. auto *N = GenericDINode::get(Context, 15, Header, Ops1);
  932. EXPECT_EQ(15u, N->getTag());
  933. EXPECT_EQ(2u, N->getNumOperands());
  934. EXPECT_EQ(Header, N->getHeader());
  935. EXPECT_EQ(MDString::get(Context, Header), N->getOperand(0));
  936. EXPECT_EQ(1u, N->getNumDwarfOperands());
  937. EXPECT_EQ(Empty, N->getDwarfOperand(0));
  938. EXPECT_EQ(Empty, N->getOperand(1));
  939. ASSERT_TRUE(N->isUniqued());
  940. EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
  941. N->replaceOperandWith(1, nullptr);
  942. EXPECT_EQ(15u, N->getTag());
  943. EXPECT_EQ(Header, N->getHeader());
  944. EXPECT_EQ(nullptr, N->getDwarfOperand(0));
  945. ASSERT_TRUE(N->isUniqued());
  946. Metadata *Ops2[] = {nullptr};
  947. EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops2));
  948. N->replaceDwarfOperandWith(0, Empty);
  949. EXPECT_EQ(15u, N->getTag());
  950. EXPECT_EQ(Header, N->getHeader());
  951. EXPECT_EQ(Empty, N->getDwarfOperand(0));
  952. ASSERT_TRUE(N->isUniqued());
  953. EXPECT_EQ(N, GenericDINode::get(Context, 15, Header, Ops1));
  954. TempGenericDINode Temp = N->clone();
  955. EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
  956. }
  957. TEST_F(GenericDINodeTest, getEmptyHeader) {
  958. // Canonicalize !"" to null.
  959. auto *N = GenericDINode::get(Context, 15, StringRef(), None);
  960. EXPECT_EQ(StringRef(), N->getHeader());
  961. EXPECT_EQ(nullptr, N->getOperand(0));
  962. }
  963. typedef MetadataTest DISubrangeTest;
  964. TEST_F(DISubrangeTest, get) {
  965. auto *N = DISubrange::get(Context, 5, 7);
  966. auto Count = N->getCount();
  967. EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
  968. ASSERT_TRUE(Count);
  969. ASSERT_TRUE(Count.is<ConstantInt*>());
  970. EXPECT_EQ(5, Count.get<ConstantInt*>()->getSExtValue());
  971. EXPECT_EQ(7, N->getLowerBound());
  972. EXPECT_EQ(N, DISubrange::get(Context, 5, 7));
  973. EXPECT_EQ(DISubrange::get(Context, 5, 0), DISubrange::get(Context, 5));
  974. TempDISubrange Temp = N->clone();
  975. EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
  976. }
  977. TEST_F(DISubrangeTest, getEmptyArray) {
  978. auto *N = DISubrange::get(Context, -1, 0);
  979. auto Count = N->getCount();
  980. EXPECT_EQ(dwarf::DW_TAG_subrange_type, N->getTag());
  981. ASSERT_TRUE(Count);
  982. ASSERT_TRUE(Count.is<ConstantInt*>());
  983. EXPECT_EQ(-1, Count.get<ConstantInt*>()->getSExtValue());
  984. EXPECT_EQ(0, N->getLowerBound());
  985. EXPECT_EQ(N, DISubrange::get(Context, -1, 0));
  986. }
  987. TEST_F(DISubrangeTest, getVariableCount) {
  988. DILocalScope *Scope = getSubprogram();
  989. DIFile *File = getFile();
  990. DIType *Type = getDerivedType();
  991. DINode::DIFlags Flags = static_cast<DINode::DIFlags>(7);
  992. auto *VlaExpr = DILocalVariable::get(Context, Scope, "vla_expr", File, 8,
  993. Type, 2, Flags, 8);
  994. auto *N = DISubrange::get(Context, VlaExpr, 0);
  995. auto Count = N->getCount();
  996. ASSERT_TRUE(Count);
  997. ASSERT_TRUE(Count.is<DIVariable*>());
  998. EXPECT_EQ(VlaExpr, Count.get<DIVariable*>());
  999. ASSERT_TRUE(isa<DIVariable>(N->getRawCountNode()));
  1000. EXPECT_EQ(0, N->getLowerBound());
  1001. EXPECT_EQ("vla_expr", Count.get<DIVariable*>()->getName());
  1002. EXPECT_EQ(N, DISubrange::get(Context, VlaExpr, 0));
  1003. }
  1004. typedef MetadataTest DIEnumeratorTest;
  1005. TEST_F(DIEnumeratorTest, get) {
  1006. auto *N = DIEnumerator::get(Context, 7, false, "name");
  1007. EXPECT_EQ(dwarf::DW_TAG_enumerator, N->getTag());
  1008. EXPECT_EQ(7, N->getValue());
  1009. EXPECT_FALSE(N->isUnsigned());
  1010. EXPECT_EQ("name", N->getName());
  1011. EXPECT_EQ(N, DIEnumerator::get(Context, 7, false, "name"));
  1012. EXPECT_NE(N, DIEnumerator::get(Context, 7, true, "name"));
  1013. EXPECT_NE(N, DIEnumerator::get(Context, 8, false, "name"));
  1014. EXPECT_NE(N, DIEnumerator::get(Context, 7, false, "nam"));
  1015. TempDIEnumerator Temp = N->clone();
  1016. EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
  1017. }
  1018. typedef MetadataTest DIBasicTypeTest;
  1019. TEST_F(DIBasicTypeTest, get) {
  1020. auto *N =
  1021. DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33, 26, 7,
  1022. DINode::FlagZero);
  1023. EXPECT_EQ(dwarf::DW_TAG_base_type, N->getTag());
  1024. EXPECT_EQ("special", N->getName());
  1025. EXPECT_EQ(33u, N->getSizeInBits());
  1026. EXPECT_EQ(26u, N->getAlignInBits());
  1027. EXPECT_EQ(7u, N->getEncoding());
  1028. EXPECT_EQ(0u, N->getLine());
  1029. EXPECT_EQ(DINode::FlagZero, N->getFlags());
  1030. EXPECT_EQ(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
  1031. 26, 7, DINode::FlagZero));
  1032. EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type,
  1033. "special", 33, 26, 7, DINode::FlagZero));
  1034. EXPECT_NE(N,
  1035. DIBasicType::get(Context, dwarf::DW_TAG_base_type, "s", 33, 26, 7,
  1036. DINode::FlagZero));
  1037. EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 32,
  1038. 26, 7, DINode::FlagZero));
  1039. EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
  1040. 25, 7, DINode::FlagZero));
  1041. EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
  1042. 26, 6, DINode::FlagZero));
  1043. EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
  1044. 26, 7, DINode::FlagBigEndian));
  1045. EXPECT_NE(N, DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special", 33,
  1046. 26, 7, DINode::FlagLittleEndian));
  1047. TempDIBasicType Temp = N->clone();
  1048. EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
  1049. }
  1050. TEST_F(DIBasicTypeTest, getWithLargeValues) {
  1051. auto *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "special",
  1052. UINT64_MAX, UINT32_MAX - 1, 7, DINode::FlagZero);
  1053. EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
  1054. EXPECT_EQ(UINT32_MAX - 1, N->getAlignInBits());
  1055. }
  1056. TEST_F(DIBasicTypeTest, getUnspecified) {
  1057. auto *N =
  1058. DIBasicType::get(Context, dwarf::DW_TAG_unspecified_type, "unspecified");
  1059. EXPECT_EQ(dwarf::DW_TAG_unspecified_type, N->getTag());
  1060. EXPECT_EQ("unspecified", N->getName());
  1061. EXPECT_EQ(0u, N->getSizeInBits());
  1062. EXPECT_EQ(0u, N->getAlignInBits());
  1063. EXPECT_EQ(0u, N->getEncoding());
  1064. EXPECT_EQ(0u, N->getLine());
  1065. EXPECT_EQ(DINode::FlagZero, N->getFlags());
  1066. }
  1067. typedef MetadataTest DITypeTest;
  1068. TEST_F(DITypeTest, clone) {
  1069. // Check that DIType has a specialized clone that returns TempDIType.
  1070. DIType *N = DIBasicType::get(Context, dwarf::DW_TAG_base_type, "int", 32, 32,
  1071. dwarf::DW_ATE_signed, DINode::FlagZero);
  1072. TempDIType Temp = N->clone();
  1073. EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
  1074. }
  1075. TEST_F(DITypeTest, cloneWithFlags) {
  1076. // void (void)
  1077. Metadata *TypesOps[] = {nullptr};
  1078. Metadata *Types = MDTuple::get(Context, TypesOps);
  1079. DIType *D =
  1080. DISubroutineType::getDistinct(Context, DINode::FlagZero, 0, Types);
  1081. EXPECT_EQ(DINode::FlagZero, D->getFlags());
  1082. TempDIType D2 = D->cloneWithFlags(DINode::FlagRValueReference);
  1083. EXPECT_EQ(DINode::FlagRValueReference, D2->getFlags());
  1084. EXPECT_EQ(DINode::FlagZero, D->getFlags());
  1085. TempDIType T =
  1086. DISubroutineType::getTemporary(Context, DINode::FlagZero, 0, Types);
  1087. EXPECT_EQ(DINode::FlagZero, T->getFlags());
  1088. TempDIType T2 = T->cloneWithFlags(DINode::FlagRValueReference);
  1089. EXPECT_EQ(DINode::FlagRValueReference, T2->getFlags());
  1090. EXPECT_EQ(DINode::FlagZero, T->getFlags());
  1091. }
  1092. typedef MetadataTest DIDerivedTypeTest;
  1093. TEST_F(DIDerivedTypeTest, get) {
  1094. DIFile *File = getFile();
  1095. DIScope *Scope = getSubprogram();
  1096. DIType *BaseType = getBasicType("basic");
  1097. MDTuple *ExtraData = getTuple();
  1098. unsigned DWARFAddressSpace = 8;
  1099. DINode::DIFlags Flags5 = static_cast<DINode::DIFlags>(5);
  1100. DINode::DIFlags Flags4 = static_cast<DINode::DIFlags>(4);
  1101. auto *N =
  1102. DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "something", File,
  1103. 1, Scope, BaseType, 2, 3, 4, DWARFAddressSpace, Flags5,
  1104. ExtraData);
  1105. EXPECT_EQ(dwarf::DW_TAG_pointer_type, N->getTag());
  1106. EXPECT_EQ("something", N->getName());
  1107. EXPECT_EQ(File, N->getFile());
  1108. EXPECT_EQ(1u, N->getLine());
  1109. EXPECT_EQ(Scope, N->getScope());
  1110. EXPECT_EQ(BaseType, N->getBaseType());
  1111. EXPECT_EQ(2u, N->getSizeInBits());
  1112. EXPECT_EQ(3u, N->getAlignInBits());
  1113. EXPECT_EQ(4u, N->getOffsetInBits());
  1114. EXPECT_EQ(DWARFAddressSpace, N->getDWARFAddressSpace().getValue());
  1115. EXPECT_EQ(5u, N->getFlags());
  1116. EXPECT_EQ(ExtraData, N->getExtraData());
  1117. EXPECT_EQ(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
  1118. "something", File, 1, Scope, BaseType, 2, 3,
  1119. 4, DWARFAddressSpace, Flags5, ExtraData));
  1120. EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_reference_type,
  1121. "something", File, 1, Scope, BaseType, 2, 3,
  1122. 4, DWARFAddressSpace, Flags5, ExtraData));
  1123. EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type, "else",
  1124. File, 1, Scope, BaseType, 2, 3,
  1125. 4, DWARFAddressSpace, Flags5, ExtraData));
  1126. EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
  1127. "something", getFile(), 1, Scope, BaseType, 2,
  1128. 3, 4, DWARFAddressSpace, Flags5, ExtraData));
  1129. EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
  1130. "something", File, 2, Scope, BaseType, 2, 3,
  1131. 4, DWARFAddressSpace, Flags5, ExtraData));
  1132. EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
  1133. "something", File, 1, getSubprogram(),
  1134. BaseType, 2, 3, 4, DWARFAddressSpace, Flags5,
  1135. ExtraData));
  1136. EXPECT_NE(N, DIDerivedType::get(
  1137. Context, dwarf::DW_TAG_pointer_type, "something", File, 1,
  1138. Scope, getBasicType("basic2"), 2, 3, 4, DWARFAddressSpace,
  1139. Flags5, ExtraData));
  1140. EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
  1141. "something", File, 1, Scope, BaseType, 3, 3,
  1142. 4, DWARFAddressSpace, Flags5, ExtraData));
  1143. EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
  1144. "something", File, 1, Scope, BaseType, 2, 2,
  1145. 4, DWARFAddressSpace, Flags5, ExtraData));
  1146. EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
  1147. "something", File, 1, Scope, BaseType, 2, 3,
  1148. 5, DWARFAddressSpace, Flags5, ExtraData));
  1149. EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
  1150. "something", File, 1, Scope, BaseType, 2, 3,
  1151. 4, DWARFAddressSpace + 1, Flags5, ExtraData));
  1152. EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
  1153. "something", File, 1, Scope, BaseType, 2, 3,
  1154. 4, DWARFAddressSpace, Flags4, ExtraData));
  1155. EXPECT_NE(N, DIDerivedType::get(Context, dwarf::DW_TAG_pointer_type,
  1156. "something", File, 1, Scope, BaseType, 2, 3,
  1157. 4, DWARFAddressSpace, Flags5, getTuple()));
  1158. TempDIDerivedType Temp = N->clone();
  1159. EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
  1160. }
  1161. TEST_F(DIDerivedTypeTest, getWithLargeValues) {
  1162. DIFile *File = getFile();
  1163. DIScope *Scope = getSubprogram();
  1164. DIType *BaseType = getBasicType("basic");
  1165. MDTuple *ExtraData = getTuple();
  1166. DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
  1167. auto *N = DIDerivedType::get(
  1168. Context, dwarf::DW_TAG_pointer_type, "something", File, 1, Scope,
  1169. BaseType, UINT64_MAX, UINT32_MAX - 1, UINT64_MAX - 2, UINT32_MAX - 3,
  1170. Flags, ExtraData);
  1171. EXPECT_EQ(UINT64_MAX, N->getSizeInBits());
  1172. EXPECT_EQ(UINT32_MAX - 1, N->getAlignInBits());
  1173. EXPECT_EQ(UINT64_MAX - 2, N->getOffsetInBits());
  1174. EXPECT_EQ(UINT32_MAX - 3, N->getDWARFAddressSpace().getValue());
  1175. }
  1176. typedef MetadataTest DICompositeTypeTest;
  1177. TEST_F(DICompositeTypeTest, get) {
  1178. unsigned Tag = dwarf::DW_TAG_structure_type;
  1179. StringRef Name = "some name";
  1180. DIFile *File = getFile();
  1181. unsigned Line = 1;
  1182. DIScope *Scope = getSubprogram();
  1183. DIType *BaseType = getCompositeType();
  1184. uint64_t SizeInBits = 2;
  1185. uint32_t AlignInBits = 3;
  1186. uint64_t OffsetInBits = 4;
  1187. DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
  1188. MDTuple *Elements = getTuple();
  1189. unsigned RuntimeLang = 6;
  1190. DIType *VTableHolder = getCompositeType();
  1191. MDTuple *TemplateParams = getTuple();
  1192. StringRef Identifier = "some id";
  1193. auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
  1194. BaseType, SizeInBits, AlignInBits,
  1195. OffsetInBits, Flags, Elements, RuntimeLang,
  1196. VTableHolder, TemplateParams, Identifier);
  1197. EXPECT_EQ(Tag, N->getTag());
  1198. EXPECT_EQ(Name, N->getName());
  1199. EXPECT_EQ(File, N->getFile());
  1200. EXPECT_EQ(Line, N->getLine());
  1201. EXPECT_EQ(Scope, N->getScope());
  1202. EXPECT_EQ(BaseType, N->getBaseType());
  1203. EXPECT_EQ(SizeInBits, N->getSizeInBits());
  1204. EXPECT_EQ(AlignInBits, N->getAlignInBits());
  1205. EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
  1206. EXPECT_EQ(Flags, N->getFlags());
  1207. EXPECT_EQ(Elements, N->getElements().get());
  1208. EXPECT_EQ(RuntimeLang, N->getRuntimeLang());
  1209. EXPECT_EQ(VTableHolder, N->getVTableHolder());
  1210. EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
  1211. EXPECT_EQ(Identifier, N->getIdentifier());
  1212. EXPECT_EQ(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
  1213. BaseType, SizeInBits, AlignInBits,
  1214. OffsetInBits, Flags, Elements, RuntimeLang,
  1215. VTableHolder, TemplateParams, Identifier));
  1216. EXPECT_NE(N, DICompositeType::get(Context, Tag + 1, Name, File, Line, Scope,
  1217. BaseType, SizeInBits, AlignInBits,
  1218. OffsetInBits, Flags, Elements, RuntimeLang,
  1219. VTableHolder, TemplateParams, Identifier));
  1220. EXPECT_NE(N, DICompositeType::get(Context, Tag, "abc", File, Line, Scope,
  1221. BaseType, SizeInBits, AlignInBits,
  1222. OffsetInBits, Flags, Elements, RuntimeLang,
  1223. VTableHolder, TemplateParams, Identifier));
  1224. EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, getFile(), Line, Scope,
  1225. BaseType, SizeInBits, AlignInBits,
  1226. OffsetInBits, Flags, Elements, RuntimeLang,
  1227. VTableHolder, TemplateParams, Identifier));
  1228. EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line + 1, Scope,
  1229. BaseType, SizeInBits, AlignInBits,
  1230. OffsetInBits, Flags, Elements, RuntimeLang,
  1231. VTableHolder, TemplateParams, Identifier));
  1232. EXPECT_NE(N, DICompositeType::get(
  1233. Context, Tag, Name, File, Line, getSubprogram(), BaseType,
  1234. SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
  1235. RuntimeLang, VTableHolder, TemplateParams, Identifier));
  1236. EXPECT_NE(N, DICompositeType::get(
  1237. Context, Tag, Name, File, Line, Scope, getBasicType("other"),
  1238. SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
  1239. RuntimeLang, VTableHolder, TemplateParams, Identifier));
  1240. EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
  1241. BaseType, SizeInBits + 1, AlignInBits,
  1242. OffsetInBits, Flags, Elements, RuntimeLang,
  1243. VTableHolder, TemplateParams, Identifier));
  1244. EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
  1245. BaseType, SizeInBits, AlignInBits + 1,
  1246. OffsetInBits, Flags, Elements, RuntimeLang,
  1247. VTableHolder, TemplateParams, Identifier));
  1248. EXPECT_NE(N, DICompositeType::get(
  1249. Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
  1250. AlignInBits, OffsetInBits + 1, Flags, Elements, RuntimeLang,
  1251. VTableHolder, TemplateParams, Identifier));
  1252. DINode::DIFlags FlagsPOne = static_cast<DINode::DIFlags>(Flags + 1);
  1253. EXPECT_NE(N, DICompositeType::get(
  1254. Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
  1255. AlignInBits, OffsetInBits, FlagsPOne, Elements, RuntimeLang,
  1256. VTableHolder, TemplateParams, Identifier));
  1257. EXPECT_NE(N, DICompositeType::get(
  1258. Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
  1259. AlignInBits, OffsetInBits, Flags, getTuple(), RuntimeLang,
  1260. VTableHolder, TemplateParams, Identifier));
  1261. EXPECT_NE(N, DICompositeType::get(
  1262. Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
  1263. AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang + 1,
  1264. VTableHolder, TemplateParams, Identifier));
  1265. EXPECT_NE(N, DICompositeType::get(
  1266. Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
  1267. AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
  1268. getCompositeType(), TemplateParams, Identifier));
  1269. EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
  1270. BaseType, SizeInBits, AlignInBits,
  1271. OffsetInBits, Flags, Elements, RuntimeLang,
  1272. VTableHolder, getTuple(), Identifier));
  1273. EXPECT_NE(N, DICompositeType::get(Context, Tag, Name, File, Line, Scope,
  1274. BaseType, SizeInBits, AlignInBits,
  1275. OffsetInBits, Flags, Elements, RuntimeLang,
  1276. VTableHolder, TemplateParams, "other"));
  1277. // Be sure that missing identifiers get null pointers.
  1278. EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
  1279. BaseType, SizeInBits, AlignInBits,
  1280. OffsetInBits, Flags, Elements, RuntimeLang,
  1281. VTableHolder, TemplateParams, "")
  1282. ->getRawIdentifier());
  1283. EXPECT_FALSE(DICompositeType::get(Context, Tag, Name, File, Line, Scope,
  1284. BaseType, SizeInBits, AlignInBits,
  1285. OffsetInBits, Flags, Elements, RuntimeLang,
  1286. VTableHolder, TemplateParams)
  1287. ->getRawIdentifier());
  1288. TempDICompositeType Temp = N->clone();
  1289. EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
  1290. }
  1291. TEST_F(DICompositeTypeTest, getWithLargeValues) {
  1292. unsigned Tag = dwarf::DW_TAG_structure_type;
  1293. StringRef Name = "some name";
  1294. DIFile *File = getFile();
  1295. unsigned Line = 1;
  1296. DIScope *Scope = getSubprogram();
  1297. DIType *BaseType = getCompositeType();
  1298. uint64_t SizeInBits = UINT64_MAX;
  1299. uint32_t AlignInBits = UINT32_MAX - 1;
  1300. uint64_t OffsetInBits = UINT64_MAX - 2;
  1301. DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
  1302. MDTuple *Elements = getTuple();
  1303. unsigned RuntimeLang = 6;
  1304. DIType *VTableHolder = getCompositeType();
  1305. MDTuple *TemplateParams = getTuple();
  1306. StringRef Identifier = "some id";
  1307. auto *N = DICompositeType::get(Context, Tag, Name, File, Line, Scope,
  1308. BaseType, SizeInBits, AlignInBits,
  1309. OffsetInBits, Flags, Elements, RuntimeLang,
  1310. VTableHolder, TemplateParams, Identifier);
  1311. EXPECT_EQ(SizeInBits, N->getSizeInBits());
  1312. EXPECT_EQ(AlignInBits, N->getAlignInBits());
  1313. EXPECT_EQ(OffsetInBits, N->getOffsetInBits());
  1314. }
  1315. TEST_F(DICompositeTypeTest, replaceOperands) {
  1316. unsigned Tag = dwarf::DW_TAG_structure_type;
  1317. StringRef Name = "some name";
  1318. DIFile *File = getFile();
  1319. unsigned Line = 1;
  1320. DIScope *Scope = getSubprogram();
  1321. DIType *BaseType = getCompositeType();
  1322. uint64_t SizeInBits = 2;
  1323. uint32_t AlignInBits = 3;
  1324. uint64_t OffsetInBits = 4;
  1325. DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
  1326. unsigned RuntimeLang = 6;
  1327. StringRef Identifier = "some id";
  1328. auto *N = DICompositeType::get(
  1329. Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
  1330. OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier);
  1331. auto *Elements = MDTuple::getDistinct(Context, None);
  1332. EXPECT_EQ(nullptr, N->getElements().get());
  1333. N->replaceElements(Elements);
  1334. EXPECT_EQ(Elements, N->getElements().get());
  1335. N->replaceElements(nullptr);
  1336. EXPECT_EQ(nullptr, N->getElements().get());
  1337. DIType *VTableHolder = getCompositeType();
  1338. EXPECT_EQ(nullptr, N->getVTableHolder());
  1339. N->replaceVTableHolder(VTableHolder);
  1340. EXPECT_EQ(VTableHolder, N->getVTableHolder());
  1341. // As an extension, the containing type can be anything. This is
  1342. // used by Rust to associate vtables with their concrete type.
  1343. DIType *BasicType = getBasicType("basic");
  1344. N->replaceVTableHolder(BasicType);
  1345. EXPECT_EQ(BasicType, N->getVTableHolder());
  1346. N->replaceVTableHolder(nullptr);
  1347. EXPECT_EQ(nullptr, N->getVTableHolder());
  1348. auto *TemplateParams = MDTuple::getDistinct(Context, None);
  1349. EXPECT_EQ(nullptr, N->getTemplateParams().get());
  1350. N->replaceTemplateParams(TemplateParams);
  1351. EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
  1352. N->replaceTemplateParams(nullptr);
  1353. EXPECT_EQ(nullptr, N->getTemplateParams().get());
  1354. }
  1355. TEST_F(DICompositeTypeTest, variant_part) {
  1356. unsigned Tag = dwarf::DW_TAG_variant_part;
  1357. StringRef Name = "some name";
  1358. DIFile *File = getFile();
  1359. unsigned Line = 1;
  1360. DIScope *Scope = getSubprogram();
  1361. DIType *BaseType = getCompositeType();
  1362. uint64_t SizeInBits = 2;
  1363. uint32_t AlignInBits = 3;
  1364. uint64_t OffsetInBits = 4;
  1365. DINode::DIFlags Flags = static_cast<DINode::DIFlags>(5);
  1366. unsigned RuntimeLang = 6;
  1367. StringRef Identifier = "some id";
  1368. DIDerivedType *Discriminator = cast<DIDerivedType>(getDerivedType());
  1369. DIDerivedType *Discriminator2 = cast<DIDerivedType>(getDerivedType());
  1370. EXPECT_NE(Discriminator, Discriminator2);
  1371. auto *N = DICompositeType::get(
  1372. Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
  1373. OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier,
  1374. Discriminator);
  1375. // Test the hashing.
  1376. auto *Same = DICompositeType::get(
  1377. Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
  1378. OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier,
  1379. Discriminator);
  1380. auto *Other = DICompositeType::get(
  1381. Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
  1382. OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier,
  1383. Discriminator2);
  1384. auto *NoDisc = DICompositeType::get(
  1385. Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, AlignInBits,
  1386. OffsetInBits, Flags, nullptr, RuntimeLang, nullptr, nullptr, Identifier,
  1387. nullptr);
  1388. EXPECT_EQ(N, Same);
  1389. EXPECT_NE(Same, Other);
  1390. EXPECT_NE(Same, NoDisc);
  1391. EXPECT_NE(Other, NoDisc);
  1392. EXPECT_EQ(N->getDiscriminator(), Discriminator);
  1393. }
  1394. typedef MetadataTest DISubroutineTypeTest;
  1395. TEST_F(DISubroutineTypeTest, get) {
  1396. DINode::DIFlags Flags = static_cast<DINode::DIFlags>(1);
  1397. DINode::DIFlags FlagsPOne = static_cast<DINode::DIFlags>(Flags + 1);
  1398. MDTuple *TypeArray = getTuple();
  1399. auto *N = DISubroutineType::get(Context, Flags, 0, TypeArray);
  1400. EXPECT_EQ(dwarf::DW_TAG_subroutine_type, N->getTag());
  1401. EXPECT_EQ(Flags, N->getFlags());
  1402. EXPECT_EQ(TypeArray, N->getTypeArray().get());
  1403. EXPECT_EQ(N, DISubroutineType::get(Context, Flags, 0, TypeArray));
  1404. EXPECT_NE(N, DISubroutineType::get(Context, FlagsPOne, 0, TypeArray));
  1405. EXPECT_NE(N, DISubroutineType::get(Context, Flags, 0, getTuple()));
  1406. // Test the hashing of calling conventions.
  1407. auto *Fast = DISubroutineType::get(
  1408. Context, Flags, dwarf::DW_CC_BORLAND_msfastcall, TypeArray);
  1409. auto *Std = DISubroutineType::get(Context, Flags,
  1410. dwarf::DW_CC_BORLAND_stdcall, TypeArray);
  1411. EXPECT_EQ(Fast,
  1412. DISubroutineType::get(Context, Flags,
  1413. dwarf::DW_CC_BORLAND_msfastcall, TypeArray));
  1414. EXPECT_EQ(Std, DISubroutineType::get(
  1415. Context, Flags, dwarf::DW_CC_BORLAND_stdcall, TypeArray));
  1416. EXPECT_NE(N, Fast);
  1417. EXPECT_NE(N, Std);
  1418. EXPECT_NE(Fast, Std);
  1419. TempDISubroutineType Temp = N->clone();
  1420. EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
  1421. // Test always-empty operands.
  1422. EXPECT_EQ(nullptr, N->getScope());
  1423. EXPECT_EQ(nullptr, N->getFile());
  1424. EXPECT_EQ("", N->getName());
  1425. }
  1426. typedef MetadataTest DIFileTest;
  1427. TEST_F(DIFileTest, get) {
  1428. StringRef Filename = "file";
  1429. StringRef Directory = "dir";
  1430. DIFile::ChecksumKind CSKind = DIFile::ChecksumKind::CSK_MD5;
  1431. StringRef ChecksumString = "000102030405060708090a0b0c0d0e0f";
  1432. DIFile::ChecksumInfo<StringRef> Checksum(CSKind, ChecksumString);
  1433. StringRef Source = "source";
  1434. auto *N = DIFile::get(Context, Filename, Directory, Checksum, Source);
  1435. EXPECT_EQ(dwarf::DW_TAG_file_type, N->getTag());
  1436. EXPECT_EQ(Filename, N->getFilename());
  1437. EXPECT_EQ(Directory, N->getDirectory());
  1438. EXPECT_EQ(Checksum, N->getChecksum());
  1439. EXPECT_EQ(Source, N->getSource());
  1440. EXPECT_EQ(N, DIFile::get(Context, Filename, Directory, Checksum, Source));
  1441. EXPECT_NE(N, DIFile::get(Context, "other", Directory, Checksum, Source));
  1442. EXPECT_NE(N, DIFile::get(Context, Filename, "other", Checksum, Source));
  1443. DIFile::ChecksumInfo<StringRef> OtherChecksum(DIFile::ChecksumKind::CSK_SHA1, ChecksumString);
  1444. EXPECT_NE(
  1445. N, DIFile::get(Context, Filename, Directory, OtherChecksum));
  1446. StringRef OtherSource = "other";
  1447. EXPECT_NE(N, DIFile::get(Context, Filename, Directory, Checksum, OtherSource));
  1448. EXPECT_NE(N, DIFile::get(Context, Filename, Directory, Checksum));
  1449. EXPECT_NE(N, DIFile::get(Context, Filename, Directory));
  1450. TempDIFile Temp = N->clone();
  1451. EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
  1452. }
  1453. TEST_F(DIFileTest, ScopeGetFile) {
  1454. // Ensure that DIScope::getFile() returns itself.
  1455. DIScope *N = DIFile::get(Context, "file", "dir");
  1456. EXPECT_EQ(N, N->getFile());
  1457. }
  1458. typedef MetadataTest DICompileUnitTest;
  1459. TEST_F(DICompileUnitTest, get) {
  1460. unsigned SourceLanguage = 1;
  1461. DIFile *File = getFile();
  1462. StringRef Producer = "some producer";
  1463. bool IsOptimized = false;
  1464. StringRef Flags = "flag after flag";
  1465. unsigned RuntimeVersion = 2;
  1466. StringRef SplitDebugFilename = "another/file";
  1467. auto EmissionKind = DICompileUnit::FullDebug;
  1468. MDTuple *EnumTypes = getTuple();
  1469. MDTuple *RetainedTypes = getTuple();
  1470. MDTuple *GlobalVariables = getTuple();
  1471. MDTuple *ImportedEntities = getTuple();
  1472. uint64_t DWOId = 0x10000000c0ffee;
  1473. MDTuple *Macros = getTuple();
  1474. auto *N = DICompileUnit::getDistinct(
  1475. Context, SourceLanguage, File, Producer, IsOptimized, Flags,
  1476. RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
  1477. RetainedTypes, GlobalVariables, ImportedEntities, Macros, DWOId, true,
  1478. false, DICompileUnit::DebugNameTableKind::Default, false);
  1479. EXPECT_EQ(dwarf::DW_TAG_compile_unit, N->getTag());
  1480. EXPECT_EQ(SourceLanguage, N->getSourceLanguage());
  1481. EXPECT_EQ(File, N->getFile());
  1482. EXPECT_EQ(Producer, N->getProducer());
  1483. EXPECT_EQ(IsOptimized, N->isOptimized());
  1484. EXPECT_EQ(Flags, N->getFlags());
  1485. EXPECT_EQ(RuntimeVersion, N->getRuntimeVersion());
  1486. EXPECT_EQ(SplitDebugFilename, N->getSplitDebugFilename());
  1487. EXPECT_EQ(EmissionKind, N->getEmissionKind());
  1488. EXPECT_EQ(EnumTypes, N->getEnumTypes().get());
  1489. EXPECT_EQ(RetainedTypes, N->getRetainedTypes().get());
  1490. EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
  1491. EXPECT_EQ(ImportedEntities, N->getImportedEntities().get());
  1492. EXPECT_EQ(Macros, N->getMacros().get());
  1493. EXPECT_EQ(DWOId, N->getDWOId());
  1494. TempDICompileUnit Temp = N->clone();
  1495. EXPECT_EQ(dwarf::DW_TAG_compile_unit, Temp->getTag());
  1496. EXPECT_EQ(SourceLanguage, Temp->getSourceLanguage());
  1497. EXPECT_EQ(File, Temp->getFile());
  1498. EXPECT_EQ(Producer, Temp->getProducer());
  1499. EXPECT_EQ(IsOptimized, Temp->isOptimized());
  1500. EXPECT_EQ(Flags, Temp->getFlags());
  1501. EXPECT_EQ(RuntimeVersion, Temp->getRuntimeVersion());
  1502. EXPECT_EQ(SplitDebugFilename, Temp->getSplitDebugFilename());
  1503. EXPECT_EQ(EmissionKind, Temp->getEmissionKind());
  1504. EXPECT_EQ(EnumTypes, Temp->getEnumTypes().get());
  1505. EXPECT_EQ(RetainedTypes, Temp->getRetainedTypes().get());
  1506. EXPECT_EQ(GlobalVariables, Temp->getGlobalVariables().get());
  1507. EXPECT_EQ(ImportedEntities, Temp->getImportedEntities().get());
  1508. EXPECT_EQ(Macros, Temp->getMacros().get());
  1509. EXPECT_EQ(DWOId, Temp->getDWOId());
  1510. auto *TempAddress = Temp.get();
  1511. auto *Clone = MDNode::replaceWithPermanent(std::move(Temp));
  1512. EXPECT_TRUE(Clone->isDistinct());
  1513. EXPECT_EQ(TempAddress, Clone);
  1514. }
  1515. TEST_F(DICompileUnitTest, replaceArrays) {
  1516. unsigned SourceLanguage = 1;
  1517. DIFile *File = getFile();
  1518. StringRef Producer = "some producer";
  1519. bool IsOptimized = false;
  1520. StringRef Flags = "flag after flag";
  1521. unsigned RuntimeVersion = 2;
  1522. StringRef SplitDebugFilename = "another/file";
  1523. auto EmissionKind = DICompileUnit::FullDebug;
  1524. MDTuple *EnumTypes = MDTuple::getDistinct(Context, None);
  1525. MDTuple *RetainedTypes = MDTuple::getDistinct(Context, None);
  1526. MDTuple *ImportedEntities = MDTuple::getDistinct(Context, None);
  1527. uint64_t DWOId = 0xc0ffee;
  1528. auto *N = DICompileUnit::getDistinct(
  1529. Context, SourceLanguage, File, Producer, IsOptimized, Flags,
  1530. RuntimeVersion, SplitDebugFilename, EmissionKind, EnumTypes,
  1531. RetainedTypes, nullptr, ImportedEntities, nullptr, DWOId, true, false,
  1532. DICompileUnit::DebugNameTableKind::Default, false);
  1533. auto *GlobalVariables = MDTuple::getDistinct(Context, None);
  1534. EXPECT_EQ(nullptr, N->getGlobalVariables().get());
  1535. N->replaceGlobalVariables(GlobalVariables);
  1536. EXPECT_EQ(GlobalVariables, N->getGlobalVariables().get());
  1537. N->replaceGlobalVariables(nullptr);
  1538. EXPECT_EQ(nullptr, N->getGlobalVariables().get());
  1539. auto *Macros = MDTuple::getDistinct(Context, None);
  1540. EXPECT_EQ(nullptr, N->getMacros().get());
  1541. N->replaceMacros(Macros);
  1542. EXPECT_EQ(Macros, N->getMacros().get());
  1543. N->replaceMacros(nullptr);
  1544. EXPECT_EQ(nullptr, N->getMacros().get());
  1545. }
  1546. typedef MetadataTest DISubprogramTest;
  1547. TEST_F(DISubprogramTest, get) {
  1548. DIScope *Scope = getCompositeType();
  1549. StringRef Name = "name";
  1550. StringRef LinkageName = "linkage";
  1551. DIFile *File = getFile();
  1552. unsigned Line = 2;
  1553. DISubroutineType *Type = getSubroutineType();
  1554. bool IsLocalToUnit = false;
  1555. bool IsDefinition = true;
  1556. unsigned ScopeLine = 3;
  1557. DIType *ContainingType = getCompositeType();
  1558. unsigned Virtuality = 2;
  1559. unsigned VirtualIndex = 5;
  1560. int ThisAdjustment = -3;
  1561. DINode::DIFlags Flags = static_cast<DINode::DIFlags>(6);
  1562. bool IsOptimized = false;
  1563. MDTuple *TemplateParams = getTuple();
  1564. DISubprogram *Declaration = getSubprogram();
  1565. MDTuple *RetainedNodes = getTuple();
  1566. MDTuple *ThrownTypes = getTuple();
  1567. DICompileUnit *Unit = getUnit();
  1568. DISubprogram::DISPFlags SPFlags =
  1569. static_cast<DISubprogram::DISPFlags>(Virtuality);
  1570. assert(!IsLocalToUnit && IsDefinition && !IsOptimized &&
  1571. "bools and SPFlags have to match");
  1572. SPFlags |= DISubprogram::SPFlagDefinition;
  1573. auto *N = DISubprogram::get(
  1574. Context, Scope, Name, LinkageName, File, Line, Type, ScopeLine,
  1575. ContainingType, VirtualIndex, ThisAdjustment, Flags, SPFlags, Unit,
  1576. TemplateParams, Declaration, RetainedNodes, ThrownTypes);
  1577. EXPECT_EQ(dwarf::DW_TAG_subprogram, N->getTag());
  1578. EXPECT_EQ(Scope, N->getScope());
  1579. EXPECT_EQ(Name, N->getName());
  1580. EXPECT_EQ(LinkageName, N->getLinkageName());
  1581. EXPECT_EQ(File, N->getFile());
  1582. EXPECT_EQ(Line, N->getLine());
  1583. EXPECT_EQ(Type, N->getType());
  1584. EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
  1585. EXPECT_EQ(IsDefinition, N->isDefinition());
  1586. EXPECT_EQ(ScopeLine, N->getScopeLine());
  1587. EXPECT_EQ(ContainingType, N->getContainingType());
  1588. EXPECT_EQ(Virtuality, N->getVirtuality());
  1589. EXPECT_EQ(VirtualIndex, N->getVirtualIndex());
  1590. EXPECT_EQ(ThisAdjustment, N->getThisAdjustment());
  1591. EXPECT_EQ(Flags, N->getFlags());
  1592. EXPECT_EQ(IsOptimized, N->isOptimized());
  1593. EXPECT_EQ(Unit, N->getUnit());
  1594. EXPECT_EQ(TemplateParams, N->getTemplateParams().get());
  1595. EXPECT_EQ(Declaration, N->getDeclaration());
  1596. EXPECT_EQ(RetainedNodes, N->getRetainedNodes().get());
  1597. EXPECT_EQ(ThrownTypes, N->getThrownTypes().get());
  1598. EXPECT_EQ(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
  1599. Type, ScopeLine, ContainingType, VirtualIndex,
  1600. ThisAdjustment, Flags, SPFlags, Unit,
  1601. TemplateParams, Declaration, RetainedNodes,
  1602. ThrownTypes));
  1603. EXPECT_NE(N, DISubprogram::get(Context, getCompositeType(), Name, LinkageName,
  1604. File, Line, Type, ScopeLine, ContainingType,
  1605. VirtualIndex, ThisAdjustment, Flags, SPFlags,
  1606. Unit, TemplateParams, Declaration,
  1607. RetainedNodes, ThrownTypes));
  1608. EXPECT_NE(N, DISubprogram::get(Context, Scope, "other", LinkageName, File,
  1609. Line, Type, ScopeLine, ContainingType,
  1610. VirtualIndex, ThisAdjustment, Flags, SPFlags,
  1611. Unit, TemplateParams, Declaration,
  1612. RetainedNodes, ThrownTypes));
  1613. EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, "other", File, Line,
  1614. Type, ScopeLine, ContainingType, VirtualIndex,
  1615. ThisAdjustment, Flags, SPFlags, Unit,
  1616. TemplateParams, Declaration, RetainedNodes,
  1617. ThrownTypes));
  1618. EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, getFile(),
  1619. Line, Type, ScopeLine, ContainingType,
  1620. VirtualIndex, ThisAdjustment, Flags, SPFlags,
  1621. Unit, TemplateParams, Declaration,
  1622. RetainedNodes, ThrownTypes));
  1623. EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File,
  1624. Line + 1, Type, ScopeLine, ContainingType,
  1625. VirtualIndex, ThisAdjustment, Flags, SPFlags,
  1626. Unit, TemplateParams, Declaration,
  1627. RetainedNodes, ThrownTypes));
  1628. EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
  1629. getSubroutineType(), ScopeLine, ContainingType,
  1630. VirtualIndex, ThisAdjustment, Flags, SPFlags,
  1631. Unit, TemplateParams, Declaration,
  1632. RetainedNodes, ThrownTypes));
  1633. EXPECT_NE(N, DISubprogram::get(
  1634. Context, Scope, Name, LinkageName, File, Line, Type,
  1635. ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
  1636. Flags, SPFlags ^ DISubprogram::SPFlagLocalToUnit, Unit,
  1637. TemplateParams, Declaration, RetainedNodes, ThrownTypes));
  1638. EXPECT_NE(N, DISubprogram::get(
  1639. Context, Scope, Name, LinkageName, File, Line, Type,
  1640. ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
  1641. Flags, SPFlags ^ DISubprogram::SPFlagDefinition, Unit,
  1642. TemplateParams, Declaration, RetainedNodes, ThrownTypes));
  1643. EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
  1644. Type, ScopeLine + 1, ContainingType,
  1645. VirtualIndex, ThisAdjustment, Flags, SPFlags,
  1646. Unit, TemplateParams, Declaration,
  1647. RetainedNodes, ThrownTypes));
  1648. EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
  1649. Type, ScopeLine, getCompositeType(),
  1650. VirtualIndex, ThisAdjustment, Flags, SPFlags,
  1651. Unit, TemplateParams, Declaration,
  1652. RetainedNodes, ThrownTypes));
  1653. EXPECT_NE(N, DISubprogram::get(
  1654. Context, Scope, Name, LinkageName, File, Line, Type,
  1655. ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
  1656. Flags, SPFlags ^ DISubprogram::SPFlagVirtual, Unit,
  1657. TemplateParams, Declaration, RetainedNodes, ThrownTypes));
  1658. EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
  1659. Type, ScopeLine, ContainingType,
  1660. VirtualIndex + 1, ThisAdjustment, Flags,
  1661. SPFlags, Unit, TemplateParams, Declaration,
  1662. RetainedNodes, ThrownTypes));
  1663. EXPECT_NE(N, DISubprogram::get(
  1664. Context, Scope, Name, LinkageName, File, Line, Type,
  1665. ScopeLine, ContainingType, VirtualIndex, ThisAdjustment,
  1666. Flags, SPFlags ^ DISubprogram::SPFlagOptimized, Unit,
  1667. TemplateParams, Declaration, RetainedNodes, ThrownTypes));
  1668. EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
  1669. Type, ScopeLine, ContainingType, VirtualIndex,
  1670. ThisAdjustment, Flags, SPFlags, nullptr,
  1671. TemplateParams, Declaration, RetainedNodes,
  1672. ThrownTypes));
  1673. EXPECT_NE(N,
  1674. DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
  1675. Type, ScopeLine, ContainingType, VirtualIndex,
  1676. ThisAdjustment, Flags, SPFlags, Unit, getTuple(),
  1677. Declaration, RetainedNodes, ThrownTypes));
  1678. EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
  1679. Type, ScopeLine, ContainingType, VirtualIndex,
  1680. ThisAdjustment, Flags, SPFlags, Unit,
  1681. TemplateParams, getSubprogram(), RetainedNodes,
  1682. ThrownTypes));
  1683. EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
  1684. Type, ScopeLine, ContainingType, VirtualIndex,
  1685. ThisAdjustment, Flags, SPFlags, Unit,
  1686. TemplateParams, Declaration, getTuple()));
  1687. EXPECT_NE(N, DISubprogram::get(Context, Scope, Name, LinkageName, File, Line,
  1688. Type, ScopeLine, ContainingType, VirtualIndex,
  1689. ThisAdjustment, Flags, SPFlags, Unit,
  1690. TemplateParams, Declaration, RetainedNodes,
  1691. getTuple()));
  1692. TempDISubprogram Temp = N->clone();
  1693. EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
  1694. }
  1695. typedef MetadataTest DILexicalBlockTest;
  1696. TEST_F(DILexicalBlockTest, get) {
  1697. DILocalScope *Scope = getSubprogram();
  1698. DIFile *File = getFile();
  1699. unsigned Line = 5;
  1700. unsigned Column = 8;
  1701. auto *N = DILexicalBlock::get(Context, Scope, File, Line, Column);
  1702. EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
  1703. EXPECT_EQ(Scope, N->getScope());
  1704. EXPECT_EQ(File, N->getFile());
  1705. EXPECT_EQ(Line, N->getLine());
  1706. EXPECT_EQ(Column, N->getColumn());
  1707. EXPECT_EQ(N, DILexicalBlock::get(Context, Scope, File, Line, Column));
  1708. EXPECT_NE(N,
  1709. DILexicalBlock::get(Context, getSubprogram(), File, Line, Column));
  1710. EXPECT_NE(N, DILexicalBlock::get(Context, Scope, getFile(), Line, Column));
  1711. EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line + 1, Column));
  1712. EXPECT_NE(N, DILexicalBlock::get(Context, Scope, File, Line, Column + 1));
  1713. TempDILexicalBlock Temp = N->clone();
  1714. EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
  1715. }
  1716. TEST_F(DILexicalBlockTest, Overflow) {
  1717. DISubprogram *SP = getSubprogram();
  1718. DIFile *F = getFile();
  1719. {
  1720. auto *LB = DILexicalBlock::get(Context, SP, F, 2, 7);
  1721. EXPECT_EQ(2u, LB->getLine());
  1722. EXPECT_EQ(7u, LB->getColumn());
  1723. }
  1724. unsigned U16 = 1u << 16;
  1725. {
  1726. auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 - 1);
  1727. EXPECT_EQ(UINT32_MAX, LB->getLine());
  1728. EXPECT_EQ(U16 - 1, LB->getColumn());
  1729. }
  1730. {
  1731. auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16);
  1732. EXPECT_EQ(UINT32_MAX, LB->getLine());
  1733. EXPECT_EQ(0u, LB->getColumn());
  1734. }
  1735. {
  1736. auto *LB = DILexicalBlock::get(Context, SP, F, UINT32_MAX, U16 + 1);
  1737. EXPECT_EQ(UINT32_MAX, LB->getLine());
  1738. EXPECT_EQ(0u, LB->getColumn());
  1739. }
  1740. }
  1741. typedef MetadataTest DILexicalBlockFileTest;
  1742. TEST_F(DILexicalBlockFileTest, get) {
  1743. DILocalScope *Scope = getSubprogram();
  1744. DIFile *File = getFile();
  1745. unsigned Discriminator = 5;
  1746. auto *N = DILexicalBlockFile::get(Context, Scope, File, Discriminator);
  1747. EXPECT_EQ(dwarf::DW_TAG_lexical_block, N->getTag());
  1748. EXPECT_EQ(Scope, N->getScope());
  1749. EXPECT_EQ(File, N->getFile());
  1750. EXPECT_EQ(Discriminator, N->getDiscriminator());
  1751. EXPECT_EQ(N, DILexicalBlockFile::get(Context, Scope, File, Discriminator));
  1752. EXPECT_NE(N, DILexicalBlockFile::get(Context, getSubprogram(), File,
  1753. Discriminator));
  1754. EXPECT_NE(N,
  1755. DILexicalBlockFile::get(Context, Scope, getFile(), Discriminator));
  1756. EXPECT_NE(N,
  1757. DILexicalBlockFile::get(Context, Scope, File, Discriminator + 1));
  1758. TempDILexicalBlockFile Temp = N->clone();
  1759. EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
  1760. }
  1761. typedef MetadataTest DINamespaceTest;
  1762. TEST_F(DINamespaceTest, get) {
  1763. DIScope *Scope = getFile();
  1764. StringRef Name = "namespace";
  1765. bool ExportSymbols = true;
  1766. auto *N = DINamespace::get(Context, Scope, Name, ExportSymbols);
  1767. EXPECT_EQ(dwarf::DW_TAG_namespace, N->getTag());
  1768. EXPECT_EQ(Scope, N->getScope());
  1769. EXPECT_EQ(Name, N->getName());
  1770. EXPECT_EQ(N, DINamespace::get(Context, Scope, Name, ExportSymbols));
  1771. EXPECT_NE(N, DINamespace::get(Context, getFile(), Name, ExportSymbols));
  1772. EXPECT_NE(N, DINamespace::get(Context, Scope, "other", ExportSymbols));
  1773. EXPECT_NE(N, DINamespace::get(Context, Scope, Name, !ExportSymbols));
  1774. TempDINamespace Temp = N->clone();
  1775. EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
  1776. }
  1777. typedef MetadataTest DIModuleTest;
  1778. TEST_F(DIModuleTest, get) {
  1779. DIScope *Scope = getFile();
  1780. StringRef Name = "module";
  1781. StringRef ConfigMacro = "-DNDEBUG";
  1782. StringRef Includes = "-I.";
  1783. StringRef Sysroot = "/";
  1784. auto *N = DIModule::get(Context, Scope, Name, ConfigMacro, Includes, Sysroot);
  1785. EXPECT_EQ(dwarf::DW_TAG_module, N->getTag());
  1786. EXPECT_EQ(Scope, N->getScope());
  1787. EXPECT_EQ(Name, N->getName());
  1788. EXPECT_EQ(ConfigMacro, N->getConfigurationMacros());
  1789. EXPECT_EQ(Includes, N->getIncludePath());
  1790. EXPECT_EQ(Sysroot, N->getISysRoot());
  1791. EXPECT_EQ(N, DIModule::get(Context, Scope, Name,
  1792. ConfigMacro, Includes, Sysroot));
  1793. EXPECT_NE(N, DIModule::get(Context, getFile(), Name,
  1794. ConfigMacro, Includes, Sysroot));
  1795. EXPECT_NE(N, DIModule::get(Context, Scope, "other",
  1796. ConfigMacro, Includes, Sysroot));
  1797. EXPECT_NE(N, DIModule::get(Context, Scope, Name,
  1798. "other", Includes, Sysroot));
  1799. EXPECT_NE(N, DIModule::get(Context, Scope, Name,
  1800. ConfigMacro, "other", Sysroot));
  1801. EXPECT_NE(N, DIModule::get(Context, Scope, Name,
  1802. ConfigMacro, Includes, "other"));
  1803. TempDIModule Temp = N->clone();
  1804. EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
  1805. }
  1806. typedef MetadataTest DITemplateTypeParameterTest;
  1807. TEST_F(DITemplateTypeParameterTest, get) {
  1808. StringRef Name = "template";
  1809. DIType *Type = getBasicType("basic");
  1810. auto *N = DITemplateTypeParameter::get(Context, Name, Type);
  1811. EXPECT_EQ(dwarf::DW_TAG_template_type_parameter, N->getTag());
  1812. EXPECT_EQ(Name, N->getName());
  1813. EXPECT_EQ(Type, N->getType());
  1814. EXPECT_EQ(N, DITemplateTypeParameter::get(Context, Name, Type));
  1815. EXPECT_NE(N, DITemplateTypeParameter::get(Context, "other", Type));
  1816. EXPECT_NE(N,
  1817. DITemplateTypeParameter::get(Context, Name, getBasicType("other")));
  1818. TempDITemplateTypeParameter Temp = N->clone();
  1819. EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
  1820. }
  1821. typedef MetadataTest DITemplateValueParameterTest;
  1822. TEST_F(DITemplateValueParameterTest, get) {
  1823. unsigned Tag = dwarf::DW_TAG_template_value_parameter;
  1824. StringRef Name = "template";
  1825. DIType *Type = getBasicType("basic");
  1826. Metadata *Value = getConstantAsMetadata();
  1827. auto *N = DITemplateValueParameter::get(Context, Tag, Name, Type, Value);
  1828. EXPECT_EQ(Tag, N->getTag());
  1829. EXPECT_EQ(Name, N->getName());
  1830. EXPECT_EQ(Type, N->getType());
  1831. EXPECT_EQ(Value, N->getValue());
  1832. EXPECT_EQ(N, DITemplateValueParameter::get(Context, Tag, Name, Type, Value));
  1833. EXPECT_NE(N, DITemplateValueParameter::get(
  1834. Context, dwarf::DW_TAG_GNU_template_template_param, Name,
  1835. Type, Value));
  1836. EXPECT_NE(N,
  1837. DITemplateValueParameter::get(Context, Tag, "other", Type, Value));
  1838. EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name,
  1839. getBasicType("other"), Value));
  1840. EXPECT_NE(N, DITemplateValueParameter::get(Context, Tag, Name, Type,
  1841. getConstantAsMetadata()));
  1842. TempDITemplateValueParameter Temp = N->clone();
  1843. EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
  1844. }
  1845. typedef MetadataTest DIGlobalVariableTest;
  1846. TEST_F(DIGlobalVariableTest, get) {
  1847. DIScope *Scope = getSubprogram();
  1848. StringRef Name = "name";
  1849. StringRef LinkageName = "linkage";
  1850. DIFile *File = getFile();
  1851. unsigned Line = 5;
  1852. DIType *Type = getDerivedType();
  1853. bool IsLocalToUnit = false;
  1854. bool IsDefinition = true;
  1855. MDTuple *templateParams = getTuple();
  1856. DIDerivedType *StaticDataMemberDeclaration =
  1857. cast<DIDerivedType>(getDerivedType());
  1858. uint32_t AlignInBits = 8;
  1859. auto *N = DIGlobalVariable::get(
  1860. Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
  1861. IsDefinition, StaticDataMemberDeclaration, templateParams, AlignInBits);
  1862. EXPECT_EQ(dwarf::DW_TAG_variable, N->getTag());
  1863. EXPECT_EQ(Scope, N->getScope());
  1864. EXPECT_EQ(Name, N->getName());
  1865. EXPECT_EQ(LinkageName, N->getLinkageName());
  1866. EXPECT_EQ(File, N->getFile());
  1867. EXPECT_EQ(Line, N->getLine());
  1868. EXPECT_EQ(Type, N->getType());
  1869. EXPECT_EQ(IsLocalToUnit, N->isLocalToUnit());
  1870. EXPECT_EQ(IsDefinition, N->isDefinition());
  1871. EXPECT_EQ(StaticDataMemberDeclaration, N->getStaticDataMemberDeclaration());
  1872. EXPECT_EQ(templateParams, N->getTemplateParams());
  1873. EXPECT_EQ(AlignInBits, N->getAlignInBits());
  1874. EXPECT_EQ(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
  1875. Line, Type, IsLocalToUnit, IsDefinition,
  1876. StaticDataMemberDeclaration,
  1877. templateParams, AlignInBits));
  1878. EXPECT_NE(N, DIGlobalVariable::get(
  1879. Context, getSubprogram(), Name, LinkageName, File, Line,
  1880. Type, IsLocalToUnit, IsDefinition,
  1881. StaticDataMemberDeclaration, templateParams, AlignInBits));
  1882. EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, "other", LinkageName, File,
  1883. Line, Type, IsLocalToUnit, IsDefinition,
  1884. StaticDataMemberDeclaration,
  1885. templateParams, AlignInBits));
  1886. EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, "other", File, Line,
  1887. Type, IsLocalToUnit, IsDefinition,
  1888. StaticDataMemberDeclaration,
  1889. templateParams, AlignInBits));
  1890. EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName,
  1891. getFile(), Line, Type, IsLocalToUnit,
  1892. IsDefinition, StaticDataMemberDeclaration,
  1893. templateParams, AlignInBits));
  1894. EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
  1895. Line + 1, Type, IsLocalToUnit,
  1896. IsDefinition, StaticDataMemberDeclaration,
  1897. templateParams, AlignInBits));
  1898. EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
  1899. Line, getDerivedType(), IsLocalToUnit,
  1900. IsDefinition, StaticDataMemberDeclaration,
  1901. templateParams, AlignInBits));
  1902. EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
  1903. Line, Type, !IsLocalToUnit, IsDefinition,
  1904. StaticDataMemberDeclaration,
  1905. templateParams, AlignInBits));
  1906. EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
  1907. Line, Type, IsLocalToUnit, !IsDefinition,
  1908. StaticDataMemberDeclaration,
  1909. templateParams, AlignInBits));
  1910. EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
  1911. Line, Type, IsLocalToUnit, IsDefinition,
  1912. cast<DIDerivedType>(getDerivedType()),
  1913. templateParams, AlignInBits));
  1914. EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
  1915. Line, Type, IsLocalToUnit, IsDefinition,
  1916. StaticDataMemberDeclaration, nullptr,
  1917. AlignInBits));
  1918. EXPECT_NE(N, DIGlobalVariable::get(Context, Scope, Name, LinkageName, File,
  1919. Line, Type, IsLocalToUnit, IsDefinition,
  1920. StaticDataMemberDeclaration,
  1921. templateParams, (AlignInBits << 1)));
  1922. TempDIGlobalVariable Temp = N->clone();
  1923. EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
  1924. }
  1925. typedef MetadataTest DIGlobalVariableExpressionTest;
  1926. TEST_F(DIGlobalVariableExpressionTest, get) {
  1927. DIScope *Scope = getSubprogram();
  1928. StringRef Name = "name";
  1929. StringRef LinkageName = "linkage";
  1930. DIFile *File = getFile();
  1931. unsigned Line = 5;
  1932. DIType *Type = getDerivedType();
  1933. bool IsLocalToUnit = false;
  1934. bool IsDefinition = true;
  1935. MDTuple *templateParams = getTuple();
  1936. auto *Expr = DIExpression::get(Context, {1, 2});
  1937. auto *Expr2 = DIExpression::get(Context, {1, 2, 3});
  1938. DIDerivedType *StaticDataMemberDeclaration =
  1939. cast<DIDerivedType>(getDerivedType());
  1940. uint32_t AlignInBits = 8;
  1941. auto *Var = DIGlobalVariable::get(
  1942. Context, Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
  1943. IsDefinition, StaticDataMemberDeclaration, templateParams, AlignInBits);
  1944. auto *Var2 = DIGlobalVariable::get(
  1945. Context, Scope, "other", LinkageName, File, Line, Type, IsLocalToUnit,
  1946. IsDefinition, StaticDataMemberDeclaration, templateParams, AlignInBits);
  1947. auto *N = DIGlobalVariableExpression::get(Context, Var, Expr);
  1948. EXPECT_EQ(Var, N->getVariable());
  1949. EXPECT_EQ(Expr, N->getExpression());
  1950. EXPECT_EQ(N, DIGlobalVariableExpression::get(Context, Var, Expr));
  1951. EXPECT_NE(N, DIGlobalVariableExpression::get(Context, Var2, Expr));
  1952. EXPECT_NE(N, DIGlobalVariableExpression::get(Context, Var, Expr2));
  1953. TempDIGlobalVariableExpression Temp = N->clone();
  1954. EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
  1955. }
  1956. typedef MetadataTest DILocalVariableTest;
  1957. TEST_F(DILocalVariableTest, get) {
  1958. DILocalScope *Scope = getSubprogram();
  1959. StringRef Name = "name";
  1960. DIFile *File = getFile();
  1961. unsigned Line = 5;
  1962. DIType *Type = getDerivedType();
  1963. unsigned Arg = 6;
  1964. DINode::DIFlags Flags = static_cast<DINode::DIFlags>(7);
  1965. uint32_t AlignInBits = 8;
  1966. auto *N =
  1967. DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg, Flags,
  1968. AlignInBits);
  1969. EXPECT_TRUE(N->isParameter());
  1970. EXPECT_EQ(Scope, N->getScope());
  1971. EXPECT_EQ(Name, N->getName());
  1972. EXPECT_EQ(File, N->getFile());
  1973. EXPECT_EQ(Line, N->getLine());
  1974. EXPECT_EQ(Type, N->getType());
  1975. EXPECT_EQ(Arg, N->getArg());
  1976. EXPECT_EQ(Flags, N->getFlags());
  1977. EXPECT_EQ(AlignInBits, N->getAlignInBits());
  1978. EXPECT_EQ(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type, Arg,
  1979. Flags, AlignInBits));
  1980. EXPECT_FALSE(
  1981. DILocalVariable::get(Context, Scope, Name, File, Line, Type, 0, Flags,
  1982. AlignInBits)->isParameter());
  1983. EXPECT_NE(N, DILocalVariable::get(Context, getSubprogram(), Name, File, Line,
  1984. Type, Arg, Flags, AlignInBits));
  1985. EXPECT_NE(N, DILocalVariable::get(Context, Scope, "other", File, Line, Type,
  1986. Arg, Flags, AlignInBits));
  1987. EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, getFile(), Line, Type,
  1988. Arg, Flags, AlignInBits));
  1989. EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line + 1, Type,
  1990. Arg, Flags, AlignInBits));
  1991. EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line,
  1992. getDerivedType(), Arg, Flags, AlignInBits));
  1993. EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type,
  1994. Arg + 1, Flags, AlignInBits));
  1995. EXPECT_NE(N, DILocalVariable::get(Context, Scope, Name, File, Line, Type,
  1996. Arg, Flags, (AlignInBits << 1)));
  1997. TempDILocalVariable Temp = N->clone();
  1998. EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
  1999. }
  2000. TEST_F(DILocalVariableTest, getArg256) {
  2001. EXPECT_EQ(255u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
  2002. 0, nullptr, 255, DINode::FlagZero, 0)
  2003. ->getArg());
  2004. EXPECT_EQ(256u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
  2005. 0, nullptr, 256, DINode::FlagZero, 0)
  2006. ->getArg());
  2007. EXPECT_EQ(257u, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
  2008. 0, nullptr, 257, DINode::FlagZero, 0)
  2009. ->getArg());
  2010. unsigned Max = UINT16_MAX;
  2011. EXPECT_EQ(Max, DILocalVariable::get(Context, getSubprogram(), "", getFile(),
  2012. 0, nullptr, Max, DINode::FlagZero, 0)
  2013. ->getArg());
  2014. }
  2015. typedef MetadataTest DIExpressionTest;
  2016. TEST_F(DIExpressionTest, get) {
  2017. uint64_t Elements[] = {2, 6, 9, 78, 0};
  2018. auto *N = DIExpression::get(Context, Elements);
  2019. EXPECT_EQ(makeArrayRef(Elements), N->getElements());
  2020. EXPECT_EQ(N, DIExpression::get(Context, Elements));
  2021. EXPECT_EQ(5u, N->getNumElements());
  2022. EXPECT_EQ(2u, N->getElement(0));
  2023. EXPECT_EQ(6u, N->getElement(1));
  2024. EXPECT_EQ(9u, N->getElement(2));
  2025. EXPECT_EQ(78u, N->getElement(3));
  2026. EXPECT_EQ(0u, N->getElement(4));
  2027. TempDIExpression Temp = N->clone();
  2028. EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
  2029. // Test DIExpression::prepend().
  2030. uint64_t Elts0[] = {dwarf::DW_OP_LLVM_fragment, 0, 32};
  2031. auto *N0 = DIExpression::get(Context, Elts0);
  2032. uint8_t DIExprFlags = DIExpression::ApplyOffset;
  2033. DIExprFlags |= DIExpression::DerefBefore;
  2034. DIExprFlags |= DIExpression::DerefAfter;
  2035. DIExprFlags |= DIExpression::StackValue;
  2036. auto *N0WithPrependedOps = DIExpression::prepend(N0, DIExprFlags, 64);
  2037. uint64_t Elts1[] = {dwarf::DW_OP_deref,
  2038. dwarf::DW_OP_plus_uconst, 64,
  2039. dwarf::DW_OP_deref,
  2040. dwarf::DW_OP_stack_value,
  2041. dwarf::DW_OP_LLVM_fragment, 0, 32};
  2042. auto *N1 = DIExpression::get(Context, Elts1);
  2043. EXPECT_EQ(N0WithPrependedOps, N1);
  2044. // Test DIExpression::append().
  2045. uint64_t Elts2[] = {dwarf::DW_OP_deref, dwarf::DW_OP_plus_uconst, 64,
  2046. dwarf::DW_OP_deref, dwarf::DW_OP_stack_value};
  2047. auto *N2 = DIExpression::append(N0, Elts2);
  2048. EXPECT_EQ(N0WithPrependedOps, N2);
  2049. }
  2050. TEST_F(DIExpressionTest, isValid) {
  2051. #define EXPECT_VALID(...) \
  2052. do { \
  2053. uint64_t Elements[] = {__VA_ARGS__}; \
  2054. EXPECT_TRUE(DIExpression::get(Context, Elements)->isValid()); \
  2055. } while (false)
  2056. #define EXPECT_INVALID(...) \
  2057. do { \
  2058. uint64_t Elements[] = {__VA_ARGS__}; \
  2059. EXPECT_FALSE(DIExpression::get(Context, Elements)->isValid()); \
  2060. } while (false)
  2061. // Empty expression should be valid.
  2062. EXPECT_TRUE(DIExpression::get(Context, None));
  2063. // Valid constructions.
  2064. EXPECT_VALID(dwarf::DW_OP_plus_uconst, 6);
  2065. EXPECT_VALID(dwarf::DW_OP_constu, 6, dwarf::DW_OP_plus);
  2066. EXPECT_VALID(dwarf::DW_OP_deref);
  2067. EXPECT_VALID(dwarf::DW_OP_LLVM_fragment, 3, 7);
  2068. EXPECT_VALID(dwarf::DW_OP_plus_uconst, 6, dwarf::DW_OP_deref);
  2069. EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus_uconst, 6);
  2070. EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_LLVM_fragment, 3, 7);
  2071. EXPECT_VALID(dwarf::DW_OP_deref, dwarf::DW_OP_plus_uconst, 6,
  2072. dwarf::DW_OP_LLVM_fragment, 3, 7);
  2073. // Invalid constructions.
  2074. EXPECT_INVALID(~0u);
  2075. EXPECT_INVALID(dwarf::DW_OP_plus, 0);
  2076. EXPECT_INVALID(dwarf::DW_OP_plus_uconst);
  2077. EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment);
  2078. EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment, 3);
  2079. EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment, 3, 7, dwarf::DW_OP_plus_uconst, 3);
  2080. EXPECT_INVALID(dwarf::DW_OP_LLVM_fragment, 3, 7, dwarf::DW_OP_deref);
  2081. #undef EXPECT_VALID
  2082. #undef EXPECT_INVALID
  2083. }
  2084. typedef MetadataTest DIObjCPropertyTest;
  2085. TEST_F(DIObjCPropertyTest, get) {
  2086. StringRef Name = "name";
  2087. DIFile *File = getFile();
  2088. unsigned Line = 5;
  2089. StringRef GetterName = "getter";
  2090. StringRef SetterName = "setter";
  2091. unsigned Attributes = 7;
  2092. DIType *Type = getBasicType("basic");
  2093. auto *N = DIObjCProperty::get(Context, Name, File, Line, GetterName,
  2094. SetterName, Attributes, Type);
  2095. EXPECT_EQ(dwarf::DW_TAG_APPLE_property, N->getTag());
  2096. EXPECT_EQ(Name, N->getName());
  2097. EXPECT_EQ(File, N->getFile());
  2098. EXPECT_EQ(Line, N->getLine());
  2099. EXPECT_EQ(GetterName, N->getGetterName());
  2100. EXPECT_EQ(SetterName, N->getSetterName());
  2101. EXPECT_EQ(Attributes, N->getAttributes());
  2102. EXPECT_EQ(Type, N->getType());
  2103. EXPECT_EQ(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
  2104. SetterName, Attributes, Type));
  2105. EXPECT_NE(N, DIObjCProperty::get(Context, "other", File, Line, GetterName,
  2106. SetterName, Attributes, Type));
  2107. EXPECT_NE(N, DIObjCProperty::get(Context, Name, getFile(), Line, GetterName,
  2108. SetterName, Attributes, Type));
  2109. EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line + 1, GetterName,
  2110. SetterName, Attributes, Type));
  2111. EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, "other",
  2112. SetterName, Attributes, Type));
  2113. EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
  2114. "other", Attributes, Type));
  2115. EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
  2116. SetterName, Attributes + 1, Type));
  2117. EXPECT_NE(N, DIObjCProperty::get(Context, Name, File, Line, GetterName,
  2118. SetterName, Attributes,
  2119. getBasicType("other")));
  2120. TempDIObjCProperty Temp = N->clone();
  2121. EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
  2122. }
  2123. typedef MetadataTest DIImportedEntityTest;
  2124. TEST_F(DIImportedEntityTest, get) {
  2125. unsigned Tag = dwarf::DW_TAG_imported_module;
  2126. DIScope *Scope = getSubprogram();
  2127. DINode *Entity = getCompositeType();
  2128. DIFile *File = getFile();
  2129. unsigned Line = 5;
  2130. StringRef Name = "name";
  2131. auto *N =
  2132. DIImportedEntity::get(Context, Tag, Scope, Entity, File, Line, Name);
  2133. EXPECT_EQ(Tag, N->getTag());
  2134. EXPECT_EQ(Scope, N->getScope());
  2135. EXPECT_EQ(Entity, N->getEntity());
  2136. EXPECT_EQ(File, N->getFile());
  2137. EXPECT_EQ(Line, N->getLine());
  2138. EXPECT_EQ(Name, N->getName());
  2139. EXPECT_EQ(
  2140. N, DIImportedEntity::get(Context, Tag, Scope, Entity, File, Line, Name));
  2141. EXPECT_NE(N,
  2142. DIImportedEntity::get(Context, dwarf::DW_TAG_imported_declaration,
  2143. Scope, Entity, File, Line, Name));
  2144. EXPECT_NE(N, DIImportedEntity::get(Context, Tag, getSubprogram(), Entity,
  2145. File, Line, Name));
  2146. EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, getCompositeType(),
  2147. File, Line, Name));
  2148. EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, Entity, nullptr, Line,
  2149. Name));
  2150. EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, Entity, File,
  2151. Line + 1, Name));
  2152. EXPECT_NE(N, DIImportedEntity::get(Context, Tag, Scope, Entity, File, Line,
  2153. "other"));
  2154. TempDIImportedEntity Temp = N->clone();
  2155. EXPECT_EQ(N, MDNode::replaceWithUniqued(std::move(Temp)));
  2156. }
  2157. typedef MetadataTest MetadataAsValueTest;
  2158. TEST_F(MetadataAsValueTest, MDNode) {
  2159. MDNode *N = MDNode::get(Context, None);
  2160. auto *V = MetadataAsValue::get(Context, N);
  2161. EXPECT_TRUE(V->getType()->isMetadataTy());
  2162. EXPECT_EQ(N, V->getMetadata());
  2163. auto *V2 = MetadataAsValue::get(Context, N);
  2164. EXPECT_EQ(V, V2);
  2165. }
  2166. TEST_F(MetadataAsValueTest, MDNodeMDNode) {
  2167. MDNode *N = MDNode::get(Context, None);
  2168. Metadata *Ops[] = {N};
  2169. MDNode *N2 = MDNode::get(Context, Ops);
  2170. auto *V = MetadataAsValue::get(Context, N2);
  2171. EXPECT_TRUE(V->getType()->isMetadataTy());
  2172. EXPECT_EQ(N2, V->getMetadata());
  2173. auto *V2 = MetadataAsValue::get(Context, N2);
  2174. EXPECT_EQ(V, V2);
  2175. auto *V3 = MetadataAsValue::get(Context, N);
  2176. EXPECT_TRUE(V3->getType()->isMetadataTy());
  2177. EXPECT_NE(V, V3);
  2178. EXPECT_EQ(N, V3->getMetadata());
  2179. }
  2180. TEST_F(MetadataAsValueTest, MDNodeConstant) {
  2181. auto *C = ConstantInt::getTrue(Context);
  2182. auto *MD = ConstantAsMetadata::get(C);
  2183. Metadata *Ops[] = {MD};
  2184. auto *N = MDNode::get(Context, Ops);
  2185. auto *V = MetadataAsValue::get(Context, MD);
  2186. EXPECT_TRUE(V->getType()->isMetadataTy());
  2187. EXPECT_EQ(MD, V->getMetadata());
  2188. auto *V2 = MetadataAsValue::get(Context, N);
  2189. EXPECT_EQ(MD, V2->getMetadata());
  2190. EXPECT_EQ(V, V2);
  2191. }
  2192. typedef MetadataTest ValueAsMetadataTest;
  2193. TEST_F(ValueAsMetadataTest, UpdatesOnRAUW) {
  2194. Type *Ty = Type::getInt1PtrTy(Context);
  2195. std::unique_ptr<GlobalVariable> GV0(
  2196. new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
  2197. auto *MD = ValueAsMetadata::get(GV0.get());
  2198. EXPECT_TRUE(MD->getValue() == GV0.get());
  2199. ASSERT_TRUE(GV0->use_empty());
  2200. std::unique_ptr<GlobalVariable> GV1(
  2201. new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
  2202. GV0->replaceAllUsesWith(GV1.get());
  2203. EXPECT_TRUE(MD->getValue() == GV1.get());
  2204. }
  2205. TEST_F(ValueAsMetadataTest, TempTempReplacement) {
  2206. // Create a constant.
  2207. ConstantAsMetadata *CI =
  2208. ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
  2209. auto Temp1 = MDTuple::getTemporary(Context, None);
  2210. auto Temp2 = MDTuple::getTemporary(Context, {CI});
  2211. auto *N = MDTuple::get(Context, {Temp1.get()});
  2212. // Test replacing a temporary node with another temporary node.
  2213. Temp1->replaceAllUsesWith(Temp2.get());
  2214. EXPECT_EQ(N->getOperand(0), Temp2.get());
  2215. // Clean up Temp2 for teardown.
  2216. Temp2->replaceAllUsesWith(nullptr);
  2217. }
  2218. TEST_F(ValueAsMetadataTest, CollidingDoubleUpdates) {
  2219. // Create a constant.
  2220. ConstantAsMetadata *CI =
  2221. ConstantAsMetadata::get(ConstantInt::get(Context, APInt(8, 0)));
  2222. // Create a temporary to prevent nodes from resolving.
  2223. auto Temp = MDTuple::getTemporary(Context, None);
  2224. // When the first operand of N1 gets reset to nullptr, it'll collide with N2.
  2225. Metadata *Ops1[] = {CI, CI, Temp.get()};
  2226. Metadata *Ops2[] = {nullptr, CI, Temp.get()};
  2227. auto *N1 = MDTuple::get(Context, Ops1);
  2228. auto *N2 = MDTuple::get(Context, Ops2);
  2229. ASSERT_NE(N1, N2);
  2230. // Tell metadata that the constant is getting deleted.
  2231. //
  2232. // After this, N1 will be invalid, so don't touch it.
  2233. ValueAsMetadata::handleDeletion(CI->getValue());
  2234. EXPECT_EQ(nullptr, N2->getOperand(0));
  2235. EXPECT_EQ(nullptr, N2->getOperand(1));
  2236. EXPECT_EQ(Temp.get(), N2->getOperand(2));
  2237. // Clean up Temp for teardown.
  2238. Temp->replaceAllUsesWith(nullptr);
  2239. }
  2240. typedef MetadataTest TrackingMDRefTest;
  2241. TEST_F(TrackingMDRefTest, UpdatesOnRAUW) {
  2242. Type *Ty = Type::getInt1PtrTy(Context);
  2243. std::unique_ptr<GlobalVariable> GV0(
  2244. new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
  2245. TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV0.get()));
  2246. EXPECT_TRUE(MD->getValue() == GV0.get());
  2247. ASSERT_TRUE(GV0->use_empty());
  2248. std::unique_ptr<GlobalVariable> GV1(
  2249. new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
  2250. GV0->replaceAllUsesWith(GV1.get());
  2251. EXPECT_TRUE(MD->getValue() == GV1.get());
  2252. // Reset it, so we don't inadvertently test deletion.
  2253. MD.reset();
  2254. }
  2255. TEST_F(TrackingMDRefTest, UpdatesOnDeletion) {
  2256. Type *Ty = Type::getInt1PtrTy(Context);
  2257. std::unique_ptr<GlobalVariable> GV(
  2258. new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage));
  2259. TypedTrackingMDRef<ValueAsMetadata> MD(ValueAsMetadata::get(GV.get()));
  2260. EXPECT_TRUE(MD->getValue() == GV.get());
  2261. ASSERT_TRUE(GV->use_empty());
  2262. GV.reset();
  2263. EXPECT_TRUE(!MD);
  2264. }
  2265. TEST(NamedMDNodeTest, Search) {
  2266. LLVMContext Context;
  2267. ConstantAsMetadata *C =
  2268. ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 1));
  2269. ConstantAsMetadata *C2 =
  2270. ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Context), 2));
  2271. Metadata *const V = C;
  2272. Metadata *const V2 = C2;
  2273. MDNode *n = MDNode::get(Context, V);
  2274. MDNode *n2 = MDNode::get(Context, V2);
  2275. Module M("MyModule", Context);
  2276. const char *Name = "llvm.NMD1";
  2277. NamedMDNode *NMD = M.getOrInsertNamedMetadata(Name);
  2278. NMD->addOperand(n);
  2279. NMD->addOperand(n2);
  2280. std::string Str;
  2281. raw_string_ostream oss(Str);
  2282. NMD->print(oss);
  2283. EXPECT_STREQ("!llvm.NMD1 = !{!0, !1}\n",
  2284. oss.str().c_str());
  2285. }
  2286. typedef MetadataTest FunctionAttachmentTest;
  2287. TEST_F(FunctionAttachmentTest, setMetadata) {
  2288. Function *F = getFunction("foo");
  2289. ASSERT_FALSE(F->hasMetadata());
  2290. EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
  2291. EXPECT_EQ(nullptr, F->getMetadata("dbg"));
  2292. EXPECT_EQ(nullptr, F->getMetadata("other"));
  2293. DISubprogram *SP1 = getSubprogram();
  2294. DISubprogram *SP2 = getSubprogram();
  2295. ASSERT_NE(SP1, SP2);
  2296. F->setMetadata("dbg", SP1);
  2297. EXPECT_TRUE(F->hasMetadata());
  2298. EXPECT_EQ(SP1, F->getMetadata(LLVMContext::MD_dbg));
  2299. EXPECT_EQ(SP1, F->getMetadata("dbg"));
  2300. EXPECT_EQ(nullptr, F->getMetadata("other"));
  2301. F->setMetadata(LLVMContext::MD_dbg, SP2);
  2302. EXPECT_TRUE(F->hasMetadata());
  2303. EXPECT_EQ(SP2, F->getMetadata(LLVMContext::MD_dbg));
  2304. EXPECT_EQ(SP2, F->getMetadata("dbg"));
  2305. EXPECT_EQ(nullptr, F->getMetadata("other"));
  2306. F->setMetadata("dbg", nullptr);
  2307. EXPECT_FALSE(F->hasMetadata());
  2308. EXPECT_EQ(nullptr, F->getMetadata(LLVMContext::MD_dbg));
  2309. EXPECT_EQ(nullptr, F->getMetadata("dbg"));
  2310. EXPECT_EQ(nullptr, F->getMetadata("other"));
  2311. MDTuple *T1 = getTuple();
  2312. MDTuple *T2 = getTuple();
  2313. ASSERT_NE(T1, T2);
  2314. F->setMetadata("other1", T1);
  2315. F->setMetadata("other2", T2);
  2316. EXPECT_TRUE(F->hasMetadata());
  2317. EXPECT_EQ(T1, F->getMetadata("other1"));
  2318. EXPECT_EQ(T2, F->getMetadata("other2"));
  2319. EXPECT_EQ(nullptr, F->getMetadata("dbg"));
  2320. F->setMetadata("other1", T2);
  2321. F->setMetadata("other2", T1);
  2322. EXPECT_EQ(T2, F->getMetadata("other1"));
  2323. EXPECT_EQ(T1, F->getMetadata("other2"));
  2324. F->setMetadata("other1", nullptr);
  2325. F->setMetadata("other2", nullptr);
  2326. EXPECT_FALSE(F->hasMetadata());
  2327. EXPECT_EQ(nullptr, F->getMetadata("other1"));
  2328. EXPECT_EQ(nullptr, F->getMetadata("other2"));
  2329. }
  2330. TEST_F(FunctionAttachmentTest, getAll) {
  2331. Function *F = getFunction("foo");
  2332. MDTuple *T1 = getTuple();
  2333. MDTuple *T2 = getTuple();
  2334. MDTuple *P = getTuple();
  2335. DISubprogram *SP = getSubprogram();
  2336. F->setMetadata("other1", T2);
  2337. F->setMetadata(LLVMContext::MD_dbg, SP);
  2338. F->setMetadata("other2", T1);
  2339. F->setMetadata(LLVMContext::MD_prof, P);
  2340. F->setMetadata("other2", T2);
  2341. F->setMetadata("other1", T1);
  2342. SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
  2343. F->getAllMetadata(MDs);
  2344. ASSERT_EQ(4u, MDs.size());
  2345. EXPECT_EQ(LLVMContext::MD_dbg, MDs[0].first);
  2346. EXPECT_EQ(LLVMContext::MD_prof, MDs[1].first);
  2347. EXPECT_EQ(Context.getMDKindID("other1"), MDs[2].first);
  2348. EXPECT_EQ(Context.getMDKindID("other2"), MDs[3].first);
  2349. EXPECT_EQ(SP, MDs[0].second);
  2350. EXPECT_EQ(P, MDs[1].second);
  2351. EXPECT_EQ(T1, MDs[2].second);
  2352. EXPECT_EQ(T2, MDs[3].second);
  2353. }
  2354. TEST_F(FunctionAttachmentTest, Verifier) {
  2355. Function *F = getFunction("foo");
  2356. F->setMetadata("attach", getTuple());
  2357. F->setIsMaterializable(true);
  2358. // Confirm this is materializable.
  2359. ASSERT_TRUE(F->isMaterializable());
  2360. // Materializable functions cannot have metadata attachments.
  2361. EXPECT_TRUE(verifyFunction(*F));
  2362. // Function declarations can.
  2363. F->setIsMaterializable(false);
  2364. EXPECT_FALSE(verifyModule(*F->getParent()));
  2365. EXPECT_FALSE(verifyFunction(*F));
  2366. // So can definitions.
  2367. (void)new UnreachableInst(Context, BasicBlock::Create(Context, "bb", F));
  2368. EXPECT_FALSE(verifyModule(*F->getParent()));
  2369. EXPECT_FALSE(verifyFunction(*F));
  2370. }
  2371. TEST_F(FunctionAttachmentTest, EntryCount) {
  2372. Function *F = getFunction("foo");
  2373. EXPECT_FALSE(F->getEntryCount().hasValue());
  2374. F->setEntryCount(12304, Function::PCT_Real);
  2375. auto Count = F->getEntryCount();
  2376. EXPECT_TRUE(Count.hasValue());
  2377. EXPECT_EQ(12304u, Count.getCount());
  2378. EXPECT_EQ(Function::PCT_Real, Count.getType());
  2379. // Repeat the same for synthetic counts.
  2380. F = getFunction("bar");
  2381. EXPECT_FALSE(F->getEntryCount().hasValue());
  2382. F->setEntryCount(123, Function::PCT_Synthetic);
  2383. Count = F->getEntryCount(true /*allow synthetic*/);
  2384. EXPECT_TRUE(Count.hasValue());
  2385. EXPECT_EQ(123u, Count.getCount());
  2386. EXPECT_EQ(Function::PCT_Synthetic, Count.getType());
  2387. }
  2388. TEST_F(FunctionAttachmentTest, SubprogramAttachment) {
  2389. Function *F = getFunction("foo");
  2390. DISubprogram *SP = getSubprogram();
  2391. F->setSubprogram(SP);
  2392. // Note that the static_cast confirms that F->getSubprogram() actually
  2393. // returns an DISubprogram.
  2394. EXPECT_EQ(SP, static_cast<DISubprogram *>(F->getSubprogram()));
  2395. EXPECT_EQ(SP, F->getMetadata("dbg"));
  2396. EXPECT_EQ(SP, F->getMetadata(LLVMContext::MD_dbg));
  2397. }
  2398. typedef MetadataTest DistinctMDOperandPlaceholderTest;
  2399. TEST_F(DistinctMDOperandPlaceholderTest, getID) {
  2400. EXPECT_EQ(7u, DistinctMDOperandPlaceholder(7).getID());
  2401. }
  2402. TEST_F(DistinctMDOperandPlaceholderTest, replaceUseWith) {
  2403. // Set up some placeholders.
  2404. DistinctMDOperandPlaceholder PH0(7);
  2405. DistinctMDOperandPlaceholder PH1(3);
  2406. DistinctMDOperandPlaceholder PH2(0);
  2407. Metadata *Ops[] = {&PH0, &PH1, &PH2};
  2408. auto *D = MDTuple::getDistinct(Context, Ops);
  2409. ASSERT_EQ(&PH0, D->getOperand(0));
  2410. ASSERT_EQ(&PH1, D->getOperand(1));
  2411. ASSERT_EQ(&PH2, D->getOperand(2));
  2412. // Replace them.
  2413. auto *N0 = MDTuple::get(Context, None);
  2414. auto *N1 = MDTuple::get(Context, N0);
  2415. PH0.replaceUseWith(N0);
  2416. PH1.replaceUseWith(N1);
  2417. PH2.replaceUseWith(nullptr);
  2418. EXPECT_EQ(N0, D->getOperand(0));
  2419. EXPECT_EQ(N1, D->getOperand(1));
  2420. EXPECT_EQ(nullptr, D->getOperand(2));
  2421. }
  2422. TEST_F(DistinctMDOperandPlaceholderTest, replaceUseWithNoUser) {
  2423. // There is no user, but we can still call replace.
  2424. DistinctMDOperandPlaceholder(7).replaceUseWith(MDTuple::get(Context, None));
  2425. }
  2426. // Test various assertions in metadata tracking. Don't run these tests if gtest
  2427. // will use SEH to recover from them. Two of these tests get halfway through
  2428. // inserting metadata into DenseMaps for tracking purposes, and then they
  2429. // assert, and we attempt to destroy an LLVMContext with broken invariants,
  2430. // leading to infinite loops.
  2431. #if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG) && !defined(GTEST_HAS_SEH)
  2432. TEST_F(DistinctMDOperandPlaceholderTest, MetadataAsValue) {
  2433. // This shouldn't crash.
  2434. DistinctMDOperandPlaceholder PH(7);
  2435. EXPECT_DEATH(MetadataAsValue::get(Context, &PH),
  2436. "Unexpected callback to owner");
  2437. }
  2438. TEST_F(DistinctMDOperandPlaceholderTest, UniquedMDNode) {
  2439. // This shouldn't crash.
  2440. DistinctMDOperandPlaceholder PH(7);
  2441. EXPECT_DEATH(MDTuple::get(Context, &PH), "Unexpected callback to owner");
  2442. }
  2443. TEST_F(DistinctMDOperandPlaceholderTest, SecondDistinctMDNode) {
  2444. // This shouldn't crash.
  2445. DistinctMDOperandPlaceholder PH(7);
  2446. MDTuple::getDistinct(Context, &PH);
  2447. EXPECT_DEATH(MDTuple::getDistinct(Context, &PH),
  2448. "Placeholders can only be used once");
  2449. }
  2450. TEST_F(DistinctMDOperandPlaceholderTest, TrackingMDRefAndDistinctMDNode) {
  2451. // TrackingMDRef doesn't install an owner callback, so it can't be detected
  2452. // as an invalid use. However, using a placeholder in a TrackingMDRef *and*
  2453. // a distinct node isn't possible and we should assert.
  2454. //
  2455. // (There's no positive test for using TrackingMDRef because it's not a
  2456. // useful thing to do.)
  2457. {
  2458. DistinctMDOperandPlaceholder PH(7);
  2459. MDTuple::getDistinct(Context, &PH);
  2460. EXPECT_DEATH(TrackingMDRef Ref(&PH), "Placeholders can only be used once");
  2461. }
  2462. {
  2463. DistinctMDOperandPlaceholder PH(7);
  2464. TrackingMDRef Ref(&PH);
  2465. EXPECT_DEATH(MDTuple::getDistinct(Context, &PH),
  2466. "Placeholders can only be used once");
  2467. }
  2468. }
  2469. #endif
  2470. } // end namespace