implement support for writing out diagnostic group tables.
[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 void ClangDiagGroupsEmitter::run(std::ostream &OS) {
65   // Invert the 1-[0/1] mapping of diags to group into a one to many mapping of
66   // groups to diags in the group.
67   std::map<std::string, std::vector<const Record*> > DiagsInGroup;
68   
69   const std::vector<Record*> &Diags =
70     Records.getAllDerivedDefinitions("Diagnostic");
71   
72   for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
73     const Record *R = Diags[i];
74     DefInit *DI = dynamic_cast<DefInit*>(R->getValueInit("Group"));
75     if (DI == 0) continue;
76     DiagsInGroup[DI->getDef()->getValueAsString("GroupName")].push_back(R);
77   }
78   
79   // Walk through the groups emitting an array for each diagnostic of the diags
80   // that are mapped to.
81   OS << "\n#ifdef GET_DIAG_ARRAYS\n";
82   unsigned IDNo = 0;
83   unsigned MaxLen = 0;
84   for (std::map<std::string, std::vector<const Record*> >::iterator
85        I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
86     MaxLen = std::max(MaxLen, (unsigned)I->first.size());
87     
88     OS << "static const short DiagArray" << IDNo++
89        << "[] = { ";
90     std::vector<const Record*> &V = I->second;
91     for (unsigned i = 0, e = V.size(); i != e; ++i)
92       OS << "diag::" << V[i]->getName() << ", ";
93     OS << "-1 };\n";
94   }
95   OS << "#endif // GET_DIAG_ARRAYS\n\n";
96   
97   // Emit the table now.
98   OS << "\n#ifdef GET_DIAG_TABLE\n";
99   IDNo = 0;
100   for (std::map<std::string, std::vector<const Record*> >::iterator
101        I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
102     std::string S = I->first;
103     EscapeString(S);
104     OS << "  { \"" << S << "\","
105        << std::string(MaxLen-I->first.size()+1, ' ')
106        << "DiagArray" << IDNo++ << " },\n";
107   }
108   OS << "#endif // GET_DIAG_TABLE\n\n";
109 }