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, |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 68 | Diagnostic &Diags) { |
Chris Lattner | 8ac3af9 | 2009-04-16 00:53:55 +0000 | [diff] [blame] | 69 | // Option exists, poke all the members of its diagnostic set. |
| 70 | if (const short *Member = Group->Members) { |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 71 | for (; *Member != -1; ++Member) |
Chris Lattner | 8ac3af9 | 2009-04-16 00:53:55 +0000 | [diff] [blame] | 72 | Diags.setDiagnosticMapping(*Member, Mapping); |
Chris Lattner | 8ac3af9 | 2009-04-16 00:53:55 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // Enable/disable all subgroups along with this one. |
| 76 | if (const char *SubGroups = Group->SubGroups) { |
| 77 | for (; *SubGroups != (char)-1; ++SubGroups) |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 78 | MapGroupMembers(&OptionTable[(unsigned char)*SubGroups], Mapping, Diags); |
Chris Lattner | 8ac3af9 | 2009-04-16 00:53:55 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Chris Lattner | d613c3d | 2009-04-08 22:37:15 +0000 | [diff] [blame] | 82 | bool clang::ProcessWarningOptions(Diagnostic &Diags) { |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 83 | Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 84 | Diags.setIgnoreAllWarnings(OptNoWarnings); |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 85 | |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 86 | // If -pedantic or -pedantic-errors was specified, then we want to map all |
| 87 | // extension diagnostics onto WARNING or ERROR unless the user has futz'd |
| 88 | // around with them explicitly. |
| 89 | if (OptPedanticErrors) |
| 90 | Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Error); |
| 91 | else if (OptPedantic) |
| 92 | Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Warn); |
| 93 | else |
| 94 | Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Ignore); |
| 95 | |
Chris Lattner | 6280dbc | 2009-04-15 04:51:48 +0000 | [diff] [blame] | 96 | // FIXME: -Wfatal-errors / -Wfatal-errors=foo |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 98 | for (unsigned i = 0, e = OptWarnings.size(); i != e; ++i) { |
| 99 | const std::string &Opt = OptWarnings[i]; |
| 100 | const char *OptStart = &Opt[0]; |
| 101 | const char *OptEnd = OptStart+Opt.size(); |
| 102 | assert(*OptEnd == 0 && "Expect null termination for lower-bound search"); |
| 103 | |
| 104 | // Check to see if this warning starts with "no-", if so, this is a negative |
| 105 | // form of the option. |
| 106 | bool isPositive = true; |
| 107 | if (OptEnd-OptStart > 3 && memcmp(OptStart, "no-", 3) == 0) { |
| 108 | isPositive = false; |
| 109 | OptStart += 3; |
| 110 | } |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 1d13a5d | 2009-04-15 04:37:12 +0000 | [diff] [blame] | 112 | // Figure out how this option affects the warning. If -Wfoo, map the |
| 113 | // diagnostic to a warning, if -Wno-foo, map it to ignore. |
| 114 | diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE; |
| 115 | |
| 116 | // -Wsystem-headers is a special case, not driven by the option table. It |
| 117 | // cannot be controlled with -Werror. |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 118 | if (OptEnd-OptStart == 14 && memcmp(OptStart, "system-headers", 14) == 0) { |
| 119 | Diags.setSuppressSystemWarnings(!isPositive); |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 120 | continue; |
| 121 | } |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 122 | |
| 123 | // -Werror/-Wno-error is a special case, not controlled by the option table. |
| 124 | // It also has the "specifier" form of -Werror=foo. |
| 125 | if (OptEnd-OptStart >= 5 && memcmp(OptStart, "error", 5) == 0) { |
| 126 | const char *Specifier = 0; |
| 127 | if (OptEnd-OptStart != 5) { // Specifier must be present. |
| 128 | if (OptStart[5] != '=' || OptEnd-OptStart == 6) { |
Chris Lattner | 9a7e556 | 2009-04-15 22:48:58 +0000 | [diff] [blame] | 129 | fprintf(stderr, "warning: unknown -Werror warning specifier: -W%s\n", |
| 130 | Opt.c_str()); |
| 131 | continue; |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 132 | } |
| 133 | Specifier = OptStart+6; |
| 134 | } |
| 135 | |
| 136 | if (Specifier == 0) { |
| 137 | Diags.setWarningsAsErrors(true); |
| 138 | continue; |
| 139 | } |
| 140 | |
Chris Lattner | 1d13a5d | 2009-04-15 04:37:12 +0000 | [diff] [blame] | 141 | // -Werror=foo maps foo to Error, -Wno-error=foo maps it to Warning. |
Chris Lattner | 2b07d8f | 2009-04-16 04:32:54 +0000 | [diff] [blame] | 142 | Mapping = isPositive ? diag::MAP_ERROR : diag::MAP_WARNING_NO_WERROR; |
Chris Lattner | 1d13a5d | 2009-04-15 04:37:12 +0000 | [diff] [blame] | 143 | OptStart = Specifier; |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Chris Lattner | 8ac3af9 | 2009-04-16 00:53:55 +0000 | [diff] [blame] | 146 | WarningOption Key = { OptStart, 0, 0 }; |
Chris Lattner | d613c3d | 2009-04-08 22:37:15 +0000 | [diff] [blame] | 147 | const WarningOption *Found = |
| 148 | std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key, |
| 149 | WarningOptionCompare); |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 150 | if (Found == OptionTable + OptionTableSize || |
Chris Lattner | 1d13a5d | 2009-04-15 04:37:12 +0000 | [diff] [blame] | 151 | strcmp(Found->Name, OptStart) != 0) { |
Chris Lattner | 9a7e556 | 2009-04-15 22:48:58 +0000 | [diff] [blame] | 152 | fprintf(stderr, "warning: unknown warning option: -W%s\n", Opt.c_str()); |
| 153 | continue; |
Sebastian Redl | c5613db | 2009-03-07 12:09:25 +0000 | [diff] [blame] | 154 | } |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 155 | |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 156 | MapGroupMembers(Found, Mapping, Diags); |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 159 | return false; |
| 160 | } |