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 | |
Chris Lattner | d7492c4 | 2009-04-15 20:58:49 +0000 | [diff] [blame^] | 46 | struct WarningOption { |
| 47 | const char *Name; |
| 48 | const short *Members; |
Chris Lattner | d613c3d | 2009-04-08 22:37:15 +0000 | [diff] [blame] | 49 | }; |
Steve Naroff | 17f689f | 2009-03-31 15:00:11 +0000 | [diff] [blame] | 50 | |
Chris Lattner | d7492c4 | 2009-04-15 20:58:49 +0000 | [diff] [blame^] | 51 | #define GET_DIAG_ARRAYS |
| 52 | #include "clang/Basic/DiagnosticGroups.inc" |
| 53 | #undef GET_DIAG_ARRAYS |
| 54 | |
| 55 | // Second the table of options, sorted by name for fast binary lookup. |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 56 | static const WarningOption OptionTable[] = { |
Chris Lattner | d7492c4 | 2009-04-15 20:58:49 +0000 | [diff] [blame^] | 57 | #define GET_DIAG_TABLE |
| 58 | #include "clang/Basic/DiagnosticGroups.inc" |
| 59 | #undef GET_DIAG_TABLE |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 60 | }; |
| 61 | static const size_t OptionTableSize = |
| 62 | sizeof(OptionTable) / sizeof(OptionTable[0]); |
| 63 | |
Chris Lattner | d613c3d | 2009-04-08 22:37:15 +0000 | [diff] [blame] | 64 | static bool WarningOptionCompare(const WarningOption &LHS, |
| 65 | const WarningOption &RHS) { |
| 66 | return strcmp(LHS.Name, RHS.Name) < 0; |
| 67 | } |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 68 | |
Chris Lattner | d613c3d | 2009-04-08 22:37:15 +0000 | [diff] [blame] | 69 | bool clang::ProcessWarningOptions(Diagnostic &Diags) { |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 70 | Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 71 | Diags.setIgnoreAllWarnings(OptNoWarnings); |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 6280dbc | 2009-04-15 04:51:48 +0000 | [diff] [blame] | 73 | // FIXME: -fdiagnostics-show-option |
| 74 | // FIXME: -Wfatal-errors / -Wfatal-errors=foo |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 75 | |
Chris Lattner | 9dbbdbf | 2009-04-15 14:42:02 +0000 | [diff] [blame] | 76 | /// ControlledDiags - Keep track of the options that the user explicitly |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 77 | /// poked with -Wfoo, -Wno-foo, or -Werror=foo. |
| 78 | llvm::SmallVector<unsigned short, 256> ControlledDiags; |
| 79 | |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 80 | for (unsigned i = 0, e = OptWarnings.size(); i != e; ++i) { |
| 81 | const std::string &Opt = OptWarnings[i]; |
| 82 | const char *OptStart = &Opt[0]; |
| 83 | const char *OptEnd = OptStart+Opt.size(); |
| 84 | assert(*OptEnd == 0 && "Expect null termination for lower-bound search"); |
| 85 | |
| 86 | // Check to see if this warning starts with "no-", if so, this is a negative |
| 87 | // form of the option. |
| 88 | bool isPositive = true; |
| 89 | if (OptEnd-OptStart > 3 && memcmp(OptStart, "no-", 3) == 0) { |
| 90 | isPositive = false; |
| 91 | OptStart += 3; |
| 92 | } |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 1d13a5d | 2009-04-15 04:37:12 +0000 | [diff] [blame] | 94 | // Figure out how this option affects the warning. If -Wfoo, map the |
| 95 | // diagnostic to a warning, if -Wno-foo, map it to ignore. |
| 96 | diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE; |
| 97 | |
| 98 | // -Wsystem-headers is a special case, not driven by the option table. It |
| 99 | // cannot be controlled with -Werror. |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 100 | if (OptEnd-OptStart == 14 && memcmp(OptStart, "system-headers", 14) == 0) { |
| 101 | Diags.setSuppressSystemWarnings(!isPositive); |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 102 | continue; |
| 103 | } |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 104 | |
| 105 | // -Werror/-Wno-error is a special case, not controlled by the option table. |
| 106 | // It also has the "specifier" form of -Werror=foo. |
| 107 | if (OptEnd-OptStart >= 5 && memcmp(OptStart, "error", 5) == 0) { |
| 108 | const char *Specifier = 0; |
| 109 | if (OptEnd-OptStart != 5) { // Specifier must be present. |
| 110 | if (OptStart[5] != '=' || OptEnd-OptStart == 6) { |
Chris Lattner | 6280dbc | 2009-04-15 04:51:48 +0000 | [diff] [blame] | 111 | fprintf(stderr, "error: unknown warning option: -W%s\n", Opt.c_str()); |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 112 | return true; |
| 113 | } |
| 114 | Specifier = OptStart+6; |
| 115 | } |
| 116 | |
| 117 | if (Specifier == 0) { |
| 118 | Diags.setWarningsAsErrors(true); |
| 119 | continue; |
| 120 | } |
| 121 | |
Chris Lattner | 1d13a5d | 2009-04-15 04:37:12 +0000 | [diff] [blame] | 122 | // -Werror=foo maps foo to Error, -Wno-error=foo maps it to Warning. |
| 123 | Mapping = isPositive ? diag::MAP_ERROR : diag::MAP_WARNING; |
| 124 | OptStart = Specifier; |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Chris Lattner | d7492c4 | 2009-04-15 20:58:49 +0000 | [diff] [blame^] | 127 | WarningOption Key = { OptStart, 0 }; |
Chris Lattner | d613c3d | 2009-04-08 22:37:15 +0000 | [diff] [blame] | 128 | const WarningOption *Found = |
| 129 | std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key, |
| 130 | WarningOptionCompare); |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 131 | if (Found == OptionTable + OptionTableSize || |
Chris Lattner | 1d13a5d | 2009-04-15 04:37:12 +0000 | [diff] [blame] | 132 | strcmp(Found->Name, OptStart) != 0) { |
Chris Lattner | 6280dbc | 2009-04-15 04:51:48 +0000 | [diff] [blame] | 133 | fprintf(stderr, "error: unknown warning option: -W%s\n", Opt.c_str()); |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 134 | return true; |
Sebastian Redl | c5613db | 2009-03-07 12:09:25 +0000 | [diff] [blame] | 135 | } |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 136 | |
Chris Lattner | 6280dbc | 2009-04-15 04:51:48 +0000 | [diff] [blame] | 137 | // Option exists, poke all the members of its diagnostic set. |
Chris Lattner | d7492c4 | 2009-04-15 20:58:49 +0000 | [diff] [blame^] | 138 | for (const short *Member = Found->Members; *Member != -1; ++Member) { |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 139 | Diags.setDiagnosticMapping(*Member, Mapping); |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 140 | ControlledDiags.push_back(*Member); |
| 141 | } |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 142 | } |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 143 | |
| 144 | // If -pedantic or -pedantic-errors was specified, then we want to map all |
| 145 | // extension diagnostics onto WARNING or ERROR unless the user has futz'd |
| 146 | // around with them explicitly. |
| 147 | if (OptPedantic || OptPedanticErrors) { |
| 148 | // Sort the array of options that has been poked at directly so we can do |
| 149 | // efficient queries. |
| 150 | std::sort(ControlledDiags.begin(), ControlledDiags.end()); |
| 151 | |
| 152 | // Don't worry about iteration off the end down below. |
| 153 | ControlledDiags.push_back(diag::DIAG_UPPER_LIMIT); |
| 154 | |
| 155 | diag::Mapping Mapping = |
| 156 | OptPedanticErrors ? diag::MAP_ERROR : diag::MAP_WARNING; |
| 157 | |
| 158 | // Loop over all of the extension diagnostics. Unless they were explicitly |
| 159 | // controlled, reset their mapping to Mapping. We walk through the |
Chris Lattner | 9dbbdbf | 2009-04-15 14:42:02 +0000 | [diff] [blame] | 160 | // ControlledDiags in parallel with this walk, which is faster than |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 161 | // repeatedly binary searching it. |
| 162 | // |
| 163 | llvm::SmallVectorImpl<unsigned short>::iterator ControlledDiagsIt = |
| 164 | ControlledDiags.begin(); |
| 165 | |
| 166 | // TODO: if it matters, we could make tblgen produce a list of just the |
| 167 | // extension diags to avoid skipping ones that don't matter. |
| 168 | for (unsigned short i = 0; i != diag::DIAG_UPPER_LIMIT; ++i) { |
| 169 | // If this diagnostic was controlled, ignore it. |
| 170 | if (i == *ControlledDiagsIt) { |
| 171 | ++ControlledDiagsIt; |
| 172 | while (i == *ControlledDiagsIt) // ControlledDiags can have dupes. |
| 173 | ++ControlledDiagsIt; |
| 174 | // Do not map this diagnostic ID#. |
| 175 | continue; |
| 176 | } |
| 177 | |
| 178 | // Okay, the user didn't control this ID. If it is an example, map it. |
| 179 | if (Diagnostic::isBuiltinExtensionDiag(i)) |
| 180 | Diags.setDiagnosticMapping(i, Mapping); |
| 181 | } |
| 182 | } |
| 183 | |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 184 | return false; |
| 185 | } |