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