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