blob: f9501fb494dad61500118bdf0aed151dd80d62f9 [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"
17#include "llvm/Support/Streams.h"
18#include "llvm/ADT/VectorExtras.h"
19
20using namespace llvm;
21typedef std::vector<Record*> RecordVector;
22typedef std::vector<Record*> SuperClassVector;
23typedef std::vector<RecordVal> RecordValVector;
24
Ted Kremenek557f7f82009-03-13 22:53:41 +000025static const RecordVal* findRecordVal(const Record& R, const std::string &key) {
26 const RecordValVector &Vals = R.getValues();
Ted Kremenek04a847e2009-03-13 22:21:17 +000027 for (RecordValVector::const_iterator I=Vals.begin(), E=Vals.end(); I!=E; ++I)
28 if ((*I).getName() == key)
29 return &*I;
30
31 return 0;
32}
33
34static const Record* getDiagKind(const Record* DiagClass, const Record &R) {
35 const SuperClassVector &SC = R.getSuperClasses();
36 for (SuperClassVector::const_iterator I=SC.begin(), E=SC.end(); I!=E; ++I)
37 if ((*I)->isSubClassOf(DiagClass))
38 return *I;
39
40 return 0;
41}
42
43static void EmitEscaped(std::ostream& OS, const std::string &s) {
44 for (std::string::const_iterator I=s.begin(), E=s.end(); I!=E; ++I)
45 switch (*I) {
46 default: OS << *I; break;
47 case '\"': OS << "\\" << *I; break;
48 case '\\': OS << "\\\\"; break;
49 }
50}
51
Ted Kremenek557f7f82009-03-13 22:53:41 +000052static void EmitAllCaps(std::ostream& OS, const std::string &s) {
53 for (std::string::const_iterator I=s.begin(), E=s.end(); I!=E; ++I)
54 OS << char(toupper(*I));
55}
56
Ted Kremenek04a847e2009-03-13 22:21:17 +000057static void ProcessDiag(std::ostream& OS, const Record* DiagClass,
58 const Record& R) {
59
60 const Record* DiagKind = getDiagKind(DiagClass, R);
61 if (!DiagKind)
62 return;
63
64 OS << "DIAG(" << R.getName() << ", ";
Ted Kremenek557f7f82009-03-13 22:53:41 +000065 EmitAllCaps(OS, DiagKind->getName());
Ted Kremenek04a847e2009-03-13 22:21:17 +000066
Ted Kremenek557f7f82009-03-13 22:53:41 +000067 const RecordVal* Text = findRecordVal(R, "Text");
Ted Kremenek04a847e2009-03-13 22:21:17 +000068 assert(Text && "No 'Text' entry in Diagnostic.");
69 const StringInit* TextVal = dynamic_cast<const StringInit*>(Text->getValue());
70 assert(TextVal && "Value 'Text' must be a string.");
71 OS << ", \"";
72 EmitEscaped(OS, TextVal->getValue());
73 OS << "\")\n";
74}
75
76void ClangDiagsDefsEmitter::run(std::ostream &OS) {
77 const RecordVector &Diags = Records.getAllDerivedDefinitions("Diagnostic");
78
79 const Record* DiagClass = Records.getClass("Diagnostic");
80 assert(DiagClass && "No Diagnostic class defined.");
81
Ted Kremenek557f7f82009-03-13 22:53:41 +000082 // Write the #if guard
83 if (!Component.empty()) {
84 OS << "#ifdef ";
85 EmitAllCaps(OS, Component);
86 OS << "START\n__";
87 EmitAllCaps(OS, Component);
88 OS << "START = DIAG_START_";
89 EmitAllCaps(OS, Component);
90 OS << ",\n#undef ";
91 EmitAllCaps(OS, Component);
92 OS << "START\n#endif\n";
93 }
94
Ted Kremenek04a847e2009-03-13 22:21:17 +000095 for (RecordVector::const_iterator I=Diags.begin(), E=Diags.end(); I!=E; ++I) {
Ted Kremenek557f7f82009-03-13 22:53:41 +000096 if (!Component.empty()) {
97 const RecordVal* V = findRecordVal(**I, "Component");
98 if (!V)
99 continue;
100
101 const StringInit* SV = dynamic_cast<const StringInit*>(V->getValue());
102 if (SV->getValue() != Component)
103 continue;
104 }
105
Ted Kremenek04a847e2009-03-13 22:21:17 +0000106 ProcessDiag(OS, DiagClass, **I);
107 }
Ted Kremenek557f7f82009-03-13 22:53:41 +0000108}