1 //=- ClangDiagnosticsEmitter.cpp - Generate Clang diagnostics tables -*- C++ -*-
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // These tablegen backends emit Clang diagnostics tables.
12 //===----------------------------------------------------------------------===//
14 #include "ClangDiagnosticsEmitter.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/ADT/VectorExtras.h"
25 //===----------------------------------------------------------------------===//
26 // Warning Tables (.inc file) generation.
27 //===----------------------------------------------------------------------===//
29 void ClangDiagsDefsEmitter::run(raw_ostream &OS) {
30 // Write the #if guard
31 if (!Component.empty()) {
32 std::string ComponentName = UppercaseString(Component);
33 OS << "#ifdef " << ComponentName << "START\n";
34 OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName
36 OS << "#undef " << ComponentName << "START\n";
40 const std::vector<Record*> &Diags =
41 Records.getAllDerivedDefinitions("Diagnostic");
43 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
44 const Record &R = *Diags[i];
45 // Filter by component.
46 if (!Component.empty() && Component != R.getValueAsString("Component"))
49 OS << "DIAG(" << R.getName() << ", ";
50 OS << R.getValueAsDef("Class")->getName();
51 OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
53 // Description string.
55 OS.write_escaped(R.getValueAsString("Text")) << '"';
57 // Warning associated with the diagnostic.
58 if (DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group"))) {
60 OS.write_escaped(DI->getDef()->getValueAsString("GroupName")) << '"';
66 if (R.getValueAsBit("SFINAE"))
74 //===----------------------------------------------------------------------===//
75 // Warning Group Tables generation
76 //===----------------------------------------------------------------------===//
79 std::vector<const Record*> DiagsInGroup;
80 std::vector<std::string> SubGroups;
84 void ClangDiagGroupsEmitter::run(raw_ostream &OS) {
85 // Invert the 1-[0/1] mapping of diags to group into a one to many mapping of
86 // groups to diags in the group.
87 std::map<std::string, GroupInfo> DiagsInGroup;
89 std::vector<Record*> Diags =
90 Records.getAllDerivedDefinitions("Diagnostic");
91 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
92 const Record *R = Diags[i];
93 DefInit *DI = dynamic_cast<DefInit*>(R->getValueInit("Group"));
94 if (DI == 0) continue;
95 std::string GroupName = DI->getDef()->getValueAsString("GroupName");
96 DiagsInGroup[GroupName].DiagsInGroup.push_back(R);
99 // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
100 // groups (these are warnings that GCC supports that clang never produces).
101 Diags = Records.getAllDerivedDefinitions("DiagGroup");
102 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
103 Record *Group = Diags[i];
104 GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
106 std::vector<Record*> SubGroups = Group->getValueAsListOfDefs("SubGroups");
107 for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
108 GI.SubGroups.push_back(SubGroups[j]->getValueAsString("GroupName"));
111 // Assign unique ID numbers to the groups.
113 for (std::map<std::string, GroupInfo>::iterator
114 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo)
115 I->second.IDNo = IDNo;
117 // Walk through the groups emitting an array for each diagnostic of the diags
118 // that are mapped to.
119 OS << "\n#ifdef GET_DIAG_ARRAYS\n";
121 for (std::map<std::string, GroupInfo>::iterator
122 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
123 MaxLen = std::max(MaxLen, (unsigned)I->first.size());
125 std::vector<const Record*> &V = I->second.DiagsInGroup;
127 OS << "static const short DiagArray" << I->second.IDNo << "[] = { ";
128 for (unsigned i = 0, e = V.size(); i != e; ++i)
129 OS << "diag::" << V[i]->getName() << ", ";
133 const std::vector<std::string> &SubGroups = I->second.SubGroups;
134 if (!SubGroups.empty()) {
135 OS << "static const char DiagSubGroup" << I->second.IDNo << "[] = { ";
136 for (unsigned i = 0, e = SubGroups.size(); i != e; ++i) {
137 std::map<std::string, GroupInfo>::iterator RI =
138 DiagsInGroup.find(SubGroups[i]);
139 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
140 OS << RI->second.IDNo << ", ";
145 OS << "#endif // GET_DIAG_ARRAYS\n\n";
147 // Emit the table now.
148 OS << "\n#ifdef GET_DIAG_TABLE\n";
149 for (std::map<std::string, GroupInfo>::iterator
150 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
151 // Group option string.
153 OS.write_escaped(I->first) << "\","
154 << std::string(MaxLen-I->first.size()+1, ' ');
156 // Diagnostics in the group.
157 if (I->second.DiagsInGroup.empty())
160 OS << "DiagArray" << I->second.IDNo << ", ";
163 if (I->second.SubGroups.empty())
166 OS << "DiagSubGroup" << I->second.IDNo;
169 OS << "#endif // GET_DIAG_TABLE\n\n";