|
8 | 8 |
|
9 | 9 | #include "OptEmitter.h" |
10 | 10 | #include "llvm/ADT/Twine.h" |
| 11 | +#include "llvm/Support/OptionStrCmp.h" |
11 | 12 | #include "llvm/TableGen/Error.h" |
12 | 13 | #include "llvm/TableGen/Record.h" |
13 | | -#include <cctype> |
14 | | -#include <cstring> |
15 | | - |
16 | | -namespace llvm { |
17 | | - |
18 | | -// Ordering on Info. The logic should match with the consumer-side function in |
19 | | -// llvm/Option/OptTable.h. |
20 | | -// FIXME: Make this take StringRefs instead of null terminated strings to |
21 | | -// simplify callers. |
22 | | -static int StrCmpOptionName(const char *A, const char *B) { |
23 | | - const char *X = A, *Y = B; |
24 | | - char a = tolower(*A), b = tolower(*B); |
25 | | - while (a == b) { |
26 | | - if (a == '\0') |
27 | | - return strcmp(A, B); |
28 | | - |
29 | | - a = tolower(*++X); |
30 | | - b = tolower(*++Y); |
31 | | - } |
32 | | - |
33 | | - if (a == '\0') // A is a prefix of B. |
34 | | - return 1; |
35 | | - if (b == '\0') // B is a prefix of A. |
36 | | - return -1; |
37 | | - |
38 | | - // Otherwise lexicographic. |
39 | | - return (a < b) ? -1 : 1; |
40 | | -} |
41 | 14 |
|
42 | 15 | // Returns true if A is ordered before B. |
43 | | -bool CompareOptionRecords(const Record *A, const Record *B) { |
| 16 | +bool llvm::IsOptionRecordsLess(const Record *A, const Record *B) { |
44 | 17 | if (A == B) |
45 | 18 | return false; |
| 19 | + |
46 | 20 | // Sentinel options precede all others and are only ordered by precedence. |
47 | | - bool ASent = A->getValueAsDef("Kind")->getValueAsBit("Sentinel"); |
48 | | - bool BSent = B->getValueAsDef("Kind")->getValueAsBit("Sentinel"); |
| 21 | + const Record *AKind = A->getValueAsDef("Kind"); |
| 22 | + const Record *BKind = B->getValueAsDef("Kind"); |
| 23 | + |
| 24 | + bool ASent = AKind->getValueAsBit("Sentinel"); |
| 25 | + bool BSent = BKind->getValueAsBit("Sentinel"); |
49 | 26 | if (ASent != BSent) |
50 | 27 | return ASent; |
51 | 28 |
|
52 | | - // Compare options by name, unless they are sentinels. |
53 | | - if (!ASent) |
54 | | - if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").str().c_str(), |
55 | | - B->getValueAsString("Name").str().c_str())) |
56 | | - return Cmp < 0; |
| 29 | + std::vector<StringRef> APrefixes = A->getValueAsListOfStrings("Prefixes"); |
| 30 | + std::vector<StringRef> BPrefixes = B->getValueAsListOfStrings("Prefixes"); |
57 | 31 |
|
| 32 | + // Compare options by name, unless they are sentinels. |
58 | 33 | if (!ASent) { |
59 | | - std::vector<StringRef> APrefixes = A->getValueAsListOfStrings("Prefixes"); |
60 | | - std::vector<StringRef> BPrefixes = B->getValueAsListOfStrings("Prefixes"); |
| 34 | + if (int Cmp = StrCmpOptionName(A->getValueAsString("Name"), |
| 35 | + B->getValueAsString("Name"))) |
| 36 | + return Cmp < 0; |
61 | 37 |
|
62 | | - for (std::vector<StringRef>::const_iterator APre = APrefixes.begin(), |
63 | | - AEPre = APrefixes.end(), |
64 | | - BPre = BPrefixes.begin(), |
65 | | - BEPre = BPrefixes.end(); |
66 | | - APre != AEPre && BPre != BEPre; ++APre, ++BPre) { |
67 | | - if (int Cmp = StrCmpOptionName(APre->str().c_str(), BPre->str().c_str())) |
68 | | - return Cmp < 0; |
69 | | - } |
| 38 | + if (int Cmp = StrCmpOptionPrefixes(APrefixes, BPrefixes)) |
| 39 | + return Cmp < 0; |
70 | 40 | } |
71 | 41 |
|
72 | 42 | // Then by the kind precedence; |
73 | | - int APrec = A->getValueAsDef("Kind")->getValueAsInt("Precedence"); |
74 | | - int BPrec = B->getValueAsDef("Kind")->getValueAsInt("Precedence"); |
75 | | - if (APrec == BPrec && A->getValueAsListOfStrings("Prefixes") == |
76 | | - B->getValueAsListOfStrings("Prefixes")) { |
| 43 | + int APrec = AKind->getValueAsInt("Precedence"); |
| 44 | + int BPrec = BKind->getValueAsInt("Precedence"); |
| 45 | + if (APrec == BPrec && APrefixes == BPrefixes) { |
77 | 46 | PrintError(A->getLoc(), Twine("Option is equivalent to")); |
78 | 47 | PrintError(B->getLoc(), Twine("Other defined here")); |
79 | 48 | PrintFatalError("Equivalent Options found."); |
80 | 49 | } |
81 | 50 | return APrec < BPrec; |
82 | 51 | } |
83 | | - |
84 | | -} // namespace llvm |
0 commit comments