aboutsummaryrefslogtreecommitdiff
path: root/llvm/utils/TableGen/Common/OptEmitter.cpp
blob: f914909ebe5e48fc90bae2c21aa7b77734aa89a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//===- OptEmitter.cpp - Helper for emitting options -------------*- C++ -*-===//
//
// 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 "OptEmitter.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/OptionStrCmp.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"

// Returns true if A is ordered before B.
bool llvm::IsOptionRecordsLess(const Record *A, const Record *B) {
  if (A == B)
    return false;

  // Sentinel options precede all others and are only ordered by precedence.
  const Record *AKind = A->getValueAsDef("Kind");
  const Record *BKind = B->getValueAsDef("Kind");

  bool ASent = AKind->getValueAsBit("Sentinel");
  bool BSent = BKind->getValueAsBit("Sentinel");
  if (ASent != BSent)
    return ASent;

  std::vector<StringRef> APrefixes = A->getValueAsListOfStrings("Prefixes");
  std::vector<StringRef> BPrefixes = B->getValueAsListOfStrings("Prefixes");

  // Compare options by name, unless they are sentinels.
  if (!ASent) {
    if (int Cmp = StrCmpOptionName(A->getValueAsString("Name"),
                                   B->getValueAsString("Name")))
      return Cmp < 0;

    if (int Cmp = StrCmpOptionPrefixes(APrefixes, BPrefixes))
      return Cmp < 0;
  }

  // Then by the kind precedence;
  int APrec = AKind->getValueAsInt("Precedence");
  int BPrec = BKind->getValueAsInt("Precedence");
  if (APrec == BPrec && APrefixes == BPrefixes) {
    PrintError(A->getLoc(), Twine("Option is equivalent to"));
    PrintError(B->getLoc(), Twine("Other defined here"));
    PrintFatalError("Equivalent Options found.");
  }
  return APrec < BPrec;
}