Ted Kremenek | 04a847e | 2009-03-13 22:21:17 +0000 | [diff] [blame] | 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/Streams.h" |
| 18 | #include "llvm/ADT/VectorExtras.h" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | typedef std::vector<Record*> RecordVector; |
| 22 | typedef std::vector<Record*> SuperClassVector; |
| 23 | typedef std::vector<RecordVal> RecordValVector; |
| 24 | |
Ted Kremenek | 557f7f8 | 2009-03-13 22:53:41 +0000 | [diff] [blame] | 25 | static const RecordVal* findRecordVal(const Record& R, const std::string &key) { |
| 26 | const RecordValVector &Vals = R.getValues(); |
Ted Kremenek | 04a847e | 2009-03-13 22:21:17 +0000 | [diff] [blame] | 27 | 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 | |
| 34 | static 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 | |
| 43 | static 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 Kremenek | 557f7f8 | 2009-03-13 22:53:41 +0000 | [diff] [blame] | 52 | static 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 Kremenek | 04a847e | 2009-03-13 22:21:17 +0000 | [diff] [blame] | 57 | static 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 Kremenek | 557f7f8 | 2009-03-13 22:53:41 +0000 | [diff] [blame] | 65 | EmitAllCaps(OS, DiagKind->getName()); |
Ted Kremenek | 04a847e | 2009-03-13 22:21:17 +0000 | [diff] [blame] | 66 | |
Ted Kremenek | 557f7f8 | 2009-03-13 22:53:41 +0000 | [diff] [blame] | 67 | const RecordVal* Text = findRecordVal(R, "Text"); |
Ted Kremenek | 04a847e | 2009-03-13 22:21:17 +0000 | [diff] [blame] | 68 | 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 | |
| 76 | void 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 Kremenek | 557f7f8 | 2009-03-13 22:53:41 +0000 | [diff] [blame] | 82 | // 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 Kremenek | 04a847e | 2009-03-13 22:21:17 +0000 | [diff] [blame] | 95 | for (RecordVector::const_iterator I=Diags.begin(), E=Diags.end(); I!=E; ++I) { |
Ted Kremenek | 557f7f8 | 2009-03-13 22:53:41 +0000 | [diff] [blame] | 96 | 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 Kremenek | 04a847e | 2009-03-13 22:21:17 +0000 | [diff] [blame] | 106 | ProcessDiag(OS, DiagClass, **I); |
| 107 | } |
Ted Kremenek | 557f7f8 | 2009-03-13 22:53:41 +0000 | [diff] [blame] | 108 | } |