start producing subgroup info.
[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     OS << ", \"";
54     std::string S = R.getValueAsString("Text");
55     EscapeString(S);
56     OS << S << "\")\n";
57   }
58 }
59
60 //===----------------------------------------------------------------------===//
61 // Warning Group Tables generation
62 //===----------------------------------------------------------------------===//
63
64 struct GroupInfo {
65   std::vector<const Record*> DiagsInGroup;
66   std::vector<std::string> SubGroups;
67 };
68
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;
73   
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);
82   }
83   
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")];
90     
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"));
94   }
95   
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";
99   unsigned IDNo = 0;
100   unsigned MaxLen = 0;
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());
104     
105     std::vector<const Record*> &V = I->second.DiagsInGroup;
106     if (V.empty()) continue;
107     
108     OS << "static const short DiagArray" << IDNo++
109        << "[] = { ";
110     for (unsigned i = 0, e = V.size(); i != e; ++i)
111       OS << "diag::" << V[i]->getName() << ", ";
112     OS << "-1 };\n";
113   }
114   OS << "#endif // GET_DIAG_ARRAYS\n\n";
115   
116   // Emit the table now.
117   OS << "\n#ifdef GET_DIAG_TABLE\n";
118   IDNo = 0;
119   for (std::map<std::string, GroupInfo>::iterator
120        I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
121     std::string S = I->first;
122     EscapeString(S);
123     // Group option string.
124     OS << "  { \"" << S << "\","
125       << std::string(MaxLen-I->first.size()+1, ' ');
126     
127     // Diagnostics in the group.
128     if (I->second.DiagsInGroup.empty())
129       OS << "0, ";
130     else
131       OS << "DiagArray" << IDNo++ << ", ";
132     
133     // FIXME: Subgroups.
134     OS << 0;
135     OS << " },\n";
136   }
137   OS << "#endif // GET_DIAG_TABLE\n\n";
138 }