Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Diagnostic-related interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Basic/Diagnostic.h" |
| 15 | #include "clang/Basic/SourceLocation.h" |
| 16 | #include <cassert> |
| 17 | using namespace clang; |
| 18 | |
| 19 | /// Flag values for diagnostics. |
| 20 | enum { |
| 21 | // Diagnostic classes. |
| 22 | NOTE = 0x01, |
| 23 | WARNING = 0x02, |
| 24 | EXTENSION = 0x03, |
| 25 | ERROR = 0x04, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | class_mask = 0x07 |
| 27 | }; |
| 28 | |
| 29 | /// DiagnosticFlags - A set of flags, or'd together, that describe the |
| 30 | /// diagnostic. |
| 31 | static unsigned char DiagnosticFlags[] = { |
| 32 | #define DIAG(ENUM,FLAGS,DESC) FLAGS, |
| 33 | #include "clang/Basic/DiagnosticKinds.def" |
| 34 | 0 |
| 35 | }; |
| 36 | |
| 37 | /// getDiagClass - Return the class field of the diagnostic. |
| 38 | /// |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame^] | 39 | static unsigned getBuiltinDiagClass(unsigned DiagID) { |
| 40 | assert(DiagID < diag::NUM_BUILTIN_DIAGNOSTICS && |
| 41 | "Diagnostic ID out of range!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 42 | return DiagnosticFlags[DiagID] & class_mask; |
| 43 | } |
| 44 | |
| 45 | /// DiagnosticText - An english message to print for the diagnostic. These |
| 46 | /// should be localized. |
| 47 | static const char * const DiagnosticText[] = { |
| 48 | #define DIAG(ENUM,FLAGS,DESC) DESC, |
| 49 | #include "clang/Basic/DiagnosticKinds.def" |
| 50 | 0 |
| 51 | }; |
| 52 | |
| 53 | Diagnostic::Diagnostic(DiagnosticClient &client) : Client(client) { |
| 54 | WarningsAsErrors = false; |
| 55 | WarnOnExtensions = false; |
| 56 | ErrorOnExtensions = false; |
| 57 | // Clear all mappings, setting them to MAP_DEFAULT. |
| 58 | memset(DiagMappings, 0, sizeof(DiagMappings)); |
| 59 | |
| 60 | ErrorOccurred = false; |
| 61 | NumDiagnostics = 0; |
| 62 | NumErrors = 0; |
| 63 | } |
| 64 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame^] | 65 | /// isBuiltinNoteWarningOrExtension - Return true if the unmapped diagnostic |
| 66 | /// level of the specified diagnostic ID is a Note, Warning, or Extension. |
| 67 | /// Note that this only works on builtin diagnostics, not custom ones. |
| 68 | bool Diagnostic::isBuiltinNoteWarningOrExtension(unsigned DiagID) { |
| 69 | return DiagID < diag::NUM_BUILTIN_DIAGNOSTICS && |
| 70 | getBuiltinDiagClass(DiagID) < ERROR; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | |
| 74 | /// getDescription - Given a diagnostic ID, return a description of the |
| 75 | /// issue. |
| 76 | const char *Diagnostic::getDescription(unsigned DiagID) { |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame^] | 77 | if (DiagID < diag::NUM_BUILTIN_DIAGNOSTICS) |
| 78 | return DiagnosticText[DiagID]; |
| 79 | else |
| 80 | assert(0 && "FIXME: IMPLEMENT"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /// getDiagnosticLevel - Based on the way the client configured the Diagnostic |
| 84 | /// object, classify the specified diagnostic ID into a Level, consumable by |
| 85 | /// the DiagnosticClient. |
| 86 | Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const { |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame^] | 87 | if (DiagID >= diag::NUM_BUILTIN_DIAGNOSTICS) { |
| 88 | // FIXME: HAndle custom here. |
| 89 | assert(0 && "unimp"); |
| 90 | } |
| 91 | |
| 92 | unsigned DiagClass = getBuiltinDiagClass(DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 93 | |
| 94 | // Specific non-error diagnostics may be mapped to various levels from ignored |
| 95 | // to error. |
| 96 | if (DiagClass < ERROR) { |
| 97 | switch (getDiagnosticMapping((diag::kind)DiagID)) { |
| 98 | case diag::MAP_DEFAULT: break; |
| 99 | case diag::MAP_IGNORE: return Ignored; |
| 100 | case diag::MAP_WARNING: DiagClass = WARNING; break; |
| 101 | case diag::MAP_ERROR: DiagClass = ERROR; break; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | // Map diagnostic classes based on command line argument settings. |
| 106 | if (DiagClass == EXTENSION) { |
| 107 | if (ErrorOnExtensions) |
| 108 | DiagClass = ERROR; |
| 109 | else if (WarnOnExtensions) |
| 110 | DiagClass = WARNING; |
| 111 | else |
| 112 | return Ignored; |
| 113 | } |
| 114 | |
| 115 | // If warnings are to be treated as errors, indicate this as such. |
| 116 | if (DiagClass == WARNING && WarningsAsErrors) |
| 117 | DiagClass = ERROR; |
| 118 | |
| 119 | switch (DiagClass) { |
| 120 | default: assert(0 && "Unknown diagnostic class!"); |
| 121 | case NOTE: return Diagnostic::Note; |
| 122 | case WARNING: return Diagnostic::Warning; |
| 123 | case ERROR: return Diagnostic::Error; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | /// Report - Issue the message to the client. If the client wants us to stop |
| 128 | /// compilation, return true, otherwise return false. DiagID is a member of |
| 129 | /// the diag::kind enum. |
| 130 | void Diagnostic::Report(SourceLocation Pos, unsigned DiagID, |
| 131 | const std::string *Strs, unsigned NumStrs, |
| 132 | const SourceRange *Ranges, unsigned NumRanges) { |
| 133 | // Figure out the diagnostic level of this message. |
| 134 | Diagnostic::Level DiagLevel = getDiagnosticLevel(DiagID); |
| 135 | |
| 136 | // If the client doesn't care about this message, don't issue it. |
| 137 | if (DiagLevel == Diagnostic::Ignored) |
| 138 | return; |
| 139 | |
| 140 | if (DiagLevel >= Diagnostic::Error) { |
| 141 | ErrorOccurred = true; |
| 142 | ++NumErrors; |
| 143 | } |
| 144 | |
| 145 | // Are we going to ignore this diagnosic? |
| 146 | if (Client.IgnoreDiagnostic(DiagLevel, Pos)) |
| 147 | return; |
| 148 | |
| 149 | // Finally, report it. |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame^] | 150 | Client.HandleDiagnostic(*this, DiagLevel, Pos, (diag::kind)DiagID, |
| 151 | Strs, NumStrs, Ranges, NumRanges); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 152 | ++NumDiagnostics; |
| 153 | } |
| 154 | |
| 155 | DiagnosticClient::~DiagnosticClient() {} |