Skip to content

Allow Equivalent option with mutally exclusive OptionFlag. #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion clang/lib/Driver/DriverOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,23 @@ static const OptTable::Info InfoTable[] = {
#undef OPTION
};

// 2 options with different mutually exclusive flags will not happen at same
// time.
static const unsigned MutuallyExclusiveFlags[] = {
clang::driver::options::ClangFlags::CC1Option,
clang::driver::options::ClangFlags::CLOption,
clang::driver::options::ClangFlags::CoreOption,
clang::driver::options::ClangFlags::DXCOption,
clang::driver::options::ClangFlags::FlangOnlyOption |
clang::driver::options::ClangFlags::FlangOption,
};

namespace {

class DriverOptTable : public OptTable {
public:
DriverOptTable()
: OptTable(InfoTable) {}
: OptTable(InfoTable, /*IgnoreCase*/ false, MutuallyExclusiveFlags) {}
};

}
Expand Down
3 changes: 2 additions & 1 deletion llvm/include/llvm/Option/OptTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ class OptTable {
unsigned &Index) const;

protected:
OptTable(ArrayRef<Info> OptionInfos, bool IgnoreCase = false);
OptTable(ArrayRef<Info> OptionInfos, bool IgnoreCase = false,
ArrayRef<unsigned> MutuallyExclusiveFlags = {});

public:
~OptTable();
Expand Down
26 changes: 23 additions & 3 deletions llvm/lib/Option/OptTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ static int StrCmpOptionName(const char *A, const char *B) {
return strcmp(A, B);
}

static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) {
static inline bool
OptInfoCmpLessThan(const OptTable::Info &A, const OptTable::Info &B,
ArrayRef<unsigned> MutuallyExclusiveFlags) {
if (&A == &B)
return false;

Expand All @@ -77,6 +79,23 @@ static inline bool operator<(const OptTable::Info &A, const OptTable::Info &B) {
return N < 0;
}

if (!MutuallyExclusiveFlags.empty()) {
// For Infos with different MutuallyExclusiveFlag, it is OK to not in order,
// they'll not active at same time.
unsigned MutuallyExclusiveFlagsA = 0;
unsigned MutuallyExclusiveFlagsB = 0;
for (auto Flag : MutuallyExclusiveFlags) {
if (A.Flags & Flag)
MutuallyExclusiveFlagsA |= Flag;
if (B.Flags & Flag)
MutuallyExclusiveFlagsB |= Flag;
}

if (MutuallyExclusiveFlagsA != 0 && MutuallyExclusiveFlagsB != 0 &&
(MutuallyExclusiveFlagsA & MutuallyExclusiveFlagsB) == 0)
return true;
}

// Names are the same, check that classes are in order; exactly one
// should be joined, and it should succeed the other.
assert(((A.Kind == Option::JoinedClass) ^ (B.Kind == Option::JoinedClass)) &&
Expand All @@ -95,7 +114,8 @@ static inline bool operator<(const OptTable::Info &I, const char *Name) {

OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {}

OptTable::OptTable(ArrayRef<Info> OptionInfos, bool IgnoreCase)
OptTable::OptTable(ArrayRef<Info> OptionInfos, bool IgnoreCase,
ArrayRef<unsigned> MutuallyExclusiveFlags)
: OptionInfos(OptionInfos), IgnoreCase(IgnoreCase) {
// Explicitly zero initialize the error to work around a bug in array
// value-initialization on MinGW with gcc 4.3.5.
Expand Down Expand Up @@ -128,7 +148,7 @@ OptTable::OptTable(ArrayRef<Info> OptionInfos, bool IgnoreCase)

// Check that options are in order.
for (unsigned i = FirstSearchableIndex + 1, e = getNumOptions(); i != e; ++i){
if (!(getInfo(i) < getInfo(i + 1))) {
if (!OptInfoCmpLessThan(getInfo(i), getInfo(i + 1), MutuallyExclusiveFlags)) {
getOption(i).dump();
getOption(i + 1).dump();
llvm_unreachable("Options are not in order!");
Expand Down