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 | |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTDiagnostic.h" |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 15 | #include "clang/Analysis/AnalysisDiagnostic.h" |
Ted Kremenek | ec55c94 | 2010-04-12 19:54:17 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 17 | #include "clang/Basic/FileManager.h" |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 18 | #include "clang/Basic/IdentifierTable.h" |
Ted Kremenek | ec55c94 | 2010-04-12 19:54:17 +0000 | [diff] [blame] | 19 | #include "clang/Basic/PartialDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "clang/Basic/SourceLocation.h" |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 21 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | ec55c94 | 2010-04-12 19:54:17 +0000 | [diff] [blame] | 22 | #include "clang/Driver/DriverDiagnostic.h" |
| 23 | #include "clang/Frontend/FrontendDiagnostic.h" |
| 24 | #include "clang/Lex/LexDiagnostic.h" |
| 25 | #include "clang/Parse/ParseDiagnostic.h" |
| 26 | #include "clang/Sema/SemaDiagnostic.h" |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/StringExtras.h" |
Ted Kremenek | ec55c94 | 2010-04-12 19:54:17 +0000 | [diff] [blame] | 29 | #include "llvm/Support/MemoryBuffer.h" |
Daniel Dunbar | 23e47c6 | 2009-10-17 18:12:14 +0000 | [diff] [blame] | 30 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | ec55c94 | 2010-04-12 19:54:17 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 32 | #include <vector> |
| 33 | #include <map> |
Chris Lattner | 87cf5ac | 2008-03-10 17:04:53 +0000 | [diff] [blame] | 34 | #include <cstring> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | using namespace clang; |
| 36 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 37 | //===----------------------------------------------------------------------===// |
| 38 | // Builtin Diagnostic information |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 41 | namespace { |
| 42 | |
Chris Lattner | 121f60c | 2009-04-16 06:07:15 +0000 | [diff] [blame] | 43 | // Diagnostic classes. |
| 44 | enum { |
| 45 | CLASS_NOTE = 0x01, |
| 46 | CLASS_WARNING = 0x02, |
| 47 | CLASS_EXTENSION = 0x03, |
| 48 | CLASS_ERROR = 0x04 |
| 49 | }; |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 33dd282 | 2009-04-16 06:00:24 +0000 | [diff] [blame] | 51 | struct StaticDiagInfoRec { |
Chris Lattner | 121f60c | 2009-04-16 06:07:15 +0000 | [diff] [blame] | 52 | unsigned short DiagID; |
| 53 | unsigned Mapping : 3; |
| 54 | unsigned Class : 3; |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 55 | bool SFINAE : 1; |
Chris Lattner | 27b0f51 | 2010-05-04 20:44:26 +0000 | [diff] [blame] | 56 | unsigned Category : 5; |
| 57 | |
Chris Lattner | 121f60c | 2009-04-16 06:07:15 +0000 | [diff] [blame] | 58 | const char *Description; |
Chris Lattner | 33dd282 | 2009-04-16 06:00:24 +0000 | [diff] [blame] | 59 | const char *OptionGroup; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 87d854e | 2009-04-16 06:13:46 +0000 | [diff] [blame] | 61 | bool operator<(const StaticDiagInfoRec &RHS) const { |
| 62 | return DiagID < RHS.DiagID; |
| 63 | } |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
Dan Gohman | 3c46e8d | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Chris Lattner | 33dd282 | 2009-04-16 06:00:24 +0000 | [diff] [blame] | 68 | static const StaticDiagInfoRec StaticDiagInfo[] = { |
Chris Lattner | 27b0f51 | 2010-05-04 20:44:26 +0000 | [diff] [blame] | 69 | #define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP,SFINAE, CATEGORY) \ |
| 70 | { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, CATEGORY, DESC, GROUP }, |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 71 | #include "clang/Basic/DiagnosticCommonKinds.inc" |
| 72 | #include "clang/Basic/DiagnosticDriverKinds.inc" |
| 73 | #include "clang/Basic/DiagnosticFrontendKinds.inc" |
| 74 | #include "clang/Basic/DiagnosticLexKinds.inc" |
| 75 | #include "clang/Basic/DiagnosticParseKinds.inc" |
| 76 | #include "clang/Basic/DiagnosticASTKinds.inc" |
| 77 | #include "clang/Basic/DiagnosticSemaKinds.inc" |
| 78 | #include "clang/Basic/DiagnosticAnalysisKinds.inc" |
Chris Lattner | 27b0f51 | 2010-05-04 20:44:26 +0000 | [diff] [blame] | 79 | { 0, 0, 0, 0, 0, 0, 0} |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 80 | }; |
Chris Lattner | 8a941e0 | 2009-04-15 16:56:26 +0000 | [diff] [blame] | 81 | #undef DIAG |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 87d854e | 2009-04-16 06:13:46 +0000 | [diff] [blame] | 83 | /// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID, |
| 84 | /// or null if the ID is invalid. |
Chris Lattner | 33dd282 | 2009-04-16 06:00:24 +0000 | [diff] [blame] | 85 | static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) { |
Chris Lattner | 87d854e | 2009-04-16 06:13:46 +0000 | [diff] [blame] | 86 | unsigned NumDiagEntries = sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1; |
| 87 | |
| 88 | // If assertions are enabled, verify that the StaticDiagInfo array is sorted. |
| 89 | #ifndef NDEBUG |
| 90 | static bool IsFirst = true; |
| 91 | if (IsFirst) { |
Chris Lattner | 5a3ce9b | 2009-10-16 02:34:51 +0000 | [diff] [blame] | 92 | for (unsigned i = 1; i != NumDiagEntries; ++i) { |
| 93 | assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID && |
| 94 | "Diag ID conflict, the enums at the start of clang::diag (in " |
| 95 | "Diagnostic.h) probably need to be increased"); |
| 96 | |
Chris Lattner | 87d854e | 2009-04-16 06:13:46 +0000 | [diff] [blame] | 97 | assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] && |
| 98 | "Improperly sorted diag info"); |
Chris Lattner | 5a3ce9b | 2009-10-16 02:34:51 +0000 | [diff] [blame] | 99 | } |
Chris Lattner | 87d854e | 2009-04-16 06:13:46 +0000 | [diff] [blame] | 100 | IsFirst = false; |
| 101 | } |
| 102 | #endif |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | |
Chris Lattner | 87d854e | 2009-04-16 06:13:46 +0000 | [diff] [blame] | 104 | // Search the diagnostic table with a binary search. |
Chris Lattner | 27b0f51 | 2010-05-04 20:44:26 +0000 | [diff] [blame] | 105 | StaticDiagInfoRec Find = { DiagID, 0, 0, 0, 0, 0, 0 }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 87d854e | 2009-04-16 06:13:46 +0000 | [diff] [blame] | 107 | const StaticDiagInfoRec *Found = |
| 108 | std::lower_bound(StaticDiagInfo, StaticDiagInfo + NumDiagEntries, Find); |
| 109 | if (Found == StaticDiagInfo + NumDiagEntries || |
| 110 | Found->DiagID != DiagID) |
| 111 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 87d854e | 2009-04-16 06:13:46 +0000 | [diff] [blame] | 113 | return Found; |
Chris Lattner | 33dd282 | 2009-04-16 06:00:24 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static unsigned GetDefaultDiagMapping(unsigned DiagID) { |
| 117 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
Chris Lattner | 121f60c | 2009-04-16 06:07:15 +0000 | [diff] [blame] | 118 | return Info->Mapping; |
Chris Lattner | 691f1ae | 2009-04-16 04:12:40 +0000 | [diff] [blame] | 119 | return diag::MAP_FATAL; |
| 120 | } |
| 121 | |
Chris Lattner | d51d74a | 2009-04-16 05:44:38 +0000 | [diff] [blame] | 122 | /// getWarningOptionForDiag - Return the lowest-level warning option that |
| 123 | /// enables the specified diagnostic. If there is no -Wfoo flag that controls |
| 124 | /// the diagnostic, this returns null. |
| 125 | const char *Diagnostic::getWarningOptionForDiag(unsigned DiagID) { |
Chris Lattner | 33dd282 | 2009-04-16 06:00:24 +0000 | [diff] [blame] | 126 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
| 127 | return Info->OptionGroup; |
| 128 | return 0; |
Chris Lattner | d51d74a | 2009-04-16 05:44:38 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Chris Lattner | c9b8890 | 2010-05-04 21:13:21 +0000 | [diff] [blame] | 131 | /// getWarningOptionForDiag - Return the category number that a specified |
| 132 | /// DiagID belongs to, or 0 if no category. |
| 133 | unsigned Diagnostic::getCategoryNumberForDiag(unsigned DiagID) { |
| 134 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
| 135 | return Info->Category; |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | /// getCategoryNameFromID - Given a category ID, return the name of the |
| 140 | /// category, an empty string if CategoryID is zero, or null if CategoryID is |
| 141 | /// invalid. |
| 142 | const char *Diagnostic::getCategoryNameFromID(unsigned CategoryID) { |
| 143 | // Second the table of options, sorted by name for fast binary lookup. |
| 144 | static const char *CategoryNameTable[] = { |
| 145 | #define GET_CATEGORY_TABLE |
| 146 | #define CATEGORY(X) X, |
| 147 | #include "clang/Basic/DiagnosticGroups.inc" |
| 148 | #undef GET_CATEGORY_TABLE |
| 149 | "<<END>>" |
| 150 | }; |
| 151 | static const size_t CategoryNameTableSize = |
| 152 | sizeof(CategoryNameTable) / sizeof(CategoryNameTable[0])-1; |
| 153 | |
| 154 | if (CategoryID >= CategoryNameTableSize) return 0; |
| 155 | return CategoryNameTable[CategoryID]; |
| 156 | } |
| 157 | |
| 158 | |
| 159 | |
Douglas Gregor | eab5d1e | 2010-03-25 22:17:48 +0000 | [diff] [blame] | 160 | Diagnostic::SFINAEResponse |
| 161 | Diagnostic::getDiagnosticSFINAEResponse(unsigned DiagID) { |
| 162 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) { |
| 163 | if (!Info->SFINAE) |
| 164 | return SFINAE_Report; |
| 165 | |
| 166 | if (Info->Class == CLASS_ERROR) |
| 167 | return SFINAE_SubstitutionFailure; |
| 168 | |
| 169 | // Suppress notes, warnings, and extensions; |
| 170 | return SFINAE_Suppress; |
| 171 | } |
| 172 | |
| 173 | return SFINAE_Report; |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 176 | /// getDiagClass - Return the class field of the diagnostic. |
| 177 | /// |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 178 | static unsigned getBuiltinDiagClass(unsigned DiagID) { |
Chris Lattner | 121f60c | 2009-04-16 06:07:15 +0000 | [diff] [blame] | 179 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
| 180 | return Info->Class; |
| 181 | return ~0U; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 184 | //===----------------------------------------------------------------------===// |
| 185 | // Custom Diagnostic information |
| 186 | //===----------------------------------------------------------------------===// |
| 187 | |
| 188 | namespace clang { |
| 189 | namespace diag { |
| 190 | class CustomDiagInfo { |
| 191 | typedef std::pair<Diagnostic::Level, std::string> DiagDesc; |
| 192 | std::vector<DiagDesc> DiagInfo; |
| 193 | std::map<DiagDesc, unsigned> DiagIDs; |
| 194 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 196 | /// getDescription - Return the description of the specified custom |
| 197 | /// diagnostic. |
| 198 | const char *getDescription(unsigned DiagID) const { |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 199 | assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() && |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 200 | "Invalid diagnosic ID"); |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 201 | return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second.c_str(); |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 202 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 203 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 204 | /// getLevel - Return the level of the specified custom diagnostic. |
| 205 | Diagnostic::Level getLevel(unsigned DiagID) const { |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 206 | assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() && |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 207 | "Invalid diagnosic ID"); |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 208 | return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first; |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 209 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | |
Daniel Dunbar | 32d4d80 | 2009-12-01 17:42:06 +0000 | [diff] [blame] | 211 | unsigned getOrCreateDiagID(Diagnostic::Level L, llvm::StringRef Message, |
Chris Lattner | a1f23cc | 2008-10-17 21:24:47 +0000 | [diff] [blame] | 212 | Diagnostic &Diags) { |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 213 | DiagDesc D(L, Message); |
| 214 | // Check to see if it already exists. |
| 215 | std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D); |
| 216 | if (I != DiagIDs.end() && I->first == D) |
| 217 | return I->second; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 218 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 219 | // If not, assign a new ID. |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 220 | unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT; |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 221 | DiagIDs.insert(std::make_pair(D, ID)); |
| 222 | DiagInfo.push_back(D); |
| 223 | return ID; |
| 224 | } |
| 225 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 226 | |
| 227 | } // end diag namespace |
| 228 | } // end clang namespace |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 229 | |
| 230 | |
| 231 | //===----------------------------------------------------------------------===// |
| 232 | // Common Diagnostic implementation |
| 233 | //===----------------------------------------------------------------------===// |
| 234 | |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 235 | static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT, |
| 236 | const char *Modifier, unsigned ML, |
| 237 | const char *Argument, unsigned ArgLen, |
Chris Lattner | b54d8af | 2009-10-20 05:25:22 +0000 | [diff] [blame] | 238 | const Diagnostic::ArgumentValue *PrevArgs, |
| 239 | unsigned NumPrevArgs, |
Chris Lattner | 92dd386 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 240 | llvm::SmallVectorImpl<char> &Output, |
| 241 | void *Cookie) { |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 242 | const char *Str = "<can't format argument>"; |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 243 | Output.append(Str, Str+strlen(Str)); |
| 244 | } |
| 245 | |
| 246 | |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 247 | Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) { |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 248 | ArgToStringFn = DummyArgToStringFn; |
Chris Lattner | 92dd386 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 249 | ArgToStringCookie = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 250 | |
Douglas Gregor | cc5888d | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 251 | AllExtensionsSilenced = 0; |
| 252 | IgnoreAllWarnings = false; |
| 253 | WarningsAsErrors = false; |
| 254 | ErrorsAsFatal = false; |
| 255 | SuppressSystemWarnings = false; |
| 256 | SuppressAllDiagnostics = false; |
| 257 | ShowOverloads = Ovl_All; |
| 258 | ExtBehavior = Ext_Ignore; |
| 259 | |
| 260 | ErrorLimit = 0; |
| 261 | TemplateBacktraceLimit = 0; |
| 262 | CustomDiagInfo = 0; |
| 263 | |
| 264 | // Set all mappings to 'unset'. |
| 265 | DiagMappingsStack.clear(); |
| 266 | DiagMappingsStack.push_back(DiagMappings()); |
| 267 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 268 | Reset(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 271 | Diagnostic::~Diagnostic() { |
| 272 | delete CustomDiagInfo; |
| 273 | } |
| 274 | |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 275 | |
| 276 | void Diagnostic::pushMappings() { |
John Thompson | ca2c3e2 | 2009-10-23 02:21:17 +0000 | [diff] [blame] | 277 | // Avoids undefined behavior when the stack has to resize. |
| 278 | DiagMappingsStack.reserve(DiagMappingsStack.size() + 1); |
Chris Lattner | 04ae2df | 2009-07-12 21:18:45 +0000 | [diff] [blame] | 279 | DiagMappingsStack.push_back(DiagMappingsStack.back()); |
| 280 | } |
| 281 | |
| 282 | bool Diagnostic::popMappings() { |
| 283 | if (DiagMappingsStack.size() == 1) |
| 284 | return false; |
| 285 | |
| 286 | DiagMappingsStack.pop_back(); |
| 287 | return true; |
| 288 | } |
| 289 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 290 | /// getCustomDiagID - Return an ID for a diagnostic with the specified message |
| 291 | /// and level. If this is the first request for this diagnosic, it is |
| 292 | /// registered and created, otherwise the existing ID is returned. |
Daniel Dunbar | 32d4d80 | 2009-12-01 17:42:06 +0000 | [diff] [blame] | 293 | unsigned Diagnostic::getCustomDiagID(Level L, llvm::StringRef Message) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | if (CustomDiagInfo == 0) |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 295 | CustomDiagInfo = new diag::CustomDiagInfo(); |
Chris Lattner | a1f23cc | 2008-10-17 21:24:47 +0000 | [diff] [blame] | 296 | return CustomDiagInfo->getOrCreateDiagID(L, Message, *this); |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 300 | /// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic |
| 301 | /// level of the specified diagnostic ID is a Warning or Extension. |
| 302 | /// This only works on builtin diagnostics, not custom ones, and is not legal to |
| 303 | /// call on NOTEs. |
| 304 | bool Diagnostic::isBuiltinWarningOrExtension(unsigned DiagID) { |
Chris Lattner | 8a941e0 | 2009-04-15 16:56:26 +0000 | [diff] [blame] | 305 | return DiagID < diag::DIAG_UPPER_LIMIT && |
| 306 | getBuiltinDiagClass(DiagID) != CLASS_ERROR; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 309 | /// \brief Determine whether the given built-in diagnostic ID is a |
| 310 | /// Note. |
| 311 | bool Diagnostic::isBuiltinNote(unsigned DiagID) { |
Chris Lattner | 8a941e0 | 2009-04-15 16:56:26 +0000 | [diff] [blame] | 312 | return DiagID < diag::DIAG_UPPER_LIMIT && |
| 313 | getBuiltinDiagClass(DiagID) == CLASS_NOTE; |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 316 | /// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic |
Chris Lattner | 04e4427 | 2010-04-12 21:53:11 +0000 | [diff] [blame] | 317 | /// ID is for an extension of some sort. This also returns EnabledByDefault, |
| 318 | /// which is set to indicate whether the diagnostic is ignored by default (in |
| 319 | /// which case -pedantic enables it) or treated as a warning/error by default. |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 320 | /// |
Chris Lattner | 04e4427 | 2010-04-12 21:53:11 +0000 | [diff] [blame] | 321 | bool Diagnostic::isBuiltinExtensionDiag(unsigned DiagID, |
| 322 | bool &EnabledByDefault) { |
| 323 | if (DiagID >= diag::DIAG_UPPER_LIMIT || |
| 324 | getBuiltinDiagClass(DiagID) != CLASS_EXTENSION) |
| 325 | return false; |
| 326 | |
Eli Friedman | 270c035 | 2010-08-01 22:13:15 +0000 | [diff] [blame] | 327 | EnabledByDefault = GetDefaultDiagMapping(DiagID) != diag::MAP_IGNORE; |
Chris Lattner | 04e4427 | 2010-04-12 21:53:11 +0000 | [diff] [blame] | 328 | return true; |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 331 | void Diagnostic::Reset() { |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 332 | ErrorOccurred = false; |
| 333 | FatalErrorOccurred = false; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 334 | |
| 335 | NumWarnings = 0; |
| 336 | NumErrors = 0; |
| 337 | NumErrorsSuppressed = 0; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 338 | CurDiagID = ~0U; |
| 339 | LastDiagLevel = Ignored; |
| 340 | DelayedDiagID = 0; |
Douglas Gregor | abc563f | 2010-07-19 21:46:24 +0000 | [diff] [blame] | 341 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 342 | |
| 343 | /// getDescription - Given a diagnostic ID, return a description of the |
| 344 | /// issue. |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 345 | const char *Diagnostic::getDescription(unsigned DiagID) const { |
Chris Lattner | 121f60c | 2009-04-16 06:07:15 +0000 | [diff] [blame] | 346 | if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID)) |
| 347 | return Info->Description; |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 348 | return CustomDiagInfo->getDescription(DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Douglas Gregor | 93ea5cb | 2010-03-22 15:10:57 +0000 | [diff] [blame] | 351 | void Diagnostic::SetDelayedDiagnostic(unsigned DiagID, llvm::StringRef Arg1, |
| 352 | llvm::StringRef Arg2) { |
| 353 | if (DelayedDiagID) |
| 354 | return; |
| 355 | |
| 356 | DelayedDiagID = DiagID; |
Douglas Gregor | 9e2dac9 | 2010-03-22 15:47:45 +0000 | [diff] [blame] | 357 | DelayedDiagArg1 = Arg1.str(); |
| 358 | DelayedDiagArg2 = Arg2.str(); |
Douglas Gregor | 93ea5cb | 2010-03-22 15:10:57 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | void Diagnostic::ReportDelayed() { |
| 362 | Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2; |
| 363 | DelayedDiagID = 0; |
| 364 | DelayedDiagArg1.clear(); |
| 365 | DelayedDiagArg2.clear(); |
| 366 | } |
| 367 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 368 | /// getDiagnosticLevel - Based on the way the client configured the Diagnostic |
| 369 | /// object, classify the specified diagnostic ID into a Level, consumable by |
| 370 | /// the DiagnosticClient. |
| 371 | Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const { |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 372 | // Handle custom diagnostics, which cannot be mapped. |
Chris Lattner | 19e8e2c | 2009-01-29 17:46:13 +0000 | [diff] [blame] | 373 | if (DiagID >= diag::DIAG_UPPER_LIMIT) |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 374 | return CustomDiagInfo->getLevel(DiagID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 375 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 376 | unsigned DiagClass = getBuiltinDiagClass(DiagID); |
Chris Lattner | 8a941e0 | 2009-04-15 16:56:26 +0000 | [diff] [blame] | 377 | assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!"); |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 378 | return getDiagnosticLevel(DiagID, DiagClass); |
| 379 | } |
| 380 | |
| 381 | /// getDiagnosticLevel - Based on the way the client configured the Diagnostic |
| 382 | /// object, classify the specified diagnostic ID into a Level, consumable by |
| 383 | /// the DiagnosticClient. |
| 384 | Diagnostic::Level |
| 385 | Diagnostic::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 386 | // Specific non-error diagnostics may be mapped to various levels from ignored |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 387 | // to error. Errors can only be mapped to fatal. |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 388 | Diagnostic::Level Result = Diagnostic::Fatal; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 389 | |
Chris Lattner | 691f1ae | 2009-04-16 04:12:40 +0000 | [diff] [blame] | 390 | // Get the mapping information, if unset, compute it lazily. |
| 391 | unsigned MappingInfo = getDiagnosticMappingInfo((diag::kind)DiagID); |
| 392 | if (MappingInfo == 0) { |
| 393 | MappingInfo = GetDefaultDiagMapping(DiagID); |
| 394 | setDiagnosticMappingInternal(DiagID, MappingInfo, false); |
| 395 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 396 | |
Chris Lattner | 691f1ae | 2009-04-16 04:12:40 +0000 | [diff] [blame] | 397 | switch (MappingInfo & 7) { |
| 398 | default: assert(0 && "Unknown mapping!"); |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 399 | case diag::MAP_IGNORE: |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 400 | // Ignore this, unless this is an extension diagnostic and we're mapping |
| 401 | // them onto warnings or errors. |
| 402 | if (!isBuiltinExtensionDiag(DiagID) || // Not an extension |
| 403 | ExtBehavior == Ext_Ignore || // Extensions ignored anyway |
| 404 | (MappingInfo & 8) != 0) // User explicitly mapped it. |
| 405 | return Diagnostic::Ignored; |
| 406 | Result = Diagnostic::Warning; |
| 407 | if (ExtBehavior == Ext_Error) Result = Diagnostic::Error; |
Chris Lattner | e663c72 | 2009-12-22 23:12:53 +0000 | [diff] [blame] | 408 | if (Result == Diagnostic::Error && ErrorsAsFatal) |
| 409 | Result = Diagnostic::Fatal; |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 410 | break; |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 411 | case diag::MAP_ERROR: |
| 412 | Result = Diagnostic::Error; |
Chris Lattner | e663c72 | 2009-12-22 23:12:53 +0000 | [diff] [blame] | 413 | if (ErrorsAsFatal) |
| 414 | Result = Diagnostic::Fatal; |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 415 | break; |
| 416 | case diag::MAP_FATAL: |
| 417 | Result = Diagnostic::Fatal; |
| 418 | break; |
| 419 | case diag::MAP_WARNING: |
| 420 | // If warnings are globally mapped to ignore or error, do it. |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 421 | if (IgnoreAllWarnings) |
| 422 | return Diagnostic::Ignored; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | |
Chris Lattner | 2b07d8f | 2009-04-16 04:32:54 +0000 | [diff] [blame] | 424 | Result = Diagnostic::Warning; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 426 | // If this is an extension diagnostic and we're in -pedantic-error mode, and |
| 427 | // if the user didn't explicitly map it, upgrade to an error. |
| 428 | if (ExtBehavior == Ext_Error && |
| 429 | (MappingInfo & 8) == 0 && |
| 430 | isBuiltinExtensionDiag(DiagID)) |
| 431 | Result = Diagnostic::Error; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | |
Chris Lattner | 2b07d8f | 2009-04-16 04:32:54 +0000 | [diff] [blame] | 433 | if (WarningsAsErrors) |
| 434 | Result = Diagnostic::Error; |
Chris Lattner | e663c72 | 2009-12-22 23:12:53 +0000 | [diff] [blame] | 435 | if (Result == Diagnostic::Error && ErrorsAsFatal) |
| 436 | Result = Diagnostic::Fatal; |
Chris Lattner | 2b07d8f | 2009-04-16 04:32:54 +0000 | [diff] [blame] | 437 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 438 | |
Chris Lattner | 2b07d8f | 2009-04-16 04:32:54 +0000 | [diff] [blame] | 439 | case diag::MAP_WARNING_NO_WERROR: |
| 440 | // Diagnostics specified with -Wno-error=foo should be set to warnings, but |
| 441 | // not be adjusted by -Werror or -pedantic-errors. |
| 442 | Result = Diagnostic::Warning; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 443 | |
Chris Lattner | 2b07d8f | 2009-04-16 04:32:54 +0000 | [diff] [blame] | 444 | // If warnings are globally mapped to ignore or error, do it. |
| 445 | if (IgnoreAllWarnings) |
| 446 | return Diagnostic::Ignored; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 447 | |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 448 | break; |
Chris Lattner | e663c72 | 2009-12-22 23:12:53 +0000 | [diff] [blame] | 449 | |
| 450 | case diag::MAP_ERROR_NO_WFATAL: |
| 451 | // Diagnostics specified as -Wno-fatal-error=foo should be errors, but |
| 452 | // unaffected by -Wfatal-errors. |
| 453 | Result = Diagnostic::Error; |
| 454 | break; |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 455 | } |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 456 | |
| 457 | // Okay, we're about to return this as a "diagnostic to emit" one last check: |
| 458 | // if this is any sort of extension warning, and if we're in an __extension__ |
| 459 | // block, silence it. |
| 460 | if (AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID)) |
| 461 | return Diagnostic::Ignored; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 462 | |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 463 | return Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Chris Lattner | 3bc172b | 2009-04-19 22:34:23 +0000 | [diff] [blame] | 466 | struct WarningOption { |
| 467 | const char *Name; |
| 468 | const short *Members; |
Chandler Carruth | 5ef12b3 | 2010-05-13 07:43:05 +0000 | [diff] [blame] | 469 | const short *SubGroups; |
Chris Lattner | 3bc172b | 2009-04-19 22:34:23 +0000 | [diff] [blame] | 470 | }; |
| 471 | |
| 472 | #define GET_DIAG_ARRAYS |
| 473 | #include "clang/Basic/DiagnosticGroups.inc" |
| 474 | #undef GET_DIAG_ARRAYS |
| 475 | |
| 476 | // Second the table of options, sorted by name for fast binary lookup. |
| 477 | static const WarningOption OptionTable[] = { |
| 478 | #define GET_DIAG_TABLE |
| 479 | #include "clang/Basic/DiagnosticGroups.inc" |
| 480 | #undef GET_DIAG_TABLE |
| 481 | }; |
| 482 | static const size_t OptionTableSize = |
| 483 | sizeof(OptionTable) / sizeof(OptionTable[0]); |
| 484 | |
| 485 | static bool WarningOptionCompare(const WarningOption &LHS, |
| 486 | const WarningOption &RHS) { |
| 487 | return strcmp(LHS.Name, RHS.Name) < 0; |
| 488 | } |
| 489 | |
| 490 | static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping, |
| 491 | Diagnostic &Diags) { |
| 492 | // Option exists, poke all the members of its diagnostic set. |
| 493 | if (const short *Member = Group->Members) { |
| 494 | for (; *Member != -1; ++Member) |
| 495 | Diags.setDiagnosticMapping(*Member, Mapping); |
| 496 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 497 | |
Chris Lattner | 3bc172b | 2009-04-19 22:34:23 +0000 | [diff] [blame] | 498 | // Enable/disable all subgroups along with this one. |
Chandler Carruth | 5ef12b3 | 2010-05-13 07:43:05 +0000 | [diff] [blame] | 499 | if (const short *SubGroups = Group->SubGroups) { |
| 500 | for (; *SubGroups != (short)-1; ++SubGroups) |
| 501 | MapGroupMembers(&OptionTable[(short)*SubGroups], Mapping, Diags); |
Chris Lattner | 3bc172b | 2009-04-19 22:34:23 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
| 505 | /// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g. |
| 506 | /// "unknown-pragmas" to have the specified mapping. This returns true and |
| 507 | /// ignores the request if "Group" was unknown, false otherwise. |
| 508 | bool Diagnostic::setDiagnosticGroupMapping(const char *Group, |
| 509 | diag::Mapping Map) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | |
Chris Lattner | 3bc172b | 2009-04-19 22:34:23 +0000 | [diff] [blame] | 511 | WarningOption Key = { Group, 0, 0 }; |
| 512 | const WarningOption *Found = |
| 513 | std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key, |
| 514 | WarningOptionCompare); |
| 515 | if (Found == OptionTable + OptionTableSize || |
| 516 | strcmp(Found->Name, Group) != 0) |
| 517 | return true; // Option not found. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 518 | |
Chris Lattner | 3bc172b | 2009-04-19 22:34:23 +0000 | [diff] [blame] | 519 | MapGroupMembers(Found, Map, *this); |
| 520 | return false; |
| 521 | } |
| 522 | |
| 523 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 524 | /// ProcessDiag - This is the method used to report a diagnostic that is |
| 525 | /// finally fully formed. |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 526 | bool Diagnostic::ProcessDiag() { |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 527 | DiagnosticInfo Info(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 528 | |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 529 | if (SuppressAllDiagnostics) |
| 530 | return false; |
| 531 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 532 | // Figure out the diagnostic level of this message. |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 533 | Diagnostic::Level DiagLevel; |
| 534 | unsigned DiagID = Info.getID(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 535 | |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 536 | // ShouldEmitInSystemHeader - True if this diagnostic should be produced even |
| 537 | // in a system header. |
| 538 | bool ShouldEmitInSystemHeader; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 539 | |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 540 | if (DiagID >= diag::DIAG_UPPER_LIMIT) { |
| 541 | // Handle custom diagnostics, which cannot be mapped. |
| 542 | DiagLevel = CustomDiagInfo->getLevel(DiagID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 543 | |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 544 | // Custom diagnostics always are emitted in system headers. |
| 545 | ShouldEmitInSystemHeader = true; |
| 546 | } else { |
| 547 | // Get the class of the diagnostic. If this is a NOTE, map it onto whatever |
| 548 | // the diagnostic level was for the previous diagnostic so that it is |
| 549 | // filtered the same as the previous diagnostic. |
| 550 | unsigned DiagClass = getBuiltinDiagClass(DiagID); |
Chris Lattner | 8a941e0 | 2009-04-15 16:56:26 +0000 | [diff] [blame] | 551 | if (DiagClass == CLASS_NOTE) { |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 552 | DiagLevel = Diagnostic::Note; |
| 553 | ShouldEmitInSystemHeader = false; // extra consideration is needed |
| 554 | } else { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 555 | // 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] | 556 | // Check the original Diag ID here, because we also want to ignore |
| 557 | // extensions and warnings in -Werror and -pedantic-errors modes, which |
| 558 | // *map* warnings/extensions to errors. |
Chris Lattner | 8a941e0 | 2009-04-15 16:56:26 +0000 | [diff] [blame] | 559 | ShouldEmitInSystemHeader = DiagClass == CLASS_ERROR; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 560 | |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 561 | DiagLevel = getDiagnosticLevel(DiagID, DiagClass); |
| 562 | } |
| 563 | } |
| 564 | |
Douglas Gregor | 525c4b0 | 2009-03-19 18:55:06 +0000 | [diff] [blame] | 565 | if (DiagLevel != Diagnostic::Note) { |
| 566 | // Record that a fatal error occurred only when we see a second |
| 567 | // non-note diagnostic. This allows notes to be attached to the |
| 568 | // fatal error, but suppresses any diagnostics that follow those |
| 569 | // notes. |
| 570 | if (LastDiagLevel == Diagnostic::Fatal) |
| 571 | FatalErrorOccurred = true; |
| 572 | |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 573 | LastDiagLevel = DiagLevel; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 574 | } |
Douglas Gregor | 525c4b0 | 2009-03-19 18:55:06 +0000 | [diff] [blame] | 575 | |
| 576 | // If a fatal error has already been emitted, silence all subsequent |
| 577 | // diagnostics. |
Douglas Gregor | 1864f2e | 2010-04-14 22:19:45 +0000 | [diff] [blame] | 578 | if (FatalErrorOccurred) { |
Chandler Carruth | 7a5bf5f | 2010-08-18 06:46:41 +0000 | [diff] [blame] | 579 | if (DiagLevel >= Diagnostic::Error && Client->IncludeInDiagnosticCounts()) { |
Douglas Gregor | 1864f2e | 2010-04-14 22:19:45 +0000 | [diff] [blame] | 580 | ++NumErrors; |
| 581 | ++NumErrorsSuppressed; |
| 582 | } |
Chandler Carruth | 7a5bf5f | 2010-08-18 06:46:41 +0000 | [diff] [blame] | 583 | |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 584 | return false; |
Douglas Gregor | 1864f2e | 2010-04-14 22:19:45 +0000 | [diff] [blame] | 585 | } |
Douglas Gregor | 525c4b0 | 2009-03-19 18:55:06 +0000 | [diff] [blame] | 586 | |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 587 | // If the client doesn't care about this message, don't issue it. If this is |
| 588 | // a note and the last real diagnostic was ignored, ignore it too. |
| 589 | if (DiagLevel == Diagnostic::Ignored || |
| 590 | (DiagLevel == Diagnostic::Note && LastDiagLevel == Diagnostic::Ignored)) |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 591 | return false; |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 592 | |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 593 | // If this diagnostic is in a system header and is not a clang error, suppress |
| 594 | // it. |
| 595 | if (SuppressSystemWarnings && !ShouldEmitInSystemHeader && |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 596 | Info.getLocation().isValid() && |
John McCall | 779cf42 | 2010-02-11 10:04:29 +0000 | [diff] [blame] | 597 | Info.getLocation().getInstantiationLoc().isInSystemHeader() && |
Chris Lattner | 336f26b | 2009-02-17 06:52:20 +0000 | [diff] [blame] | 598 | (DiagLevel != Diagnostic::Note || LastDiagLevel == Diagnostic::Ignored)) { |
| 599 | LastDiagLevel = Diagnostic::Ignored; |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 600 | return false; |
Chris Lattner | 336f26b | 2009-02-17 06:52:20 +0000 | [diff] [blame] | 601 | } |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 602 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 603 | if (DiagLevel >= Diagnostic::Error) { |
Chandler Carruth | 7a5bf5f | 2010-08-18 06:46:41 +0000 | [diff] [blame] | 604 | if (Client->IncludeInDiagnosticCounts()) { |
| 605 | ErrorOccurred = true; |
| 606 | ++NumErrors; |
| 607 | } |
| 608 | |
Chris Lattner | b205ac9 | 2010-04-07 20:21:58 +0000 | [diff] [blame] | 609 | // If we've emitted a lot of errors, emit a fatal error after it to stop a |
| 610 | // flood of bogus errors. |
Chris Lattner | c100214 | 2010-04-07 20:37:06 +0000 | [diff] [blame] | 611 | if (ErrorLimit && NumErrors >= ErrorLimit && |
Chris Lattner | b205ac9 | 2010-04-07 20:21:58 +0000 | [diff] [blame] | 612 | DiagLevel == Diagnostic::Error) |
| 613 | SetDelayedDiagnostic(diag::fatal_too_many_errors); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 614 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 615 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 616 | // Finally, report it. |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 617 | Client->HandleDiagnostic(DiagLevel, Info); |
Chris Lattner | 53eee7b | 2010-04-07 18:47:42 +0000 | [diff] [blame] | 618 | if (Client->IncludeInDiagnosticCounts()) { |
| 619 | if (DiagLevel == Diagnostic::Warning) |
| 620 | ++NumWarnings; |
| 621 | } |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 622 | |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 623 | CurDiagID = ~0U; |
Douglas Gregor | 5e9f35c | 2009-06-14 07:33:30 +0000 | [diff] [blame] | 624 | |
| 625 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Douglas Gregor | b535041 | 2010-10-13 17:22:14 +0000 | [diff] [blame] | 628 | void DiagnosticBuilder::FlushCounts() { |
| 629 | DiagObj->NumDiagArgs = NumArgs; |
| 630 | DiagObj->NumDiagRanges = NumRanges; |
| 631 | DiagObj->NumFixItHints = NumFixItHints; |
| 632 | } |
| 633 | |
Douglas Gregor | 93ea5cb | 2010-03-22 15:10:57 +0000 | [diff] [blame] | 634 | bool DiagnosticBuilder::Emit() { |
| 635 | // If DiagObj is null, then its soul was stolen by the copy ctor |
| 636 | // or the user called Emit(). |
| 637 | if (DiagObj == 0) return false; |
| 638 | |
| 639 | // When emitting diagnostics, we set the final argument count into |
| 640 | // the Diagnostic object. |
Douglas Gregor | b535041 | 2010-10-13 17:22:14 +0000 | [diff] [blame] | 641 | FlushCounts(); |
Douglas Gregor | 93ea5cb | 2010-03-22 15:10:57 +0000 | [diff] [blame] | 642 | |
| 643 | // Process the diagnostic, sending the accumulated information to the |
| 644 | // DiagnosticClient. |
| 645 | bool Emitted = DiagObj->ProcessDiag(); |
| 646 | |
| 647 | // Clear out the current diagnostic object. |
Douglas Gregor | 9e2dac9 | 2010-03-22 15:47:45 +0000 | [diff] [blame] | 648 | unsigned DiagID = DiagObj->CurDiagID; |
Douglas Gregor | 93ea5cb | 2010-03-22 15:10:57 +0000 | [diff] [blame] | 649 | DiagObj->Clear(); |
| 650 | |
| 651 | // If there was a delayed diagnostic, emit it now. |
Douglas Gregor | 9e2dac9 | 2010-03-22 15:47:45 +0000 | [diff] [blame] | 652 | if (DiagObj->DelayedDiagID && DiagObj->DelayedDiagID != DiagID) |
Douglas Gregor | 93ea5cb | 2010-03-22 15:10:57 +0000 | [diff] [blame] | 653 | DiagObj->ReportDelayed(); |
| 654 | |
| 655 | // This diagnostic is dead. |
| 656 | DiagObj = 0; |
| 657 | |
| 658 | return Emitted; |
| 659 | } |
| 660 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 661 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 662 | DiagnosticClient::~DiagnosticClient() {} |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 663 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 664 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 665 | /// ModifierIs - Return true if the specified modifier matches specified string. |
| 666 | template <std::size_t StrLen> |
| 667 | static bool ModifierIs(const char *Modifier, unsigned ModifierLen, |
| 668 | const char (&Str)[StrLen]) { |
| 669 | return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1); |
| 670 | } |
| 671 | |
John McCall | 909c182 | 2010-01-14 20:11:39 +0000 | [diff] [blame] | 672 | /// ScanForward - Scans forward, looking for the given character, skipping |
| 673 | /// nested clauses and escaped characters. |
| 674 | static const char *ScanFormat(const char *I, const char *E, char Target) { |
| 675 | unsigned Depth = 0; |
| 676 | |
| 677 | for ( ; I != E; ++I) { |
| 678 | if (Depth == 0 && *I == Target) return I; |
| 679 | if (Depth != 0 && *I == '}') Depth--; |
| 680 | |
| 681 | if (*I == '%') { |
| 682 | I++; |
| 683 | if (I == E) break; |
| 684 | |
| 685 | // Escaped characters get implicitly skipped here. |
| 686 | |
| 687 | // Format specifier. |
| 688 | if (!isdigit(*I) && !ispunct(*I)) { |
| 689 | for (I++; I != E && !isdigit(*I) && *I != '{'; I++) ; |
| 690 | if (I == E) break; |
| 691 | if (*I == '{') |
| 692 | Depth++; |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | return E; |
| 697 | } |
| 698 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 699 | /// HandleSelectModifier - Handle the integer 'select' modifier. This is used |
| 700 | /// like this: %select{foo|bar|baz}2. This means that the integer argument |
| 701 | /// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'. |
| 702 | /// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'. |
| 703 | /// This is very useful for certain classes of variant diagnostics. |
John McCall | 9f28614 | 2010-01-13 23:58:20 +0000 | [diff] [blame] | 704 | static void HandleSelectModifier(const DiagnosticInfo &DInfo, unsigned ValNo, |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 705 | const char *Argument, unsigned ArgumentLen, |
| 706 | llvm::SmallVectorImpl<char> &OutStr) { |
| 707 | const char *ArgumentEnd = Argument+ArgumentLen; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 708 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 709 | // Skip over 'ValNo' |'s. |
| 710 | while (ValNo) { |
John McCall | 909c182 | 2010-01-14 20:11:39 +0000 | [diff] [blame] | 711 | const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|'); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 712 | assert(NextVal != ArgumentEnd && "Value for integer select modifier was" |
| 713 | " larger than the number of options in the diagnostic string!"); |
| 714 | Argument = NextVal+1; // Skip this string. |
| 715 | --ValNo; |
| 716 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 717 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 718 | // Get the end of the value. This is either the } or the |. |
John McCall | 909c182 | 2010-01-14 20:11:39 +0000 | [diff] [blame] | 719 | const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|'); |
John McCall | 9f28614 | 2010-01-13 23:58:20 +0000 | [diff] [blame] | 720 | |
| 721 | // Recursively format the result of the select clause into the output string. |
| 722 | DInfo.FormatDiagnostic(Argument, EndPtr, OutStr); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | /// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the |
| 726 | /// letter 's' to the string if the value is not 1. This is used in cases like |
| 727 | /// this: "you idiot, you have %4 parameter%s4!". |
| 728 | static void HandleIntegerSModifier(unsigned ValNo, |
| 729 | llvm::SmallVectorImpl<char> &OutStr) { |
| 730 | if (ValNo != 1) |
| 731 | OutStr.push_back('s'); |
| 732 | } |
| 733 | |
John McCall | 3be16b7 | 2010-01-14 00:50:32 +0000 | [diff] [blame] | 734 | /// HandleOrdinalModifier - Handle the integer 'ord' modifier. This |
| 735 | /// prints the ordinal form of the given integer, with 1 corresponding |
| 736 | /// to the first ordinal. Currently this is hard-coded to use the |
| 737 | /// English form. |
| 738 | static void HandleOrdinalModifier(unsigned ValNo, |
| 739 | llvm::SmallVectorImpl<char> &OutStr) { |
| 740 | assert(ValNo != 0 && "ValNo must be strictly positive!"); |
| 741 | |
| 742 | llvm::raw_svector_ostream Out(OutStr); |
| 743 | |
| 744 | // We could use text forms for the first N ordinals, but the numeric |
| 745 | // forms are actually nicer in diagnostics because they stand out. |
| 746 | Out << ValNo; |
| 747 | |
| 748 | // It is critically important that we do this perfectly for |
| 749 | // user-written sequences with over 100 elements. |
| 750 | switch (ValNo % 100) { |
| 751 | case 11: |
| 752 | case 12: |
| 753 | case 13: |
| 754 | Out << "th"; return; |
| 755 | default: |
| 756 | switch (ValNo % 10) { |
| 757 | case 1: Out << "st"; return; |
| 758 | case 2: Out << "nd"; return; |
| 759 | case 3: Out << "rd"; return; |
| 760 | default: Out << "th"; return; |
| 761 | } |
| 762 | } |
| 763 | } |
| 764 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 765 | |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 766 | /// PluralNumber - Parse an unsigned integer and advance Start. |
Chris Lattner | d2aa7c9 | 2009-04-15 17:13:42 +0000 | [diff] [blame] | 767 | static unsigned PluralNumber(const char *&Start, const char *End) { |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 768 | // Programming 101: Parse a decimal number :-) |
| 769 | unsigned Val = 0; |
| 770 | while (Start != End && *Start >= '0' && *Start <= '9') { |
| 771 | Val *= 10; |
| 772 | Val += *Start - '0'; |
| 773 | ++Start; |
| 774 | } |
| 775 | return Val; |
| 776 | } |
| 777 | |
| 778 | /// TestPluralRange - Test if Val is in the parsed range. Modifies Start. |
Chris Lattner | d2aa7c9 | 2009-04-15 17:13:42 +0000 | [diff] [blame] | 779 | static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) { |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 780 | if (*Start != '[') { |
| 781 | unsigned Ref = PluralNumber(Start, End); |
| 782 | return Ref == Val; |
| 783 | } |
| 784 | |
| 785 | ++Start; |
| 786 | unsigned Low = PluralNumber(Start, End); |
| 787 | assert(*Start == ',' && "Bad plural expression syntax: expected ,"); |
| 788 | ++Start; |
| 789 | unsigned High = PluralNumber(Start, End); |
| 790 | assert(*Start == ']' && "Bad plural expression syntax: expected )"); |
| 791 | ++Start; |
| 792 | return Low <= Val && Val <= High; |
| 793 | } |
| 794 | |
| 795 | /// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier. |
Chris Lattner | d2aa7c9 | 2009-04-15 17:13:42 +0000 | [diff] [blame] | 796 | static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) { |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 797 | // Empty condition? |
| 798 | if (*Start == ':') |
| 799 | return true; |
| 800 | |
| 801 | while (1) { |
| 802 | char C = *Start; |
| 803 | if (C == '%') { |
| 804 | // Modulo expression |
| 805 | ++Start; |
| 806 | unsigned Arg = PluralNumber(Start, End); |
| 807 | assert(*Start == '=' && "Bad plural expression syntax: expected ="); |
| 808 | ++Start; |
| 809 | unsigned ValMod = ValNo % Arg; |
| 810 | if (TestPluralRange(ValMod, Start, End)) |
| 811 | return true; |
| 812 | } else { |
Sebastian Redl | e206532 | 2008-11-27 07:28:14 +0000 | [diff] [blame] | 813 | assert((C == '[' || (C >= '0' && C <= '9')) && |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 814 | "Bad plural expression syntax: unexpected character"); |
| 815 | // Range expression |
| 816 | if (TestPluralRange(ValNo, Start, End)) |
| 817 | return true; |
| 818 | } |
| 819 | |
| 820 | // Scan for next or-expr part. |
| 821 | Start = std::find(Start, End, ','); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 822 | if (Start == End) |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 823 | break; |
| 824 | ++Start; |
| 825 | } |
| 826 | return false; |
| 827 | } |
| 828 | |
| 829 | /// HandlePluralModifier - Handle the integer 'plural' modifier. This is used |
| 830 | /// for complex plural forms, or in languages where all plurals are complex. |
| 831 | /// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are |
| 832 | /// conditions that are tested in order, the form corresponding to the first |
| 833 | /// that applies being emitted. The empty condition is always true, making the |
| 834 | /// last form a default case. |
| 835 | /// Conditions are simple boolean expressions, where n is the number argument. |
| 836 | /// Here are the rules. |
| 837 | /// condition := expression | empty |
| 838 | /// empty := -> always true |
| 839 | /// expression := numeric [',' expression] -> logical or |
| 840 | /// numeric := range -> true if n in range |
| 841 | /// | '%' number '=' range -> true if n % number in range |
| 842 | /// range := number |
| 843 | /// | '[' number ',' number ']' -> ranges are inclusive both ends |
| 844 | /// |
| 845 | /// Here are some examples from the GNU gettext manual written in this form: |
| 846 | /// English: |
| 847 | /// {1:form0|:form1} |
| 848 | /// Latvian: |
| 849 | /// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0} |
| 850 | /// Gaeilge: |
| 851 | /// {1:form0|2:form1|:form2} |
| 852 | /// Romanian: |
| 853 | /// {1:form0|0,%100=[1,19]:form1|:form2} |
| 854 | /// Lithuanian: |
| 855 | /// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1} |
| 856 | /// Russian (requires repeated form): |
| 857 | /// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2} |
| 858 | /// Slovak |
| 859 | /// {1:form0|[2,4]:form1|:form2} |
| 860 | /// Polish (requires repeated form): |
| 861 | /// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2} |
John McCall | e53a44b | 2010-10-14 01:55:31 +0000 | [diff] [blame] | 862 | static void HandlePluralModifier(const DiagnosticInfo &DInfo, unsigned ValNo, |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 863 | const char *Argument, unsigned ArgumentLen, |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 864 | llvm::SmallVectorImpl<char> &OutStr) { |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 865 | const char *ArgumentEnd = Argument + ArgumentLen; |
| 866 | while (1) { |
| 867 | assert(Argument < ArgumentEnd && "Plural expression didn't match."); |
| 868 | const char *ExprEnd = Argument; |
| 869 | while (*ExprEnd != ':') { |
| 870 | assert(ExprEnd != ArgumentEnd && "Plural missing expression end"); |
| 871 | ++ExprEnd; |
| 872 | } |
| 873 | if (EvalPluralExpr(ValNo, Argument, ExprEnd)) { |
| 874 | Argument = ExprEnd + 1; |
John McCall | 909c182 | 2010-01-14 20:11:39 +0000 | [diff] [blame] | 875 | ExprEnd = ScanFormat(Argument, ArgumentEnd, '|'); |
John McCall | e53a44b | 2010-10-14 01:55:31 +0000 | [diff] [blame] | 876 | |
| 877 | // Recursively format the result of the plural clause into the |
| 878 | // output string. |
| 879 | DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 880 | return; |
| 881 | } |
John McCall | 909c182 | 2010-01-14 20:11:39 +0000 | [diff] [blame] | 882 | Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1; |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 883 | } |
| 884 | } |
| 885 | |
| 886 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 887 | /// FormatDiagnostic - Format this diagnostic into a string, substituting the |
| 888 | /// formal arguments into the %0 slots. The result is appended onto the Str |
| 889 | /// array. |
| 890 | void DiagnosticInfo:: |
| 891 | FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const { |
| 892 | const char *DiagStr = getDiags()->getDescription(getID()); |
| 893 | const char *DiagEnd = DiagStr+strlen(DiagStr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 894 | |
John McCall | 9f28614 | 2010-01-13 23:58:20 +0000 | [diff] [blame] | 895 | FormatDiagnostic(DiagStr, DiagEnd, OutStr); |
| 896 | } |
| 897 | |
| 898 | void DiagnosticInfo:: |
| 899 | FormatDiagnostic(const char *DiagStr, const char *DiagEnd, |
| 900 | llvm::SmallVectorImpl<char> &OutStr) const { |
| 901 | |
Chris Lattner | b54d8af | 2009-10-20 05:25:22 +0000 | [diff] [blame] | 902 | /// FormattedArgs - Keep track of all of the arguments formatted by |
| 903 | /// ConvertArgToString and pass them into subsequent calls to |
| 904 | /// ConvertArgToString, allowing the implementation to avoid redundancies in |
| 905 | /// obvious cases. |
| 906 | llvm::SmallVector<Diagnostic::ArgumentValue, 8> FormattedArgs; |
| 907 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 908 | while (DiagStr != DiagEnd) { |
| 909 | if (DiagStr[0] != '%') { |
| 910 | // Append non-%0 substrings to Str if we have one. |
| 911 | const char *StrEnd = std::find(DiagStr, DiagEnd, '%'); |
| 912 | OutStr.append(DiagStr, StrEnd); |
| 913 | DiagStr = StrEnd; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 914 | continue; |
John McCall | 909c182 | 2010-01-14 20:11:39 +0000 | [diff] [blame] | 915 | } else if (ispunct(DiagStr[1])) { |
| 916 | OutStr.push_back(DiagStr[1]); // %% -> %. |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 917 | DiagStr += 2; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 918 | continue; |
| 919 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 920 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 921 | // Skip the %. |
| 922 | ++DiagStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 923 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 924 | // This must be a placeholder for a diagnostic argument. The format for a |
| 925 | // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0". |
| 926 | // The digit is a number from 0-9 indicating which argument this comes from. |
| 927 | // The modifier is a string of digits from the set [-a-z]+, arguments is a |
| 928 | // brace enclosed string. |
| 929 | const char *Modifier = 0, *Argument = 0; |
| 930 | unsigned ModifierLen = 0, ArgumentLen = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 931 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 932 | // Check to see if we have a modifier. If so eat it. |
| 933 | if (!isdigit(DiagStr[0])) { |
| 934 | Modifier = DiagStr; |
| 935 | while (DiagStr[0] == '-' || |
| 936 | (DiagStr[0] >= 'a' && DiagStr[0] <= 'z')) |
| 937 | ++DiagStr; |
| 938 | ModifierLen = DiagStr-Modifier; |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 939 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 940 | // If we have an argument, get it next. |
| 941 | if (DiagStr[0] == '{') { |
| 942 | ++DiagStr; // Skip {. |
| 943 | Argument = DiagStr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 944 | |
John McCall | 909c182 | 2010-01-14 20:11:39 +0000 | [diff] [blame] | 945 | DiagStr = ScanFormat(DiagStr, DiagEnd, '}'); |
| 946 | assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!"); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 947 | ArgumentLen = DiagStr-Argument; |
| 948 | ++DiagStr; // Skip }. |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 949 | } |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 950 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 951 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 952 | assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic"); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 953 | unsigned ArgNo = *DiagStr++ - '0'; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 954 | |
Chris Lattner | b54d8af | 2009-10-20 05:25:22 +0000 | [diff] [blame] | 955 | Diagnostic::ArgumentKind Kind = getArgKind(ArgNo); |
| 956 | |
| 957 | switch (Kind) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 958 | // ---- STRINGS ---- |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 959 | case Diagnostic::ak_std_string: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 960 | const std::string &S = getArgStdStr(ArgNo); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 961 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
| 962 | OutStr.append(S.begin(), S.end()); |
| 963 | break; |
| 964 | } |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 965 | case Diagnostic::ak_c_string: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 966 | const char *S = getArgCStr(ArgNo); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 967 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
Daniel Dunbar | e46e354 | 2009-04-20 06:13:16 +0000 | [diff] [blame] | 968 | |
| 969 | // Don't crash if get passed a null pointer by accident. |
| 970 | if (!S) |
| 971 | S = "(null)"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 972 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 973 | OutStr.append(S, S + strlen(S)); |
| 974 | break; |
| 975 | } |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 976 | // ---- INTEGERS ---- |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 977 | case Diagnostic::ak_sint: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 978 | int Val = getArgSInt(ArgNo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 979 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 980 | if (ModifierIs(Modifier, ModifierLen, "select")) { |
John McCall | e53a44b | 2010-10-14 01:55:31 +0000 | [diff] [blame] | 981 | HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen, |
| 982 | OutStr); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 983 | } else if (ModifierIs(Modifier, ModifierLen, "s")) { |
| 984 | HandleIntegerSModifier(Val, OutStr); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 985 | } else if (ModifierIs(Modifier, ModifierLen, "plural")) { |
John McCall | e53a44b | 2010-10-14 01:55:31 +0000 | [diff] [blame] | 986 | HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen, |
| 987 | OutStr); |
John McCall | 3be16b7 | 2010-01-14 00:50:32 +0000 | [diff] [blame] | 988 | } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) { |
| 989 | HandleOrdinalModifier((unsigned)Val, OutStr); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 990 | } else { |
| 991 | assert(ModifierLen == 0 && "Unknown integer modifier"); |
Daniel Dunbar | 23e47c6 | 2009-10-17 18:12:14 +0000 | [diff] [blame] | 992 | llvm::raw_svector_ostream(OutStr) << Val; |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 993 | } |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 994 | break; |
| 995 | } |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 996 | case Diagnostic::ak_uint: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 997 | unsigned Val = getArgUInt(ArgNo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 998 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 999 | if (ModifierIs(Modifier, ModifierLen, "select")) { |
John McCall | 9f28614 | 2010-01-13 23:58:20 +0000 | [diff] [blame] | 1000 | HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 1001 | } else if (ModifierIs(Modifier, ModifierLen, "s")) { |
| 1002 | HandleIntegerSModifier(Val, OutStr); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 1003 | } else if (ModifierIs(Modifier, ModifierLen, "plural")) { |
John McCall | e53a44b | 2010-10-14 01:55:31 +0000 | [diff] [blame] | 1004 | HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen, |
| 1005 | OutStr); |
John McCall | 3be16b7 | 2010-01-14 00:50:32 +0000 | [diff] [blame] | 1006 | } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) { |
| 1007 | HandleOrdinalModifier(Val, OutStr); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 1008 | } else { |
| 1009 | assert(ModifierLen == 0 && "Unknown integer modifier"); |
Daniel Dunbar | 23e47c6 | 2009-10-17 18:12:14 +0000 | [diff] [blame] | 1010 | llvm::raw_svector_ostream(OutStr) << Val; |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 1011 | } |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 1012 | break; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 1013 | } |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1014 | // ---- NAMES and TYPES ---- |
| 1015 | case Diagnostic::ak_identifierinfo: { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1016 | const IdentifierInfo *II = getArgIdentifier(ArgNo); |
| 1017 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
Daniel Dunbar | e46e354 | 2009-04-20 06:13:16 +0000 | [diff] [blame] | 1018 | |
| 1019 | // Don't crash if get passed a null pointer by accident. |
| 1020 | if (!II) { |
| 1021 | const char *S = "(null)"; |
| 1022 | OutStr.append(S, S + strlen(S)); |
| 1023 | continue; |
| 1024 | } |
| 1025 | |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 1026 | llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\''; |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 1027 | break; |
| 1028 | } |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 1029 | case Diagnostic::ak_qualtype: |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 1030 | case Diagnostic::ak_declarationname: |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 1031 | case Diagnostic::ak_nameddecl: |
Douglas Gregor | dacd434 | 2009-08-26 00:04:55 +0000 | [diff] [blame] | 1032 | case Diagnostic::ak_nestednamespec: |
Douglas Gregor | 3f09327 | 2009-10-13 21:16:44 +0000 | [diff] [blame] | 1033 | case Diagnostic::ak_declcontext: |
Chris Lattner | b54d8af | 2009-10-20 05:25:22 +0000 | [diff] [blame] | 1034 | getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo), |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 1035 | Modifier, ModifierLen, |
Chris Lattner | b54d8af | 2009-10-20 05:25:22 +0000 | [diff] [blame] | 1036 | Argument, ArgumentLen, |
| 1037 | FormattedArgs.data(), FormattedArgs.size(), |
| 1038 | OutStr); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 1039 | break; |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1040 | } |
Chris Lattner | b54d8af | 2009-10-20 05:25:22 +0000 | [diff] [blame] | 1041 | |
| 1042 | // Remember this argument info for subsequent formatting operations. Turn |
| 1043 | // std::strings into a null terminated string to make it be the same case as |
| 1044 | // all the other ones. |
| 1045 | if (Kind != Diagnostic::ak_std_string) |
| 1046 | FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo))); |
| 1047 | else |
| 1048 | FormattedArgs.push_back(std::make_pair(Diagnostic::ak_c_string, |
| 1049 | (intptr_t)getArgStdStr(ArgNo).c_str())); |
| 1050 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1051 | } |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 1052 | } |
Ted Kremenek | cabe668 | 2009-01-23 20:28:53 +0000 | [diff] [blame] | 1053 | |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1054 | StoredDiagnostic::StoredDiagnostic() { } |
| 1055 | |
| 1056 | StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, |
| 1057 | llvm::StringRef Message) |
Douglas Gregor | 0a812cf | 2010-02-18 23:07:20 +0000 | [diff] [blame] | 1058 | : Level(Level), Loc(), Message(Message) { } |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1059 | |
| 1060 | StoredDiagnostic::StoredDiagnostic(Diagnostic::Level Level, |
| 1061 | const DiagnosticInfo &Info) |
Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 1062 | : Level(Level), Loc(Info.getLocation()) { |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1063 | llvm::SmallString<64> Message; |
| 1064 | Info.FormatDiagnostic(Message); |
| 1065 | this->Message.assign(Message.begin(), Message.end()); |
| 1066 | |
| 1067 | Ranges.reserve(Info.getNumRanges()); |
| 1068 | for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I) |
| 1069 | Ranges.push_back(Info.getRange(I)); |
| 1070 | |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1071 | FixIts.reserve(Info.getNumFixItHints()); |
| 1072 | for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I) |
| 1073 | FixIts.push_back(Info.getFixItHint(I)); |
Douglas Gregor | a88084b | 2010-02-18 18:08:43 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | StoredDiagnostic::~StoredDiagnostic() { } |
| 1077 | |
Ted Kremenek | cabe668 | 2009-01-23 20:28:53 +0000 | [diff] [blame] | 1078 | /// IncludeInDiagnosticCounts - This method (whose default implementation |
| 1079 | /// returns true) indicates whether the diagnostics handled by this |
| 1080 | /// DiagnosticClient should be included in the number of diagnostics |
| 1081 | /// reported by Diagnostic. |
| 1082 | bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; } |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 1083 | |
| 1084 | PartialDiagnostic::StorageAllocator::StorageAllocator() { |
| 1085 | for (unsigned I = 0; I != NumCached; ++I) |
| 1086 | FreeList[I] = Cached + I; |
| 1087 | NumFreeListEntries = NumCached; |
| 1088 | } |
| 1089 | |
| 1090 | PartialDiagnostic::StorageAllocator::~StorageAllocator() { |
| 1091 | assert(NumFreeListEntries == NumCached && "A partial is on the lamb"); |
| 1092 | } |