Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Driver/Multilib.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Multilib.cpp - Multilib Implementation -----------------------------===//
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
#include "clang/Driver/Multilib.h"
10
#include "clang/Basic/LLVM.h"
11
#include "llvm/ADT/SmallString.h"
12
#include "llvm/ADT/StringMap.h"
13
#include "llvm/ADT/StringRef.h"
14
#include "llvm/ADT/StringSet.h"
15
#include "llvm/Support/Compiler.h"
16
#include "llvm/Support/ErrorHandling.h"
17
#include "llvm/Support/Path.h"
18
#include "llvm/Support/Regex.h"
19
#include "llvm/Support/raw_ostream.h"
20
#include <algorithm>
21
#include <cassert>
22
#include <string>
23
24
using namespace clang;
25
using namespace driver;
26
using namespace llvm::sys;
27
28
/// normalize Segment to "/foo/bar" or "".
29
299k
static void normalizePathSegment(std::string &Segment) {
30
299k
  StringRef seg = Segment;
31
299k
32
299k
  // Prune trailing "/" or "./"
33
374k
  while (true) {
34
374k
    StringRef last = path::filename(seg);
35
374k
    if (last != ".")
36
299k
      break;
37
75.0k
    seg = path::parent_path(seg);
38
75.0k
  }
39
299k
40
299k
  if (seg.empty() || 
seg == "/"171k
) {
41
131k
    Segment.clear();
42
131k
    return;
43
131k
  }
44
168k
45
168k
  // Add leading '/'
46
168k
  if (seg.front() != '/') {
47
342
    Segment = "/" + seg.str();
48
168k
  } else {
49
168k
    Segment = seg;
50
168k
  }
51
168k
}
52
53
Multilib::Multilib(StringRef GCCSuffix, StringRef OSSuffix,
54
                   StringRef IncludeSuffix, int Priority)
55
    : GCCSuffix(GCCSuffix), OSSuffix(OSSuffix), IncludeSuffix(IncludeSuffix),
56
99.1k
      Priority(Priority) {
57
99.1k
  normalizePathSegment(this->GCCSuffix);
58
99.1k
  normalizePathSegment(this->OSSuffix);
59
99.1k
  normalizePathSegment(this->IncludeSuffix);
60
99.1k
}
61
62
829
Multilib &Multilib::gccSuffix(StringRef S) {
63
829
  GCCSuffix = S;
64
829
  normalizePathSegment(GCCSuffix);
65
829
  return *this;
66
829
}
67
68
417
Multilib &Multilib::osSuffix(StringRef S) {
69
417
  OSSuffix = S;
70
417
  normalizePathSegment(OSSuffix);
71
417
  return *this;
72
417
}
73
74
829
Multilib &Multilib::includeSuffix(StringRef S) {
75
829
  IncludeSuffix = S;
76
829
  normalizePathSegment(IncludeSuffix);
77
829
  return *this;
78
829
}
79
80
0
LLVM_DUMP_METHOD void Multilib::dump() const {
81
0
  print(llvm::errs());
82
0
}
83
84
16
void Multilib::print(raw_ostream &OS) const {
85
16
  assert(GCCSuffix.empty() || (StringRef(GCCSuffix).front() == '/'));
86
16
  if (GCCSuffix.empty())
87
13
    OS << ".";
88
3
  else {
89
3
    OS << StringRef(GCCSuffix).drop_front();
90
3
  }
91
16
  OS << ";";
92
44
  for (StringRef Flag : Flags) {
93
44
    if (Flag.front() == '+')
94
16
      OS << "@" << Flag.substr(1);
95
44
  }
96
16
}
97
98
52.9k
bool Multilib::isValid() const {
99
52.9k
  llvm::StringMap<int> FlagSet;
100
522k
  for (unsigned I = 0, N = Flags.size(); I != N; 
++I469k
) {
101
471k
    StringRef Flag(Flags[I]);
102
471k
    llvm::StringMap<int>::iterator SI = FlagSet.find(Flag.substr(1));
103
471k
104
471k
    assert(StringRef(Flag).front() == '+' || StringRef(Flag).front() == '-');
105
471k
106
471k
    if (SI == FlagSet.end())
107
461k
      FlagSet[Flag.substr(1)] = I;
108
9.63k
    else if (Flags[I] != Flags[SI->getValue()])
109
2.14k
      return false;
110
471k
  }
111
52.9k
  
return true50.7k
;
112
52.9k
}
113
114
673
bool Multilib::operator==(const Multilib &Other) const {
115
673
  // Check whether the flags sets match
116
673
  // allowing for the match to be order invariant
117
673
  llvm::StringSet<> MyFlags;
118
673
  for (const auto &Flag : Flags)
119
1.95k
    MyFlags.insert(Flag);
120
673
121
673
  for (const auto &Flag : Other.Flags)
122
1.21k
    if (MyFlags.find(Flag) == MyFlags.end())
123
427
      return false;
124
673
125
673
  
if (246
osSuffix() != Other.osSuffix()246
)
126
2
    return false;
127
244
128
244
  if (gccSuffix() != Other.gccSuffix())
129
201
    return false;
130
43
131
43
  if (includeSuffix() != Other.includeSuffix())
132
2
    return false;
133
41
134
41
  return true;
135
41
}
136
137
16
raw_ostream &clang::driver::operator<<(raw_ostream &OS, const Multilib &M) {
138
16
  M.print(OS);
139
16
  return OS;
140
16
}
141
142
756
MultilibSet &MultilibSet::Maybe(const Multilib &M) {
143
756
  Multilib Opposite;
144
756
  // Negate any '+' flags
145
1.14k
  for (StringRef Flag : M.flags()) {
146
1.14k
    if (Flag.front() == '+')
147
750
      Opposite.flags().push_back(("-" + Flag.substr(1)).str());
148
1.14k
  }
149
756
  return Either(M, Opposite);
150
756
}
151
152
919
MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2) {
153
919
  return Either({M1, M2});
154
919
}
155
156
MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
157
265
                                 const Multilib &M3) {
158
265
  return Either({M1, M2, M3});
159
265
}
160
161
MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
162
22
                                 const Multilib &M3, const Multilib &M4) {
163
22
  return Either({M1, M2, M3, M4});
164
22
}
165
166
MultilibSet &MultilibSet::Either(const Multilib &M1, const Multilib &M2,
167
                                 const Multilib &M3, const Multilib &M4,
168
120
                                 const Multilib &M5) {
169
120
  return Either({M1, M2, M3, M4, M5});
170
120
}
171
172
52.8k
static Multilib compose(const Multilib &Base, const Multilib &New) {
173
52.8k
  SmallString<128> GCCSuffix;
174
52.8k
  llvm::sys::path::append(GCCSuffix, "/", Base.gccSuffix(), New.gccSuffix());
175
52.8k
  SmallString<128> OSSuffix;
176
52.8k
  llvm::sys::path::append(OSSuffix, "/", Base.osSuffix(), New.osSuffix());
177
52.8k
  SmallString<128> IncludeSuffix;
178
52.8k
  llvm::sys::path::append(IncludeSuffix, "/", Base.includeSuffix(),
179
52.8k
                          New.includeSuffix());
180
52.8k
181
52.8k
  Multilib Composed(GCCSuffix, OSSuffix, IncludeSuffix);
182
52.8k
183
52.8k
  Multilib::flags_list &Flags = Composed.flags();
184
52.8k
185
52.8k
  Flags.insert(Flags.end(), Base.flags().begin(), Base.flags().end());
186
52.8k
  Flags.insert(Flags.end(), New.flags().begin(), New.flags().end());
187
52.8k
188
52.8k
  return Composed;
189
52.8k
}
190
191
1.47k
MultilibSet &MultilibSet::Either(ArrayRef<Multilib> MultilibSegments) {
192
1.47k
  multilib_list Composed;
193
1.47k
194
1.47k
  if (Multilibs.empty())
195
408
    Multilibs.insert(Multilibs.end(), MultilibSegments.begin(),
196
408
                     MultilibSegments.end());
197
1.06k
  else {
198
2.31k
    for (const auto &New : MultilibSegments) {
199
52.8k
      for (const auto &Base : *this) {
200
52.8k
        Multilib MO = compose(Base, New);
201
52.8k
        if (MO.isValid())
202
50.7k
          Composed.push_back(MO);
203
52.8k
      }
204
2.31k
    }
205
1.06k
206
1.06k
    Multilibs = Composed;
207
1.06k
  }
208
1.47k
209
1.47k
  return *this;
210
1.47k
}
211
212
678
MultilibSet &MultilibSet::FilterOut(FilterCallback F) {
213
678
  filterInPlace(F, Multilibs);
214
678
  return *this;
215
678
}
216
217
1.12k
MultilibSet &MultilibSet::FilterOut(const char *Regex) {
218
1.12k
  llvm::Regex R(Regex);
219
#ifndef NDEBUG
220
  std::string Error;
221
  if (!R.isValid(Error)) {
222
    llvm::errs() << Error;
223
    llvm_unreachable("Invalid regex!");
224
  }
225
#endif
226
227
43.0k
  filterInPlace([&R](const Multilib &M) { return R.match(M.gccSuffix()); },
228
1.12k
                Multilibs);
229
1.12k
  return *this;
230
1.12k
}
231
232
1.08k
void MultilibSet::push_back(const Multilib &M) { Multilibs.push_back(M); }
233
234
2
void MultilibSet::combineWith(const MultilibSet &Other) {
235
2
  Multilibs.insert(Multilibs.end(), Other.begin(), Other.end());
236
2
}
237
238
27.9k
static bool isFlagEnabled(StringRef Flag) {
239
27.9k
  char Indicator = Flag.front();
240
27.9k
  assert(Indicator == '+' || Indicator == '-');
241
27.9k
  return Indicator == '+';
242
27.9k
}
243
244
535
bool MultilibSet::select(const Multilib::flags_list &Flags, Multilib &M) const {
245
535
  llvm::StringMap<bool> FlagSet;
246
535
247
535
  // Stuff all of the flags into the FlagSet such that a true mappend indicates
248
535
  // the flag was enabled, and a false mappend indicates the flag was disabled.
249
535
  for (StringRef Flag : Flags)
250
4.98k
    FlagSet[Flag.substr(1)] = isFlagEnabled(Flag);
251
535
252
7.73k
  multilib_list Filtered = filterCopy([&FlagSet](const Multilib &M) {
253
23.0k
    for (StringRef Flag : M.flags()) {
254
23.0k
      llvm::StringMap<bool>::const_iterator SI = FlagSet.find(Flag.substr(1));
255
23.0k
      if (SI != FlagSet.end())
256
23.0k
        if (SI->getValue() != isFlagEnabled(Flag))
257
7.27k
          return true;
258
23.0k
    }
259
7.73k
    
return false456
;
260
7.73k
  }, Multilibs);
261
535
262
535
  if (Filtered.empty())
263
85
    return false;
264
450
  if (Filtered.size() == 1) {
265
446
    M = Filtered[0];
266
446
    return true;
267
446
  }
268
4
269
4
  // Sort multilibs by priority and select the one with the highest priority.
270
4
  llvm::sort(Filtered.begin(), Filtered.end(),
271
8
             [](const Multilib &a, const Multilib &b) -> bool {
272
8
               return a.priority() > b.priority();
273
8
             });
274
4
275
4
  if (Filtered[0].priority() > Filtered[1].priority()) {
276
4
    M = Filtered[0];
277
4
    return true;
278
4
  }
279
0
280
0
  // TODO: We should consider returning llvm::Error rather than aborting.
281
0
  assert(false && "More than one multilib with the same priority");
282
0
  return false;
283
0
}
284
285
0
LLVM_DUMP_METHOD void MultilibSet::dump() const {
286
0
  print(llvm::errs());
287
0
}
288
289
0
void MultilibSet::print(raw_ostream &OS) const {
290
0
  for (const auto &M : *this)
291
0
    OS << M << "\n";
292
0
}
293
294
MultilibSet::multilib_list MultilibSet::filterCopy(FilterCallback F,
295
535
                                                   const multilib_list &Ms) {
296
535
  multilib_list Copy(Ms);
297
535
  filterInPlace(F, Copy);
298
535
  return Copy;
299
535
}
300
301
2.33k
void MultilibSet::filterInPlace(FilterCallback F, multilib_list &Ms) {
302
2.33k
  Ms.erase(std::remove_if(Ms.begin(), Ms.end(), F), Ms.end());
303
2.33k
}
304
305
0
raw_ostream &clang::driver::operator<<(raw_ostream &OS, const MultilibSet &MS) {
306
0
  MS.print(OS);
307
0
  return OS;
308
0
}