Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Diagnostic-related interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 27ceb9d | 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 | |
Chris Lattner | 43b628c | 2008-11-19 07:32:16 +0000 | [diff] [blame] | 24 | #include "clang/Basic/IdentifierTable.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | #include "clang/Basic/SourceLocation.h" |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringExtras.h" |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 28 | #include <vector> |
| 29 | #include <map> |
Chris Lattner | 87cf5ac | 2008-03-10 17:04:53 +0000 | [diff] [blame] | 30 | #include <cstring> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 33 | //===----------------------------------------------------------------------===// |
| 34 | // Builtin Diagnostic information |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 37 | // DefaultDiagnosticMappings - This specifies the default mapping for each diag, |
| 38 | // based on its kind. Yay for macros? |
| 39 | |
| 40 | struct DefaultMappingInfo { |
| 41 | unsigned DiagID : 14; |
| 42 | unsigned Mapping : 2; |
| 43 | }; |
| 44 | |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 45 | static const DefaultMappingInfo DefaultMappings[] = { |
Chris Lattner | 691f1ae | 2009-04-16 04:12:40 +0000 | [diff] [blame^] | 46 | #define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC) { diag::ENUM, DEFAULT_MAPPING-1 }, |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 47 | #include "clang/Basic/DiagnosticCommonKinds.inc" |
| 48 | #include "clang/Basic/DiagnosticDriverKinds.inc" |
| 49 | #include "clang/Basic/DiagnosticFrontendKinds.inc" |
| 50 | #include "clang/Basic/DiagnosticLexKinds.inc" |
| 51 | #include "clang/Basic/DiagnosticParseKinds.inc" |
| 52 | #include "clang/Basic/DiagnosticASTKinds.inc" |
| 53 | #include "clang/Basic/DiagnosticSemaKinds.inc" |
| 54 | #include "clang/Basic/DiagnosticAnalysisKinds.inc" |
| 55 | { 0, 0 } |
| 56 | }; |
Chris Lattner | 8a941e0 | 2009-04-15 16:56:26 +0000 | [diff] [blame] | 57 | #undef DIAG |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 691f1ae | 2009-04-16 04:12:40 +0000 | [diff] [blame^] | 59 | static unsigned GetDefaultDiagMapping(unsigned DiagID) { |
| 60 | // FIXME: Binary search. |
| 61 | for (unsigned i = 0, e = sizeof(DefaultMappings)/sizeof(DefaultMappings[0]); |
| 62 | i != e; ++i) |
| 63 | if (DefaultMappings[i].DiagID == DiagID) |
| 64 | return DefaultMappings[i].Mapping+1; |
| 65 | return diag::MAP_FATAL; |
| 66 | } |
| 67 | |
| 68 | |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 69 | // Diagnostic classes. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 70 | enum { |
Chris Lattner | 8a941e0 | 2009-04-15 16:56:26 +0000 | [diff] [blame] | 71 | CLASS_NOTE = 0x01, |
| 72 | CLASS_WARNING = 0x02, |
| 73 | CLASS_EXTENSION = 0x03, |
| 74 | CLASS_ERROR = 0x04 |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 77 | /// DiagnosticClasses - The class for each diagnostic. |
Chris Lattner | 4ac072a | 2009-04-15 16:44:12 +0000 | [diff] [blame] | 78 | #define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC) CLASS, |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 79 | static unsigned char DiagnosticClassesCommon[] = { |
Sebastian Redl | 4d7a089 | 2009-03-19 23:18:26 +0000 | [diff] [blame] | 80 | #include "clang/Basic/DiagnosticCommonKinds.inc" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 81 | 0 |
| 82 | }; |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 83 | static unsigned char DiagnosticClassesDriver[] = { |
Sebastian Redl | 4d7a089 | 2009-03-19 23:18:26 +0000 | [diff] [blame] | 84 | #include "clang/Basic/DiagnosticDriverKinds.inc" |
Daniel Dunbar | 4ad4b3e | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 85 | 0 |
| 86 | }; |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 87 | static unsigned char DiagnosticClassesFrontend[] = { |
Sebastian Redl | 4d7a089 | 2009-03-19 23:18:26 +0000 | [diff] [blame] | 88 | #include "clang/Basic/DiagnosticFrontendKinds.inc" |
Daniel Dunbar | 50f4f46 | 2009-03-12 10:14:16 +0000 | [diff] [blame] | 89 | 0 |
| 90 | }; |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 91 | static unsigned char DiagnosticClassesLex[] = { |
Sebastian Redl | 4d7a089 | 2009-03-19 23:18:26 +0000 | [diff] [blame] | 92 | #include "clang/Basic/DiagnosticLexKinds.inc" |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 93 | 0 |
| 94 | }; |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 95 | static unsigned char DiagnosticClassesParse[] = { |
Sebastian Redl | 4d7a089 | 2009-03-19 23:18:26 +0000 | [diff] [blame] | 96 | #include "clang/Basic/DiagnosticParseKinds.inc" |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 97 | 0 |
| 98 | }; |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 99 | static unsigned char DiagnosticClassesAST[] = { |
Sebastian Redl | 4d7a089 | 2009-03-19 23:18:26 +0000 | [diff] [blame] | 100 | #include "clang/Basic/DiagnosticASTKinds.inc" |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 101 | 0 |
| 102 | }; |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 103 | static unsigned char DiagnosticClassesSema[] = { |
Sebastian Redl | 4d7a089 | 2009-03-19 23:18:26 +0000 | [diff] [blame] | 104 | #include "clang/Basic/DiagnosticSemaKinds.inc" |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 105 | 0 |
| 106 | }; |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 107 | static unsigned char DiagnosticClassesAnalysis[] = { |
Sebastian Redl | 4d7a089 | 2009-03-19 23:18:26 +0000 | [diff] [blame] | 108 | #include "clang/Basic/DiagnosticAnalysisKinds.inc" |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 109 | 0 |
| 110 | }; |
| 111 | #undef DIAG |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 112 | |
| 113 | /// getDiagClass - Return the class field of the diagnostic. |
| 114 | /// |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 115 | static unsigned getBuiltinDiagClass(unsigned DiagID) { |
Chris Lattner | 19e8e2c | 2009-01-29 17:46:13 +0000 | [diff] [blame] | 116 | assert(DiagID < diag::DIAG_UPPER_LIMIT && |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 117 | "Diagnostic ID out of range!"); |
Chris Lattner | fe526d2 | 2009-04-15 18:48:23 +0000 | [diff] [blame] | 118 | unsigned char *Arr; |
| 119 | unsigned ArrSize; |
| 120 | if (DiagID <= diag::DIAG_START_DRIVER) { |
| 121 | DiagID -= 0; |
| 122 | Arr = DiagnosticClassesCommon; |
| 123 | ArrSize = sizeof(DiagnosticClassesCommon); |
| 124 | } else if (DiagID <= diag::DIAG_START_FRONTEND) { |
| 125 | DiagID -= diag::DIAG_START_DRIVER + 1; |
| 126 | Arr = DiagnosticClassesDriver; |
| 127 | ArrSize = sizeof(DiagnosticClassesDriver); |
| 128 | } else if (DiagID <= diag::DIAG_START_LEX) { |
| 129 | DiagID -= diag::DIAG_START_FRONTEND + 1; |
| 130 | Arr = DiagnosticClassesFrontend; |
| 131 | ArrSize = sizeof(DiagnosticClassesFrontend); |
| 132 | } else if (DiagID <= diag::DIAG_START_PARSE) { |
| 133 | DiagID -= diag::DIAG_START_LEX + 1; |
| 134 | Arr = DiagnosticClassesLex; |
| 135 | ArrSize = sizeof(DiagnosticClassesLex); |
| 136 | } else if (DiagID <= diag::DIAG_START_AST) { |
| 137 | DiagID -= diag::DIAG_START_PARSE + 1; |
| 138 | Arr = DiagnosticClassesParse; |
| 139 | ArrSize = sizeof(DiagnosticClassesParse); |
| 140 | } else if (DiagID <= diag::DIAG_START_SEMA) { |
| 141 | DiagID -= diag::DIAG_START_AST + 1; |
| 142 | Arr = DiagnosticClassesAST; |
| 143 | ArrSize = sizeof(DiagnosticClassesAST); |
| 144 | |
| 145 | } else if (DiagID <= diag::DIAG_START_ANALYSIS) { |
| 146 | DiagID -= diag::DIAG_START_SEMA + 1; |
| 147 | Arr = DiagnosticClassesSema; |
| 148 | ArrSize = sizeof(DiagnosticClassesSema); |
| 149 | } else { |
| 150 | DiagID -= diag::DIAG_START_ANALYSIS + 1; |
| 151 | Arr = DiagnosticClassesAnalysis; |
| 152 | ArrSize = sizeof(DiagnosticClassesAnalysis); |
| 153 | } |
| 154 | return DiagID < ArrSize ? Arr[DiagID] : ~0U; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /// DiagnosticText - An english message to print for the diagnostic. These |
| 158 | /// should be localized. |
Chris Lattner | 4ac072a | 2009-04-15 16:44:12 +0000 | [diff] [blame] | 159 | #define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC) DESC, |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 160 | static const char * const DiagnosticTextCommon[] = { |
Sebastian Redl | 4d7a089 | 2009-03-19 23:18:26 +0000 | [diff] [blame] | 161 | #include "clang/Basic/DiagnosticCommonKinds.inc" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 162 | 0 |
| 163 | }; |
Daniel Dunbar | 4ad4b3e | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 164 | static const char * const DiagnosticTextDriver[] = { |
Sebastian Redl | 4d7a089 | 2009-03-19 23:18:26 +0000 | [diff] [blame] | 165 | #include "clang/Basic/DiagnosticDriverKinds.inc" |
Daniel Dunbar | 4ad4b3e | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 166 | 0 |
| 167 | }; |
Daniel Dunbar | 50f4f46 | 2009-03-12 10:14:16 +0000 | [diff] [blame] | 168 | static const char * const DiagnosticTextFrontend[] = { |
Sebastian Redl | 4d7a089 | 2009-03-19 23:18:26 +0000 | [diff] [blame] | 169 | #include "clang/Basic/DiagnosticFrontendKinds.inc" |
Daniel Dunbar | 50f4f46 | 2009-03-12 10:14:16 +0000 | [diff] [blame] | 170 | 0 |
| 171 | }; |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 172 | static const char * const DiagnosticTextLex[] = { |
Sebastian Redl | 4d7a089 | 2009-03-19 23:18:26 +0000 | [diff] [blame] | 173 | #include "clang/Basic/DiagnosticLexKinds.inc" |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 174 | 0 |
| 175 | }; |
| 176 | static const char * const DiagnosticTextParse[] = { |
Sebastian Redl | 4d7a089 | 2009-03-19 23:18:26 +0000 | [diff] [blame] | 177 | #include "clang/Basic/DiagnosticParseKinds.inc" |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 178 | 0 |
| 179 | }; |
| 180 | static const char * const DiagnosticTextAST[] = { |
Sebastian Redl | 4d7a089 | 2009-03-19 23:18:26 +0000 | [diff] [blame] | 181 | #include "clang/Basic/DiagnosticASTKinds.inc" |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 182 | 0 |
| 183 | }; |
| 184 | static const char * const DiagnosticTextSema[] = { |
Sebastian Redl | 4d7a089 | 2009-03-19 23:18:26 +0000 | [diff] [blame] | 185 | #include "clang/Basic/DiagnosticSemaKinds.inc" |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 186 | 0 |
| 187 | }; |
| 188 | static const char * const DiagnosticTextAnalysis[] = { |
Sebastian Redl | 4d7a089 | 2009-03-19 23:18:26 +0000 | [diff] [blame] | 189 | #include "clang/Basic/DiagnosticAnalysisKinds.inc" |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 190 | 0 |
| 191 | }; |
| 192 | #undef DIAG |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 194 | //===----------------------------------------------------------------------===// |
| 195 | // Custom Diagnostic information |
| 196 | //===----------------------------------------------------------------------===// |
| 197 | |
| 198 | namespace clang { |
| 199 | namespace diag { |
| 200 | class CustomDiagInfo { |
| 201 | typedef std::pair<Diagnostic::Level, std::string> DiagDesc; |
| 202 | std::vector<DiagDesc> DiagInfo; |
| 203 | std::map<DiagDesc, unsigned> DiagIDs; |
| 204 | public: |
| 205 | |
| 206 | /// getDescription - Return the description of the specified custom |
| 207 | /// diagnostic. |
| 208 | const char *getDescription(unsigned DiagID) const { |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 209 | assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() && |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 210 | "Invalid diagnosic ID"); |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 211 | return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second.c_str(); |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /// getLevel - Return the level of the specified custom diagnostic. |
| 215 | Diagnostic::Level getLevel(unsigned DiagID) const { |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 216 | assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() && |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 217 | "Invalid diagnosic ID"); |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 218 | return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first; |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Chris Lattner | a1f23cc | 2008-10-17 21:24:47 +0000 | [diff] [blame] | 221 | unsigned getOrCreateDiagID(Diagnostic::Level L, const char *Message, |
| 222 | Diagnostic &Diags) { |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 223 | DiagDesc D(L, Message); |
| 224 | // Check to see if it already exists. |
| 225 | std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D); |
| 226 | if (I != DiagIDs.end() && I->first == D) |
| 227 | return I->second; |
| 228 | |
| 229 | // If not, assign a new ID. |
Chris Lattner | 88eccaf | 2009-01-29 06:55:46 +0000 | [diff] [blame] | 230 | unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT; |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 231 | DiagIDs.insert(std::make_pair(D, ID)); |
| 232 | DiagInfo.push_back(D); |
Chris Lattner | a1f23cc | 2008-10-17 21:24:47 +0000 | [diff] [blame] | 233 | |
| 234 | // If this is a warning, and all warnings are supposed to map to errors, |
| 235 | // insert the mapping now. |
| 236 | if (L == Diagnostic::Warning && Diags.getWarningsAsErrors()) |
| 237 | Diags.setDiagnosticMapping((diag::kind)ID, diag::MAP_ERROR); |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 238 | return ID; |
| 239 | } |
| 240 | }; |
| 241 | |
| 242 | } // end diag namespace |
| 243 | } // end clang namespace |
| 244 | |
| 245 | |
| 246 | //===----------------------------------------------------------------------===// |
| 247 | // Common Diagnostic implementation |
| 248 | //===----------------------------------------------------------------------===// |
| 249 | |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 250 | static void DummyArgToStringFn(Diagnostic::ArgumentKind AK, intptr_t QT, |
| 251 | const char *Modifier, unsigned ML, |
| 252 | const char *Argument, unsigned ArgLen, |
Chris Lattner | 92dd386 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 253 | llvm::SmallVectorImpl<char> &Output, |
| 254 | void *Cookie) { |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 255 | const char *Str = "<can't format argument>"; |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 256 | Output.append(Str, Str+strlen(Str)); |
| 257 | } |
| 258 | |
| 259 | |
Ted Kremenek | b4398aa | 2008-08-07 17:49:57 +0000 | [diff] [blame] | 260 | Diagnostic::Diagnostic(DiagnosticClient *client) : Client(client) { |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 261 | AllExtensionsSilenced = 0; |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 262 | IgnoreAllWarnings = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 263 | WarningsAsErrors = false; |
Daniel Dunbar | 2fe0997 | 2008-09-12 18:10:20 +0000 | [diff] [blame] | 264 | SuppressSystemWarnings = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 265 | |
| 266 | ErrorOccurred = false; |
Chris Lattner | 1522142 | 2009-02-06 04:16:02 +0000 | [diff] [blame] | 267 | FatalErrorOccurred = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 268 | NumDiagnostics = 0; |
| 269 | NumErrors = 0; |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 270 | CustomDiagInfo = 0; |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 271 | CurDiagID = ~0U; |
Douglas Gregor | 525c4b0 | 2009-03-19 18:55:06 +0000 | [diff] [blame] | 272 | LastDiagLevel = Ignored; |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 273 | |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 274 | ArgToStringFn = DummyArgToStringFn; |
Chris Lattner | 92dd386 | 2009-02-19 23:53:20 +0000 | [diff] [blame] | 275 | ArgToStringCookie = 0; |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 276 | |
Chris Lattner | 691f1ae | 2009-04-16 04:12:40 +0000 | [diff] [blame^] | 277 | // Set all mappings to 'unset'. |
| 278 | memset(DiagMappings, 0, sizeof(DiagMappings)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 281 | Diagnostic::~Diagnostic() { |
| 282 | delete CustomDiagInfo; |
| 283 | } |
| 284 | |
| 285 | /// getCustomDiagID - Return an ID for a diagnostic with the specified message |
| 286 | /// and level. If this is the first request for this diagnosic, it is |
| 287 | /// registered and created, otherwise the existing ID is returned. |
| 288 | unsigned Diagnostic::getCustomDiagID(Level L, const char *Message) { |
| 289 | if (CustomDiagInfo == 0) |
| 290 | CustomDiagInfo = new diag::CustomDiagInfo(); |
Chris Lattner | a1f23cc | 2008-10-17 21:24:47 +0000 | [diff] [blame] | 291 | return CustomDiagInfo->getOrCreateDiagID(L, Message, *this); |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 295 | /// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic |
| 296 | /// level of the specified diagnostic ID is a Warning or Extension. |
| 297 | /// This only works on builtin diagnostics, not custom ones, and is not legal to |
| 298 | /// call on NOTEs. |
| 299 | bool Diagnostic::isBuiltinWarningOrExtension(unsigned DiagID) { |
Chris Lattner | 8a941e0 | 2009-04-15 16:56:26 +0000 | [diff] [blame] | 300 | return DiagID < diag::DIAG_UPPER_LIMIT && |
| 301 | getBuiltinDiagClass(DiagID) != CLASS_ERROR; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 304 | /// \brief Determine whether the given built-in diagnostic ID is a |
| 305 | /// Note. |
| 306 | bool Diagnostic::isBuiltinNote(unsigned DiagID) { |
Chris Lattner | 8a941e0 | 2009-04-15 16:56:26 +0000 | [diff] [blame] | 307 | return DiagID < diag::DIAG_UPPER_LIMIT && |
| 308 | getBuiltinDiagClass(DiagID) == CLASS_NOTE; |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 309 | } |
| 310 | |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 311 | /// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic |
| 312 | /// ID is for an extension of some sort. |
| 313 | /// |
| 314 | bool Diagnostic::isBuiltinExtensionDiag(unsigned DiagID) { |
Chris Lattner | 8a941e0 | 2009-04-15 16:56:26 +0000 | [diff] [blame] | 315 | return DiagID < diag::DIAG_UPPER_LIMIT && |
| 316 | getBuiltinDiagClass(DiagID) == CLASS_EXTENSION; |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 319 | |
| 320 | /// getDescription - Given a diagnostic ID, return a description of the |
| 321 | /// issue. |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 322 | const char *Diagnostic::getDescription(unsigned DiagID) const { |
Daniel Dunbar | 4ad4b3e | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 323 | if (DiagID < diag::DIAG_START_DRIVER) |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 324 | return DiagnosticTextCommon[DiagID]; |
Daniel Dunbar | 50f4f46 | 2009-03-12 10:14:16 +0000 | [diff] [blame] | 325 | else if (DiagID < diag::DIAG_START_FRONTEND) |
Daniel Dunbar | 4ad4b3e | 2009-03-12 08:55:43 +0000 | [diff] [blame] | 326 | return DiagnosticTextDriver[DiagID - diag::DIAG_START_DRIVER - 1]; |
Daniel Dunbar | 50f4f46 | 2009-03-12 10:14:16 +0000 | [diff] [blame] | 327 | else if (DiagID < diag::DIAG_START_LEX) |
| 328 | return DiagnosticTextFrontend[DiagID - diag::DIAG_START_FRONTEND - 1]; |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 329 | else if (DiagID < diag::DIAG_START_PARSE) |
| 330 | return DiagnosticTextLex[DiagID - diag::DIAG_START_LEX - 1]; |
| 331 | else if (DiagID < diag::DIAG_START_AST) |
| 332 | return DiagnosticTextParse[DiagID - diag::DIAG_START_PARSE - 1]; |
| 333 | else if (DiagID < diag::DIAG_START_SEMA) |
| 334 | return DiagnosticTextAST[DiagID - diag::DIAG_START_AST - 1]; |
| 335 | else if (DiagID < diag::DIAG_START_ANALYSIS) |
| 336 | return DiagnosticTextSema[DiagID - diag::DIAG_START_SEMA - 1]; |
| 337 | else if (DiagID < diag::DIAG_UPPER_LIMIT) |
| 338 | return DiagnosticTextAnalysis[DiagID - diag::DIAG_START_ANALYSIS - 1]; |
Chris Lattner | 20c6b3b | 2009-01-27 18:30:58 +0000 | [diff] [blame] | 339 | return CustomDiagInfo->getDescription(DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /// getDiagnosticLevel - Based on the way the client configured the Diagnostic |
| 343 | /// object, classify the specified diagnostic ID into a Level, consumable by |
| 344 | /// the DiagnosticClient. |
| 345 | Diagnostic::Level Diagnostic::getDiagnosticLevel(unsigned DiagID) const { |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 346 | // Handle custom diagnostics, which cannot be mapped. |
Chris Lattner | 19e8e2c | 2009-01-29 17:46:13 +0000 | [diff] [blame] | 347 | if (DiagID >= diag::DIAG_UPPER_LIMIT) |
Chris Lattner | 182745a | 2007-12-02 01:09:57 +0000 | [diff] [blame] | 348 | return CustomDiagInfo->getLevel(DiagID); |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 349 | |
| 350 | unsigned DiagClass = getBuiltinDiagClass(DiagID); |
Chris Lattner | 8a941e0 | 2009-04-15 16:56:26 +0000 | [diff] [blame] | 351 | assert(DiagClass != CLASS_NOTE && "Cannot get diagnostic level of a note!"); |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 352 | return getDiagnosticLevel(DiagID, DiagClass); |
| 353 | } |
| 354 | |
| 355 | /// getDiagnosticLevel - Based on the way the client configured the Diagnostic |
| 356 | /// object, classify the specified diagnostic ID into a Level, consumable by |
| 357 | /// the DiagnosticClient. |
| 358 | Diagnostic::Level |
| 359 | Diagnostic::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 360 | // Specific non-error diagnostics may be mapped to various levels from ignored |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 361 | // to error. Errors can only be mapped to fatal. |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 362 | Diagnostic::Level Result = Diagnostic::Fatal; |
Chris Lattner | 691f1ae | 2009-04-16 04:12:40 +0000 | [diff] [blame^] | 363 | |
| 364 | // Get the mapping information, if unset, compute it lazily. |
| 365 | unsigned MappingInfo = getDiagnosticMappingInfo((diag::kind)DiagID); |
| 366 | if (MappingInfo == 0) { |
| 367 | MappingInfo = GetDefaultDiagMapping(DiagID); |
| 368 | setDiagnosticMappingInternal(DiagID, MappingInfo, false); |
| 369 | } |
| 370 | |
| 371 | switch (MappingInfo & 7) { |
| 372 | default: assert(0 && "Unknown mapping!"); |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 373 | case diag::MAP_IGNORE: |
| 374 | return Diagnostic::Ignored; |
| 375 | case diag::MAP_ERROR: |
| 376 | Result = Diagnostic::Error; |
| 377 | break; |
| 378 | case diag::MAP_FATAL: |
| 379 | Result = Diagnostic::Fatal; |
| 380 | break; |
| 381 | case diag::MAP_WARNING: |
| 382 | // If warnings are globally mapped to ignore or error, do it. |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 383 | if (IgnoreAllWarnings) |
| 384 | return Diagnostic::Ignored; |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 385 | Result = WarningsAsErrors ? Diagnostic::Error : Diagnostic::Warning; |
| 386 | break; |
Chris Lattner | 5b4681c | 2008-05-29 15:36:45 +0000 | [diff] [blame] | 387 | } |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 388 | |
| 389 | // Okay, we're about to return this as a "diagnostic to emit" one last check: |
| 390 | // if this is any sort of extension warning, and if we're in an __extension__ |
| 391 | // block, silence it. |
| 392 | if (AllExtensionsSilenced && isBuiltinExtensionDiag(DiagID)) |
| 393 | return Diagnostic::Ignored; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 394 | |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 395 | return Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 398 | /// ProcessDiag - This is the method used to report a diagnostic that is |
| 399 | /// finally fully formed. |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 400 | void Diagnostic::ProcessDiag() { |
| 401 | DiagnosticInfo Info(this); |
Douglas Gregor | 525c4b0 | 2009-03-19 18:55:06 +0000 | [diff] [blame] | 402 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 403 | // Figure out the diagnostic level of this message. |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 404 | Diagnostic::Level DiagLevel; |
| 405 | unsigned DiagID = Info.getID(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 406 | |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 407 | // ShouldEmitInSystemHeader - True if this diagnostic should be produced even |
| 408 | // in a system header. |
| 409 | bool ShouldEmitInSystemHeader; |
| 410 | |
| 411 | if (DiagID >= diag::DIAG_UPPER_LIMIT) { |
| 412 | // Handle custom diagnostics, which cannot be mapped. |
| 413 | DiagLevel = CustomDiagInfo->getLevel(DiagID); |
| 414 | |
| 415 | // Custom diagnostics always are emitted in system headers. |
| 416 | ShouldEmitInSystemHeader = true; |
| 417 | } else { |
| 418 | // Get the class of the diagnostic. If this is a NOTE, map it onto whatever |
| 419 | // the diagnostic level was for the previous diagnostic so that it is |
| 420 | // filtered the same as the previous diagnostic. |
| 421 | unsigned DiagClass = getBuiltinDiagClass(DiagID); |
Chris Lattner | 8a941e0 | 2009-04-15 16:56:26 +0000 | [diff] [blame] | 422 | if (DiagClass == CLASS_NOTE) { |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 423 | DiagLevel = Diagnostic::Note; |
| 424 | ShouldEmitInSystemHeader = false; // extra consideration is needed |
| 425 | } else { |
| 426 | // If this is not an error and we are in a system header, we ignore it. |
| 427 | // Check the original Diag ID here, because we also want to ignore |
| 428 | // extensions and warnings in -Werror and -pedantic-errors modes, which |
| 429 | // *map* warnings/extensions to errors. |
Chris Lattner | 8a941e0 | 2009-04-15 16:56:26 +0000 | [diff] [blame] | 430 | ShouldEmitInSystemHeader = DiagClass == CLASS_ERROR; |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 431 | |
| 432 | DiagLevel = getDiagnosticLevel(DiagID, DiagClass); |
| 433 | } |
| 434 | } |
| 435 | |
Douglas Gregor | 525c4b0 | 2009-03-19 18:55:06 +0000 | [diff] [blame] | 436 | if (DiagLevel != Diagnostic::Note) { |
| 437 | // Record that a fatal error occurred only when we see a second |
| 438 | // non-note diagnostic. This allows notes to be attached to the |
| 439 | // fatal error, but suppresses any diagnostics that follow those |
| 440 | // notes. |
| 441 | if (LastDiagLevel == Diagnostic::Fatal) |
| 442 | FatalErrorOccurred = true; |
| 443 | |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 444 | LastDiagLevel = DiagLevel; |
Douglas Gregor | 525c4b0 | 2009-03-19 18:55:06 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | // If a fatal error has already been emitted, silence all subsequent |
| 448 | // diagnostics. |
| 449 | if (FatalErrorOccurred) |
| 450 | return; |
| 451 | |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 452 | // If the client doesn't care about this message, don't issue it. If this is |
| 453 | // a note and the last real diagnostic was ignored, ignore it too. |
| 454 | if (DiagLevel == Diagnostic::Ignored || |
| 455 | (DiagLevel == Diagnostic::Note && LastDiagLevel == Diagnostic::Ignored)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 456 | return; |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 457 | |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 458 | // If this diagnostic is in a system header and is not a clang error, suppress |
| 459 | // it. |
| 460 | if (SuppressSystemWarnings && !ShouldEmitInSystemHeader && |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 461 | Info.getLocation().isValid() && |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 462 | Info.getLocation().getSpellingLoc().isInSystemHeader() && |
Chris Lattner | 336f26b | 2009-02-17 06:52:20 +0000 | [diff] [blame] | 463 | (DiagLevel != Diagnostic::Note || LastDiagLevel == Diagnostic::Ignored)) { |
| 464 | LastDiagLevel = Diagnostic::Ignored; |
Chris Lattner | 7097d91 | 2008-02-03 09:00:04 +0000 | [diff] [blame] | 465 | return; |
Chris Lattner | 336f26b | 2009-02-17 06:52:20 +0000 | [diff] [blame] | 466 | } |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 467 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 468 | if (DiagLevel >= Diagnostic::Error) { |
| 469 | ErrorOccurred = true; |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 470 | ++NumErrors; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 471 | } |
Chris Lattner | f5d2328 | 2009-02-17 06:49:55 +0000 | [diff] [blame] | 472 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 473 | // Finally, report it. |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 474 | Client->HandleDiagnostic(DiagLevel, Info); |
Ted Kremenek | cabe668 | 2009-01-23 20:28:53 +0000 | [diff] [blame] | 475 | if (Client->IncludeInDiagnosticCounts()) ++NumDiagnostics; |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 476 | |
Douglas Gregor | ee1828a | 2009-03-10 18:03:33 +0000 | [diff] [blame] | 477 | CurDiagID = ~0U; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 480 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 481 | DiagnosticClient::~DiagnosticClient() {} |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 482 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 483 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 484 | /// ModifierIs - Return true if the specified modifier matches specified string. |
| 485 | template <std::size_t StrLen> |
| 486 | static bool ModifierIs(const char *Modifier, unsigned ModifierLen, |
| 487 | const char (&Str)[StrLen]) { |
| 488 | return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1); |
| 489 | } |
| 490 | |
| 491 | /// HandleSelectModifier - Handle the integer 'select' modifier. This is used |
| 492 | /// like this: %select{foo|bar|baz}2. This means that the integer argument |
| 493 | /// "%2" has a value from 0-2. If the value is 0, the diagnostic prints 'foo'. |
| 494 | /// If the value is 1, it prints 'bar'. If it has the value 2, it prints 'baz'. |
| 495 | /// This is very useful for certain classes of variant diagnostics. |
| 496 | static void HandleSelectModifier(unsigned ValNo, |
| 497 | const char *Argument, unsigned ArgumentLen, |
| 498 | llvm::SmallVectorImpl<char> &OutStr) { |
| 499 | const char *ArgumentEnd = Argument+ArgumentLen; |
| 500 | |
| 501 | // Skip over 'ValNo' |'s. |
| 502 | while (ValNo) { |
| 503 | const char *NextVal = std::find(Argument, ArgumentEnd, '|'); |
| 504 | assert(NextVal != ArgumentEnd && "Value for integer select modifier was" |
| 505 | " larger than the number of options in the diagnostic string!"); |
| 506 | Argument = NextVal+1; // Skip this string. |
| 507 | --ValNo; |
| 508 | } |
| 509 | |
| 510 | // Get the end of the value. This is either the } or the |. |
| 511 | const char *EndPtr = std::find(Argument, ArgumentEnd, '|'); |
| 512 | // Add the value to the output string. |
| 513 | OutStr.append(Argument, EndPtr); |
| 514 | } |
| 515 | |
| 516 | /// HandleIntegerSModifier - Handle the integer 's' modifier. This adds the |
| 517 | /// letter 's' to the string if the value is not 1. This is used in cases like |
| 518 | /// this: "you idiot, you have %4 parameter%s4!". |
| 519 | static void HandleIntegerSModifier(unsigned ValNo, |
| 520 | llvm::SmallVectorImpl<char> &OutStr) { |
| 521 | if (ValNo != 1) |
| 522 | OutStr.push_back('s'); |
| 523 | } |
| 524 | |
| 525 | |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 526 | /// PluralNumber - Parse an unsigned integer and advance Start. |
Chris Lattner | d2aa7c9 | 2009-04-15 17:13:42 +0000 | [diff] [blame] | 527 | static unsigned PluralNumber(const char *&Start, const char *End) { |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 528 | // Programming 101: Parse a decimal number :-) |
| 529 | unsigned Val = 0; |
| 530 | while (Start != End && *Start >= '0' && *Start <= '9') { |
| 531 | Val *= 10; |
| 532 | Val += *Start - '0'; |
| 533 | ++Start; |
| 534 | } |
| 535 | return Val; |
| 536 | } |
| 537 | |
| 538 | /// TestPluralRange - Test if Val is in the parsed range. Modifies Start. |
Chris Lattner | d2aa7c9 | 2009-04-15 17:13:42 +0000 | [diff] [blame] | 539 | static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) { |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 540 | if (*Start != '[') { |
| 541 | unsigned Ref = PluralNumber(Start, End); |
| 542 | return Ref == Val; |
| 543 | } |
| 544 | |
| 545 | ++Start; |
| 546 | unsigned Low = PluralNumber(Start, End); |
| 547 | assert(*Start == ',' && "Bad plural expression syntax: expected ,"); |
| 548 | ++Start; |
| 549 | unsigned High = PluralNumber(Start, End); |
| 550 | assert(*Start == ']' && "Bad plural expression syntax: expected )"); |
| 551 | ++Start; |
| 552 | return Low <= Val && Val <= High; |
| 553 | } |
| 554 | |
| 555 | /// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier. |
Chris Lattner | d2aa7c9 | 2009-04-15 17:13:42 +0000 | [diff] [blame] | 556 | static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) { |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 557 | // Empty condition? |
| 558 | if (*Start == ':') |
| 559 | return true; |
| 560 | |
| 561 | while (1) { |
| 562 | char C = *Start; |
| 563 | if (C == '%') { |
| 564 | // Modulo expression |
| 565 | ++Start; |
| 566 | unsigned Arg = PluralNumber(Start, End); |
| 567 | assert(*Start == '=' && "Bad plural expression syntax: expected ="); |
| 568 | ++Start; |
| 569 | unsigned ValMod = ValNo % Arg; |
| 570 | if (TestPluralRange(ValMod, Start, End)) |
| 571 | return true; |
| 572 | } else { |
Sebastian Redl | e206532 | 2008-11-27 07:28:14 +0000 | [diff] [blame] | 573 | assert((C == '[' || (C >= '0' && C <= '9')) && |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 574 | "Bad plural expression syntax: unexpected character"); |
| 575 | // Range expression |
| 576 | if (TestPluralRange(ValNo, Start, End)) |
| 577 | return true; |
| 578 | } |
| 579 | |
| 580 | // Scan for next or-expr part. |
| 581 | Start = std::find(Start, End, ','); |
| 582 | if(Start == End) |
| 583 | break; |
| 584 | ++Start; |
| 585 | } |
| 586 | return false; |
| 587 | } |
| 588 | |
| 589 | /// HandlePluralModifier - Handle the integer 'plural' modifier. This is used |
| 590 | /// for complex plural forms, or in languages where all plurals are complex. |
| 591 | /// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are |
| 592 | /// conditions that are tested in order, the form corresponding to the first |
| 593 | /// that applies being emitted. The empty condition is always true, making the |
| 594 | /// last form a default case. |
| 595 | /// Conditions are simple boolean expressions, where n is the number argument. |
| 596 | /// Here are the rules. |
| 597 | /// condition := expression | empty |
| 598 | /// empty := -> always true |
| 599 | /// expression := numeric [',' expression] -> logical or |
| 600 | /// numeric := range -> true if n in range |
| 601 | /// | '%' number '=' range -> true if n % number in range |
| 602 | /// range := number |
| 603 | /// | '[' number ',' number ']' -> ranges are inclusive both ends |
| 604 | /// |
| 605 | /// Here are some examples from the GNU gettext manual written in this form: |
| 606 | /// English: |
| 607 | /// {1:form0|:form1} |
| 608 | /// Latvian: |
| 609 | /// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0} |
| 610 | /// Gaeilge: |
| 611 | /// {1:form0|2:form1|:form2} |
| 612 | /// Romanian: |
| 613 | /// {1:form0|0,%100=[1,19]:form1|:form2} |
| 614 | /// Lithuanian: |
| 615 | /// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1} |
| 616 | /// Russian (requires repeated form): |
| 617 | /// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2} |
| 618 | /// Slovak |
| 619 | /// {1:form0|[2,4]:form1|:form2} |
| 620 | /// Polish (requires repeated form): |
| 621 | /// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2} |
| 622 | static void HandlePluralModifier(unsigned ValNo, |
| 623 | const char *Argument, unsigned ArgumentLen, |
| 624 | llvm::SmallVectorImpl<char> &OutStr) |
| 625 | { |
| 626 | const char *ArgumentEnd = Argument + ArgumentLen; |
| 627 | while (1) { |
| 628 | assert(Argument < ArgumentEnd && "Plural expression didn't match."); |
| 629 | const char *ExprEnd = Argument; |
| 630 | while (*ExprEnd != ':') { |
| 631 | assert(ExprEnd != ArgumentEnd && "Plural missing expression end"); |
| 632 | ++ExprEnd; |
| 633 | } |
| 634 | if (EvalPluralExpr(ValNo, Argument, ExprEnd)) { |
| 635 | Argument = ExprEnd + 1; |
| 636 | ExprEnd = std::find(Argument, ArgumentEnd, '|'); |
| 637 | OutStr.append(Argument, ExprEnd); |
| 638 | return; |
| 639 | } |
| 640 | Argument = std::find(Argument, ArgumentEnd - 1, '|') + 1; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 645 | /// FormatDiagnostic - Format this diagnostic into a string, substituting the |
| 646 | /// formal arguments into the %0 slots. The result is appended onto the Str |
| 647 | /// array. |
| 648 | void DiagnosticInfo:: |
| 649 | FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const { |
| 650 | const char *DiagStr = getDiags()->getDescription(getID()); |
| 651 | const char *DiagEnd = DiagStr+strlen(DiagStr); |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 652 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 653 | while (DiagStr != DiagEnd) { |
| 654 | if (DiagStr[0] != '%') { |
| 655 | // Append non-%0 substrings to Str if we have one. |
| 656 | const char *StrEnd = std::find(DiagStr, DiagEnd, '%'); |
| 657 | OutStr.append(DiagStr, StrEnd); |
| 658 | DiagStr = StrEnd; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 659 | continue; |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 660 | } else if (DiagStr[1] == '%') { |
| 661 | OutStr.push_back('%'); // %% -> %. |
| 662 | DiagStr += 2; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 663 | continue; |
| 664 | } |
| 665 | |
| 666 | // Skip the %. |
| 667 | ++DiagStr; |
| 668 | |
| 669 | // This must be a placeholder for a diagnostic argument. The format for a |
| 670 | // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0". |
| 671 | // The digit is a number from 0-9 indicating which argument this comes from. |
| 672 | // The modifier is a string of digits from the set [-a-z]+, arguments is a |
| 673 | // brace enclosed string. |
| 674 | const char *Modifier = 0, *Argument = 0; |
| 675 | unsigned ModifierLen = 0, ArgumentLen = 0; |
| 676 | |
| 677 | // Check to see if we have a modifier. If so eat it. |
| 678 | if (!isdigit(DiagStr[0])) { |
| 679 | Modifier = DiagStr; |
| 680 | while (DiagStr[0] == '-' || |
| 681 | (DiagStr[0] >= 'a' && DiagStr[0] <= 'z')) |
| 682 | ++DiagStr; |
| 683 | ModifierLen = DiagStr-Modifier; |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 684 | |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 685 | // If we have an argument, get it next. |
| 686 | if (DiagStr[0] == '{') { |
| 687 | ++DiagStr; // Skip {. |
| 688 | Argument = DiagStr; |
| 689 | |
| 690 | for (; DiagStr[0] != '}'; ++DiagStr) |
| 691 | assert(DiagStr[0] && "Mismatched {}'s in diagnostic string!"); |
| 692 | ArgumentLen = DiagStr-Argument; |
| 693 | ++DiagStr; // Skip }. |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 694 | } |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | assert(isdigit(*DiagStr) && "Invalid format for argument in diagnostic"); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 698 | unsigned ArgNo = *DiagStr++ - '0'; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 699 | |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 700 | switch (getArgKind(ArgNo)) { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 701 | // ---- STRINGS ---- |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 702 | case Diagnostic::ak_std_string: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 703 | const std::string &S = getArgStdStr(ArgNo); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 704 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
| 705 | OutStr.append(S.begin(), S.end()); |
| 706 | break; |
| 707 | } |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 708 | case Diagnostic::ak_c_string: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 709 | const char *S = getArgCStr(ArgNo); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 710 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
| 711 | OutStr.append(S, S + strlen(S)); |
| 712 | break; |
| 713 | } |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 714 | // ---- INTEGERS ---- |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 715 | case Diagnostic::ak_sint: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 716 | int Val = getArgSInt(ArgNo); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 717 | |
| 718 | if (ModifierIs(Modifier, ModifierLen, "select")) { |
| 719 | HandleSelectModifier((unsigned)Val, Argument, ArgumentLen, OutStr); |
| 720 | } else if (ModifierIs(Modifier, ModifierLen, "s")) { |
| 721 | HandleIntegerSModifier(Val, OutStr); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 722 | } else if (ModifierIs(Modifier, ModifierLen, "plural")) { |
| 723 | HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 724 | } else { |
| 725 | assert(ModifierLen == 0 && "Unknown integer modifier"); |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 726 | // FIXME: Optimize |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 727 | std::string S = llvm::itostr(Val); |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 728 | OutStr.append(S.begin(), S.end()); |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 729 | } |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 730 | break; |
| 731 | } |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 732 | case Diagnostic::ak_uint: { |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 733 | unsigned Val = getArgUInt(ArgNo); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 734 | |
| 735 | if (ModifierIs(Modifier, ModifierLen, "select")) { |
| 736 | HandleSelectModifier(Val, Argument, ArgumentLen, OutStr); |
| 737 | } else if (ModifierIs(Modifier, ModifierLen, "s")) { |
| 738 | HandleIntegerSModifier(Val, OutStr); |
Sebastian Redl | e4c452c | 2008-11-22 13:44:36 +0000 | [diff] [blame] | 739 | } else if (ModifierIs(Modifier, ModifierLen, "plural")) { |
| 740 | HandlePluralModifier((unsigned)Val, Argument, ArgumentLen, OutStr); |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 741 | } else { |
| 742 | assert(ModifierLen == 0 && "Unknown integer modifier"); |
| 743 | |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 744 | // FIXME: Optimize |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 745 | std::string S = llvm::utostr_32(Val); |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 746 | OutStr.append(S.begin(), S.end()); |
Chris Lattner | 30bc965 | 2008-11-19 07:22:31 +0000 | [diff] [blame] | 747 | } |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 748 | break; |
Chris Lattner | af7ae4e | 2008-11-21 07:50:02 +0000 | [diff] [blame] | 749 | } |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 750 | // ---- NAMES and TYPES ---- |
| 751 | case Diagnostic::ak_identifierinfo: { |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 752 | const IdentifierInfo *II = getArgIdentifier(ArgNo); |
| 753 | assert(ModifierLen == 0 && "No modifiers for strings yet"); |
Chris Lattner | d0344a4 | 2009-02-19 23:45:49 +0000 | [diff] [blame] | 754 | OutStr.push_back('\''); |
Chris Lattner | 08631c5 | 2008-11-23 21:45:46 +0000 | [diff] [blame] | 755 | OutStr.append(II->getName(), II->getName() + II->getLength()); |
| 756 | OutStr.push_back('\''); |
| 757 | break; |
| 758 | } |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 759 | case Diagnostic::ak_qualtype: |
Chris Lattner | 011bb4e | 2008-11-23 20:28:15 +0000 | [diff] [blame] | 760 | case Diagnostic::ak_declarationname: |
Douglas Gregor | 47b9a1c | 2009-02-04 17:27:36 +0000 | [diff] [blame] | 761 | case Diagnostic::ak_nameddecl: |
Chris Lattner | 3fdf4b0 | 2008-11-23 09:21:17 +0000 | [diff] [blame] | 762 | getDiags()->ConvertArgToString(getArgKind(ArgNo), getRawArg(ArgNo), |
| 763 | Modifier, ModifierLen, |
| 764 | Argument, ArgumentLen, OutStr); |
Chris Lattner | 22caddc | 2008-11-23 09:13:29 +0000 | [diff] [blame] | 765 | break; |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 766 | } |
| 767 | } |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 768 | } |
Ted Kremenek | cabe668 | 2009-01-23 20:28:53 +0000 | [diff] [blame] | 769 | |
| 770 | /// IncludeInDiagnosticCounts - This method (whose default implementation |
| 771 | /// returns true) indicates whether the diagnostics handled by this |
| 772 | /// DiagnosticClient should be included in the number of diagnostics |
| 773 | /// reported by Diagnostic. |
| 774 | bool DiagnosticClient::IncludeInDiagnosticCounts() const { return true; } |