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