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