1 //===- unittests/Driver/MultilibBuilderTest.cpp --- MultilibBuilder tests
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 // Unit tests for MultilibBuilder and MultilibSetBuilder
12 //===----------------------------------------------------------------------===//
14 #include "clang/Driver/MultilibBuilder.h"
15 #include "../../lib/Driver/ToolChains/CommonArgs.h"
16 #include "clang/Basic/LLVM.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/StringSwitch.h"
20 #include "gtest/gtest.h"
22 using llvm::is_contained
;
23 using namespace clang
;
24 using namespace driver
;
26 TEST(MultilibBuilderTest
, MultilibValidity
) {
28 ASSERT_TRUE(MultilibBuilder().isValid()) << "Empty multilib is not valid";
30 ASSERT_TRUE(MultilibBuilder().flag("-foo").isValid())
31 << "Single indicative flag is not valid";
33 ASSERT_TRUE(MultilibBuilder().flag("-foo", /*Disallow=*/true).isValid())
34 << "Single contraindicative flag is not valid";
37 MultilibBuilder().flag("-foo").flag("-foo", /*Disallow=*/true).isValid())
38 << "Conflicting flags should invalidate the Multilib";
40 ASSERT_TRUE(MultilibBuilder().flag("-foo").flag("-foo").isValid())
41 << "Multilib should be valid even if it has the same flag "
44 ASSERT_TRUE(MultilibBuilder()
46 .flag("-foobar", /*Disallow=*/true)
48 << "Seemingly conflicting prefixes shouldn't actually conflict";
51 TEST(MultilibBuilderTest
, Construction1
) {
52 MultilibBuilder
M("gcc64", "os64", "inc64");
53 ASSERT_TRUE(M
.gccSuffix() == "/gcc64");
54 ASSERT_TRUE(M
.osSuffix() == "/os64");
55 ASSERT_TRUE(M
.includeSuffix() == "/inc64");
58 TEST(MultilibBuilderTest
, Construction3
) {
60 MultilibBuilder().flag("-f1").flag("-f2").flag("-f3", /*Disallow=*/true);
61 for (const std::string
&A
: M
.flags()) {
62 ASSERT_TRUE(llvm::StringSwitch
<bool>(A
)
63 .Cases("-f1", "-f2", "!f3", true)
68 TEST(MultilibBuilderTest
, SetConstruction1
) {
70 MultilibSet MS
= MultilibSetBuilder()
71 .Maybe(MultilibBuilder("64").flag("-m64"))
73 ASSERT_TRUE(MS
.size() == 2);
74 for (MultilibSet::const_iterator I
= MS
.begin(), E
= MS
.end(); I
!= E
; ++I
) {
75 if (I
->gccSuffix() == "/64")
76 ASSERT_TRUE(*I
->flags().begin() == "-m64");
77 else if (I
->gccSuffix() == "")
78 ASSERT_TRUE(*I
->flags().begin() == "!m64");
80 FAIL() << "Unrecognized gccSufix: " << I
->gccSuffix();
84 TEST(MultilibBuilderTest
, SetConstruction2
) {
86 MultilibSet MS
= MultilibSetBuilder()
87 .Maybe(MultilibBuilder("sof").flag("-sof"))
88 .Maybe(MultilibBuilder("el").flag("-EL"))
90 ASSERT_TRUE(MS
.size() == 4);
91 for (MultilibSet::const_iterator I
= MS
.begin(), E
= MS
.end(); I
!= E
; ++I
) {
92 ASSERT_TRUE(llvm::StringSwitch
<bool>(I
->gccSuffix())
93 .Cases("", "/sof", "/el", "/sof/el", true)
95 << "Multilib " << *I
<< " wasn't expected";
96 ASSERT_TRUE(llvm::StringSwitch
<bool>(I
->gccSuffix())
97 .Case("", is_contained(I
->flags(), "!sof"))
98 .Case("/sof", is_contained(I
->flags(), "-sof"))
99 .Case("/el", is_contained(I
->flags(), "!sof"))
100 .Case("/sof/el", is_contained(I
->flags(), "-sof"))
102 << "Multilib " << *I
<< " didn't have the appropriate {-,!}sof flag";
103 ASSERT_TRUE(llvm::StringSwitch
<bool>(I
->gccSuffix())
104 .Case("", is_contained(I
->flags(), "!EL"))
105 .Case("/sof", is_contained(I
->flags(), "!EL"))
106 .Case("/el", is_contained(I
->flags(), "-EL"))
107 .Case("/sof/el", is_contained(I
->flags(), "-EL"))
109 << "Multilib " << *I
<< " didn't have the appropriate {-,!}EL flag";
113 TEST(MultilibBuilderTest
, SetRegexFilter
) {
114 MultilibSetBuilder MB
;
115 MB
.Maybe(MultilibBuilder("one"))
116 .Maybe(MultilibBuilder("two"))
117 .Maybe(MultilibBuilder("three"))
119 MultilibSet MS
= MB
.makeMultilibSet();
120 ASSERT_EQ(MS
.size(), (unsigned)2 * 2 * 2)
121 << "Size before filter was incorrect. Contents:\n"
123 MB
.FilterOut("/one/two/three");
124 MS
= MB
.makeMultilibSet();
125 ASSERT_EQ(MS
.size(), (unsigned)2 * 2 * 2 - 1)
126 << "Size after filter was incorrect. Contents:\n"
128 for (MultilibSet::const_iterator I
= MS
.begin(), E
= MS
.end(); I
!= E
; ++I
) {
129 ASSERT_TRUE(I
->gccSuffix() != "/one/two/three")
130 << "The filter should have removed " << *I
;
134 TEST(MultilibBuilderTest
, SetFilterObject
) {
135 MultilibSet MS
= MultilibSetBuilder()
136 .Maybe(MultilibBuilder("orange"))
137 .Maybe(MultilibBuilder("pear"))
138 .Maybe(MultilibBuilder("plum"))
140 ASSERT_EQ((int)MS
.size(), 1 /* Default */ + 1 /* pear */ + 1 /* plum */ +
141 1 /* pear/plum */ + 1 /* orange */ +
142 1 /* orange/pear */ + 1 /* orange/plum */ +
143 1 /* orange/pear/plum */)
144 << "Size before filter was incorrect. Contents:\n"
146 MS
.FilterOut([](const Multilib
&M
) {
147 return StringRef(M
.gccSuffix()).startswith("/p");
149 ASSERT_EQ((int)MS
.size(), 1 /* Default */ + 1 /* orange */ +
150 1 /* orange/pear */ + 1 /* orange/plum */ +
151 1 /* orange/pear/plum */)
152 << "Size after filter was incorrect. Contents:\n"
154 for (MultilibSet::const_iterator I
= MS
.begin(), E
= MS
.end(); I
!= E
; ++I
) {
155 ASSERT_FALSE(StringRef(I
->gccSuffix()).startswith("/p"))
156 << "The filter should have removed " << *I
;
160 TEST(MultilibBuilderTest
, SetSelection1
) {
161 MultilibSet MS1
= MultilibSetBuilder()
162 .Maybe(MultilibBuilder("64").flag("-m64"))
165 Multilib::flags_list FlagM64
= {"-m64"};
166 llvm::SmallVector
<Multilib
> SelectionM64
;
167 ASSERT_TRUE(MS1
.select(FlagM64
, SelectionM64
))
168 << "Flag set was {\"-m64\"}, but selection not found";
169 ASSERT_TRUE(SelectionM64
.back().gccSuffix() == "/64")
170 << "Selection picked " << SelectionM64
.back()
171 << " which was not expected";
173 Multilib::flags_list FlagNoM64
= {"!m64"};
174 llvm::SmallVector
<Multilib
> SelectionNoM64
;
175 ASSERT_TRUE(MS1
.select(FlagNoM64
, SelectionNoM64
))
176 << "Flag set was {\"!m64\"}, but selection not found";
177 ASSERT_TRUE(SelectionNoM64
.back().gccSuffix() == "")
178 << "Selection picked " << SelectionNoM64
.back()
179 << " which was not expected";
182 TEST(MultilibBuilderTest
, SetSelection2
) {
183 MultilibSet MS2
= MultilibSetBuilder()
184 .Maybe(MultilibBuilder("el").flag("-EL"))
185 .Maybe(MultilibBuilder("sf").flag("-SF"))
188 for (unsigned I
= 0; I
< 4; ++I
) {
191 Multilib::flags_list Flags
;
193 Flags
.push_back("-EL");
195 Flags
.push_back("!EL");
198 Flags
.push_back("-SF");
200 Flags
.push_back("!SF");
202 llvm::SmallVector
<Multilib
> Selection
;
203 ASSERT_TRUE(MS2
.select(Flags
, Selection
))
204 << "Selection failed for " << (IsEL
? "-EL" : "!EL") << " "
205 << (IsSF
? "-SF" : "!SF");
213 ASSERT_EQ(Selection
.back().gccSuffix(), Suffix
)
214 << "Selection picked " << Selection
.back()
215 << " which was not expected ";