prove diagnostic -> group mapping information.
[oota-llvm.git] / utils / TableGen / ClangDiagnosticsEmitter.cpp
1 //=- ClangDiagnosticsEmitter.cpp - Generate Clang diagnostics tables -*- C++ -*-
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // These tablegen backends emit Clang diagnostics tables.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "ClangDiagnosticsEmitter.h"
15 #include "Record.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"
22 #include <set>
23 #include <map>
24 using namespace llvm;
25
26 //===----------------------------------------------------------------------===//
27 // Warning Tables (.inc file) generation.
28 //===----------------------------------------------------------------------===//
29
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
36        << ",\n";
37     OS << "#undef " << ComponentName << "START\n";
38     OS << "#endif\n";
39   }
40
41   const std::vector<Record*> &Diags =
42     Records.getAllDerivedDefinitions("Diagnostic");
43   
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"))
48       continue;
49     
50     OS << "DIAG(" << R.getName() << ", ";
51     OS << R.getValueAsDef("Class")->getName();
52     OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
53     
54     // Description string.
55     OS << ", \"";
56     std::string S = R.getValueAsString("Text");
57     EscapeString(S);
58     OS << S << "\"";
59     
60     // Warning associated with the diagnostic.
61     if (DefInit *DI = dynamic_cast<DefInit*>(R.getValueInit("Group"))) {
62       S = DI->getDef()->getValueAsString("GroupName");
63       EscapeString(S);
64       OS << ", \"" << S << "\"";
65     } else {
66       OS << ", 0";
67     }
68     OS << ")\n";
69   }
70 }
71
72 //===----------------------------------------------------------------------===//
73 // Warning Group Tables generation
74 //===----------------------------------------------------------------------===//
75
76 struct GroupInfo {
77   std::vector<const Record*> DiagsInGroup;
78   std::vector<std::string> SubGroups;
79   unsigned IDNo;
80 };
81
82 void ClangDiagGroupsEmitter::run(std::ostream &OS) {
83   // Invert the 1-[0/1] mapping of diags to group into a one to many mapping of
84   // groups to diags in the group.
85   std::map<std::string, GroupInfo> DiagsInGroup;
86   
87   std::vector<Record*> Diags =
88     Records.getAllDerivedDefinitions("Diagnostic");
89   for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
90     const Record *R = Diags[i];
91     DefInit *DI = dynamic_cast<DefInit*>(R->getValueInit("Group"));
92     if (DI == 0) continue;
93     std::string GroupName = DI->getDef()->getValueAsString("GroupName");
94     DiagsInGroup[GroupName].DiagsInGroup.push_back(R);
95   }
96   
97   // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
98   // groups (these are warnings that GCC supports that clang never produces).
99   Diags = Records.getAllDerivedDefinitions("DiagGroup");
100   for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
101     Record *Group = Diags[i];
102     GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
103     
104     std::vector<Record*> SubGroups = Group->getValueAsListOfDefs("SubGroups");
105     for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
106       GI.SubGroups.push_back(SubGroups[j]->getValueAsString("GroupName"));
107   }
108   
109   // Assign unique ID numbers to the groups.
110   unsigned IDNo = 0;
111   for (std::map<std::string, GroupInfo>::iterator
112        I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo)
113     I->second.IDNo = IDNo;
114   
115   // Walk through the groups emitting an array for each diagnostic of the diags
116   // that are mapped to.
117   OS << "\n#ifdef GET_DIAG_ARRAYS\n";
118   unsigned MaxLen = 0;
119   for (std::map<std::string, GroupInfo>::iterator
120        I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
121     MaxLen = std::max(MaxLen, (unsigned)I->first.size());
122     
123     std::vector<const Record*> &V = I->second.DiagsInGroup;
124     if (!V.empty()) {
125       OS << "static const short DiagArray" << I->second.IDNo << "[] = { ";
126       for (unsigned i = 0, e = V.size(); i != e; ++i)
127         OS << "diag::" << V[i]->getName() << ", ";
128       OS << "-1 };\n";
129     }
130     
131     const std::vector<std::string> &SubGroups = I->second.SubGroups;
132     if (!SubGroups.empty()) {
133       OS << "static const char DiagSubGroup" << I->second.IDNo << "[] = { ";
134       for (unsigned i = 0, e = SubGroups.size(); i != e; ++i) {
135         std::map<std::string, GroupInfo>::iterator RI =
136           DiagsInGroup.find(SubGroups[i]);
137         assert(RI != DiagsInGroup.end() && "Referenced without existing?");
138         OS << RI->second.IDNo << ", ";
139       }
140       OS << "-1 };\n";
141     }
142   }
143   OS << "#endif // GET_DIAG_ARRAYS\n\n";
144   
145   // Emit the table now.
146   OS << "\n#ifdef GET_DIAG_TABLE\n";
147   for (std::map<std::string, GroupInfo>::iterator
148        I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
149     std::string S = I->first;
150     EscapeString(S);
151     // Group option string.
152     OS << "  { \"" << S << "\","
153       << std::string(MaxLen-I->first.size()+1, ' ');
154     
155     // Diagnostics in the group.
156     if (I->second.DiagsInGroup.empty())
157       OS << "0, ";
158     else
159       OS << "DiagArray" << I->second.IDNo << ", ";
160     
161     // Subgroups.
162     if (I->second.SubGroups.empty())
163       OS << 0;
164     else
165       OS << "DiagSubGroup" << I->second.IDNo;
166     OS << " },\n";
167   }
168   OS << "#endif // GET_DIAG_TABLE\n\n";
169 }