MultilibTest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. //===- unittests/Driver/MultilibTest.cpp --- Multilib 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. //
  9. // Unit tests for Multilib and MultilibSet
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Driver/Multilib.h"
  13. #include "clang/Basic/LLVM.h"
  14. #include "llvm/ADT/StringRef.h"
  15. #include "llvm/ADT/StringSwitch.h"
  16. #include "gtest/gtest.h"
  17. using namespace clang::driver;
  18. using namespace clang;
  19. TEST(MultilibTest, MultilibValidity) {
  20. ASSERT_TRUE(Multilib().isValid()) << "Empty multilib is not valid";
  21. ASSERT_TRUE(Multilib().flag("+foo").isValid())
  22. << "Single indicative flag is not valid";
  23. ASSERT_TRUE(Multilib().flag("-foo").isValid())
  24. << "Single contraindicative flag is not valid";
  25. ASSERT_FALSE(Multilib().flag("+foo").flag("-foo").isValid())
  26. << "Conflicting flags should invalidate the Multilib";
  27. ASSERT_TRUE(Multilib().flag("+foo").flag("+foo").isValid())
  28. << "Multilib should be valid even if it has the same flag twice";
  29. ASSERT_TRUE(Multilib().flag("+foo").flag("-foobar").isValid())
  30. << "Seemingly conflicting prefixes shouldn't actually conflict";
  31. }
  32. TEST(MultilibTest, OpEqReflexivity1) {
  33. Multilib M;
  34. ASSERT_TRUE(M == M) << "Multilib::operator==() is not reflexive";
  35. }
  36. TEST(MultilibTest, OpEqReflexivity2) {
  37. ASSERT_TRUE(Multilib() == Multilib())
  38. << "Separately constructed default multilibs are not equal";
  39. }
  40. TEST(MultilibTest, OpEqReflexivity3) {
  41. Multilib M1, M2;
  42. M1.flag("+foo");
  43. M2.flag("+foo");
  44. ASSERT_TRUE(M1 == M2) << "Multilibs with the same flag should be the same";
  45. }
  46. TEST(MultilibTest, OpEqInequivalence1) {
  47. Multilib M1, M2;
  48. M1.flag("+foo");
  49. M2.flag("-foo");
  50. ASSERT_FALSE(M1 == M2) << "Multilibs with conflicting flags are not the same";
  51. ASSERT_FALSE(M2 == M1)
  52. << "Multilibs with conflicting flags are not the same (commuted)";
  53. }
  54. TEST(MultilibTest, OpEqInequivalence2) {
  55. Multilib M1, M2;
  56. M2.flag("+foo");
  57. ASSERT_FALSE(M1 == M2) << "Flags make Multilibs different";
  58. }
  59. TEST(MultilibTest, OpEqEquivalence1) {
  60. Multilib M1, M2;
  61. M1.flag("+foo");
  62. M2.flag("+foo").flag("+foo");
  63. ASSERT_TRUE(M1 == M2) << "Flag duplication shouldn't affect equivalence";
  64. ASSERT_TRUE(M2 == M1)
  65. << "Flag duplication shouldn't affect equivalence (commuted)";
  66. }
  67. TEST(MultilibTest, OpEqEquivalence2) {
  68. Multilib M1("64");
  69. Multilib M2;
  70. M2.gccSuffix("/64");
  71. ASSERT_TRUE(M1 == M2)
  72. << "Constructor argument must match Multilib::gccSuffix()";
  73. ASSERT_TRUE(M2 == M1)
  74. << "Constructor argument must match Multilib::gccSuffix() (commuted)";
  75. }
  76. TEST(MultilibTest, OpEqEquivalence3) {
  77. Multilib M1("", "32");
  78. Multilib M2;
  79. M2.osSuffix("/32");
  80. ASSERT_TRUE(M1 == M2)
  81. << "Constructor argument must match Multilib::osSuffix()";
  82. ASSERT_TRUE(M2 == M1)
  83. << "Constructor argument must match Multilib::osSuffix() (commuted)";
  84. }
  85. TEST(MultilibTest, OpEqEquivalence4) {
  86. Multilib M1("", "", "16");
  87. Multilib M2;
  88. M2.includeSuffix("/16");
  89. ASSERT_TRUE(M1 == M2)
  90. << "Constructor argument must match Multilib::includeSuffix()";
  91. ASSERT_TRUE(M2 == M1)
  92. << "Constructor argument must match Multilib::includeSuffix() (commuted)";
  93. }
  94. TEST(MultilibTest, OpEqInequivalence3) {
  95. Multilib M1("foo");
  96. Multilib M2("bar");
  97. ASSERT_FALSE(M1 == M2) << "Differing gccSuffixes should be different";
  98. ASSERT_FALSE(M2 == M1)
  99. << "Differing gccSuffixes should be different (commuted)";
  100. }
  101. TEST(MultilibTest, OpEqInequivalence4) {
  102. Multilib M1("", "foo");
  103. Multilib M2("", "bar");
  104. ASSERT_FALSE(M1 == M2) << "Differing osSuffixes should be different";
  105. ASSERT_FALSE(M2 == M1)
  106. << "Differing osSuffixes should be different (commuted)";
  107. }
  108. TEST(MultilibTest, OpEqInequivalence5) {
  109. Multilib M1("", "", "foo");
  110. Multilib M2("", "", "bar");
  111. ASSERT_FALSE(M1 == M2) << "Differing includeSuffixes should be different";
  112. ASSERT_FALSE(M2 == M1)
  113. << "Differing includeSuffixes should be different (commuted)";
  114. }
  115. TEST(MultilibTest, Construction1) {
  116. Multilib M("gcc64", "os64", "inc64");
  117. ASSERT_TRUE(M.gccSuffix() == "/gcc64");
  118. ASSERT_TRUE(M.osSuffix() == "/os64");
  119. ASSERT_TRUE(M.includeSuffix() == "/inc64");
  120. }
  121. TEST(MultilibTest, Construction2) {
  122. Multilib M1;
  123. Multilib M2("");
  124. Multilib M3("", "");
  125. Multilib M4("", "", "");
  126. ASSERT_TRUE(M1 == M2)
  127. << "Default arguments to Multilib constructor broken (first argument)";
  128. ASSERT_TRUE(M1 == M3)
  129. << "Default arguments to Multilib constructor broken (second argument)";
  130. ASSERT_TRUE(M1 == M4)
  131. << "Default arguments to Multilib constructor broken (third argument)";
  132. }
  133. TEST(MultilibTest, Construction3) {
  134. Multilib M = Multilib().flag("+f1").flag("+f2").flag("-f3");
  135. for (Multilib::flags_list::const_iterator I = M.flags().begin(),
  136. E = M.flags().end();
  137. I != E; ++I) {
  138. ASSERT_TRUE(llvm::StringSwitch<bool>(*I)
  139. .Cases("+f1", "+f2", "-f3", true)
  140. .Default(false));
  141. }
  142. }
  143. static bool hasFlag(const Multilib &M, StringRef Flag) {
  144. for (Multilib::flags_list::const_iterator I = M.flags().begin(),
  145. E = M.flags().end();
  146. I != E; ++I) {
  147. if (*I == Flag)
  148. return true;
  149. else if (StringRef(*I).substr(1) == Flag.substr(1))
  150. return false;
  151. }
  152. return false;
  153. }
  154. TEST(MultilibTest, SetConstruction1) {
  155. // Single maybe
  156. MultilibSet MS;
  157. ASSERT_TRUE(MS.size() == 0);
  158. MS.Maybe(Multilib("64").flag("+m64"));
  159. ASSERT_TRUE(MS.size() == 2);
  160. for (MultilibSet::const_iterator I = MS.begin(), E = MS.end(); I != E; ++I) {
  161. if (I->gccSuffix() == "/64")
  162. ASSERT_TRUE(I->flags()[0] == "+m64");
  163. else if (I->gccSuffix() == "")
  164. ASSERT_TRUE(I->flags()[0] == "-m64");
  165. else
  166. FAIL() << "Unrecognized gccSufix: " << I->gccSuffix();
  167. }
  168. }
  169. TEST(MultilibTest, SetConstruction2) {
  170. // Double maybe
  171. MultilibSet MS;
  172. MS.Maybe(Multilib("sof").flag("+sof"));
  173. MS.Maybe(Multilib("el").flag("+EL"));
  174. ASSERT_TRUE(MS.size() == 4);
  175. for (MultilibSet::const_iterator I = MS.begin(), E = MS.end(); I != E; ++I) {
  176. ASSERT_TRUE(I->isValid()) << "Multilb " << *I << " should be valid";
  177. ASSERT_TRUE(llvm::StringSwitch<bool>(I->gccSuffix())
  178. .Cases("", "/sof", "/el", "/sof/el", true)
  179. .Default(false))
  180. << "Multilib " << *I << " wasn't expected";
  181. ASSERT_TRUE(llvm::StringSwitch<bool>(I->gccSuffix())
  182. .Case("", hasFlag(*I, "-sof"))
  183. .Case("/sof", hasFlag(*I, "+sof"))
  184. .Case("/el", hasFlag(*I, "-sof"))
  185. .Case("/sof/el", hasFlag(*I, "+sof"))
  186. .Default(false))
  187. << "Multilib " << *I << " didn't have the appropriate {+,-}sof flag";
  188. ASSERT_TRUE(llvm::StringSwitch<bool>(I->gccSuffix())
  189. .Case("", hasFlag(*I, "-EL"))
  190. .Case("/sof", hasFlag(*I, "-EL"))
  191. .Case("/el", hasFlag(*I, "+EL"))
  192. .Case("/sof/el", hasFlag(*I, "+EL"))
  193. .Default(false))
  194. << "Multilib " << *I << " didn't have the appropriate {+,-}EL flag";
  195. }
  196. }
  197. TEST(MultilibTest, SetPushback) {
  198. MultilibSet MS;
  199. MS.push_back(Multilib("one"));
  200. MS.push_back(Multilib("two"));
  201. ASSERT_TRUE(MS.size() == 2);
  202. for (MultilibSet::const_iterator I = MS.begin(), E = MS.end(); I != E; ++I) {
  203. ASSERT_TRUE(llvm::StringSwitch<bool>(I->gccSuffix())
  204. .Cases("/one", "/two", true)
  205. .Default(false));
  206. }
  207. MS.clear();
  208. ASSERT_TRUE(MS.size() == 0);
  209. }
  210. TEST(MultilibTest, SetRegexFilter) {
  211. MultilibSet MS;
  212. MS.Maybe(Multilib("one"));
  213. MS.Maybe(Multilib("two"));
  214. MS.Maybe(Multilib("three"));
  215. ASSERT_EQ(MS.size(), (unsigned)2 * 2 * 2)
  216. << "Size before filter was incorrect. Contents:\n" << MS;
  217. MS.FilterOut("/one/two/three");
  218. ASSERT_EQ(MS.size(), (unsigned)2 * 2 * 2 - 1)
  219. << "Size after filter was incorrect. Contents:\n" << MS;
  220. for (MultilibSet::const_iterator I = MS.begin(), E = MS.end(); I != E; ++I) {
  221. ASSERT_TRUE(I->gccSuffix() != "/one/two/three")
  222. << "The filter should have removed " << *I;
  223. }
  224. }
  225. TEST(MultilibTest, SetFilterObject) {
  226. MultilibSet MS;
  227. MS.Maybe(Multilib("orange"));
  228. MS.Maybe(Multilib("pear"));
  229. MS.Maybe(Multilib("plum"));
  230. ASSERT_EQ((int)MS.size(), 1 /* Default */ +
  231. 1 /* pear */ +
  232. 1 /* plum */ +
  233. 1 /* pear/plum */ +
  234. 1 /* orange */ +
  235. 1 /* orange/pear */ +
  236. 1 /* orange/plum */ +
  237. 1 /* orange/pear/plum */ )
  238. << "Size before filter was incorrect. Contents:\n" << MS;
  239. MS.FilterOut([](const Multilib &M) {
  240. return StringRef(M.gccSuffix()).startswith("/p");
  241. });
  242. ASSERT_EQ((int)MS.size(), 1 /* Default */ +
  243. 1 /* orange */ +
  244. 1 /* orange/pear */ +
  245. 1 /* orange/plum */ +
  246. 1 /* orange/pear/plum */ )
  247. << "Size after filter was incorrect. Contents:\n" << MS;
  248. for (MultilibSet::const_iterator I = MS.begin(), E = MS.end(); I != E; ++I) {
  249. ASSERT_FALSE(StringRef(I->gccSuffix()).startswith("/p"))
  250. << "The filter should have removed " << *I;
  251. }
  252. }
  253. TEST(MultilibTest, SetSelection1) {
  254. MultilibSet MS1 = MultilibSet()
  255. .Maybe(Multilib("64").flag("+m64"));
  256. Multilib::flags_list FlagM64;
  257. FlagM64.push_back("+m64");
  258. Multilib SelectionM64;
  259. ASSERT_TRUE(MS1.select(FlagM64, SelectionM64))
  260. << "Flag set was {\"+m64\"}, but selection not found";
  261. ASSERT_TRUE(SelectionM64.gccSuffix() == "/64")
  262. << "Selection picked " << SelectionM64 << " which was not expected";
  263. Multilib::flags_list FlagNoM64;
  264. FlagNoM64.push_back("-m64");
  265. Multilib SelectionNoM64;
  266. ASSERT_TRUE(MS1.select(FlagNoM64, SelectionNoM64))
  267. << "Flag set was {\"-m64\"}, but selection not found";
  268. ASSERT_TRUE(SelectionNoM64.gccSuffix() == "")
  269. << "Selection picked " << SelectionNoM64 << " which was not expected";
  270. }
  271. TEST(MultilibTest, SetSelection2) {
  272. MultilibSet MS2 = MultilibSet()
  273. .Maybe(Multilib("el").flag("+EL"))
  274. .Maybe(Multilib("sf").flag("+SF"));
  275. for (unsigned I = 0; I < 4; ++I) {
  276. bool IsEL = I & 0x1;
  277. bool IsSF = I & 0x2;
  278. Multilib::flags_list Flags;
  279. if (IsEL)
  280. Flags.push_back("+EL");
  281. else
  282. Flags.push_back("-EL");
  283. if (IsSF)
  284. Flags.push_back("+SF");
  285. else
  286. Flags.push_back("-SF");
  287. Multilib Selection;
  288. ASSERT_TRUE(MS2.select(Flags, Selection)) << "Selection failed for "
  289. << (IsEL ? "+EL" : "-EL") << " "
  290. << (IsSF ? "+SF" : "-SF");
  291. std::string Suffix;
  292. if (IsEL)
  293. Suffix += "/el";
  294. if (IsSF)
  295. Suffix += "/sf";
  296. ASSERT_EQ(Selection.gccSuffix(), Suffix) << "Selection picked " << Selection
  297. << " which was not expected ";
  298. }
  299. }
  300. TEST(MultilibTest, SetCombineWith) {
  301. MultilibSet Coffee;
  302. Coffee.push_back(Multilib("coffee"));
  303. MultilibSet Milk;
  304. Milk.push_back(Multilib("milk"));
  305. MultilibSet Latte;
  306. ASSERT_EQ(Latte.size(), (unsigned)0);
  307. Latte.combineWith(Coffee);
  308. ASSERT_EQ(Latte.size(), (unsigned)1);
  309. Latte.combineWith(Milk);
  310. ASSERT_EQ(Latte.size(), (unsigned)2);
  311. }
  312. TEST(MultilibTest, SetPriority) {
  313. MultilibSet MS;
  314. MS.push_back(Multilib("foo", {}, {}, 1).flag("+foo"));
  315. MS.push_back(Multilib("bar", {}, {}, 2).flag("+bar"));
  316. Multilib::flags_list Flags1;
  317. Flags1.push_back("+foo");
  318. Flags1.push_back("-bar");
  319. Multilib Selection1;
  320. ASSERT_TRUE(MS.select(Flags1, Selection1))
  321. << "Flag set was {\"+foo\"}, but selection not found";
  322. ASSERT_TRUE(Selection1.gccSuffix() == "/foo")
  323. << "Selection picked " << Selection1 << " which was not expected";
  324. Multilib::flags_list Flags2;
  325. Flags2.push_back("+foo");
  326. Flags2.push_back("+bar");
  327. Multilib Selection2;
  328. ASSERT_TRUE(MS.select(Flags2, Selection2))
  329. << "Flag set was {\"+bar\"}, but selection not found";
  330. ASSERT_TRUE(Selection2.gccSuffix() == "/bar")
  331. << "Selection picked " << Selection2 << " which was not expected";
  332. }