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