blob: d0ca5a13d727c261d7574aa74fe6aa63f17ab33e [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 Lattnerac9e7da2009-04-15 20:02:32 +000064void ClangDiagGroupsEmitter::run(std::ostream &OS) {
Chris Lattner2f8c1d52009-04-15 20:55:08 +000065 // 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;
Chris Lattner457cde42009-04-15 20:13:18 +000068
Chris Lattner2f8c1d52009-04-15 20:55:08 +000069 const std::vector<Record*> &Diags =
70 Records.getAllDerivedDefinitions("Diagnostic");
Ted Kremenek8b9d0272009-03-18 21:16:16 +000071
Chris Lattner2f8c1d52009-04-15 20:55:08 +000072 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);
Ted Kremenek8b9d0272009-03-18 21:16:16 +000077 }
Chris Lattner2f8c1d52009-04-15 20:55:08 +000078
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());
Ted Kremenek3ac82fe2009-03-18 21:28:47 +000087
Chris Lattner2f8c1d52009-04-15 20:55:08 +000088 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";
Ted Kremenek3ac82fe2009-03-18 21:28:47 +000094 }
Chris Lattner2f8c1d52009-04-15 20:55:08 +000095 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";
Ted Kremenek8b9d0272009-03-18 21:16:16 +0000109}