045eb7a47c65171babbf5ec45bd48cde82ac49eb
[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   std::vector<Record*> Diags =
70     Records.getAllDerivedDefinitions("Diagnostic");
71   for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
72     const Record *R = Diags[i];
73     DefInit *DI = dynamic_cast<DefInit*>(R->getValueInit("Group"));
74     if (DI == 0) continue;
75     DiagsInGroup[DI->getDef()->getValueAsString("GroupName")].push_back(R);
76   }
77   
78   // Add all DiagGroup's to the DiagsInGroup list to make sure we pick up empty
79   // groups (these are warnings that GCC supports that clang never produces).
80   Diags = Records.getAllDerivedDefinitions("DiagGroup");
81   for (unsigned i = 0, e = Diags.size(); i != e; ++i) {
82     DiagsInGroup[Diags[i]->getValueAsString("GroupName")];
83   }
84   
85   // Walk through the groups emitting an array for each diagnostic of the diags
86   // that are mapped to.
87   OS << "\n#ifdef GET_DIAG_ARRAYS\n";
88   unsigned IDNo = 0;
89   unsigned MaxLen = 0;
90   for (std::map<std::string, std::vector<const Record*> >::iterator
91        I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
92     MaxLen = std::max(MaxLen, (unsigned)I->first.size());
93     
94     OS << "static const short DiagArray" << IDNo++
95        << "[] = { ";
96     std::vector<const Record*> &V = I->second;
97     for (unsigned i = 0, e = V.size(); i != e; ++i)
98       OS << "diag::" << V[i]->getName() << ", ";
99     OS << "-1 };\n";
100   }
101   OS << "#endif // GET_DIAG_ARRAYS\n\n";
102   
103   // Emit the table now.
104   OS << "\n#ifdef GET_DIAG_TABLE\n";
105   IDNo = 0;
106   for (std::map<std::string, std::vector<const Record*> >::iterator
107        I = DiagsInGroup.begin(), E = DiagsInGroup.end(); I != E; ++I) {
108     std::string S = I->first;
109     EscapeString(S);
110     OS << "  { \"" << S << "\","
111        << std::string(MaxLen-I->first.size()+1, ' ')
112        << "DiagArray" << IDNo++ << " },\n";
113   }
114   OS << "#endif // GET_DIAG_TABLE\n\n";
115 }