clang  9.0.0svn
Multilib.h
Go to the documentation of this file.
1 //===- Multilib.h -----------------------------------------------*- C++ -*-===//
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 #ifndef LLVM_CLANG_DRIVER_MULTILIB_H
10 #define LLVM_CLANG_DRIVER_MULTILIB_H
11 
12 #include "clang/Basic/LLVM.h"
13 #include "llvm/ADT/ArrayRef.h"
14 #include "llvm/ADT/STLExtras.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/Compiler.h"
17 #include <cassert>
18 #include <functional>
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
23 namespace clang {
24 namespace driver {
25 
26 /// This corresponds to a single GCC Multilib, or a segment of one controlled
27 /// by a command line flag
28 class Multilib {
29 public:
30  using flags_list = std::vector<std::string>;
31 
32 private:
33  std::string GCCSuffix;
34  std::string OSSuffix;
35  std::string IncludeSuffix;
36  flags_list Flags;
37 
38 public:
39  Multilib(StringRef GCCSuffix = {}, StringRef OSSuffix = {},
40  StringRef IncludeSuffix = {});
41 
42  /// Get the detected GCC installation path suffix for the multi-arch
43  /// target variant. Always starts with a '/', unless empty
44  const std::string &gccSuffix() const {
45  assert(GCCSuffix.empty() ||
46  (StringRef(GCCSuffix).front() == '/' && GCCSuffix.size() > 1));
47  return GCCSuffix;
48  }
49 
50  /// Set the GCC installation path suffix.
51  Multilib &gccSuffix(StringRef S);
52 
53  /// Get the detected os path suffix for the multi-arch
54  /// target variant. Always starts with a '/', unless empty
55  const std::string &osSuffix() const {
56  assert(OSSuffix.empty() ||
57  (StringRef(OSSuffix).front() == '/' && OSSuffix.size() > 1));
58  return OSSuffix;
59  }
60 
61  /// Set the os path suffix.
62  Multilib &osSuffix(StringRef S);
63 
64  /// Get the include directory suffix. Always starts with a '/', unless
65  /// empty
66  const std::string &includeSuffix() const {
67  assert(IncludeSuffix.empty() ||
68  (StringRef(IncludeSuffix).front() == '/' && IncludeSuffix.size() > 1));
69  return IncludeSuffix;
70  }
71 
72  /// Set the include directory suffix
73  Multilib &includeSuffix(StringRef S);
74 
75  /// Get the flags that indicate or contraindicate this multilib's use
76  /// All elements begin with either '+' or '-'
77  const flags_list &flags() const { return Flags; }
78  flags_list &flags() { return Flags; }
79 
80  /// Add a flag to the flags list
81  /// \p Flag must be a flag accepted by the driver with its leading '-' removed,
82  /// and replaced with either:
83  /// '-' which contraindicates using this multilib with that flag
84  /// or:
85  /// '+' which promotes using this multilib in the presence of that flag
86  /// otherwise '-print-multi-lib' will not emit them correctly.
87  Multilib &flag(StringRef F) {
88  assert(F.front() == '+' || F.front() == '-');
89  Flags.push_back(F);
90  return *this;
91  }
92 
93  LLVM_DUMP_METHOD void dump() const;
94  /// print summary of the Multilib
95  void print(raw_ostream &OS) const;
96 
97  /// Check whether any of the 'against' flags contradict the 'for' flags.
98  bool isValid() const;
99 
100  /// Check whether the default is selected
101  bool isDefault() const
102  { return GCCSuffix.empty() && OSSuffix.empty() && IncludeSuffix.empty(); }
103 
104  bool operator==(const Multilib &Other) const;
105 };
106 
107 raw_ostream &operator<<(raw_ostream &OS, const Multilib &M);
108 
109 class MultilibSet {
110 public:
111  using multilib_list = std::vector<Multilib>;
112  using iterator = multilib_list::iterator;
113  using const_iterator = multilib_list::const_iterator;
114  using IncludeDirsFunc =
115  std::function<std::vector<std::string>(const Multilib &M)>;
116  using FilterCallback = llvm::function_ref<bool(const Multilib &)>;
117 
118 private:
119  multilib_list Multilibs;
120  IncludeDirsFunc IncludeCallback;
121  IncludeDirsFunc FilePathsCallback;
122 
123 public:
124  MultilibSet() = default;
125 
126  /// Add an optional Multilib segment
127  MultilibSet &Maybe(const Multilib &M);
128 
129  /// Add a set of mutually incompatible Multilib segments
130  MultilibSet &Either(const Multilib &M1, const Multilib &M2);
131  MultilibSet &Either(const Multilib &M1, const Multilib &M2,
132  const Multilib &M3);
133  MultilibSet &Either(const Multilib &M1, const Multilib &M2,
134  const Multilib &M3, const Multilib &M4);
135  MultilibSet &Either(const Multilib &M1, const Multilib &M2,
136  const Multilib &M3, const Multilib &M4,
137  const Multilib &M5);
138  MultilibSet &Either(ArrayRef<Multilib> Ms);
139 
140  /// Filter out some subset of the Multilibs using a user defined callback
141  MultilibSet &FilterOut(FilterCallback F);
142 
143  /// Filter out those Multilibs whose gccSuffix matches the given expression
144  MultilibSet &FilterOut(const char *Regex);
145 
146  /// Add a completed Multilib to the set
147  void push_back(const Multilib &M);
148 
149  /// Union this set of multilibs with another
150  void combineWith(const MultilibSet &MS);
151 
152  /// Remove all of the multilibs from the set
153  void clear() { Multilibs.clear(); }
154 
155  iterator begin() { return Multilibs.begin(); }
156  const_iterator begin() const { return Multilibs.begin(); }
157 
158  iterator end() { return Multilibs.end(); }
159  const_iterator end() const { return Multilibs.end(); }
160 
161  /// Pick the best multilib in the set, \returns false if none are compatible
162  bool select(const Multilib::flags_list &Flags, Multilib &M) const;
163 
164  unsigned size() const { return Multilibs.size(); }
165 
166  LLVM_DUMP_METHOD void dump() const;
167  void print(raw_ostream &OS) const;
168 
170  IncludeCallback = std::move(F);
171  return *this;
172  }
173 
174  const IncludeDirsFunc &includeDirsCallback() const { return IncludeCallback; }
175 
177  FilePathsCallback = std::move(F);
178  return *this;
179  }
180 
181  const IncludeDirsFunc &filePathsCallback() const { return FilePathsCallback; }
182 
183 private:
184  /// Apply the filter to Multilibs and return the subset that remains
185  static multilib_list filterCopy(FilterCallback F, const multilib_list &Ms);
186 
187  /// Apply the filter to the multilib_list, removing those that don't match
188  static void filterInPlace(FilterCallback F, multilib_list &Ms);
189 };
190 
191 raw_ostream &operator<<(raw_ostream &OS, const MultilibSet &MS);
192 
193 } // namespace driver
194 } // namespace clang
195 
196 #endif // LLVM_CLANG_DRIVER_MULTILIB_H
bool operator==(const Multilib &Other) const
Definition: Multilib.cpp:113
bool isDefault() const
Check whether the default is selected.
Definition: Multilib.h:101
const IncludeDirsFunc & includeDirsCallback() const
Definition: Multilib.h:174
std::vector< Multilib > multilib_list
Definition: Multilib.h:111
Multilib(StringRef GCCSuffix={}, StringRef OSSuffix={}, StringRef IncludeSuffix={})
Definition: Multilib.cpp:53
const IncludeDirsFunc & filePathsCallback() const
Definition: Multilib.h:181
multilib_list::const_iterator const_iterator
Definition: Multilib.h:113
Multilib & flag(StringRef F)
Add a flag to the flags list Flag must be a flag accepted by the driver with its leading &#39;-&#39; removed...
Definition: Multilib.h:87
const std::string & includeSuffix() const
Get the include directory suffix.
Definition: Multilib.h:66
raw_ostream & operator<<(raw_ostream &OS, const Multilib &M)
Definition: Multilib.cpp:136
const std::string & gccSuffix() const
Get the detected GCC installation path suffix for the multi-arch target variant.
Definition: Multilib.h:44
void clear()
Remove all of the multilibs from the set.
Definition: Multilib.h:153
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
const std::string & osSuffix() const
Get the detected os path suffix for the multi-arch target variant.
Definition: Multilib.h:55
MultilibSet & setIncludeDirsCallback(IncludeDirsFunc F)
Definition: Multilib.h:169
const_iterator end() const
Definition: Multilib.h:159
std::vector< std::string > flags_list
Definition: Multilib.h:30
unsigned size() const
Definition: Multilib.h:164
const flags_list & flags() const
Get the flags that indicate or contraindicate this multilib&#39;s use All elements begin with either &#39;+&#39; ...
Definition: Multilib.h:77
This corresponds to a single GCC Multilib, or a segment of one controlled by a command line flag...
Definition: Multilib.h:28
bool isValid() const
Check whether any of the &#39;against&#39; flags contradict the &#39;for&#39; flags.
Definition: Multilib.cpp:97
const_iterator begin() const
Definition: Multilib.h:156
char __ovld __cnfn select(char a, char b, char c)
For each component of a vector type, result[i] = if MSB of c[i] is set ? b[i] : a[i].
void print(raw_ostream &OS) const
print summary of the Multilib
Definition: Multilib.cpp:83
MultilibSet & setFilePathsCallback(IncludeDirsFunc F)
Definition: Multilib.h:176
llvm::function_ref< bool(const Multilib &)> FilterCallback
Definition: Multilib.h:116
Dataflow Directional Tag Classes.
std::function< std::vector< std::string >(const Multilib &M)> IncludeDirsFunc
Definition: Multilib.h:115
LLVM_DUMP_METHOD void dump() const
Definition: Multilib.cpp:79
flags_list & flags()
Definition: Multilib.h:78
multilib_list::iterator iterator
Definition: Multilib.h:112