summaryrefslogtreecommitdiffstats
path: root/unittests/Driver/MultilibTest.cpp
blob: c5e8e0970de0373c7f341744952f524f6607cfda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
//===- unittests/Driver/MultilibTest.cpp --- Multilib tests ---------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Unit tests for Multilib and MultilibSet
//
//===----------------------------------------------------------------------===//

#include "clang/Driver/Multilib.h"
#include "clang/Basic/LLVM.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "gtest/gtest.h"

using namespace clang::driver;
using namespace clang;

TEST(MultilibTest, MultilibValidity) {

  ASSERT_TRUE(Multilib().isValid()) << "Empty multilib is not valid";

  ASSERT_TRUE(Multilib().flag("+foo").isValid())
      << "Single indicative flag is not valid";

  ASSERT_TRUE(Multilib().flag("-foo").isValid())
      << "Single contraindicative flag is not valid";

  ASSERT_FALSE(Multilib().flag("+foo").flag("-foo").isValid())
      << "Conflicting flags should invalidate the Multilib";

  ASSERT_TRUE(Multilib().flag("+foo").flag("+foo").isValid())
      << "Multilib should be valid even if it has the same flag twice";

  ASSERT_TRUE(Multilib().flag("+foo").flag("-foobar").isValid())
      << "Seemingly conflicting prefixes shouldn't actually conflict";
}

TEST(MultilibTest, OpEqReflexivity1) {
  Multilib M;
  ASSERT_TRUE(M == M) << "Multilib::operator==() is not reflexive";
}

TEST(MultilibTest, OpEqReflexivity2) {
  ASSERT_TRUE(Multilib() == Multilib())
      << "Separately constructed default multilibs are not equal";
}

TEST(MultilibTest, OpEqReflexivity3) {
  Multilib M1, M2;
  M1.flag("+foo");
  M2.flag("+foo");
  ASSERT_TRUE(M1 == M2) << "Multilibs with the same flag should be the same";
}

TEST(MultilibTest, OpEqInequivalence1) {
  Multilib M1, M2;
  M1.flag("+foo");
  M2.flag("-foo");
  ASSERT_FALSE(M1 == M2) << "Multilibs with conflicting flags are not the same";
  ASSERT_FALSE(M2 == M1)
      << "Multilibs with conflicting flags are not the same (commuted)";
}

TEST(MultilibTest, OpEqInequivalence2) {
  Multilib M1, M2;
  M2.flag("+foo");
  ASSERT_FALSE(M1 == M2) << "Flags make Multilibs different";
}

TEST(MultilibTest, OpEqEquivalence1) {
  Multilib M1, M2;
  M1.flag("+foo");
  M2.flag("+foo").flag("+foo");
  ASSERT_TRUE(M1 == M2) << "Flag duplication shouldn't affect equivalence";
  ASSERT_TRUE(M2 == M1)
      << "Flag duplication shouldn't affect equivalence (commuted)";
}

TEST(MultilibTest, OpEqEquivalence2) {
  Multilib M1("64");
  Multilib M2;
  M2.gccSuffix("/64");
  ASSERT_TRUE(M1 == M2)
      << "Constructor argument must match Multilib::gccSuffix()";
  ASSERT_TRUE(M2 == M1)
      << "Constructor argument must match Multilib::gccSuffix() (commuted)";
}

TEST(MultilibTest, OpEqEquivalence3) {
  Multilib M1("", "32");
  Multilib M2;
  M2.osSuffix("/32");
  ASSERT_TRUE(M1 == M2)
      << "Constructor argument must match Multilib::osSuffix()";
  ASSERT_TRUE(M2 == M1)
      << "Constructor argument must match Multilib::osSuffix() (commuted)";
}

TEST(MultilibTest, OpEqEquivalence4) {
  Multilib M1("", "", "16");
  Multilib M2;
  M2.includeSuffix("/16");
  ASSERT_TRUE(M1 == M2)
      << "Constructor argument must match Multilib::includeSuffix()";
  ASSERT_TRUE(M2 == M1)
      << "Constructor argument must match Multilib::includeSuffix() (commuted)";
}

TEST(MultilibTest, OpEqInequivalence3) {
  Multilib M1("foo");
  Multilib M2("bar");
  ASSERT_FALSE(M1 == M2) << "Differing gccSuffixes should be different";
  ASSERT_FALSE(M2 == M1)
      << "Differing gccSuffixes should be different (commuted)";
}

TEST(MultilibTest, OpEqInequivalence4) {
  Multilib M1("", "foo");
  Multilib M2("", "bar");
  ASSERT_FALSE(M1 == M2) << "Differing osSuffixes should be different";
  ASSERT_FALSE(M2 == M1)
      << "Differing osSuffixes should be different (commuted)";
}

TEST(MultilibTest, OpEqInequivalence5) {
  Multilib M1("", "", "foo");
  Multilib M2("", "", "bar");
  ASSERT_FALSE(M1 == M2) << "Differing includeSuffixes should be different";
  ASSERT_FALSE(M2 == M1)
      << "Differing includeSuffixes should be different (commuted)";
}

TEST(MultilibTest, Construction1) {
  Multilib M("gcc64", "os64", "inc64");
  ASSERT_TRUE(M.gccSuffix() == "/gcc64");
  ASSERT_TRUE(M.osSuffix() == "/os64");
  ASSERT_TRUE(M.includeSuffix() == "/inc64");
}

TEST(MultilibTest, Construction2) {
  Multilib M1;
  Multilib M2("");
  Multilib M3("", "");
  Multilib M4("", "", "");
  ASSERT_TRUE(M1 == M2)
      << "Default arguments to Multilib constructor broken (first argument)";
  ASSERT_TRUE(M1 == M3)
      << "Default arguments to Multilib constructor broken (second argument)";
  ASSERT_TRUE(M1 == M4)
      << "Default arguments to Multilib constructor broken (third argument)";
}

TEST(MultilibTest, Construction3) {
  Multilib M = Multilib().flag("+f1").flag("+f2").flag("-f3");
  for (Multilib::flags_list::const_iterator I = M.flags().begin(),
                                            E = M.flags().end();
       I != E; ++I) {
    ASSERT_TRUE(llvm::StringSwitch<bool>(*I)
                    .Cases("+f1", "+f2", "-f3", true)
                    .Default(false));
  }
}

static bool hasFlag(const Multilib &M, StringRef Flag) {
  for (Multilib::flags_list::const_iterator I = M.flags().begin(),
                                            E = M.flags().end();
       I != E; ++I) {
    if (*I == Flag)
      return true;
    else if (StringRef(*I).substr(1) == Flag.substr(1))
      return false;
  }
  return false;
}

TEST(MultilibTest, SetConstruction1) {
  // Single maybe
  MultilibSet MS;
  ASSERT_TRUE(MS.size() == 0);
  MS.Maybe(Multilib("64").flag("+m64"));
  ASSERT_TRUE(MS.size() == 2);
  for (MultilibSet::const_iterator I = MS.begin(), E = MS.end(); I != E; ++I) {
    if (I->gccSuffix() == "/64")
      ASSERT_TRUE(I->flags()[0] == "+m64");
    else if (I->gccSuffix() == "")
      ASSERT_TRUE(I->flags()[0] == "-m64");
    else
      FAIL() << "Unrecognized gccSufix: " << I->gccSuffix();
  }
}

TEST(MultilibTest, SetConstruction2) {
  // Double maybe
  MultilibSet MS;
  MS.Maybe(Multilib("sof").flag("+sof"));
  MS.Maybe(Multilib("el").flag("+EL"));
  ASSERT_TRUE(MS.size() == 4);
  for (MultilibSet::const_iterator I = MS.begin(), E = MS.end(); I != E; ++I) {
    ASSERT_TRUE(I->isValid()) << "Multilb " << *I << " should be valid";
    ASSERT_TRUE(llvm::StringSwitch<bool>(I->gccSuffix())
                    .Cases("", "/sof", "/el", "/sof/el", true)
                    .Default(false))
        << "Multilib " << *I << " wasn't expected";
    ASSERT_TRUE(llvm::StringSwitch<bool>(I->gccSuffix())
                    .Case("", hasFlag(*I, "-sof"))
                    .Case("/sof", hasFlag(*I, "+sof"))
                    .Case("/el", hasFlag(*I, "-sof"))
                    .Case("/sof/el", hasFlag(*I, "+sof"))
                    .Default(false))
        << "Multilib " << *I << " didn't have the appropriate {+,-}sof flag";
    ASSERT_TRUE(llvm::StringSwitch<bool>(I->gccSuffix())
                    .Case("", hasFlag(*I, "-EL"))
                    .Case("/sof", hasFlag(*I, "-EL"))
                    .Case("/el", hasFlag(*I, "+EL"))
                    .Case("/sof/el", hasFlag(*I, "+EL"))
                    .Default(false))
        << "Multilib " << *I << " didn't have the appropriate {+,-}EL flag";
  }
}

TEST(MultilibTest, SetPushback) {
  MultilibSet MS;
  MS.push_back(Multilib("one"));
  MS.push_back(Multilib("two"));
  ASSERT_TRUE(MS.size() == 2);
  for (MultilibSet::const_iterator I = MS.begin(), E = MS.end(); I != E; ++I) {
    ASSERT_TRUE(llvm::StringSwitch<bool>(I->gccSuffix())
                    .Cases("/one", "/two", true)
                    .Default(false));
  }
  MS.clear();
  ASSERT_TRUE(MS.size() == 0);
}

TEST(MultilibTest, SetRegexFilter) {
  MultilibSet MS;
  MS.Maybe(Multilib("one"));
  MS.Maybe(Multilib("two"));
  MS.Maybe(Multilib("three"));
  ASSERT_EQ(MS.size(), (unsigned)2 * 2 * 2)
      << "Size before filter was incorrect. Contents:\n" << MS;
  MS.FilterOut("/one/two/three");
  ASSERT_EQ(MS.size(), (unsigned)2 * 2 * 2 - 1)
      << "Size after filter was incorrect. Contents:\n" << MS;
  for (MultilibSet::const_iterator I = MS.begin(), E = MS.end(); I != E; ++I) {
    ASSERT_TRUE(I->gccSuffix() != "/one/two/three")
        << "The filter should have removed " << *I;
  }
}

TEST(MultilibTest, SetFilterObject) {
  MultilibSet MS;
  MS.Maybe(Multilib("orange"));
  MS.Maybe(Multilib("pear"));
  MS.Maybe(Multilib("plum"));
  ASSERT_EQ((int)MS.size(), 1 /* Default */ +
                            1 /* pear */ +
                            1 /* plum */ +
                            1 /* pear/plum */ +
                            1 /* orange */ +
                            1 /* orange/pear */ +
                            1 /* orange/plum */ +
                            1 /* orange/pear/plum */ )
      << "Size before filter was incorrect. Contents:\n" << MS;
  MS.FilterOut([](const Multilib &M) {
    return StringRef(M.gccSuffix()).startswith("/p");
  });
  ASSERT_EQ((int)MS.size(), 1 /* Default */ +
                            1 /* orange */ +
                            1 /* orange/pear */ +
                            1 /* orange/plum */ + 
                            1 /* orange/pear/plum */ )
      << "Size after filter was incorrect. Contents:\n" << MS;
  for (MultilibSet::const_iterator I = MS.begin(), E = MS.end(); I != E; ++I) {
    ASSERT_FALSE(StringRef(I->gccSuffix()).startswith("/p"))
        << "The filter should have removed " << *I;
  }
}

TEST(MultilibTest, SetSelection1) {
  MultilibSet MS1 = MultilibSet()
    .Maybe(Multilib("64").flag("+m64"));

  Multilib::flags_list FlagM64;
  FlagM64.push_back("+m64");
  Multilib SelectionM64;
  ASSERT_TRUE(MS1.select(FlagM64, SelectionM64))
      << "Flag set was {\"+m64\"}, but selection not found";
  ASSERT_TRUE(SelectionM64.gccSuffix() == "/64")
      << "Selection picked " << SelectionM64 << " which was not expected";

  Multilib::flags_list FlagNoM64;
  FlagNoM64.push_back("-m64");
  Multilib SelectionNoM64;
  ASSERT_TRUE(MS1.select(FlagNoM64, SelectionNoM64))
      << "Flag set was {\"-m64\"}, but selection not found";
  ASSERT_TRUE(SelectionNoM64.gccSuffix() == "")
      << "Selection picked " << SelectionNoM64 << " which was not expected";
}

TEST(MultilibTest, SetSelection2) {
  MultilibSet MS2 = MultilibSet()
    .Maybe(Multilib("el").flag("+EL"))
    .Maybe(Multilib("sf").flag("+SF"));

  for (unsigned I = 0; I < 4; ++I) {
    bool IsEL = I & 0x1;
    bool IsSF = I & 0x2;
    Multilib::flags_list Flags;
    if (IsEL)
      Flags.push_back("+EL");
    else
      Flags.push_back("-EL");

    if (IsSF)
      Flags.push_back("+SF");
    else
      Flags.push_back("-SF");

    Multilib Selection;
    ASSERT_TRUE(MS2.select(Flags, Selection)) << "Selection failed for "
                                              << (IsEL ? "+EL" : "-EL") << " "
                                              << (IsSF ? "+SF" : "-SF");

    std::string Suffix;
    if (IsEL)
      Suffix += "/el";
    if (IsSF)
      Suffix += "/sf";

    ASSERT_EQ(Selection.gccSuffix(), Suffix) << "Selection picked " << Selection
                                             << " which was not expected ";
  }
}

TEST(MultilibTest, SetCombineWith) {
  MultilibSet Coffee;
  Coffee.push_back(Multilib("coffee"));
  MultilibSet Milk;
  Milk.push_back(Multilib("milk"));
  MultilibSet Latte;
  ASSERT_EQ(Latte.size(), (unsigned)0);
  Latte.combineWith(Coffee);
  ASSERT_EQ(Latte.size(), (unsigned)1);
  Latte.combineWith(Milk);
  ASSERT_EQ(Latte.size(), (unsigned)2);
}