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/Support/Streams.h"
19 #include "llvm/ADT/DenseSet.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/ADT/VectorExtras.h"
26 //===----------------------------------------------------------------------===//
27 // Warning Tables (.inc file) generation.
28 //===----------------------------------------------------------------------===//
30 void ClangDiagsDefsEmitter::run(std::ostream &OS) {
31 // Write the #if guard
32 if (!Component.empty()) {
33 std::string ComponentName = UppercaseString(Component);
34 OS << "#ifdef " << ComponentName << "START\n";
35 OS << "__" << ComponentName << "START = DIAG_START_" << ComponentName
37 OS << "#undef " << ComponentName << "START\n";
41 const std::vector<Record*> &Diags =
42 Records.getAllDerivedDefinitions("Diagnostic");
44 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
45 const Record &R = *Diags[i];
46 // Filter by component.
47 if (!Component.empty() && Component != R.getValueAsString("Component"))
50 OS << "DIAG(" << R.getName() << ", ";
51 OS << R.getValueAsDef("Class")->getName();
52 OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
54 std::string S = R.getValueAsString("Text");
60 //===----------------------------------------------------------------------===//
61 // Warning Group Tables generation
62 //===----------------------------------------------------------------------===//
65 std::vector<const Record*> DiagsInGroup;
66 std::vector<std::string> SubGroups;
69 void ClangDiagGroupsEmitter::run(std::ostream &OS) {
70 // Invert the 1-[0/1] mapping of diags to group into a one to many mapping of
71 // groups to diags in the group.
72 std::map<std::string, GroupInfo> DiagsInGroup;
74 std::vector<Record*> Diags =
75 Records.getAllDerivedDefinitions("Diagnostic");
76 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
77 const Record *R = Diags[i];
78 DefInit *DI = dynamic_cast<DefInit*>(R->getValueInit("Group"));
79 if (DI == 0) continue;
80 std::string GroupName = DI->getDef()->getValueAsString("GroupName");
81 DiagsInGroup[GroupName].DiagsInGroup.push_back(R);
84 // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
85 // groups (these are warnings that GCC supports that clang never produces).
86 Diags = Records.getAllDerivedDefinitions("DiagGroup");
87 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
88 Record *Group = Diags[i];
89 GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
91 std::vector<Record*> SubGroups = Group->getValueAsListOfDefs("SubGroups");
92 for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
93 GI.SubGroups.push_back(SubGroups[j]->getValueAsString("GroupName"));
96 // Walk through the groups emitting an array for each diagnostic of the diags
97 // that are mapped to.
98 OS << "\n#ifdef GET_DIAG_ARRAYS\n";
101 for (std::map<std::string, GroupInfo>::iterator
102 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
103 MaxLen = std::max(MaxLen, (unsigned)I->first.size());
105 std::vector<const Record*> &V = I->second.DiagsInGroup;
106 if (V.empty()) continue;
108 OS << "static const short DiagArray" << IDNo++
110 for (unsigned i = 0, e = V.size(); i != e; ++i)
111 OS << "diag::" << V[i]->getName() << ", ";
114 OS << "#endif // GET_DIAG_ARRAYS\n\n";
116 // Emit the table now.
117 OS << "\n#ifdef GET_DIAG_TABLE\n";
119 for (std::map<std::string, GroupInfo>::iterator
120 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
121 std::string S = I->first;
123 // Group option string.
124 OS << " { \"" << S << "\","
125 << std::string(MaxLen-I->first.size()+1, ' ');
127 // Diagnostics in the group.
128 if (I->second.DiagsInGroup.empty())
131 OS << "DiagArray" << IDNo++ << ", ";
137 OS << "#endif // GET_DIAG_TABLE\n\n";