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