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