Path: blob/main/contrib/llvm-project/llvm/utils/TableGen/Common/OptEmitter.cpp
35290 views
//===- OptEmitter.cpp - Helper for emitting options.----------- -----------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "OptEmitter.h"9#include "llvm/ADT/Twine.h"10#include "llvm/TableGen/Error.h"11#include "llvm/TableGen/Record.h"12#include <cctype>13#include <cstring>1415namespace llvm {1617// Ordering on Info. The logic should match with the consumer-side function in18// llvm/Option/OptTable.h.19// FIXME: Make this take StringRefs instead of null terminated strings to20// simplify callers.21static int StrCmpOptionName(const char *A, const char *B) {22const char *X = A, *Y = B;23char a = tolower(*A), b = tolower(*B);24while (a == b) {25if (a == '\0')26return strcmp(A, B);2728a = tolower(*++X);29b = tolower(*++Y);30}3132if (a == '\0') // A is a prefix of B.33return 1;34if (b == '\0') // B is a prefix of A.35return -1;3637// Otherwise lexicographic.38return (a < b) ? -1 : 1;39}4041int CompareOptionRecords(Record *const *Av, Record *const *Bv) {42const Record *A = *Av;43const Record *B = *Bv;4445// Sentinel options precede all others and are only ordered by precedence.46bool ASent = A->getValueAsDef("Kind")->getValueAsBit("Sentinel");47bool BSent = B->getValueAsDef("Kind")->getValueAsBit("Sentinel");48if (ASent != BSent)49return ASent ? -1 : 1;5051// Compare options by name, unless they are sentinels.52if (!ASent)53if (int Cmp = StrCmpOptionName(A->getValueAsString("Name").str().c_str(),54B->getValueAsString("Name").str().c_str()))55return Cmp;5657if (!ASent) {58std::vector<StringRef> APrefixes = A->getValueAsListOfStrings("Prefixes");59std::vector<StringRef> BPrefixes = B->getValueAsListOfStrings("Prefixes");6061for (std::vector<StringRef>::const_iterator APre = APrefixes.begin(),62AEPre = APrefixes.end(),63BPre = BPrefixes.begin(),64BEPre = BPrefixes.end();65APre != AEPre && BPre != BEPre; ++APre, ++BPre) {66if (int Cmp = StrCmpOptionName(APre->str().c_str(), BPre->str().c_str()))67return Cmp;68}69}7071// Then by the kind precedence;72int APrec = A->getValueAsDef("Kind")->getValueAsInt("Precedence");73int BPrec = B->getValueAsDef("Kind")->getValueAsInt("Precedence");74if (APrec == BPrec && A->getValueAsListOfStrings("Prefixes") ==75B->getValueAsListOfStrings("Prefixes")) {76PrintError(A->getLoc(), Twine("Option is equivalent to"));77PrintError(B->getLoc(), Twine("Other defined here"));78PrintFatalError("Equivalent Options found.");79}80return APrec < BPrec ? -1 : 1;81}8283} // namespace llvm848586