
This patch moves the CommonArgs utilities into a location visible by the Frontend Drivers, so that the Frontend Drivers may share option parsing code with the Compiler Driver. This is useful when the Frontend Drivers would like to verify that their incoming options are well-formed and also not reinvent the option parsing wheel. We already see code in the Clang/Flang Drivers that is parsing and verifying its incoming options. E.g. OPT_ffp_contract. This option is parsed in the Compiler Driver, Clang Driver, and Flang Driver, all with slightly different parsing code. It would be nice if the Frontend Drivers were not required to duplicate this Compiler Driver code. That way there is no/low maintenance burden on keeping all these parsing functions in sync. Along those lines, the Frontend Drivers will now have a useful mechanism to verify their incoming options are well-formed. Currently, the Frontend Drivers trust that the Compiler Driver is not passing back junk in some cases. The Language Drivers may even accept junk with no error at all. E.g.: `clang -cc1 -mprefer-vector-width=junk test.c' With this patch, we'll now be able to tighten up incomming options to the Frontend drivers in a lightweight way. --------- Co-authored-by: Cameron McInally <cmcinally@nvidia.com> Co-authored-by: Shafik Yaghmour <shafik.yaghmour@intel.com>
194 lines
5.6 KiB
C++
194 lines
5.6 KiB
C++
//===- MultilibBuilder.cpp - MultilibBuilder Implementation -===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "clang/Driver/MultilibBuilder.h"
|
|
#include "clang/Driver/CommonArgs.h"
|
|
#include "llvm/ADT/StringMap.h"
|
|
#include "llvm/Support/Path.h"
|
|
#include "llvm/Support/Regex.h"
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace clang;
|
|
using namespace driver;
|
|
|
|
/// normalize Segment to "/foo/bar" or "".
|
|
static void normalizePathSegment(std::string &Segment) {
|
|
StringRef seg = Segment;
|
|
|
|
// Prune trailing "/" or "./"
|
|
while (true) {
|
|
StringRef last = llvm::sys::path::filename(seg);
|
|
if (last != ".")
|
|
break;
|
|
seg = llvm::sys::path::parent_path(seg);
|
|
}
|
|
|
|
if (seg.empty() || seg == "/") {
|
|
Segment.clear();
|
|
return;
|
|
}
|
|
|
|
// Add leading '/'
|
|
if (seg.front() != '/') {
|
|
Segment = "/" + seg.str();
|
|
} else {
|
|
Segment = std::string(seg);
|
|
}
|
|
}
|
|
|
|
MultilibBuilder::MultilibBuilder(StringRef GCC, StringRef OS, StringRef Include)
|
|
: GCCSuffix(GCC), OSSuffix(OS), IncludeSuffix(Include) {
|
|
normalizePathSegment(GCCSuffix);
|
|
normalizePathSegment(OSSuffix);
|
|
normalizePathSegment(IncludeSuffix);
|
|
}
|
|
|
|
MultilibBuilder::MultilibBuilder(StringRef Suffix)
|
|
: MultilibBuilder(Suffix, Suffix, Suffix) {}
|
|
|
|
MultilibBuilder &MultilibBuilder::gccSuffix(StringRef S) {
|
|
GCCSuffix = std::string(S);
|
|
normalizePathSegment(GCCSuffix);
|
|
return *this;
|
|
}
|
|
|
|
MultilibBuilder &MultilibBuilder::osSuffix(StringRef S) {
|
|
OSSuffix = std::string(S);
|
|
normalizePathSegment(OSSuffix);
|
|
return *this;
|
|
}
|
|
|
|
MultilibBuilder &MultilibBuilder::includeSuffix(StringRef S) {
|
|
IncludeSuffix = std::string(S);
|
|
normalizePathSegment(IncludeSuffix);
|
|
return *this;
|
|
}
|
|
|
|
bool MultilibBuilder::isValid() const {
|
|
llvm::StringMap<int> FlagSet;
|
|
for (unsigned I = 0, N = Flags.size(); I != N; ++I) {
|
|
StringRef Flag(Flags[I]);
|
|
auto [SI, Inserted] = FlagSet.try_emplace(Flag.substr(1), I);
|
|
|
|
assert(StringRef(Flag).front() == '-' || StringRef(Flag).front() == '!');
|
|
|
|
if (!Inserted && Flags[I] != Flags[SI->getValue()])
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
MultilibBuilder &MultilibBuilder::flag(StringRef Flag, bool Disallow) {
|
|
tools::addMultilibFlag(!Disallow, Flag, Flags);
|
|
return *this;
|
|
}
|
|
|
|
Multilib MultilibBuilder::makeMultilib() const {
|
|
return Multilib(GCCSuffix, OSSuffix, IncludeSuffix, Flags);
|
|
}
|
|
|
|
MultilibSetBuilder &MultilibSetBuilder::Maybe(const MultilibBuilder &M) {
|
|
MultilibBuilder Opposite;
|
|
// Negate positive flags
|
|
for (StringRef Flag : M.flags()) {
|
|
if (Flag.front() == '-')
|
|
Opposite.flag(Flag, /*Disallow=*/true);
|
|
}
|
|
return Either(M, Opposite);
|
|
}
|
|
|
|
MultilibSetBuilder &MultilibSetBuilder::Either(const MultilibBuilder &M1,
|
|
const MultilibBuilder &M2) {
|
|
return Either({M1, M2});
|
|
}
|
|
|
|
MultilibSetBuilder &MultilibSetBuilder::Either(const MultilibBuilder &M1,
|
|
const MultilibBuilder &M2,
|
|
const MultilibBuilder &M3) {
|
|
return Either({M1, M2, M3});
|
|
}
|
|
|
|
MultilibSetBuilder &MultilibSetBuilder::Either(const MultilibBuilder &M1,
|
|
const MultilibBuilder &M2,
|
|
const MultilibBuilder &M3,
|
|
const MultilibBuilder &M4) {
|
|
return Either({M1, M2, M3, M4});
|
|
}
|
|
|
|
MultilibSetBuilder &MultilibSetBuilder::Either(const MultilibBuilder &M1,
|
|
const MultilibBuilder &M2,
|
|
const MultilibBuilder &M3,
|
|
const MultilibBuilder &M4,
|
|
const MultilibBuilder &M5) {
|
|
return Either({M1, M2, M3, M4, M5});
|
|
}
|
|
|
|
static MultilibBuilder compose(const MultilibBuilder &Base,
|
|
const MultilibBuilder &New) {
|
|
SmallString<128> GCCSuffix;
|
|
llvm::sys::path::append(GCCSuffix, "/", Base.gccSuffix(), New.gccSuffix());
|
|
SmallString<128> OSSuffix;
|
|
llvm::sys::path::append(OSSuffix, "/", Base.osSuffix(), New.osSuffix());
|
|
SmallString<128> IncludeSuffix;
|
|
llvm::sys::path::append(IncludeSuffix, "/", Base.includeSuffix(),
|
|
New.includeSuffix());
|
|
|
|
MultilibBuilder Composed(GCCSuffix, OSSuffix, IncludeSuffix);
|
|
|
|
MultilibBuilder::flags_list &Flags = Composed.flags();
|
|
|
|
llvm::append_range(Flags, Base.flags());
|
|
llvm::append_range(Flags, New.flags());
|
|
|
|
return Composed;
|
|
}
|
|
|
|
MultilibSetBuilder &
|
|
MultilibSetBuilder::Either(ArrayRef<MultilibBuilder> MultilibSegments) {
|
|
multilib_list Composed;
|
|
|
|
if (Multilibs.empty())
|
|
llvm::append_range(Multilibs, MultilibSegments);
|
|
else {
|
|
for (const auto &New : MultilibSegments) {
|
|
for (const auto &Base : Multilibs) {
|
|
MultilibBuilder MO = compose(Base, New);
|
|
if (MO.isValid())
|
|
Composed.push_back(MO);
|
|
}
|
|
}
|
|
|
|
Multilibs = Composed;
|
|
}
|
|
|
|
return *this;
|
|
}
|
|
|
|
MultilibSetBuilder &MultilibSetBuilder::FilterOut(const char *Regex) {
|
|
llvm::Regex R(Regex);
|
|
#ifndef NDEBUG
|
|
std::string Error;
|
|
if (!R.isValid(Error)) {
|
|
llvm::errs() << Error;
|
|
llvm_unreachable("Invalid regex!");
|
|
}
|
|
#endif
|
|
llvm::erase_if(Multilibs, [&R](const MultilibBuilder &M) {
|
|
return R.match(M.gccSuffix());
|
|
});
|
|
return *this;
|
|
}
|
|
|
|
MultilibSet MultilibSetBuilder::makeMultilibSet() const {
|
|
MultilibSet Result;
|
|
for (const auto &M : Multilibs) {
|
|
Result.push_back(M.makeMultilib());
|
|
}
|
|
return Result;
|
|
}
|