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