Coverage Report

Created: 2023-11-11 10:31

/Users/buildslave/jenkins/workspace/coverage/llvm-project/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 "clang/Basic/Version.h"
12
#include "llvm/ADT/SmallString.h"
13
#include "llvm/ADT/StringRef.h"
14
#include "llvm/Support/Compiler.h"
15
#include "llvm/Support/Error.h"
16
#include "llvm/Support/ErrorHandling.h"
17
#include "llvm/Support/Path.h"
18
#include "llvm/Support/Regex.h"
19
#include "llvm/Support/VersionTuple.h"
20
#include "llvm/Support/YAMLParser.h"
21
#include "llvm/Support/YAMLTraits.h"
22
#include "llvm/Support/raw_ostream.h"
23
#include <algorithm>
24
#include <cassert>
25
#include <string>
26
27
using namespace clang;
28
using namespace driver;
29
using namespace llvm::sys;
30
31
Multilib::Multilib(StringRef GCCSuffix, StringRef OSSuffix,
32
                   StringRef IncludeSuffix, const flags_list &Flags)
33
28.3k
    : GCCSuffix(GCCSuffix), OSSuffix(OSSuffix), IncludeSuffix(IncludeSuffix),
34
28.3k
      Flags(Flags) {
35
28.3k
  assert(GCCSuffix.empty() ||
36
28.3k
         (StringRef(GCCSuffix).front() == '/' && GCCSuffix.size() > 1));
37
28.3k
  assert(OSSuffix.empty() ||
38
28.3k
         (StringRef(OSSuffix).front() == '/' && OSSuffix.size() > 1));
39
28.3k
  assert(IncludeSuffix.empty() ||
40
28.3k
         (StringRef(IncludeSuffix).front() == '/' && IncludeSuffix.size() > 1));
41
28.3k
}
42
43
0
LLVM_DUMP_METHOD void Multilib::dump() const {
44
0
  print(llvm::errs());
45
0
}
46
47
64
void Multilib::print(raw_ostream &OS) const {
48
64
  if (GCCSuffix.empty())
49
34
    OS << ".";
50
30
  else {
51
30
    OS << StringRef(GCCSuffix).drop_front();
52
30
  }
53
64
  OS << ";";
54
171
  for (StringRef Flag : Flags) {
55
171
    if (Flag.front() == '-')
56
75
      OS << "@" << Flag.substr(1);
57
171
  }
58
64
}
59
60
1.17k
bool Multilib::operator==(const Multilib &Other) const {
61
  // Check whether the flags sets match
62
  // allowing for the match to be order invariant
63
1.17k
  llvm::StringSet<> MyFlags;
64
1.17k
  for (const auto &Flag : Flags)
65
3.46k
    MyFlags.insert(Flag);
66
67
1.17k
  for (const auto &Flag : Other.Flags)
68
2.06k
    if (!MyFlags.contains(Flag))
69
728
      return false;
70
71
451
  if (osSuffix() != Other.osSuffix())
72
2
    return false;
73
74
449
  if (gccSuffix() != Other.gccSuffix())
75
391
    return false;
76
77
58
  if (includeSuffix() != Other.includeSuffix())
78
2
    return false;
79
80
56
  return true;
81
58
}
82
83
64
raw_ostream &clang::driver::operator<<(raw_ostream &OS, const Multilib &M) {
84
64
  M.print(OS);
85
64
  return OS;
86
64
}
87
88
534
MultilibSet &MultilibSet::FilterOut(FilterCallback F) {
89
534
  llvm::erase_if(Multilibs, F);
90
534
  return *this;
91
534
}
92
93
6.91k
void MultilibSet::push_back(const Multilib &M) { Multilibs.push_back(M); }
94
95
bool MultilibSet::select(const Multilib::flags_list &Flags,
96
840
                         llvm::SmallVector<Multilib> &Selected) const {
97
840
  llvm::StringSet<> FlagSet(expandFlags(Flags));
98
840
  Selected.clear();
99
840
  llvm::copy_if(Multilibs, std::back_inserter(Selected),
100
2.34k
                [&FlagSet](const Multilib &M) {
101
2.34k
                  for (const std::string &F : M.flags())
102
3.55k
                    if (!FlagSet.contains(F))
103
1.76k
                      return false;
104
585
                  return true;
105
2.34k
                });
106
840
  return !Selected.empty();
107
840
}
108
109
llvm::StringSet<>
110
854
MultilibSet::expandFlags(const Multilib::flags_list &InFlags) const {
111
854
  llvm::StringSet<> Result;
112
854
  for (const auto &F : InFlags)
113
4.66k
    Result.insert(F);
114
854
  for (const FlagMatcher &M : FlagMatchers) {
115
86
    std::string RegexString(M.Match);
116
117
    // Make the regular expression match the whole string.
118
86
    if (!StringRef(M.Match).starts_with("^"))
119
86
      RegexString.insert(RegexString.begin(), '^');
120
86
    if (!StringRef(M.Match).ends_with("$"))
121
86
      RegexString.push_back('$');
122
123
86
    const llvm::Regex Regex(RegexString);
124
86
    assert(Regex.isValid());
125
86
    if (llvm::any_of(InFlags,
126
231
                     [&Regex](StringRef F) { return Regex.match(F); })) {
127
8
      Result.insert(M.Flags.begin(), M.Flags.end());
128
8
    }
129
86
  }
130
854
  return Result;
131
854
}
132
133
namespace {
134
135
// When updating this also update MULTILIB_VERSION in MultilibTest.cpp
136
static const VersionTuple MultilibVersionCurrent(1, 0);
137
138
struct MultilibSerialization {
139
  std::string Dir;
140
  std::vector<std::string> Flags;
141
};
142
143
struct MultilibSetSerialization {
144
  llvm::VersionTuple MultilibVersion;
145
  std::vector<MultilibSerialization> Multilibs;
146
  std::vector<MultilibSet::FlagMatcher> FlagMatchers;
147
};
148
149
} // end anonymous namespace
150
151
template <> struct llvm::yaml::MappingTraits<MultilibSerialization> {
152
87
  static void mapping(llvm::yaml::IO &io, MultilibSerialization &V) {
153
87
    io.mapRequired("Dir", V.Dir);
154
87
    io.mapRequired("Flags", V.Flags);
155
87
  }
156
87
  static std::string validate(IO &io, MultilibSerialization &V) {
157
87
    if (StringRef(V.Dir).starts_with("/"))
158
1
      return "paths must be relative but \"" + V.Dir + "\" starts with \"/\"";
159
86
    return std::string{};
160
87
  }
161
};
162
163
template <> struct llvm::yaml::MappingTraits<MultilibSet::FlagMatcher> {
164
46
  static void mapping(llvm::yaml::IO &io, MultilibSet::FlagMatcher &M) {
165
46
    io.mapRequired("Match", M.Match);
166
46
    io.mapRequired("Flags", M.Flags);
167
46
  }
168
46
  static std::string validate(IO &io, MultilibSet::FlagMatcher &M) {
169
46
    llvm::Regex Regex(M.Match);
170
46
    std::string RegexError;
171
46
    if (!Regex.isValid(RegexError))
172
1
      return RegexError;
173
45
    if (M.Flags.empty())
174
1
      return "value required for 'Flags'";
175
44
    return std::string{};
176
45
  }
177
};
178
179
template <> struct llvm::yaml::MappingTraits<MultilibSetSerialization> {
180
29
  static void mapping(llvm::yaml::IO &io, MultilibSetSerialization &M) {
181
29
    io.mapRequired("MultilibVersion", M.MultilibVersion);
182
29
    io.mapRequired("Variants", M.Multilibs);
183
29
    io.mapOptional("Mappings", M.FlagMatchers);
184
29
  }
185
29
  static std::string validate(IO &io, MultilibSetSerialization &M) {
186
29
    if (M.MultilibVersion.empty())
187
1
      return "missing required key 'MultilibVersion'";
188
28
    if (M.MultilibVersion.getMajor() != MultilibVersionCurrent.getMajor())
189
2
      return "multilib version " + M.MultilibVersion.getAsString() +
190
2
             " is unsupported";
191
26
    if (M.MultilibVersion.getMinor() > MultilibVersionCurrent.getMinor())
192
1
      return "multilib version " + M.MultilibVersion.getAsString() +
193
1
             " is unsupported";
194
25
    return std::string{};
195
26
  }
196
};
197
198
LLVM_YAML_IS_SEQUENCE_VECTOR(MultilibSerialization)
199
LLVM_YAML_IS_SEQUENCE_VECTOR(MultilibSet::FlagMatcher)
200
201
llvm::ErrorOr<MultilibSet>
202
MultilibSet::parseYaml(llvm::MemoryBufferRef Input,
203
                       llvm::SourceMgr::DiagHandlerTy DiagHandler,
204
29
                       void *DiagHandlerCtxt) {
205
29
  MultilibSetSerialization MS;
206
29
  llvm::yaml::Input YamlInput(Input, nullptr, DiagHandler, DiagHandlerCtxt);
207
29
  YamlInput >> MS;
208
29
  if (YamlInput.error())
209
10
    return YamlInput.error();
210
211
19
  multilib_list Multilibs;
212
19
  Multilibs.reserve(MS.Multilibs.size());
213
84
  for (const auto &M : MS.Multilibs) {
214
84
    std::string Dir;
215
84
    if (M.Dir != ".")
216
83
      Dir = "/" + M.Dir;
217
84
    Multilibs.emplace_back(Dir, Dir, Dir, M.Flags);
218
84
  }
219
220
19
  return MultilibSet(std::move(Multilibs), std::move(MS.FlagMatchers));
221
29
}
222
223
0
LLVM_DUMP_METHOD void MultilibSet::dump() const {
224
0
  print(llvm::errs());
225
0
}
226
227
0
void MultilibSet::print(raw_ostream &OS) const {
228
0
  for (const auto &M : *this)
229
0
    OS << M << "\n";
230
0
}
231
232
0
raw_ostream &clang::driver::operator<<(raw_ostream &OS, const MultilibSet &MS) {
233
0
  MS.print(OS);
234
0
  return OS;
235
0
}