blob: 4d7f92968c25812fcfe4e217f1fd1f5aff920c8b [file] [log] [blame]
Ted Kremenek04a847e2009-03-13 22:21:17 +00001//=- 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"
Ted Kremenek0f9d5102009-03-18 21:36:46 +000017#include "llvm/Support/Compiler.h"
Ted Kremenek04a847e2009-03-13 22:21:17 +000018#include "llvm/Support/Streams.h"
Ted Kremenek8b9d0272009-03-18 21:16:16 +000019#include "llvm/ADT/DenseSet.h"
Chris Lattner457cde42009-04-15 20:13:18 +000020#include "llvm/ADT/StringExtras.h"
21#include "llvm/ADT/VectorExtras.h"
Ted Kremenek8b9d0272009-03-18 21:16:16 +000022#include <set>
23#include <map>
Ted Kremenek04a847e2009-03-13 22:21:17 +000024using namespace llvm;
Ted Kremenek8b9d0272009-03-18 21:16:16 +000025
26//===----------------------------------------------------------------------===//
Ted Kremenek8b9d0272009-03-18 21:16:16 +000027// Warning Tables (.inc file) generation.
28//===----------------------------------------------------------------------===//
29
Ted Kremenek04a847e2009-03-13 22:21:17 +000030void ClangDiagsDefsEmitter::run(std::ostream &OS) {
Ted Kremenek557f7f82009-03-13 22:53:41 +000031 // Write the #if guard
32 if (!Component.empty()) {
Chris Lattner9371c332009-04-15 20:16:12 +000033 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";
Ted Kremenek557f7f82009-03-13 22:53:41 +000039 }
Chris Lattner2f8c1d52009-04-15 20:55:08 +000040
41 const std::vector<Record*> &Diags =
42 Records.getAllDerivedDefinitions("Diagnostic");
Ted Kremenek557f7f82009-03-13 22:53:41 +000043
Chris Lattner2f8c1d52009-04-15 20:55:08 +000044 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
45 const Record &R = *Diags[i];
Chris Lattnerf1624aa2009-04-15 06:26:49 +000046 // Filter by component.
47 if (!Component.empty() && Component != R.getValueAsString("Component"))
48 continue;
Ted Kremenek557f7f82009-03-13 22:53:41 +000049
Chris Lattner2f8c1d52009-04-15 20:55:08 +000050 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";
Ted Kremenek04a847e2009-03-13 22:21:17 +000057 }
Ted Kremenek557f7f82009-03-13 22:53:41 +000058}
Ted Kremenek8b9d0272009-03-18 21:16:16 +000059
60//===----------------------------------------------------------------------===//
Chris Lattner2f8c1d52009-04-15 20:55:08 +000061// Warning Group Tables generation
Ted Kremenek8b9d0272009-03-18 21:16:16 +000062//===----------------------------------------------------------------------===//
63
Chris Lattnerd0d34942009-04-16 00:53:25 +000064struct GroupInfo {
65 std::vector<const Record*> DiagsInGroup;
66 std::vector<std::string> SubGroups;
Chris Lattner9efac562009-04-16 03:16:12 +000067 unsigned IDNo;
Chris Lattnerd0d34942009-04-16 00:53:25 +000068};
69
Chris Lattnerac9e7da2009-04-15 20:02:32 +000070void ClangDiagGroupsEmitter::run(std::ostream &OS) {
Chris Lattner2f8c1d52009-04-15 20:55:08 +000071 // Invert the 1-[0/1] mapping of diags to group into a one to many mapping of
72 // groups to diags in the group.
Chris Lattnerd0d34942009-04-16 00:53:25 +000073 std::map<std::string, GroupInfo> DiagsInGroup;
Chris Lattner457cde42009-04-15 20:13:18 +000074
Chris Lattnera0ed8ca2009-04-15 22:33:02 +000075 std::vector<Record*> Diags =
Chris Lattner2f8c1d52009-04-15 20:55:08 +000076 Records.getAllDerivedDefinitions("Diagnostic");
Chris Lattner2f8c1d52009-04-15 20:55:08 +000077 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
78 const Record *R = Diags[i];
79 DefInit *DI = dynamic_cast<DefInit*>(R->getValueInit("Group"));
80 if (DI == 0) continue;
Chris Lattnerd0d34942009-04-16 00:53:25 +000081 std::string GroupName = DI->getDef()->getValueAsString("GroupName");
82 DiagsInGroup[GroupName].DiagsInGroup.push_back(R);
Ted Kremenek8b9d0272009-03-18 21:16:16 +000083 }
Chris Lattner2f8c1d52009-04-15 20:55:08 +000084
Chris Lattnera0ed8ca2009-04-15 22:33:02 +000085 // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
86 // groups (these are warnings that GCC supports that clang never produces).
87 Diags = Records.getAllDerivedDefinitions("DiagGroup");
88 for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
Chris Lattnerd0d34942009-04-16 00:53:25 +000089 Record *Group = Diags[i];
90 GroupInfo &GI = DiagsInGroup[Group->getValueAsString("GroupName")];
91
92 std::vector<Record*> SubGroups = Group->getValueAsListOfDefs("SubGroups");
93 for (unsigned j = 0, e = SubGroups.size(); j != e; ++j)
94 GI.SubGroups.push_back(SubGroups[j]->getValueAsString("GroupName"));
Chris Lattnera0ed8ca2009-04-15 22:33:02 +000095 }
96
Chris Lattner9efac562009-04-16 03:16:12 +000097 // Assign unique ID numbers to the groups.
98 unsigned IDNo = 0;
99 for (std::map<std::string, GroupInfo>::iterator
100 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I, ++IDNo)
101 I->second.IDNo = IDNo;
102
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000103 // Walk through the groups emitting an array for each diagnostic of the diags
104 // that are mapped to.
105 OS << "\n#ifdef GET_DIAG_ARRAYS\n";
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000106 unsigned MaxLen = 0;
Chris Lattnerd0d34942009-04-16 00:53:25 +0000107 for (std::map<std::string, GroupInfo>::iterator
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000108 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
109 MaxLen = std::max(MaxLen, (unsigned)I->first.size());
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000110
Chris Lattnerd0d34942009-04-16 00:53:25 +0000111 std::vector<const Record*> &V = I->second.DiagsInGroup;
Chris Lattner9efac562009-04-16 03:16:12 +0000112 if (!V.empty()) {
113 OS << "static const short DiagArray" << I->second.IDNo << "[] = { ";
114 for (unsigned i = 0, e = V.size(); i != e; ++i)
115 OS << "diag::" << V[i]->getName() << ", ";
116 OS << "-1 };\n";
117 }
Chris Lattnerd0d34942009-04-16 00:53:25 +0000118
Chris Lattner9efac562009-04-16 03:16:12 +0000119 const std::vector<std::string> &SubGroups = I->second.SubGroups;
120 if (!SubGroups.empty()) {
121 OS << "static const char DiagSubGroup" << I->second.IDNo << "[] = { ";
122 for (unsigned i = 0, e = SubGroups.size(); i != e; ++i) {
123 std::map<std::string, GroupInfo>::iterator RI =
124 DiagsInGroup.find(SubGroups[i]);
125 assert(RI != DiagsInGroup.end() && "Referenced without existing?");
126 OS << RI->second.IDNo << ", ";
127 }
128 OS << "-1 };\n";
129 }
Ted Kremenek3ac82fe2009-03-18 21:28:47 +0000130 }
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000131 OS << "#endif // GET_DIAG_ARRAYS\n\n";
132
133 // Emit the table now.
134 OS << "\n#ifdef GET_DIAG_TABLE\n";
Chris Lattnerd0d34942009-04-16 00:53:25 +0000135 for (std::map<std::string, GroupInfo>::iterator
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000136 I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
137 std::string S = I->first;
138 EscapeString(S);
Chris Lattnerd0d34942009-04-16 00:53:25 +0000139 // Group option string.
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000140 OS << " { \"" << S << "\","
Chris Lattnerd0d34942009-04-16 00:53:25 +0000141 << std::string(MaxLen-I->first.size()+1, ' ');
142
143 // Diagnostics in the group.
144 if (I->second.DiagsInGroup.empty())
145 OS << "0, ";
146 else
Chris Lattner9efac562009-04-16 03:16:12 +0000147 OS << "DiagArray" << I->second.IDNo << ", ";
Chris Lattnerd0d34942009-04-16 00:53:25 +0000148
Chris Lattner9efac562009-04-16 03:16:12 +0000149 // Subgroups.
150 if (I->second.SubGroups.empty())
151 OS << 0;
152 else
153 OS << "DiagSubGroup" << I->second.IDNo;
Chris Lattnerd0d34942009-04-16 00:53:25 +0000154 OS << " },\n";
Chris Lattner2f8c1d52009-04-15 20:55:08 +0000155 }
156 OS << "#endif // GET_DIAG_TABLE\n\n";
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000157}