1//===- DiagnosticNames.cpp - Defines a table of all builtin diagnostics ----==//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "DiagnosticNames.h"
10#include "clang/Basic/AllDiagnostics.h"
11#include "llvm/ADT/STLExtras.h"
12#include "llvm/ADT/StringTable.h"
13
14using namespace clang;
15using namespace diagtool;
16
17static const DiagnosticRecord BuiltinDiagnosticsByName[] = {
18#define DIAG_NAME_INDEX(ENUM) { #ENUM, diag::ENUM, STR_SIZE(#ENUM, uint8_t) },
19#include "clang/Basic/DiagnosticIndexName.inc"
20#undef DIAG_NAME_INDEX
21};
22
23llvm::ArrayRef<DiagnosticRecord> diagtool::getBuiltinDiagnosticsByName() {
24 return llvm::ArrayRef(BuiltinDiagnosticsByName);
25}
26
27// FIXME: Is it worth having two tables, especially when this one can get
28// out of sync easily?
29static const DiagnosticRecord BuiltinDiagnosticsByID[] = {
30#define DIAG(ENUM, CLASS, DEFAULT_MAPPING, DESC, GROUP, SFINAE, NOWERROR, \
31 SHOWINSYSHEADER, SHOWINSYSMACRO, DEFER, CATEGORY, STABLE_ID, \
32 LEGACY_STABLE_IDS) \
33 {#ENUM, diag::ENUM, STR_SIZE(#ENUM, uint8_t)},
34#include "clang/Basic/AllDiagnosticKinds.inc"
35#undef DIAG
36};
37
38static bool orderByID(const DiagnosticRecord &Left,
39 const DiagnosticRecord &Right) {
40 return Left.DiagID < Right.DiagID;
41}
42
43const DiagnosticRecord &diagtool::getDiagnosticForID(short DiagID) {
44 DiagnosticRecord Key = {.NameStr: nullptr, .DiagID: DiagID, .NameLen: 0};
45
46 // The requirement for lower_bound to produce a valid result it is
47 // enough if the BuiltinDiagnosticsByID is partitioned (by DiagID),
48 // but as we want this function to work for all possible values of
49 // DiagID sent in as argument it is better to right away check if
50 // BuiltinDiagnosticsByID is sorted.
51 assert(llvm::is_sorted(BuiltinDiagnosticsByID, orderByID) &&
52 "IDs in BuiltinDiagnosticsByID must be sorted.");
53 const DiagnosticRecord *Result =
54 llvm::lower_bound(Range: BuiltinDiagnosticsByID, Value&: Key, C: orderByID);
55 assert(Result && "diagnostic not found; table may be out of date");
56 return *Result;
57}
58
59
60#define GET_DIAG_ARRAYS
61#include "clang/Basic/DiagnosticGroups.inc"
62#undef GET_DIAG_ARRAYS
63
64// Second the table of options, sorted by name for fast binary lookup.
65static const GroupRecord OptionTable[] = {
66#define DIAG_ENTRY(GroupName, FlagNameOffset, Members, SubGroups, Docs) \
67 {FlagNameOffset, Members, SubGroups},
68#include "clang/Basic/DiagnosticGroups.inc"
69#undef DIAG_ENTRY
70};
71
72llvm::StringRef GroupRecord::getName() const {
73 return DiagGroupNames[NameOffset];
74}
75
76GroupRecord::subgroup_iterator GroupRecord::subgroup_begin() const {
77 return DiagSubGroups + SubGroups;
78}
79
80GroupRecord::subgroup_iterator GroupRecord::subgroup_end() const {
81 return nullptr;
82}
83
84llvm::iterator_range<diagtool::GroupRecord::subgroup_iterator>
85GroupRecord::subgroups() const {
86 return llvm::make_range(x: subgroup_begin(), y: subgroup_end());
87}
88
89GroupRecord::diagnostics_iterator GroupRecord::diagnostics_begin() const {
90 return DiagArrays + Members;
91}
92
93GroupRecord::diagnostics_iterator GroupRecord::diagnostics_end() const {
94 return nullptr;
95}
96
97llvm::iterator_range<diagtool::GroupRecord::diagnostics_iterator>
98GroupRecord::diagnostics() const {
99 return llvm::make_range(x: diagnostics_begin(), y: diagnostics_end());
100}
101
102llvm::ArrayRef<GroupRecord> diagtool::getDiagnosticGroups() {
103 return llvm::ArrayRef(OptionTable);
104}
105