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