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