Rahul Joshi 8ce5a32f02
[TableGen] Rework error reporting for duplicate Feature/Processor (#102257)
- Extract code for sorting and checking duplicate Records into a helper
  function and update `collectProcModels` to use the helper.
- Update `FeatureKeyValues` to:
  (a) Remove code for duplicate checks and use the helper.
  (b) Trim features with empty name explicitly to be able to use
      the helper.
- Make the sorting deterministic by using record name as a secondary
  key for sorting, and re-enable SubtargetFeatureUniqueNames.td test
  that was disabled due to the non-determinism of the error messages.
- Change wording of error message when duplicate records are found to
  be source code position agnostic, since `First` may not be before
  `Second` lexically.
2024-08-08 02:00:36 +03:00

49 lines
1.9 KiB
C++

//===- Utils.cpp - Common Utilities -----------------------------*- 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 "Utils.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/Record.h"
#include <algorithm>
using namespace llvm;
namespace {
/// Sorting predicate to sort record pointers by their Name field, and break
/// ties using record ID (which corresponds to creation/parse order).
struct LessRecordFieldNameAndID {
bool operator()(const Record *Rec1, const Record *Rec2) const {
return std::tuple(Rec1->getValueAsString("Name"), Rec1->getID()) <
std::tuple(Rec2->getValueAsString("Name"), Rec2->getID());
}
};
} // End anonymous namespace
/// Sort an array of Records on the "Name" field, and check for records with
/// duplicate "Name" field. If duplicates are found, report a fatal error.
void llvm::sortAndReportDuplicates(MutableArrayRef<Record *> Records,
StringRef ObjectName) {
llvm::sort(Records, LessRecordFieldNameAndID());
auto I = std::adjacent_find(Records.begin(), Records.end(),
[](const Record *Rec1, const Record *Rec2) {
return Rec1->getValueAsString("Name") ==
Rec2->getValueAsString("Name");
});
if (I == Records.end())
return;
// Found a duplicate name.
const Record *First = *I;
const Record *Second = *(I + 1);
StringRef Name = First->getValueAsString("Name");
PrintError(Second, ObjectName + " `" + Name + "` is already defined.");
PrintFatalNote(First, "Previous definition here.");
}