Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 1 | //===--- Warnings.cpp - C-Language Front-end ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // Command line warning options handler. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | // |
| 14 | // This file is responsible for handling all warning options. This includes |
| 15 | // a number of -Wfoo options and their variants, which are driven by TableGen- |
| 16 | // generated data, and the special cases -pedantic, -pedantic-errors, -w and |
| 17 | // -Werror. |
| 18 | // |
Sebastian Redl | c5613db | 2009-03-07 12:09:25 +0000 | [diff] [blame] | 19 | // Each warning option controls any number of actual warnings. |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 20 | // Given a warning option 'foo', the following are valid: |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 21 | // |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 22 | // -Wfoo -> alias of -Wfoo=warn |
| 23 | // -Wno-foo -> alias of -Wfoo=ignore |
| 24 | // -Werror=foo -> alias of -Wfoo=error |
| 25 | // |
Chris Lattner | d613c3d | 2009-04-08 22:37:15 +0000 | [diff] [blame] | 26 | #include "clang-cc.h" |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 27 | #include "clang/Basic/Diagnostic.h" |
| 28 | #include "clang/Sema/SemaDiagnostic.h" |
| 29 | #include "clang/Lex/LexDiagnostic.h" |
| 30 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | 3c304bd | 2009-04-11 18:40:46 +0000 | [diff] [blame] | 31 | #include <cstdio> |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 32 | #include <utility> |
| 33 | #include <algorithm> |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 36 | // This gets all -W options, including -Werror, -W[no-]system-headers, etc. The |
Chris Lattner | 1d13a5d | 2009-04-15 04:37:12 +0000 | [diff] [blame] | 37 | // driver has stripped off -Wa,foo etc. The driver has also translated -W to |
| 38 | // -Wextra, so we don't need to worry about it. |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 39 | static llvm::cl::list<std::string> |
| 40 | OptWarnings("W", llvm::cl::Prefix); |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 41 | |
| 42 | static llvm::cl::opt<bool> OptPedantic("pedantic"); |
| 43 | static llvm::cl::opt<bool> OptPedanticErrors("pedantic-errors"); |
| 44 | static llvm::cl::opt<bool> OptNoWarnings("w"); |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 45 | |
| 46 | namespace { |
| 47 | struct WarningOption { |
| 48 | const char *Name; |
| 49 | const diag::kind *Members; |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 50 | unsigned NumMembers; |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 51 | }; |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 52 | } |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 53 | #define DIAGS(a) a, unsigned(sizeof(a) / sizeof(a[0])) |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 54 | // These tables will be TableGenerated later. |
| 55 | // First the table sets describing the diagnostics controlled by each option. |
| 56 | static const diag::kind UnusedMacrosDiags[] = { diag::pp_macro_not_used }; |
| 57 | static const diag::kind FloatEqualDiags[] = { diag::warn_floatingpoint_eq }; |
Chris Lattner | 7eba5c9 | 2009-04-14 20:36:01 +0000 | [diff] [blame] | 58 | static const diag::kind ExtraTokens[] = { diag::ext_pp_extra_tokens_at_eol }; |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 59 | static const diag::kind ReadOnlySetterAttrsDiags[] = { |
| 60 | diag::warn_objc_property_attr_mutually_exclusive |
| 61 | }; |
| 62 | static const diag::kind FormatNonLiteralDiags[] = { |
| 63 | diag::warn_printf_not_string_constant |
| 64 | }; |
| 65 | static const diag::kind UndefDiags[] = { diag::warn_pp_undef_identifier }; |
| 66 | static const diag::kind ImplicitFunctionDeclarationDiags[] = { |
| 67 | diag::ext_implicit_function_decl, diag::warn_implicit_function_decl |
| 68 | }; |
Eli Friedman | 757e0dd | 2009-03-30 21:19:48 +0000 | [diff] [blame] | 69 | static const diag::kind PointerSignDiags[] = { |
| 70 | diag::ext_typecheck_convert_incompatible_pointer_sign |
| 71 | }; |
Steve Naroff | 17f689f | 2009-03-31 15:00:11 +0000 | [diff] [blame] | 72 | static const diag::kind DeprecatedDeclarations[] = { diag::warn_deprecated }; |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 73 | static const diag::kind MissingPrototypesDiags[] = { |
| 74 | diag::warn_missing_prototype |
| 75 | }; |
Chris Lattner | d613c3d | 2009-04-08 22:37:15 +0000 | [diff] [blame] | 76 | static const diag::kind TrigraphsDiags[] = { |
| 77 | diag::trigraph_ignored, diag::trigraph_ignored_block_comment, |
| 78 | diag::trigraph_ends_block_comment, diag::trigraph_converted |
| 79 | }; |
Steve Naroff | 17f689f | 2009-03-31 15:00:11 +0000 | [diff] [blame] | 80 | |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 81 | // Second the table of options. MUST be sorted by name! Binary lookup is done. |
| 82 | static const WarningOption OptionTable[] = { |
Chris Lattner | 7eba5c9 | 2009-04-14 20:36:01 +0000 | [diff] [blame] | 83 | { "deprecated-declarations", DIAGS(DeprecatedDeclarations) }, |
| 84 | { "extra-tokens", DIAGS(ExtraTokens) }, |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 85 | { "float-equal", DIAGS(FloatEqualDiags) }, |
| 86 | { "format-nonliteral", DIAGS(FormatNonLiteralDiags) }, |
| 87 | { "implicit-function-declaration", DIAGS(ImplicitFunctionDeclarationDiags) }, |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 88 | { "missing-prototypes", DIAGS(MissingPrototypesDiags) }, |
Eli Friedman | 757e0dd | 2009-03-30 21:19:48 +0000 | [diff] [blame] | 89 | { "pointer-sign", DIAGS(PointerSignDiags) }, |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 90 | { "readonly-setter-attrs", DIAGS(ReadOnlySetterAttrsDiags) }, |
Chris Lattner | d613c3d | 2009-04-08 22:37:15 +0000 | [diff] [blame] | 91 | { "trigraphs", DIAGS(TrigraphsDiags) }, |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 92 | { "undef", DIAGS(UndefDiags) }, |
| 93 | { "unused-macros", DIAGS(UnusedMacrosDiags) }, |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 94 | }; |
| 95 | static const size_t OptionTableSize = |
| 96 | sizeof(OptionTable) / sizeof(OptionTable[0]); |
| 97 | |
Chris Lattner | d613c3d | 2009-04-08 22:37:15 +0000 | [diff] [blame] | 98 | static bool WarningOptionCompare(const WarningOption &LHS, |
| 99 | const WarningOption &RHS) { |
| 100 | return strcmp(LHS.Name, RHS.Name) < 0; |
| 101 | } |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 102 | |
Chris Lattner | d613c3d | 2009-04-08 22:37:15 +0000 | [diff] [blame] | 103 | bool clang::ProcessWarningOptions(Diagnostic &Diags) { |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 104 | Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers |
| 105 | |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 106 | // FIXME: These should be mapped to group options. |
| 107 | Diags.setIgnoreAllWarnings(OptNoWarnings); |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 108 | |
| 109 | // Set some defaults that are currently set manually. This, too, should |
| 110 | // be in the tablegen stuff later. |
| 111 | Diags.setDiagnosticMapping(diag::pp_macro_not_used, diag::MAP_IGNORE); |
| 112 | Diags.setDiagnosticMapping(diag::warn_floatingpoint_eq, diag::MAP_IGNORE); |
| 113 | Diags.setDiagnosticMapping(diag::warn_objc_property_attr_mutually_exclusive, |
| 114 | diag::MAP_IGNORE); |
| 115 | Diags.setDiagnosticMapping(diag::warn_pp_undef_identifier, diag::MAP_IGNORE); |
| 116 | Diags.setDiagnosticMapping(diag::warn_implicit_function_decl, |
| 117 | diag::MAP_IGNORE); |
| 118 | |
| 119 | Diags.setDiagnosticMapping(diag::err_pp_file_not_found, diag::MAP_FATAL); |
Douglas Gregor | 525c4b0 | 2009-03-19 18:55:06 +0000 | [diff] [blame] | 120 | Diags.setDiagnosticMapping(diag::err_template_recursion_depth_exceeded, |
| 121 | diag::MAP_FATAL); |
Douglas Gregor | 8499f3f | 2009-03-31 16:35:03 +0000 | [diff] [blame] | 122 | Diags.setDiagnosticMapping(diag::warn_missing_prototype, diag::MAP_IGNORE); |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 6280dbc | 2009-04-15 04:51:48 +0000 | [diff] [blame] | 124 | // FIXME: -fdiagnostics-show-option |
| 125 | // FIXME: -Wfatal-errors / -Wfatal-errors=foo |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 9dbbdbf | 2009-04-15 14:42:02 +0000 | [diff] [blame^] | 127 | /// ControlledDiags - Keep track of the options that the user explicitly |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 128 | /// poked with -Wfoo, -Wno-foo, or -Werror=foo. |
| 129 | llvm::SmallVector<unsigned short, 256> ControlledDiags; |
| 130 | |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 131 | for (unsigned i = 0, e = OptWarnings.size(); i != e; ++i) { |
| 132 | const std::string &Opt = OptWarnings[i]; |
| 133 | const char *OptStart = &Opt[0]; |
| 134 | const char *OptEnd = OptStart+Opt.size(); |
| 135 | assert(*OptEnd == 0 && "Expect null termination for lower-bound search"); |
| 136 | |
| 137 | // Check to see if this warning starts with "no-", if so, this is a negative |
| 138 | // form of the option. |
| 139 | bool isPositive = true; |
| 140 | if (OptEnd-OptStart > 3 && memcmp(OptStart, "no-", 3) == 0) { |
| 141 | isPositive = false; |
| 142 | OptStart += 3; |
| 143 | } |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 144 | |
Chris Lattner | 1d13a5d | 2009-04-15 04:37:12 +0000 | [diff] [blame] | 145 | // Figure out how this option affects the warning. If -Wfoo, map the |
| 146 | // diagnostic to a warning, if -Wno-foo, map it to ignore. |
| 147 | diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE; |
| 148 | |
| 149 | // -Wsystem-headers is a special case, not driven by the option table. It |
| 150 | // cannot be controlled with -Werror. |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 151 | if (OptEnd-OptStart == 14 && memcmp(OptStart, "system-headers", 14) == 0) { |
| 152 | Diags.setSuppressSystemWarnings(!isPositive); |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 153 | continue; |
| 154 | } |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 155 | |
| 156 | // -Werror/-Wno-error is a special case, not controlled by the option table. |
| 157 | // It also has the "specifier" form of -Werror=foo. |
| 158 | if (OptEnd-OptStart >= 5 && memcmp(OptStart, "error", 5) == 0) { |
| 159 | const char *Specifier = 0; |
| 160 | if (OptEnd-OptStart != 5) { // Specifier must be present. |
| 161 | if (OptStart[5] != '=' || OptEnd-OptStart == 6) { |
Chris Lattner | 6280dbc | 2009-04-15 04:51:48 +0000 | [diff] [blame] | 162 | fprintf(stderr, "error: unknown warning option: -W%s\n", Opt.c_str()); |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 163 | return true; |
| 164 | } |
| 165 | Specifier = OptStart+6; |
| 166 | } |
| 167 | |
| 168 | if (Specifier == 0) { |
| 169 | Diags.setWarningsAsErrors(true); |
| 170 | continue; |
| 171 | } |
| 172 | |
Chris Lattner | 1d13a5d | 2009-04-15 04:37:12 +0000 | [diff] [blame] | 173 | // -Werror=foo maps foo to Error, -Wno-error=foo maps it to Warning. |
| 174 | Mapping = isPositive ? diag::MAP_ERROR : diag::MAP_WARNING; |
| 175 | OptStart = Specifier; |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | WarningOption Key = { OptStart, 0, 0 }; |
Chris Lattner | d613c3d | 2009-04-08 22:37:15 +0000 | [diff] [blame] | 179 | const WarningOption *Found = |
| 180 | std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key, |
| 181 | WarningOptionCompare); |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 182 | if (Found == OptionTable + OptionTableSize || |
Chris Lattner | 1d13a5d | 2009-04-15 04:37:12 +0000 | [diff] [blame] | 183 | strcmp(Found->Name, OptStart) != 0) { |
Chris Lattner | 6280dbc | 2009-04-15 04:51:48 +0000 | [diff] [blame] | 184 | fprintf(stderr, "error: unknown warning option: -W%s\n", Opt.c_str()); |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 185 | return true; |
Sebastian Redl | c5613db | 2009-03-07 12:09:25 +0000 | [diff] [blame] | 186 | } |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 187 | |
Chris Lattner | 6280dbc | 2009-04-15 04:51:48 +0000 | [diff] [blame] | 188 | // Option exists, poke all the members of its diagnostic set. |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 189 | for (const diag::kind *Member = Found->Members, |
| 190 | *E = Found->Members+Found->NumMembers; Member != E; ++Member) { |
| 191 | Diags.setDiagnosticMapping(*Member, Mapping); |
Chris Lattner | 9dbbdbf | 2009-04-15 14:42:02 +0000 | [diff] [blame^] | 192 | assert(*Member < 65536 && "ControlledDiags element too small"); |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 193 | ControlledDiags.push_back(*Member); |
| 194 | } |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 195 | } |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 196 | |
| 197 | // If -pedantic or -pedantic-errors was specified, then we want to map all |
| 198 | // extension diagnostics onto WARNING or ERROR unless the user has futz'd |
| 199 | // around with them explicitly. |
| 200 | if (OptPedantic || OptPedanticErrors) { |
| 201 | // Sort the array of options that has been poked at directly so we can do |
| 202 | // efficient queries. |
| 203 | std::sort(ControlledDiags.begin(), ControlledDiags.end()); |
| 204 | |
| 205 | // Don't worry about iteration off the end down below. |
| 206 | ControlledDiags.push_back(diag::DIAG_UPPER_LIMIT); |
| 207 | |
| 208 | diag::Mapping Mapping = |
| 209 | OptPedanticErrors ? diag::MAP_ERROR : diag::MAP_WARNING; |
| 210 | |
| 211 | // Loop over all of the extension diagnostics. Unless they were explicitly |
| 212 | // controlled, reset their mapping to Mapping. We walk through the |
Chris Lattner | 9dbbdbf | 2009-04-15 14:42:02 +0000 | [diff] [blame^] | 213 | // ControlledDiags in parallel with this walk, which is faster than |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 214 | // repeatedly binary searching it. |
| 215 | // |
| 216 | llvm::SmallVectorImpl<unsigned short>::iterator ControlledDiagsIt = |
| 217 | ControlledDiags.begin(); |
| 218 | |
| 219 | // TODO: if it matters, we could make tblgen produce a list of just the |
| 220 | // extension diags to avoid skipping ones that don't matter. |
| 221 | for (unsigned short i = 0; i != diag::DIAG_UPPER_LIMIT; ++i) { |
| 222 | // If this diagnostic was controlled, ignore it. |
| 223 | if (i == *ControlledDiagsIt) { |
| 224 | ++ControlledDiagsIt; |
| 225 | while (i == *ControlledDiagsIt) // ControlledDiags can have dupes. |
| 226 | ++ControlledDiagsIt; |
| 227 | // Do not map this diagnostic ID#. |
| 228 | continue; |
| 229 | } |
| 230 | |
| 231 | // Okay, the user didn't control this ID. If it is an example, map it. |
| 232 | if (Diagnostic::isBuiltinExtensionDiag(i)) |
| 233 | Diags.setDiagnosticMapping(i, Mapping); |
| 234 | } |
| 235 | } |
| 236 | |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 237 | return false; |
| 238 | } |