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