Skip to content

[clangl[TableGen] Change Diagnostic Emitter to use const RecordKeeper #108209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 52 additions & 46 deletions clang/utils/TableGen/ClangDiagnosticsEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ using namespace llvm;

namespace {
class DiagGroupParentMap {
RecordKeeper &Records;
std::map<const Record*, std::vector<Record*> > Mapping;
const RecordKeeper &Records;
std::map<const Record *, std::vector<const Record *>> Mapping;

public:
DiagGroupParentMap(RecordKeeper &records) : Records(records) {
std::vector<Record*> DiagGroups
= Records.getAllDerivedDefinitions("DiagGroup");
DiagGroupParentMap(const RecordKeeper &records) : Records(records) {
ArrayRef<const Record *> DiagGroups =
Records.getAllDerivedDefinitions("DiagGroup");
for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
std::vector<Record*> SubGroups =
DiagGroups[i]->getValueAsListOfDefs("SubGroups");
Expand All @@ -53,7 +54,7 @@ class DiagGroupParentMap {
}
}

const std::vector<Record*> &getParents(const Record *Group) {
const std::vector<const Record *> &getParents(const Record *Group) {
return Mapping[Group];
}
};
Expand All @@ -68,7 +69,8 @@ getCategoryFromDiagGroup(const Record *Group,

// The diag group may the subgroup of one or more other diagnostic groups,
// check these for a category as well.
const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
const std::vector<const Record *> &Parents =
DiagGroupParents.getParents(Group);
for (unsigned i = 0, e = Parents.size(); i != e; ++i) {
CatName = getCategoryFromDiagGroup(Parents[i], DiagGroupParents);
if (!CatName.empty()) return CatName;
Expand All @@ -94,19 +96,19 @@ static std::string getDiagnosticCategory(const Record *R,

namespace {
class DiagCategoryIDMap {
RecordKeeper &Records;
const RecordKeeper &Records;
StringMap<unsigned> CategoryIDs;
std::vector<std::string> CategoryStrings;
public:
DiagCategoryIDMap(RecordKeeper &records) : Records(records) {
DiagCategoryIDMap(const RecordKeeper &records) : Records(records) {
DiagGroupParentMap ParentInfo(Records);

// The zero'th category is "".
CategoryStrings.push_back("");
CategoryIDs[""] = 0;

std::vector<Record*> Diags =
Records.getAllDerivedDefinitions("Diagnostic");
ArrayRef<const Record *> Diags =
Records.getAllDerivedDefinitions("Diagnostic");
for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
std::string Category = getDiagnosticCategory(Diags[i], ParentInfo);
if (Category.empty()) continue; // Skip diags with no category.
Expand Down Expand Up @@ -153,8 +155,8 @@ static bool diagGroupBeforeByName(const Record *LHS, const Record *RHS) {

/// Invert the 1-[0/1] mapping of diags to group into a one to many
/// mapping of groups to diags in the group.
static void groupDiagnostics(const std::vector<Record*> &Diags,
const std::vector<Record*> &DiagGroups,
static void groupDiagnostics(ArrayRef<const Record *> Diags,
ArrayRef<const Record *> DiagGroups,
std::map<std::string, GroupInfo> &DiagsInGroup) {

for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
Expand All @@ -172,7 +174,7 @@ static void groupDiagnostics(const std::vector<Record*> &Diags,
// Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
// groups (these are warnings that GCC supports that clang never produces).
for (unsigned i = 0, e = DiagGroups.size(); i != e; ++i) {
Record *Group = DiagGroups[i];
const Record *Group = DiagGroups[i];
GroupInfo &GI =
DiagsInGroup[std::string(Group->getValueAsString("GroupName"))];
GI.GroupName = Group->getName();
Expand Down Expand Up @@ -255,20 +257,18 @@ class InferPedantic {
GMap;

DiagGroupParentMap &DiagGroupParents;
const std::vector<Record*> &Diags;
const std::vector<Record*> DiagGroups;
ArrayRef<const Record *> Diags;
const std::vector<const Record *> DiagGroups;
std::map<std::string, GroupInfo> &DiagsInGroup;
llvm::DenseSet<const Record*> DiagsSet;
GMap GroupCount;
public:
InferPedantic(DiagGroupParentMap &DiagGroupParents,
const std::vector<Record*> &Diags,
const std::vector<Record*> &DiagGroups,
ArrayRef<const Record *> Diags,
ArrayRef<const Record *> DiagGroups,
std::map<std::string, GroupInfo> &DiagsInGroup)
: DiagGroupParents(DiagGroupParents),
Diags(Diags),
DiagGroups(DiagGroups),
DiagsInGroup(DiagsInGroup) {}
: DiagGroupParents(DiagGroupParents), Diags(Diags),
DiagGroups(DiagGroups), DiagsInGroup(DiagsInGroup) {}

/// Compute the set of diagnostics and groups that are immediately
/// in -Wpedantic.
Expand Down Expand Up @@ -302,7 +302,8 @@ bool InferPedantic::isSubGroupOfGroup(const Record *Group,
if (GName == GroupName)
return true;

const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
const std::vector<const Record *> &Parents =
DiagGroupParents.getParents(Group);
for (unsigned i = 0, e = Parents.size(); i != e; ++i)
if (isSubGroupOfGroup(Parents[i], GName))
return true;
Expand Down Expand Up @@ -347,7 +348,8 @@ void InferPedantic::markGroup(const Record *Group) {
// group's count is equal to the number of subgroups and diagnostics in
// that group, we can safely add this group to -Wpedantic.
if (groupInPedantic(Group, /* increment */ true)) {
const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
const std::vector<const Record *> &Parents =
DiagGroupParents.getParents(Group);
for (unsigned i = 0, e = Parents.size(); i != e; ++i)
markGroup(Parents[i]);
}
Expand All @@ -359,7 +361,7 @@ void InferPedantic::compute(VecOrSet DiagsInPedantic,
// "pedantic" group. For those that aren't explicitly included in -Wpedantic,
// mark them for consideration to be included in -Wpedantic directly.
for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
Record *R = Diags[i];
const Record *R = Diags[i];
if (isExtension(R) && isOffByDefault(R)) {
DiagsSet.insert(R);
if (DefInit *Group = dyn_cast<DefInit>(R->getValueInit("Group"))) {
Expand All @@ -375,7 +377,7 @@ void InferPedantic::compute(VecOrSet DiagsInPedantic,
// march through Diags a second time to ensure the results are emitted
// in deterministic order.
for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
Record *R = Diags[i];
const Record *R = Diags[i];
if (!DiagsSet.count(R))
continue;
// Check if the group is implicitly in -Wpedantic. If so,
Expand All @@ -401,13 +403,14 @@ void InferPedantic::compute(VecOrSet DiagsInPedantic,
// march through the groups to ensure the results are emitted
/// in a deterministc order.
for (unsigned i = 0, ei = DiagGroups.size(); i != ei; ++i) {
Record *Group = DiagGroups[i];
const Record *Group = DiagGroups[i];
if (!groupInPedantic(Group))
continue;

const std::vector<Record*> &Parents = DiagGroupParents.getParents(Group);
bool AllParentsInPedantic =
llvm::all_of(Parents, [&](Record *R) { return groupInPedantic(R); });
const std::vector<const Record *> &Parents =
DiagGroupParents.getParents(Group);
bool AllParentsInPedantic = llvm::all_of(
Parents, [&](const Record *R) { return groupInPedantic(R); });
// If all the parents are in -Wpedantic, this means that this diagnostic
// group will be indirectly included by -Wpedantic already. In that
// case, do not add it directly to -Wpedantic. If the group has no
Expand Down Expand Up @@ -583,7 +586,7 @@ struct DiagnosticTextBuilder {
DiagnosticTextBuilder(DiagnosticTextBuilder const &) = delete;
DiagnosticTextBuilder &operator=(DiagnosticTextBuilder const &) = delete;

DiagnosticTextBuilder(RecordKeeper &Records) {
DiagnosticTextBuilder(const RecordKeeper &Records) {
// Build up the list of substitution records.
for (auto *S : Records.getAllDerivedDefinitions("TextSubstitution")) {
EvaluatingRecordGuard Guard(&EvaluatingRecord, S);
Expand All @@ -593,7 +596,7 @@ struct DiagnosticTextBuilder {

// Check that no diagnostic definitions have the same name as a
// substitution.
for (Record *Diag : Records.getAllDerivedDefinitions("Diagnostic")) {
for (const Record *Diag : Records.getAllDerivedDefinitions("Diagnostic")) {
StringRef Name = Diag->getName();
if (Substitutions.count(Name))
llvm::PrintFatalError(
Expand Down Expand Up @@ -1407,7 +1410,7 @@ static void verifyDiagnosticWording(const Record &Diag) {

/// ClangDiagsDefsEmitter - The top-level class emits .def files containing
/// declarations of Clang diagnostics.
void clang::EmitClangDiagsDefs(RecordKeeper &Records, raw_ostream &OS,
void clang::EmitClangDiagsDefs(const RecordKeeper &Records, raw_ostream &OS,
const std::string &Component) {
// Write the #if guard
if (!Component.empty()) {
Expand All @@ -1421,10 +1424,11 @@ void clang::EmitClangDiagsDefs(RecordKeeper &Records, raw_ostream &OS,

DiagnosticTextBuilder DiagTextBuilder(Records);

std::vector<Record *> Diags = Records.getAllDerivedDefinitions("Diagnostic");
ArrayRef<const Record *> Diags =
Records.getAllDerivedDefinitions("Diagnostic");

std::vector<Record*> DiagGroups
= Records.getAllDerivedDefinitions("DiagGroup");
ArrayRef<const Record *> DiagGroups =
Records.getAllDerivedDefinitions("DiagGroup");

std::map<std::string, GroupInfo> DiagsInGroup;
groupDiagnostics(Diags, DiagGroups, DiagsInGroup);
Expand Down Expand Up @@ -1764,21 +1768,22 @@ static void emitDiagTable(std::map<std::string, GroupInfo> &DiagsInGroup,
/// CATEGORY("Lambda Issue", DiagCat_Lambda_Issue)
/// #endif
/// \endcode
static void emitCategoryTable(RecordKeeper &Records, raw_ostream &OS) {
static void emitCategoryTable(const RecordKeeper &Records, raw_ostream &OS) {
DiagCategoryIDMap CategoriesByID(Records);
OS << "\n#ifdef GET_CATEGORY_TABLE\n";
for (auto const &C : CategoriesByID)
OS << "CATEGORY(\"" << C << "\", " << getDiagCategoryEnum(C) << ")\n";
OS << "#endif // GET_CATEGORY_TABLE\n\n";
}

void clang::EmitClangDiagGroups(RecordKeeper &Records, raw_ostream &OS) {
void clang::EmitClangDiagGroups(const RecordKeeper &Records, raw_ostream &OS) {
// Compute a mapping from a DiagGroup to all of its parents.
DiagGroupParentMap DGParentMap(Records);

std::vector<Record *> Diags = Records.getAllDerivedDefinitions("Diagnostic");
ArrayRef<const Record *> Diags =
Records.getAllDerivedDefinitions("Diagnostic");

std::vector<Record *> DiagGroups =
ArrayRef<const Record *> DiagGroups =
Records.getAllDerivedDefinitions("DiagGroup");

std::map<std::string, GroupInfo> DiagsInGroup;
Expand Down Expand Up @@ -1824,9 +1829,10 @@ struct RecordIndexElement
};
} // end anonymous namespace.

void clang::EmitClangDiagsIndexName(RecordKeeper &Records, raw_ostream &OS) {
const std::vector<Record*> &Diags =
Records.getAllDerivedDefinitions("Diagnostic");
void clang::EmitClangDiagsIndexName(const RecordKeeper &Records,
raw_ostream &OS) {
ArrayRef<const Record *> Diags =
Records.getAllDerivedDefinitions("Diagnostic");

std::vector<RecordIndexElement> Index;
Index.reserve(Diags.size());
Expand Down Expand Up @@ -1915,7 +1921,7 @@ void writeDiagnosticText(DiagnosticTextBuilder &Builder, const Record *R,
} // namespace
} // namespace docs

void clang::EmitClangDiagDocs(RecordKeeper &Records, raw_ostream &OS) {
void clang::EmitClangDiagDocs(const RecordKeeper &Records, raw_ostream &OS) {
using namespace docs;

// Get the documentation introduction paragraph.
Expand All @@ -1930,10 +1936,10 @@ void clang::EmitClangDiagDocs(RecordKeeper &Records, raw_ostream &OS) {

DiagnosticTextBuilder Builder(Records);

std::vector<Record*> Diags =
ArrayRef<const Record *> Diags =
Records.getAllDerivedDefinitions("Diagnostic");

std::vector<Record*> DiagGroups =
std::vector<const Record *> DiagGroups =
Records.getAllDerivedDefinitions("DiagGroup");
llvm::sort(DiagGroups, diagGroupBeforeByName);

Expand Down
12 changes: 7 additions & 5 deletions clang/utils/TableGen/TableGenBackends.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,11 @@ void EmitClangAttrDocTable(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
void EmitClangBuiltins(const llvm::RecordKeeper &Records,
llvm::raw_ostream &OS);

void EmitClangDiagsDefs(llvm::RecordKeeper &Records, llvm::raw_ostream &OS,
const std::string &Component);
void EmitClangDiagGroups(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
void EmitClangDiagsIndexName(llvm::RecordKeeper &Records,
void EmitClangDiagsDefs(const llvm::RecordKeeper &Records,
llvm::raw_ostream &OS, const std::string &Component);
void EmitClangDiagGroups(const llvm::RecordKeeper &Records,
llvm::raw_ostream &OS);
void EmitClangDiagsIndexName(const llvm::RecordKeeper &Records,
llvm::raw_ostream &OS);

void EmitClangSACheckers(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
Expand Down Expand Up @@ -142,7 +143,8 @@ void EmitCdeBuiltinCG(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
void EmitCdeBuiltinAliases(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);

void EmitClangAttrDocs(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
void EmitClangDiagDocs(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);
void EmitClangDiagDocs(const llvm::RecordKeeper &Records,
llvm::raw_ostream &OS);
void EmitClangOptDocs(const llvm::RecordKeeper &Records, llvm::raw_ostream &OS);

void EmitClangOpenCLBuiltins(const llvm::RecordKeeper &Records,
Expand Down
Loading