Path: blob/main/contrib/llvm-project/llvm/utils/TableGen/Common/Utils.cpp
213799 views
//===- Utils.cpp - Common Utilities -----------------------------*- C++ -*-===//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 "Utils.h"9#include "llvm/ADT/STLExtras.h"10#include "llvm/TableGen/Error.h"11#include "llvm/TableGen/Record.h"12#include <algorithm>1314using namespace llvm;1516namespace {17/// Sorting predicate to sort record pointers by their Name field, and break18/// ties using record ID (which corresponds to creation/parse order).19struct LessRecordFieldNameAndID {20bool operator()(const Record *Rec1, const Record *Rec2) const {21return std::tuple(Rec1->getValueAsString("Name"), Rec1->getID()) <22std::tuple(Rec2->getValueAsString("Name"), Rec2->getID());23}24};25} // End anonymous namespace2627/// Sort an array of Records on the "Name" field, and check for records with28/// duplicate "Name" field. If duplicates are found, report a fatal error.29void llvm::sortAndReportDuplicates(MutableArrayRef<const Record *> Records,30StringRef ObjectName) {31llvm::sort(Records, LessRecordFieldNameAndID());3233auto I = std::adjacent_find(Records.begin(), Records.end(),34[](const Record *Rec1, const Record *Rec2) {35return Rec1->getValueAsString("Name") ==36Rec2->getValueAsString("Name");37});38if (I == Records.end())39return;4041// Found a duplicate name.42const Record *First = *I;43const Record *Second = *(I + 1);44StringRef Name = First->getValueAsString("Name");45PrintError(Second, ObjectName + " `" + Name + "` is already defined.");46PrintFatalNote(First, "Previous definition here.");47}484950