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