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 | // |
Eli Friedman | b09f6e1 | 2009-05-19 04:14:29 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/Utils.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" |
Chris Lattner | 8ea841b | 2009-06-23 01:20:39 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/FrontendDiagnostic.h" |
Chris Lattner | 3c304bd | 2009-04-11 18:40:46 +0000 | [diff] [blame] | 28 | #include <cstdio> |
Eli Friedman | ac8d629 | 2009-05-19 04:30:57 +0000 | [diff] [blame] | 29 | #include <cstring> |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 30 | #include <utility> |
| 31 | #include <algorithm> |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 32 | using namespace clang; |
| 33 | |
Eli Friedman | 0eeb86e | 2009-05-19 01:17:04 +0000 | [diff] [blame] | 34 | bool clang::ProcessWarningOptions(Diagnostic &Diags, |
| 35 | std::vector<std::string> &Warnings, |
| 36 | bool Pedantic, bool PedanticErrors, |
| 37 | bool NoWarnings) { |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 38 | Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers |
Eli Friedman | 0eeb86e | 2009-05-19 01:17:04 +0000 | [diff] [blame] | 39 | Diags.setIgnoreAllWarnings(NoWarnings); |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 40 | |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 41 | // If -pedantic or -pedantic-errors was specified, then we want to map all |
| 42 | // extension diagnostics onto WARNING or ERROR unless the user has futz'd |
| 43 | // around with them explicitly. |
Eli Friedman | 0eeb86e | 2009-05-19 01:17:04 +0000 | [diff] [blame] | 44 | if (PedanticErrors) |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 45 | Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Error); |
Eli Friedman | 0eeb86e | 2009-05-19 01:17:04 +0000 | [diff] [blame] | 46 | else if (Pedantic) |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 47 | Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Warn); |
| 48 | else |
| 49 | Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Ignore); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 6280dbc | 2009-04-15 04:51:48 +0000 | [diff] [blame] | 51 | // FIXME: -Wfatal-errors / -Wfatal-errors=foo |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 52 | |
Eli Friedman | 0eeb86e | 2009-05-19 01:17:04 +0000 | [diff] [blame] | 53 | for (unsigned i = 0, e = Warnings.size(); i != e; ++i) { |
| 54 | const std::string &Opt = Warnings[i]; |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 55 | const char *OptStart = &Opt[0]; |
| 56 | const char *OptEnd = OptStart+Opt.size(); |
| 57 | assert(*OptEnd == 0 && "Expect null termination for lower-bound search"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 59 | // Check to see if this warning starts with "no-", if so, this is a negative |
| 60 | // form of the option. |
| 61 | bool isPositive = true; |
| 62 | if (OptEnd-OptStart > 3 && memcmp(OptStart, "no-", 3) == 0) { |
| 63 | isPositive = false; |
| 64 | OptStart += 3; |
| 65 | } |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 1d13a5d | 2009-04-15 04:37:12 +0000 | [diff] [blame] | 67 | // Figure out how this option affects the warning. If -Wfoo, map the |
| 68 | // diagnostic to a warning, if -Wno-foo, map it to ignore. |
| 69 | diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE; |
| 70 | |
| 71 | // -Wsystem-headers is a special case, not driven by the option table. It |
| 72 | // cannot be controlled with -Werror. |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 73 | if (OptEnd-OptStart == 14 && memcmp(OptStart, "system-headers", 14) == 0) { |
| 74 | Diags.setSuppressSystemWarnings(!isPositive); |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 75 | continue; |
| 76 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 78 | // -Werror/-Wno-error is a special case, not controlled by the option table. |
Chris Lattner | 5b912d9 | 2009-04-19 22:07:21 +0000 | [diff] [blame] | 79 | // It also has the "specifier" form of -Werror=foo and -Werror-foo. |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 80 | if (OptEnd-OptStart >= 5 && memcmp(OptStart, "error", 5) == 0) { |
| 81 | const char *Specifier = 0; |
| 82 | if (OptEnd-OptStart != 5) { // Specifier must be present. |
Chris Lattner | 5b912d9 | 2009-04-19 22:07:21 +0000 | [diff] [blame] | 83 | if ((OptStart[5] != '=' && OptStart[5] != '-') || |
| 84 | OptEnd-OptStart == 6) { |
Chris Lattner | 9a7e556 | 2009-04-15 22:48:58 +0000 | [diff] [blame] | 85 | fprintf(stderr, "warning: unknown -Werror warning specifier: -W%s\n", |
| 86 | Opt.c_str()); |
| 87 | continue; |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 88 | } |
| 89 | Specifier = OptStart+6; |
| 90 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 92 | if (Specifier == 0) { |
Shantonu Sen | b1d76bd | 2009-08-14 04:07:15 +0000 | [diff] [blame] | 93 | Diags.setWarningsAsErrors(isPositive); |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 94 | continue; |
| 95 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 1d13a5d | 2009-04-15 04:37:12 +0000 | [diff] [blame] | 97 | // -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] | 98 | Mapping = isPositive ? diag::MAP_ERROR : diag::MAP_WARNING_NO_WERROR; |
Chris Lattner | 1d13a5d | 2009-04-15 04:37:12 +0000 | [diff] [blame] | 99 | OptStart = Specifier; |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 100 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 3bc172b | 2009-04-19 22:34:23 +0000 | [diff] [blame] | 102 | if (Diags.setDiagnosticGroupMapping(OptStart, Mapping)) |
Chris Lattner | 8ea841b | 2009-06-23 01:20:39 +0000 | [diff] [blame] | 103 | Diags.Report(FullSourceLoc(), diag::warn_unknown_warning_option) |
| 104 | << ("-W" + Opt); |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 105 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 107 | return false; |
| 108 | } |