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