Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +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 llvm; |
| 18 | using namespace clang; |
| 19 | |
| 20 | /// Flag values for diagnostics. |
| 21 | enum { |
| 22 | // Diagnostic classes. |
| 23 | NOTE = 0x01, |
| 24 | WARNING = 0x02, |
| 25 | EXTENSION = 0x03, |
| 26 | ERROR = 0x04, |
| 27 | FATAL = 0x05, |
| 28 | class_mask = 0x07 |
| 29 | }; |
| 30 | |
| 31 | /// DiagnosticFlags - A set of flags, or'd together, that describe the |
| 32 | /// diagnostic. |
| 33 | static unsigned char DiagnosticFlags[] = { |
| 34 | #define DIAG(ENUM,FLAGS,DESC) FLAGS, |
| 35 | #include "clang/Basic/DiagnosticKinds.def" |
| 36 | 0 |
| 37 | }; |
| 38 | |
| 39 | /// getDiagClass - Return the class field of the diagnostic. |
| 40 | /// |
| 41 | static unsigned getDiagClass(unsigned DiagID) { |
| 42 | assert(DiagID < diag::NUM_DIAGNOSTICS && "Diagnostic ID out of range!"); |
| 43 | return DiagnosticFlags[DiagID] & class_mask; |
| 44 | } |
| 45 | |
| 46 | /// DiagnosticText - An english message to print for the diagnostic. These |
| 47 | /// should be localized. |
| 48 | static const char * const DiagnosticText[] = { |
| 49 | #define DIAG(ENUM,FLAGS,DESC) DESC, |
| 50 | #include "clang/Basic/DiagnosticKinds.def" |
| 51 | 0 |
| 52 | }; |
| 53 | |
| 54 | /// isNoteWarningOrExtension - Return true if the unmapped diagnostic level of |
| 55 | /// the specified diagnostic ID is a Note, Warning, or Extension. |
| 56 | bool Diagnostic::isNoteWarningOrExtension(unsigned DiagID) { |
| 57 | return getDiagClass(DiagID) < ERROR; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | /// getDescription - Given a diagnostic ID, return a description of the |
| 62 | /// issue. |
| 63 | const char *Diagnostic::getDescription(unsigned DiagID) { |
| 64 | assert(DiagID < diag::NUM_DIAGNOSTICS && "Diagnostic ID out of range!"); |
| 65 | return DiagnosticText[DiagID]; |
| 66 | } |
| 67 | |
| 68 | /// getDiagnosticLevel - Based on the way the client configured the Diagnostic |
| 69 | /// object, classify the specified diagnostic ID into a Level, consumable by |
| 70 | /// the DiagnosticClient. |
| 71 | Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const { |
| 72 | unsigned DiagClass = getDiagClass(DiagID); |
| 73 | |
| 74 | // TODO: specific diagnostics may be enabled or disabled. Filter those based |
| 75 | // on their DiagID. |
| 76 | |
| 77 | // Map diagnostic classes based on command line argument settings. |
| 78 | if (DiagClass == EXTENSION) { |
| 79 | if (ErrorOnExtensions) |
| 80 | DiagClass = ERROR; |
| 81 | else if (WarnOnExtensions) |
| 82 | DiagClass = WARNING; |
| 83 | else |
| 84 | return Ignored; |
| 85 | } |
| 86 | |
| 87 | // If warnings are to be treated as errors, indicate this as such. |
| 88 | if (DiagClass == WARNING && WarningsAsErrors) |
| 89 | DiagClass = ERROR; |
| 90 | |
| 91 | switch (DiagClass) { |
| 92 | default: assert(0 && "Unknown diagnostic class!"); |
| 93 | case NOTE: return Diagnostic::Note; |
| 94 | case WARNING: return Diagnostic::Warning; |
| 95 | case ERROR: return Diagnostic::Error; |
| 96 | case FATAL: return Diagnostic::Fatal; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /// Report - Issue the message to the client. If the client wants us to stop |
| 101 | /// compilation, return true, otherwise return false. DiagID is a member of |
| 102 | /// the diag::kind enum. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame^] | 103 | void Diagnostic::Report(SourceLocation Pos, unsigned DiagID, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 104 | const std::string &Extra) { |
| 105 | // Figure out the diagnostic level of this message. |
| 106 | Diagnostic::Level DiagLevel = getDiagnosticLevel(DiagID); |
| 107 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame^] | 108 | // If the client doesn't care about this message, don't issue it. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 109 | if (DiagLevel == Diagnostic::Ignored) |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame^] | 110 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 111 | |
| 112 | // Finally, report it. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame^] | 113 | Client.HandleDiagnostic(DiagLevel, Pos, (diag::kind)DiagID, Extra); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | DiagnosticClient::~DiagnosticClient() {} |