1 //=- ClangDiagnosticsEmitter.cpp - Generate Clang diagnostics tables -*- C++ -*-
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // These tablegen backends emit Clang diagnostics tables.
12 //===----------------------------------------------------------------------===//
14 #include "ClangDiagnosticsEmitter.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"
26 //===----------------------------------------------------------------------===//
27 // Warning Tables (.inc file) generation.
28 //===----------------------------------------------------------------------===//
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
37 OS << "#undef " << ComponentName << "START\n";
41 const std::vector<Record*> &Diags =
42 Records.getAllDerivedDefinitions("Diagnostic");
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"))
50 OS << "DIAG(" << R.getName() << ", ";
51 OS << R.getValueAsDef("Class")->getName();
52 OS << ", diag::" << R.getValueAsDef("DefaultMapping")->getName();
54 std::string S = R.getValueAsString("Text");
60 //===----------------------------------------------------------------------===//
61 // Warning Group Tables generation
62 //===----------------------------------------------------------------------===//
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;
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);
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")];
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";
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());
94 OS << "static const short DiagArray" << IDNo++
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() << ", ";
101 OS << "#endif // GET_DIAG_ARRAYS\n\n";
103 // Emit the table now.
104 OS << "\n#ifdef GET_DIAG_TABLE\n";
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;
110 OS << " { \"" << S << "\","
111 << std::string(MaxLen-I->first.size()+1, ' ')
112 << "DiagArray" << IDNo++ << " },\n";
114 OS << "#endif // GET_DIAG_TABLE\n\n";