1//===- DiagnosticNames.h - 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#ifndef LLVM_CLANG_TOOLS_DIAGTOOL_DIAGNOSTICNAMES_H
10#define LLVM_CLANG_TOOLS_DIAGTOOL_DIAGNOSTICNAMES_H
11
12#include "llvm/ADT/ArrayRef.h"
13#include "llvm/ADT/StringRef.h"
14#include "llvm/Support/DataTypes.h"
15
16namespace diagtool {
17
18struct DiagnosticRecord {
19 short DiagID;
20 uint16_t NameOffsetLow;
21 uint8_t NameOffsetHigh;
22 uint8_t NameLen;
23
24 llvm::StringRef getName() const;
25
26 bool operator<(const DiagnosticRecord &Other) const {
27 return getName() < Other.getName();
28 }
29};
30static_assert(sizeof(DiagnosticRecord) == 6,
31 "DiagnosticRecord must remain compact");
32
33/// Get every diagnostic in the system, sorted by name.
34llvm::ArrayRef<DiagnosticRecord> getBuiltinDiagnosticsByName();
35
36/// Get a diagnostic by its ID.
37const DiagnosticRecord &getDiagnosticForID(short DiagID);
38
39struct GroupRecord {
40 uint16_t NameOffset;
41 uint16_t Members;
42 uint16_t SubGroups;
43
44 llvm::StringRef getName() const;
45
46 template <typename RecordType> class group_iterator {
47 const short *CurrentID;
48
49 friend struct GroupRecord;
50 group_iterator(const short *Start) : CurrentID(Start) {
51 if (CurrentID && *CurrentID == -1)
52 CurrentID = nullptr;
53 }
54
55 public:
56 typedef RecordType value_type;
57 typedef const value_type &reference;
58 typedef const value_type *pointer;
59 typedef std::forward_iterator_tag iterator_category;
60 typedef std::ptrdiff_t difference_type;
61
62 inline reference operator*() const;
63 inline pointer operator->() const { return &operator*(); }
64
65 inline short getID() const { return *CurrentID; }
66
67 group_iterator &operator++() {
68 ++CurrentID;
69 if (*CurrentID == -1)
70 CurrentID = nullptr;
71 return *this;
72 }
73
74 bool operator==(const group_iterator &Other) const {
75 return CurrentID == Other.CurrentID;
76 }
77
78 bool operator!=(const group_iterator &Other) const {
79 return CurrentID != Other.CurrentID;
80 }
81 };
82
83 typedef group_iterator<GroupRecord> subgroup_iterator;
84 subgroup_iterator subgroup_begin() const;
85 subgroup_iterator subgroup_end() const;
86 llvm::iterator_range<subgroup_iterator> subgroups() const;
87
88 typedef group_iterator<DiagnosticRecord> diagnostics_iterator;
89 diagnostics_iterator diagnostics_begin() const;
90 diagnostics_iterator diagnostics_end() const;
91 llvm::iterator_range<diagnostics_iterator> diagnostics() const;
92
93 bool operator<(llvm::StringRef Other) const { return getName() < Other; }
94};
95
96 /// Get every diagnostic group in the system, sorted by name.
97 llvm::ArrayRef<GroupRecord> getDiagnosticGroups();
98
99 template<>
100 inline GroupRecord::subgroup_iterator::reference
101 GroupRecord::subgroup_iterator::operator*() const {
102 return getDiagnosticGroups()[*CurrentID];
103 }
104
105 template<>
106 inline GroupRecord::diagnostics_iterator::reference
107 GroupRecord::diagnostics_iterator::operator*() const {
108 return getDiagnosticForID(DiagID: *CurrentID);
109 }
110} // end namespace diagtool
111
112#endif
113