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 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Diagnostic-related interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 15 | |
| 16 | #include "clang/Lex/LexDiagnostic.h" |
| 17 | #include "clang/Parse/ParseDiagnostic.h" |
| 18 | #include "clang/AST/ASTDiagnostic.h" |
| 19 | #include "clang/Sema/SemaDiagnostic.h" |
| 20 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 21 | #include "clang/Analysis/AnalysisDiagnostic.h" |
| 22 | #include "clang/Driver/DriverDiagnostic.h" |
| 23 | |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 24 | #include "clang/Basic/IdentifierTable.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | #include "clang/Basic/SourceLocation.h" |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 28 | #include <vector> |
| 29 | #include <map> |
Chris Lattner | 87cf5ac | 2008-03-10 17:04:53 +0000 | [diff] [blame] | 30 | #include <cstring> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 33 | //===----------------------------------------------------------------------===// |
| 34 | // Builtin Diagnostic information |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | |
Chris Lattner | 121f60c | 2009-04-16 06:07:15 +0000 | [diff] [blame] | 37 | // Diagnostic classes. |
| 38 | enum { |
| 39 | CLASS_NOTE = 0x01, |
| 40 | CLASS_WARNING = 0x02, |
| 41 | CLASS_EXTENSION = 0x03, |
| 42 | CLASS_ERROR = 0x04 |
| 43 | }; |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 33dd282 | 2009-04-16 06:00:24 +0000 | [diff] [blame] | 45 | struct StaticDiagInfoRec { |
Chris Lattner | 121f60c | 2009-04-16 06:07:15 +0000 | [diff] [blame] | 46 | unsigned short DiagID; |
| 47 | unsigned Mapping : 3; |
| 48 | unsigned Class : 3; |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 49 | bool SFINAE : 1; |
Chris Lattner | 121f60c | 2009-04-16 06:07:15 +0000 | [diff] [blame] | 50 | const char *Description; |
Chris Lattner | 33dd282 | 2009-04-16 06:00:24 +0000 | [diff] [blame] | 51 | const char *OptionGroup; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 87d854e | 2009-04-16 06:13:46 +0000 | [diff] [blame] | 53 | bool operator<(const StaticDiagInfoRec &RHS) const { |
| 54 | return DiagID < RHS.DiagID; |
| 55 | } |
| 56 | bool operator>(const StaticDiagInfoRec &RHS) const { |
| 57 | return DiagID > RHS.DiagID; |
| 58 | } |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
Chris Lattner | 33dd282 | 2009-04-16 06:00:24 +0000 | [diff] [blame] | 61 | static const StaticDiagInfoRec StaticDiagInfo[] = { |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 62 | #define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP,SFINAE) \ |
| 63 | { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, DESC, GROUP }, |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 64 | #include "clang/Basic/DiagnosticCommonKinds.inc" |
| 65 | #include "clang/Basic/DiagnosticDriverKinds.inc" |
| 66 | #include "clang/Basic/DiagnosticFrontendKinds.inc" |
| 67 | #include "clang/Basic/DiagnosticLexKinds.inc" |
| 68 | #include "clang/Basic/DiagnosticParseKinds.inc" |
| 69 | #include "clang/Basic/DiagnosticASTKinds.inc" |
| 70 | #include "clang/Basic/DiagnosticSemaKinds.inc" |
| 71 | #include "clang/Basic/DiagnosticAnalysisKinds.inc" |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 72 | { 0, 0, 0, 0, 0, 0} |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 73 | }; |
Chris Lattner | 8a941e0 | 2009-04-15 16:56:26 +0000 | [diff] [blame] | 74 | #undef DIAG |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 75 | |
Chris Lattner | 87d854e | 2009-04-16 06:13:46 +0000 | [diff] [blame] | 76 | /// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID, |
| 77 | /// or null if the ID is invalid. |
Chris Lattner | 33dd282 | 2009-04-16 06:00:24 +0000 | [diff] [blame] | 78 | static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) { |
Chris Lattner | 87d854e | 2009-04-16 06:13:46 +0000 | [diff] [blame] | 79 | unsigned NumDiagEntries = sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1; |
| 80 | |
| 81 | // If assertions are enabled, verify that the StaticDiagInfo array is sorted. |
| 82 | #ifndef NDEBUG |
| 83 | static bool IsFirst = true; |
| 84 | if (IsFirst) { |
| 85 | for (unsigned i = 1; i != NumDiagEntries; ++i) |
| 86 | assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] && |
| 87 | "Improperly sorted diag info"); |
| 88 | IsFirst = false; |
| 89 | } |
| 90 | #endif |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 87d854e | 2009-04-16 06:13:46 +0000 | [diff] [blame] | 92 | // Search the diagnostic table with a binary search. |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 93 | StaticDiagInfoRec Find = { DiagID, 0, 0, 0, 0, 0 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | |
Chris Lattner | 87d854e | 2009-04-16 06:13:46 +0000 | [diff] [blame] | 95 | const StaticDiagInfoRec *Found = |
| 96 | std::lower_bound(StaticDiagInfo, StaticDiagInfo + NumDiagEntries, Find); |
| 97 | if (Found == StaticDiagInfo + NumDiagEntries || |
| 98 | Found->DiagID != DiagID) |
| 99 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 87d854e | 2009-04-16 06:13:46 +0000 | [diff] [blame] | 101 | return Found; |
Chris Lattner | 33dd282 | 2009-04-16 06:00:24 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static unsigned GetDefaultDiagMapping(unsigned DiagID) { |
| 105 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
Chris Lattner | 121f60c | 2009-04-16 06:07:15 +0000 | [diff] [blame] | 106 | return Info->Mapping; |
Chris Lattner | 691f1ae | 2009-04-16 04:12:40 +0000 | [diff] [blame] | 107 | return diag::MAP_FATAL; |
| 108 | } |
| 109 | |
Chris Lattner | d51d74a | 2009-04-16 05:44:38 +0000 | [diff] [blame] | 110 | /// getWarningOptionForDiag - Return the lowest-level warning option that |
| 111 | /// enables the specified diagnostic. If there is no -Wfoo flag that controls |
| 112 | /// the diagnostic, this returns null. |
| 113 | const char *Diagnostic::getWarningOptionForDiag(unsigned DiagID) { |
Chris Lattner | 33dd282 | 2009-04-16 06:00:24 +0000 | [diff] [blame] | 114 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
| 115 | return Info->OptionGroup; |
| 116 | return 0; |
Chris Lattner | d51d74a | 2009-04-16 05:44:38 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 119 | bool Diagnostic::isBuiltinSFINAEDiag(unsigned DiagID) { |
| 120 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
Douglas Gregor | 8439fac | 2009-06-15 16:52:15 +0000 | [diff] [blame] | 121 | return Info->SFINAE && Info->Class == CLASS_ERROR; |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 122 | return false; |
| 123 | } |
| 124 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 125 | /// getDiagClass - Return the class field of the diagnostic. |
| 126 | /// |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 127 | static unsigned getBuiltinDiagClass(unsigned DiagID) { |
Chris Lattner | 121f60c | 2009-04-16 06:07:15 +0000 | [diff] [blame] | 128 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
| 129 | return Info->Class; |
| 130 | return ~0U; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 133 | //===----------------------------------------------------------------------===// |
| 134 | // Custom Diagnostic information |
| 135 | //===----------------------------------------------------------------------===// |
| 136 | |
| 137 | namespace clang { |
| 138 | namespace diag { |
| 139 | class CustomDiagInfo { |
| 140 | typedef std::pair<Diagnostic::Level, std::string> DiagDesc; |
| 141 | std::vector<DiagDesc> DiagInfo; |
| 142 | std::map<DiagDesc, unsigned> DiagIDs; |
| 143 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 145 | /// getDescription - Return the description of the specified custom |
| 146 | /// diagnostic. |
| 147 | const char *getDescription(unsigned DiagID) const { |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 148 | assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() && |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 149 | "Invalid diagnosic ID"); |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 150 | return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second.c_str(); |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 151 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 153 | /// getLevel - Return the level of the specified custom diagnostic. |
| 154 | Diagnostic::Level getLevel(unsigned DiagID) const { |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 155 | assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() && |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 156 | "Invalid diagnosic ID"); |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 157 | return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first; |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 158 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 159 | |
Chris Lattner | a1f23cc | 2008-10-17 21:24:47 +0000 | [diff] [blame] | 160 | unsigned getOrCreateDiagID(Diagnostic::Level L, const char *Message, |
| 161 | Diagnostic &Diags) { |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 162 | DiagDesc D(L, Message); |
| 163 | // Check to see if it already exists. |
| 164 | std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D); |
| 165 | if (I != DiagIDs.end() && I->first == D) |
| 166 | return I->second; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 168 | // If not, assign a new ID. |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 169 | unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT; |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 170 | DiagIDs.insert(std::make_pair(D, ID)); |
| 171 | DiagInfo.push_back(D); |
| 172 | return ID; |
| 173 | } |
| 174 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 175 | |
| 176 | } // end diag namespace |
| 177 | } // end clang namespace |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 178 | |
| 179 | |
| 180 | //===----------------------------------------------------------------------===// |
| 181 | // Common Diagnostic implementation |
| 182 | //===----------------------------------------------------------------------===// |
| 183 | |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 184 | static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT, |
| 185 | const char *Modifier, unsigned ML, |
| 186 | const char *Argument, unsigned ArgLen, |
Chris Lattner | 92dd386 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 187 | llvm::SmallVectorImpl<char> &Output, |
| 188 | void *Cookie) { |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 189 | const char *Str = "<can't format argument>"; |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 190 | Output.append(Str, Str+strlen(Str)); |
| 191 | } |
| 192 | |
| 193 | |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 194 | Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) { |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 195 | AllExtensionsSilenced = 0; |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 196 | IgnoreAllWarnings = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 197 | WarningsAsErrors = false; |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 198 | SuppressSystemWarnings = false; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame^] | 199 | SuppressAllDiagnostics = false; |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 200 | ExtBehavior = Ext_Ignore; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 201 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 202 | ErrorOccurred = false; |
Chris Lattner | 1522142 | 2009-02-06 04:16:02 +0000 | [diff] [blame] | 203 | FatalErrorOccurred = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 204 | NumDiagnostics = 0; |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 205 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 206 | NumErrors = 0; |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 207 | CustomDiagInfo = 0; |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 208 | CurDiagID = ~0U; |
Douglas Gregor | 525c4b0 | 2009-03-19 18:55:06 +0000 | [diff] [blame] | 209 | LastDiagLevel = Ignored; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 211 | ArgToStringFn = DummyArgToStringFn; |
Chris Lattner | 92dd386 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 212 | ArgToStringCookie = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 213 | |
Chris Lattner | 691f1ae | 2009-04-16 04:12:40 +0000 | [diff] [blame] | 214 | // Set all mappings to 'unset'. |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 215 | DiagMappings BlankDiags(diag::DIAG_UPPER_LIMIT/2, 0); |
| 216 | DiagMappingsStack.push_back(BlankDiags); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 219 | Diagnostic::~Diagnostic() { |
| 220 | delete CustomDiagInfo; |
| 221 | } |
| 222 | |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 223 | |
| 224 | void Diagnostic::pushMappings() { |
| 225 | DiagMappingsStack.push_back(DiagMappingsStack.back()); |
| 226 | } |
| 227 | |
| 228 | bool Diagnostic::popMappings() { |
| 229 | if (DiagMappingsStack.size() == 1) |
| 230 | return false; |
| 231 | |
| 232 | DiagMappingsStack.pop_back(); |
| 233 | return true; |
| 234 | } |
| 235 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 236 | /// getCustomDiagID - Return an ID for a diagnostic with the specified message |
| 237 | /// and level. If this is the first request for this diagnosic, it is |
| 238 | /// registered and created, otherwise the existing ID is returned. |
| 239 | unsigned Diagnostic::getCustomDiagID(Level L, const char *Message) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 240 | if (CustomDiagInfo == 0) |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 241 | CustomDiagInfo = new diag::CustomDiagInfo(); |
Chris Lattner | a1f23cc | 2008-10-17 21:24:47 +0000 | [diff] [blame] | 242 | return CustomDiagInfo->getOrCreateDiagID(L, Message, *this); |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 246 | /// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic |
| 247 | /// level of the specified diagnostic ID is a Warning or Extension. |
| 248 | /// This only works on builtin diagnostics, not custom ones, and is not legal to |
| 249 | /// call on NOTEs. |
| 250 | bool Diagnostic::isBuiltinWarningOrExtension(unsigned DiagID) { |
Chris Lattner | 8a941e0 | 2009-04-15 16:56:26 +0000 | [diff] [blame] | 251 | return DiagID < diag::DIAG_UPPER_LIMIT && |
| 252 | getBuiltinDiagClass(DiagID) != CLASS_ERROR; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 255 | /// \brief Determine whether the given built-in diagnostic ID is a |
| 256 | /// Note. |
| 257 | bool Diagnostic::isBuiltinNote(unsigned DiagID) { |
Chris Lattner | 8a941e0 | 2009-04-15 16:56:26 +0000 | [diff] [blame] | 258 | return DiagID < diag::DIAG_UPPER_LIMIT && |
| 259 | getBuiltinDiagClass(DiagID) == CLASS_NOTE; |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 262 | /// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic |
| 263 | /// ID is for an extension of some sort. |
| 264 | /// |
| 265 | bool Diagnostic::isBuiltinExtensionDiag(unsigned DiagID) { |
Chris Lattner | 8a941e0 | 2009-04-15 16:56:26 +0000 | [diff] [blame] | 266 | return DiagID < diag::DIAG_UPPER_LIMIT && |
| 267 | getBuiltinDiagClass(DiagID) == CLASS_EXTENSION; |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 270 | |
| 271 | /// getDescription - Given a diagnostic ID, return a description of the |
| 272 | /// issue. |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 273 | const char *Diagnostic::getDescription(unsigned DiagID) const { |
Chris Lattner | 121f60c | 2009-04-16 06:07:15 +0000 | [diff] [blame] | 274 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
| 275 | return Info->Description; |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 276 | return CustomDiagInfo->getDescription(DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /// getDiagnosticLevel - Based on the way the client configured the Diagnostic |
| 280 | /// object, classify the specified diagnostic ID into a Level, consumable by |
| 281 | /// the DiagnosticClient. |
| 282 | Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const { |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 283 | // Handle custom diagnostics, which cannot be mapped. |
Chris Lattner | 19e8e2c | 2009-01-29 17:46:13 +0000 | [diff] [blame] | 284 | if (DiagID >= diag::DIAG_UPPER_LIMIT) |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 285 | return CustomDiagInfo->getLevel(DiagID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 287 | unsigned DiagClass = getBuiltinDiagClass(DiagID); |
Chris Lattner | 8a941e0 | 2009-04-15 16:56:26 +0000 | [diff] [blame] | 288 | assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!"); |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 289 | return getDiagnosticLevel(DiagID, DiagClass); |
| 290 | } |
| 291 | |
| 292 | /// getDiagnosticLevel - Based on the way the client configured the Diagnostic |
| 293 | /// object, classify the specified diagnostic ID into a Level, consumable by |
| 294 | /// the DiagnosticClient. |
| 295 | Diagnostic::Level |
| 296 | Diagnostic::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 297 | // Specific non-error diagnostics may be mapped to various levels from ignored |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 298 | // to error. Errors can only be mapped to fatal. |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 299 | Diagnostic::Level Result = Diagnostic::Fatal; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 300 | |
Chris Lattner | 691f1ae | 2009-04-16 04:12:40 +0000 | [diff] [blame] | 301 | // Get the mapping information, if unset, compute it lazily. |
| 302 | unsigned MappingInfo = getDiagnosticMappingInfo((diag::kind)DiagID); |
| 303 | if (MappingInfo == 0) { |
| 304 | MappingInfo = GetDefaultDiagMapping(DiagID); |
| 305 | setDiagnosticMappingInternal(DiagID, MappingInfo, false); |
| 306 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 691f1ae | 2009-04-16 04:12:40 +0000 | [diff] [blame] | 308 | switch (MappingInfo & 7) { |
| 309 | default: assert(0 && "Unknown mapping!"); |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 310 | case diag::MAP_IGNORE: |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 311 | // Ignore this, unless this is an extension diagnostic and we're mapping |
| 312 | // them onto warnings or errors. |
| 313 | if (!isBuiltinExtensionDiag(DiagID) || // Not an extension |
| 314 | ExtBehavior == Ext_Ignore || // Extensions ignored anyway |
| 315 | (MappingInfo & 8) != 0) // User explicitly mapped it. |
| 316 | return Diagnostic::Ignored; |
| 317 | Result = Diagnostic::Warning; |
| 318 | if (ExtBehavior == Ext_Error) Result = Diagnostic::Error; |
| 319 | break; |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 320 | case diag::MAP_ERROR: |
| 321 | Result = Diagnostic::Error; |
| 322 | break; |
| 323 | case diag::MAP_FATAL: |
| 324 | Result = Diagnostic::Fatal; |
| 325 | break; |
| 326 | case diag::MAP_WARNING: |
| 327 | // If warnings are globally mapped to ignore or error, do it. |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 328 | if (IgnoreAllWarnings) |
| 329 | return Diagnostic::Ignored; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 330 | |
Chris Lattner | 2b07d8f | 2009-04-16 04:32:54 +0000 | [diff] [blame] | 331 | Result = Diagnostic::Warning; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 332 | |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 333 | // If this is an extension diagnostic and we're in -pedantic-error mode, and |
| 334 | // if the user didn't explicitly map it, upgrade to an error. |
| 335 | if (ExtBehavior == Ext_Error && |
| 336 | (MappingInfo & 8) == 0 && |
| 337 | isBuiltinExtensionDiag(DiagID)) |
| 338 | Result = Diagnostic::Error; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 339 | |
Chris Lattner | 2b07d8f | 2009-04-16 04:32:54 +0000 | [diff] [blame] | 340 | if (WarningsAsErrors) |
| 341 | Result = Diagnostic::Error; |
| 342 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | |
Chris Lattner | 2b07d8f | 2009-04-16 04:32:54 +0000 | [diff] [blame] | 344 | case diag::MAP_WARNING_NO_WERROR: |
| 345 | // Diagnostics specified with -Wno-error=foo should be set to warnings, but |
| 346 | // not be adjusted by -Werror or -pedantic-errors. |
| 347 | Result = Diagnostic::Warning; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | |
Chris Lattner | 2b07d8f | 2009-04-16 04:32:54 +0000 | [diff] [blame] | 349 | // If warnings are globally mapped to ignore or error, do it. |
| 350 | if (IgnoreAllWarnings) |
| 351 | return Diagnostic::Ignored; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 352 | |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 353 | break; |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 354 | } |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 355 | |
| 356 | // Okay, we're about to return this as a "diagnostic to emit" one last check: |
| 357 | // if this is any sort of extension warning, and if we're in an __extension__ |
| 358 | // block, silence it. |
| 359 | if (AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID)) |
| 360 | return Diagnostic::Ignored; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 361 | |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 362 | return Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Chris Lattner | 3bc172b | 2009-04-19 22:34:23 +0000 | [diff] [blame] | 365 | struct WarningOption { |
| 366 | const char *Name; |
| 367 | const short *Members; |
| 368 | const char *SubGroups; |
| 369 | }; |
| 370 | |
| 371 | #define GET_DIAG_ARRAYS |
| 372 | #include "clang/Basic/DiagnosticGroups.inc" |
| 373 | #undef GET_DIAG_ARRAYS |
| 374 | |
| 375 | // Second the table of options, sorted by name for fast binary lookup. |
| 376 | static const WarningOption OptionTable[] = { |
| 377 | #define GET_DIAG_TABLE |
| 378 | #include "clang/Basic/DiagnosticGroups.inc" |
| 379 | #undef GET_DIAG_TABLE |
| 380 | }; |
| 381 | static const size_t OptionTableSize = |
| 382 | sizeof(OptionTable) / sizeof(OptionTable[0]); |
| 383 | |
| 384 | static bool WarningOptionCompare(const WarningOption &LHS, |
| 385 | const WarningOption &RHS) { |
| 386 | return strcmp(LHS.Name, RHS.Name) < 0; |
| 387 | } |
| 388 | |
| 389 | static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping, |
| 390 | Diagnostic &Diags) { |
| 391 | // Option exists, poke all the members of its diagnostic set. |
| 392 | if (const short *Member = Group->Members) { |
| 393 | for (; *Member != -1; ++Member) |
| 394 | Diags.setDiagnosticMapping(*Member, Mapping); |
| 395 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 396 | |
Chris Lattner | 3bc172b | 2009-04-19 22:34:23 +0000 | [diff] [blame] | 397 | // Enable/disable all subgroups along with this one. |
| 398 | if (const char *SubGroups = Group->SubGroups) { |
| 399 | for (; *SubGroups != (char)-1; ++SubGroups) |
| 400 | MapGroupMembers(&OptionTable[(unsigned char)*SubGroups], Mapping, Diags); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | /// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g. |
| 405 | /// "unknown-pragmas" to have the specified mapping. This returns true and |
| 406 | /// ignores the request if "Group" was unknown, false otherwise. |
| 407 | bool Diagnostic::setDiagnosticGroupMapping(const char *Group, |
| 408 | diag::Mapping Map) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | |
Chris Lattner | 3bc172b | 2009-04-19 22:34:23 +0000 | [diff] [blame] | 410 | WarningOption Key = { Group, 0, 0 }; |
| 411 | const WarningOption *Found = |
| 412 | std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key, |
| 413 | WarningOptionCompare); |
| 414 | if (Found == OptionTable + OptionTableSize || |
| 415 | strcmp(Found->Name, Group) != 0) |
| 416 | return true; // Option not found. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 417 | |
Chris Lattner | 3bc172b | 2009-04-19 22:34:23 +0000 | [diff] [blame] | 418 | MapGroupMembers(Found, Map, *this); |
| 419 | return false; |
| 420 | } |
| 421 | |
| 422 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 423 | /// ProcessDiag - This is the method used to report a diagnostic that is |
| 424 | /// finally fully formed. |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 425 | bool Diagnostic::ProcessDiag() { |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 426 | DiagnosticInfo Info(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame^] | 428 | if (SuppressAllDiagnostics) |
| 429 | return false; |
| 430 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 431 | // Figure out the diagnostic level of this message. |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 432 | Diagnostic::Level DiagLevel; |
| 433 | unsigned DiagID = Info.getID(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 435 | // ShouldEmitInSystemHeader - True if this diagnostic should be produced even |
| 436 | // in a system header. |
| 437 | bool ShouldEmitInSystemHeader; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 438 | |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 439 | if (DiagID >= diag::DIAG_UPPER_LIMIT) { |
| 440 | // Handle custom diagnostics, which cannot be mapped. |
| 441 | DiagLevel = CustomDiagInfo->getLevel(DiagID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 442 | |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 443 | // Custom diagnostics always are emitted in system headers. |
| 444 | ShouldEmitInSystemHeader = true; |
| 445 | } else { |
| 446 | // Get the class of the diagnostic. If this is a NOTE, map it onto whatever |
| 447 | // the diagnostic level was for the previous diagnostic so that it is |
| 448 | // filtered the same as the previous diagnostic. |
| 449 | unsigned DiagClass = getBuiltinDiagClass(DiagID); |
Chris Lattner | 8a941e0 | 2009-04-15 16:56:26 +0000 | [diff] [blame] | 450 | if (DiagClass == CLASS_NOTE) { |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 451 | DiagLevel = Diagnostic::Note; |
| 452 | ShouldEmitInSystemHeader = false; // extra consideration is needed |
| 453 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 454 | // If this is not an error and we are in a system header, we ignore it. |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 455 | // Check the original Diag ID here, because we also want to ignore |
| 456 | // extensions and warnings in -Werror and -pedantic-errors modes, which |
| 457 | // *map* warnings/extensions to errors. |
Chris Lattner | 8a941e0 | 2009-04-15 16:56:26 +0000 | [diff] [blame] | 458 | ShouldEmitInSystemHeader = DiagClass == CLASS_ERROR; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 460 | DiagLevel = getDiagnosticLevel(DiagID, DiagClass); |
| 461 | } |
| 462 | } |
| 463 | |
Douglas Gregor | 525c4b0 | 2009-03-19 18:55:06 +0000 | [diff] [blame] | 464 | if (DiagLevel != Diagnostic::Note) { |
| 465 | // Record that a fatal error occurred only when we see a second |
| 466 | // non-note diagnostic. This allows notes to be attached to the |
| 467 | // fatal error, but suppresses any diagnostics that follow those |
| 468 | // notes. |
| 469 | if (LastDiagLevel == Diagnostic::Fatal) |
| 470 | FatalErrorOccurred = true; |
| 471 | |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 472 | LastDiagLevel = DiagLevel; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | } |
Douglas Gregor | 525c4b0 | 2009-03-19 18:55:06 +0000 | [diff] [blame] | 474 | |
| 475 | // If a fatal error has already been emitted, silence all subsequent |
| 476 | // diagnostics. |
| 477 | if (FatalErrorOccurred) |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 478 | return false; |
Douglas Gregor | 525c4b0 | 2009-03-19 18:55:06 +0000 | [diff] [blame] | 479 | |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 480 | // If the client doesn't care about this message, don't issue it. If this is |
| 481 | // a note and the last real diagnostic was ignored, ignore it too. |
| 482 | if (DiagLevel == Diagnostic::Ignored || |
| 483 | (DiagLevel == Diagnostic::Note && LastDiagLevel == Diagnostic::Ignored)) |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 484 | return false; |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 485 | |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 486 | // If this diagnostic is in a system header and is not a clang error, suppress |
| 487 | // it. |
| 488 | if (SuppressSystemWarnings && !ShouldEmitInSystemHeader && |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 489 | Info.getLocation().isValid() && |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 490 | Info.getLocation().getSpellingLoc().isInSystemHeader() && |
Chris Lattner | 336f26b | 2009-02-17 06:52:20 +0000 | [diff] [blame] | 491 | (DiagLevel != Diagnostic::Note || LastDiagLevel == Diagnostic::Ignored)) { |
| 492 | LastDiagLevel = Diagnostic::Ignored; |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 493 | return false; |
Chris Lattner | 336f26b | 2009-02-17 06:52:20 +0000 | [diff] [blame] | 494 | } |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 495 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 496 | if (DiagLevel >= Diagnostic::Error) { |
| 497 | ErrorOccurred = true; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 498 | ++NumErrors; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 499 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 500 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 501 | // Finally, report it. |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 502 | Client->HandleDiagnostic(DiagLevel, Info); |
Ted Kremenek | cabe668 | 2009-01-23 20:28:53 +0000 | [diff] [blame] | 503 | if (Client->IncludeInDiagnosticCounts()) ++NumDiagnostics; |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 504 | |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 505 | CurDiagID = ~0U; |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 506 | |
| 507 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 510 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 511 | DiagnosticClient::~DiagnosticClient() {} |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 512 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 513 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 514 | /// ModifierIs - Return true if the specified modifier matches specified string. |
| 515 | template <std::size_t StrLen> |
| 516 | static bool ModifierIs(const char *Modifier, unsigned ModifierLen, |
| 517 | const char (&Str)[StrLen]) { |
| 518 | return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1); |
| 519 | } |
| 520 | |
| 521 | /// HandleSelectModifier - Handle the integer 'select' modifier. This is used |
| 522 | /// like this: %select{foo|bar|baz}2. This means that the integer argument |
| 523 | /// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'. |
| 524 | /// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'. |
| 525 | /// This is very useful for certain classes of variant diagnostics. |
| 526 | static void HandleSelectModifier(unsigned ValNo, |
| 527 | const char *Argument, unsigned ArgumentLen, |
| 528 | llvm::SmallVectorImpl<char> &OutStr) { |
| 529 | const char *ArgumentEnd = Argument+ArgumentLen; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 530 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 531 | // Skip over 'ValNo' |'s. |
| 532 | while (ValNo) { |
| 533 | const char *NextVal = std::find(Argument, ArgumentEnd, '|'); |
| 534 | assert(NextVal != ArgumentEnd && "Value for integer select modifier was" |
| 535 | " larger than the number of options in the diagnostic string!"); |
| 536 | Argument = NextVal+1; // Skip this string. |
| 537 | --ValNo; |
| 538 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 540 | // Get the end of the value. This is either the } or the |. |
| 541 | const char *EndPtr = std::find(Argument, ArgumentEnd, '|'); |
| 542 | // Add the value to the output string. |
| 543 | OutStr.append(Argument, EndPtr); |
| 544 | } |
| 545 | |
| 546 | /// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the |
| 547 | /// letter 's' to the string if the value is not 1. This is used in cases like |
| 548 | /// this: "you idiot, you have %4 parameter%s4!". |
| 549 | static void HandleIntegerSModifier(unsigned ValNo, |
| 550 | llvm::SmallVectorImpl<char> &OutStr) { |
| 551 | if (ValNo != 1) |
| 552 | OutStr.push_back('s'); |
| 553 | } |
| 554 | |
| 555 | |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 556 | /// PluralNumber - Parse an unsigned integer and advance Start. |
Chris Lattner | d2aa7c9 | 2009-04-15 17:13:42 +0000 | [diff] [blame] | 557 | static unsigned PluralNumber(const char *&Start, const char *End) { |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 558 | // Programming 101: Parse a decimal number :-) |
| 559 | unsigned Val = 0; |
| 560 | while (Start != End && *Start >= '0' && *Start <= '9') { |
| 561 | Val *= 10; |
| 562 | Val += *Start - '0'; |
| 563 | ++Start; |
| 564 | } |
| 565 | return Val; |
| 566 | } |
| 567 | |
| 568 | /// TestPluralRange - Test if Val is in the parsed range. Modifies Start. |
Chris Lattner | d2aa7c9 | 2009-04-15 17:13:42 +0000 | [diff] [blame] | 569 | static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) { |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 570 | if (*Start != '[') { |
| 571 | unsigned Ref = PluralNumber(Start, End); |
| 572 | return Ref == Val; |
| 573 | } |
| 574 | |
| 575 | ++Start; |
| 576 | unsigned Low = PluralNumber(Start, End); |
| 577 | assert(*Start == ',' && "Bad plural expression syntax: expected ,"); |
| 578 | ++Start; |
| 579 | unsigned High = PluralNumber(Start, End); |
| 580 | assert(*Start == ']' && "Bad plural expression syntax: expected )"); |
| 581 | ++Start; |
| 582 | return Low <= Val && Val <= High; |
| 583 | } |
| 584 | |
| 585 | /// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier. |
Chris Lattner | d2aa7c9 | 2009-04-15 17:13:42 +0000 | [diff] [blame] | 586 | static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) { |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 587 | // Empty condition? |
| 588 | if (*Start == ':') |
| 589 | return true; |
| 590 | |
| 591 | while (1) { |
| 592 | char C = *Start; |
| 593 | if (C == '%') { |
| 594 | // Modulo expression |
| 595 | ++Start; |
| 596 | unsigned Arg = PluralNumber(Start, End); |
| 597 | assert(*Start == '=' && "Bad plural expression syntax: expected ="); |
| 598 | ++Start; |
| 599 | unsigned ValMod = ValNo % Arg; |
| 600 | if (TestPluralRange(ValMod, Start, End)) |
| 601 | return true; |
| 602 | } else { |
Sebastian Redl | e206532 | 2008-11-27 07:28:14 +0000 | [diff] [blame] | 603 | assert((C == '[' || (C >= '0' && C <= '9')) && |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 604 | "Bad plural expression syntax: unexpected character"); |
| 605 | // Range expression |
| 606 | if (TestPluralRange(ValNo, Start, End)) |
| 607 | return true; |
| 608 | } |
| 609 | |
| 610 | // Scan for next or-expr part. |
| 611 | Start = std::find(Start, End, ','); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 612 | if (Start == End) |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 613 | break; |
| 614 | ++Start; |
| 615 | } |
| 616 | return false; |
| 617 | } |
| 618 | |
| 619 | /// HandlePluralModifier - Handle the integer 'plural' modifier. This is used |
| 620 | /// for complex plural forms, or in languages where all plurals are complex. |
| 621 | /// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are |
| 622 | /// conditions that are tested in order, the form corresponding to the first |
| 623 | /// that applies being emitted. The empty condition is always true, making the |
| 624 | /// last form a default case. |
| 625 | /// Conditions are simple boolean expressions, where n is the number argument. |
| 626 | /// Here are the rules. |
| 627 | /// condition := expression | empty |
| 628 | /// empty := -> always true |
| 629 | /// expression := numeric [',' expression] -> logical or |
| 630 | /// numeric := range -> true if n in range |
| 631 | /// | '%' number '=' range -> true if n % number in range |
| 632 | /// range := number |
| 633 | /// | '[' number ',' number ']' -> ranges are inclusive both ends |
| 634 | /// |
| 635 | /// Here are some examples from the GNU gettext manual written in this form: |
| 636 | /// English: |
| 637 | /// {1:form0|:form1} |
| 638 | /// Latvian: |
| 639 | /// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0} |
| 640 | /// Gaeilge: |
| 641 | /// {1:form0|2:form1|:form2} |
| 642 | /// Romanian: |
| 643 | /// {1:form0|0,%100=[1,19]:form1|:form2} |
| 644 | /// Lithuanian: |
| 645 | /// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1} |
| 646 | /// Russian (requires repeated form): |
| 647 | /// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2} |
| 648 | /// Slovak |
| 649 | /// {1:form0|[2,4]:form1|:form2} |
| 650 | /// Polish (requires repeated form): |
| 651 | /// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2} |
| 652 | static void HandlePluralModifier(unsigned ValNo, |
| 653 | const char *Argument, unsigned ArgumentLen, |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 654 | llvm::SmallVectorImpl<char> &OutStr) { |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 655 | const char *ArgumentEnd = Argument + ArgumentLen; |
| 656 | while (1) { |
| 657 | assert(Argument < ArgumentEnd && "Plural expression didn't match."); |
| 658 | const char *ExprEnd = Argument; |
| 659 | while (*ExprEnd != ':') { |
| 660 | assert(ExprEnd != ArgumentEnd && "Plural missing expression end"); |
| 661 | ++ExprEnd; |
| 662 | } |
| 663 | if (EvalPluralExpr(ValNo, Argument, ExprEnd)) { |
| 664 | Argument = ExprEnd + 1; |
| 665 | ExprEnd = std::find(Argument, ArgumentEnd, '|'); |
| 666 | OutStr.append(Argument, ExprEnd); |
| 667 | return; |
| 668 | } |
| 669 | Argument = std::find(Argument, ArgumentEnd - 1, '|') + 1; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 674 | /// FormatDiagnostic - Format this diagnostic into a string, substituting the |
| 675 | /// formal arguments into the %0 slots. The result is appended onto the Str |
| 676 | /// array. |
| 677 | void DiagnosticInfo:: |
| 678 | FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const { |
| 679 | const char *DiagStr = getDiags()->getDescription(getID()); |
| 680 | const char *DiagEnd = DiagStr+strlen(DiagStr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 681 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 682 | while (DiagStr != DiagEnd) { |
| 683 | if (DiagStr[0] != '%') { |
| 684 | // Append non-%0 substrings to Str if we have one. |
| 685 | const char *StrEnd = std::find(DiagStr, DiagEnd, '%'); |
| 686 | OutStr.append(DiagStr, StrEnd); |
| 687 | DiagStr = StrEnd; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 688 | continue; |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 689 | } else if (DiagStr[1] == '%') { |
| 690 | OutStr.push_back('%'); // %% -> %. |
| 691 | DiagStr += 2; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 692 | continue; |
| 693 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 694 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 695 | // Skip the %. |
| 696 | ++DiagStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 697 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 698 | // This must be a placeholder for a diagnostic argument. The format for a |
| 699 | // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0". |
| 700 | // The digit is a number from 0-9 indicating which argument this comes from. |
| 701 | // The modifier is a string of digits from the set [-a-z]+, arguments is a |
| 702 | // brace enclosed string. |
| 703 | const char *Modifier = 0, *Argument = 0; |
| 704 | unsigned ModifierLen = 0, ArgumentLen = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 705 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 706 | // Check to see if we have a modifier. If so eat it. |
| 707 | if (!isdigit(DiagStr[0])) { |
| 708 | Modifier = DiagStr; |
| 709 | while (DiagStr[0] == '-' || |
| 710 | (DiagStr[0] >= 'a' && DiagStr[0] <= 'z')) |
| 711 | ++DiagStr; |
| 712 | ModifierLen = DiagStr-Modifier; |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 713 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 714 | // If we have an argument, get it next. |
| 715 | if (DiagStr[0] == '{') { |
| 716 | ++DiagStr; // Skip {. |
| 717 | Argument = DiagStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 718 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 719 | for (; DiagStr[0] != '}'; ++DiagStr) |
| 720 | assert(DiagStr[0] && "Mismatched {}'s in diagnostic string!"); |
| 721 | ArgumentLen = DiagStr-Argument; |
| 722 | ++DiagStr; // Skip }. |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 723 | } |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 724 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 725 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 726 | assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic"); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 727 | unsigned ArgNo = *DiagStr++ - '0'; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 728 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 729 | switch (getArgKind(ArgNo)) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 730 | // ---- STRINGS ---- |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 731 | case Diagnostic::ak_std_string: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 732 | const std::string &S = getArgStdStr(ArgNo); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 733 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
| 734 | OutStr.append(S.begin(), S.end()); |
| 735 | break; |
| 736 | } |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 737 | case Diagnostic::ak_c_string: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 738 | const char *S = getArgCStr(ArgNo); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 739 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
Daniel Dunbar | e46e354 | 2009-04-20 06:13:16 +0000 | [diff] [blame] | 740 | |
| 741 | // Don't crash if get passed a null pointer by accident. |
| 742 | if (!S) |
| 743 | S = "(null)"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 744 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 745 | OutStr.append(S, S + strlen(S)); |
| 746 | break; |
| 747 | } |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 748 | // ---- INTEGERS ---- |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 749 | case Diagnostic::ak_sint: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 750 | int Val = getArgSInt(ArgNo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 751 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 752 | if (ModifierIs(Modifier, ModifierLen, "select")) { |
| 753 | HandleSelectModifier((unsigned)Val, Argument, ArgumentLen, OutStr); |
| 754 | } else if (ModifierIs(Modifier, ModifierLen, "s")) { |
| 755 | HandleIntegerSModifier(Val, OutStr); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 756 | } else if (ModifierIs(Modifier, ModifierLen, "plural")) { |
| 757 | HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 758 | } else { |
| 759 | assert(ModifierLen == 0 && "Unknown integer modifier"); |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 760 | // FIXME: Optimize |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 761 | std::string S = llvm::itostr(Val); |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 762 | OutStr.append(S.begin(), S.end()); |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 763 | } |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 764 | break; |
| 765 | } |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 766 | case Diagnostic::ak_uint: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 767 | unsigned Val = getArgUInt(ArgNo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 768 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 769 | if (ModifierIs(Modifier, ModifierLen, "select")) { |
| 770 | HandleSelectModifier(Val, Argument, ArgumentLen, OutStr); |
| 771 | } else if (ModifierIs(Modifier, ModifierLen, "s")) { |
| 772 | HandleIntegerSModifier(Val, OutStr); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 773 | } else if (ModifierIs(Modifier, ModifierLen, "plural")) { |
| 774 | HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 775 | } else { |
| 776 | assert(ModifierLen == 0 && "Unknown integer modifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 777 | |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 778 | // FIXME: Optimize |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 779 | std::string S = llvm::utostr_32(Val); |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 780 | OutStr.append(S.begin(), S.end()); |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 781 | } |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 782 | break; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 783 | } |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 784 | // ---- NAMES and TYPES ---- |
| 785 | case Diagnostic::ak_identifierinfo: { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 786 | const IdentifierInfo *II = getArgIdentifier(ArgNo); |
| 787 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
Daniel Dunbar | e46e354 | 2009-04-20 06:13:16 +0000 | [diff] [blame] | 788 | |
| 789 | // Don't crash if get passed a null pointer by accident. |
| 790 | if (!II) { |
| 791 | const char *S = "(null)"; |
| 792 | OutStr.append(S, S + strlen(S)); |
| 793 | continue; |
| 794 | } |
| 795 | |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 796 | OutStr.push_back('\''); |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 797 | OutStr.append(II->getName(), II->getName() + II->getLength()); |
| 798 | OutStr.push_back('\''); |
| 799 | break; |
| 800 | } |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 801 | case Diagnostic::ak_qualtype: |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 802 | case Diagnostic::ak_declarationname: |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 803 | case Diagnostic::ak_nameddecl: |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 804 | case Diagnostic::ak_nestednamespec: |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 805 | getDiags()->ConvertArgToString(getArgKind(ArgNo), getRawArg(ArgNo), |
| 806 | Modifier, ModifierLen, |
| 807 | Argument, ArgumentLen, OutStr); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 808 | break; |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 809 | } |
| 810 | } |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 811 | } |
Ted Kremenek | cabe668 | 2009-01-23 20:28:53 +0000 | [diff] [blame] | 812 | |
| 813 | /// IncludeInDiagnosticCounts - This method (whose default implementation |
| 814 | /// returns true) indicates whether the diagnostics handled by this |
| 815 | /// DiagnosticClient should be included in the number of diagnostics |
| 816 | /// reported by Diagnostic. |
| 817 | bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; } |