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- |
Chris Lattner | a3b089e | 2009-12-23 18:53:37 +0000 | [diff] [blame] | 16 | // generated data, and the special cases -pedantic, -pedantic-errors, -w, |
| 17 | // -Werror and -Wfatal-errors. |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 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 | a3b089e | 2009-12-23 18:53:37 +0000 | [diff] [blame] | 21 | // -Wfoo, -Wno-foo, -Werror=foo, -Wfatal-errors=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" |
Daniel Dunbar | 6907943 | 2009-11-12 07:28:44 +0000 | [diff] [blame] | 27 | #include "clang/Frontend/DiagnosticOptions.h" |
Chris Lattner | 8ea841b | 2009-06-23 01:20:39 +0000 | [diff] [blame] | 28 | #include "clang/Frontend/FrontendDiagnostic.h" |
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 | |
Kovarththanan Rajaratnam | 5bf932b | 2010-03-17 09:36:02 +0000 | [diff] [blame] | 34 | void clang::ProcessWarningOptions(Diagnostic &Diags, |
Daniel Dunbar | 6907943 | 2009-11-12 07:28:44 +0000 | [diff] [blame] | 35 | const DiagnosticOptions &Opts) { |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 36 | Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers |
Daniel Dunbar | 6907943 | 2009-11-12 07:28:44 +0000 | [diff] [blame] | 37 | Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame^] | 38 | Diags.setShowOverloads( |
| 39 | static_cast<Diagnostic::OverloadsShown>(Opts.ShowOverloads)); |
Chris Lattner | c100214 | 2010-04-07 20:37:06 +0000 | [diff] [blame] | 40 | |
| 41 | // Handle -ferror-limit |
| 42 | if (Opts.ErrorLimit) |
| 43 | Diags.setErrorLimit(Opts.ErrorLimit); |
Douglas Gregor | 575cf37 | 2010-04-20 07:18:24 +0000 | [diff] [blame] | 44 | if (Opts.TemplateBacktraceLimit) |
| 45 | Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit); |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 46 | |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 47 | // If -pedantic or -pedantic-errors was specified, then we want to map all |
| 48 | // extension diagnostics onto WARNING or ERROR unless the user has futz'd |
| 49 | // around with them explicitly. |
Daniel Dunbar | 6907943 | 2009-11-12 07:28:44 +0000 | [diff] [blame] | 50 | if (Opts.PedanticErrors) |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 51 | Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Error); |
Daniel Dunbar | 6907943 | 2009-11-12 07:28:44 +0000 | [diff] [blame] | 52 | else if (Opts.Pedantic) |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 53 | Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Warn); |
| 54 | else |
| 55 | Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Ignore); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 56 | |
Daniel Dunbar | 6907943 | 2009-11-12 07:28:44 +0000 | [diff] [blame] | 57 | for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) { |
| 58 | const std::string &Opt = Opts.Warnings[i]; |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 59 | const char *OptStart = &Opt[0]; |
| 60 | const char *OptEnd = OptStart+Opt.size(); |
| 61 | assert(*OptEnd == 0 && "Expect null termination for lower-bound search"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 63 | // Check to see if this warning starts with "no-", if so, this is a negative |
| 64 | // form of the option. |
| 65 | bool isPositive = true; |
| 66 | if (OptEnd-OptStart > 3 && memcmp(OptStart, "no-", 3) == 0) { |
| 67 | isPositive = false; |
| 68 | OptStart += 3; |
| 69 | } |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 1d13a5d | 2009-04-15 04:37:12 +0000 | [diff] [blame] | 71 | // Figure out how this option affects the warning. If -Wfoo, map the |
| 72 | // diagnostic to a warning, if -Wno-foo, map it to ignore. |
| 73 | diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE; |
| 74 | |
| 75 | // -Wsystem-headers is a special case, not driven by the option table. It |
| 76 | // cannot be controlled with -Werror. |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 77 | if (OptEnd-OptStart == 14 && memcmp(OptStart, "system-headers", 14) == 0) { |
| 78 | Diags.setSuppressSystemWarnings(!isPositive); |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 79 | continue; |
| 80 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 82 | // -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] | 83 | // It also has the "specifier" form of -Werror=foo and -Werror-foo. |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 84 | if (OptEnd-OptStart >= 5 && memcmp(OptStart, "error", 5) == 0) { |
| 85 | const char *Specifier = 0; |
| 86 | if (OptEnd-OptStart != 5) { // Specifier must be present. |
Chris Lattner | 5b912d9 | 2009-04-19 22:07:21 +0000 | [diff] [blame] | 87 | if ((OptStart[5] != '=' && OptStart[5] != '-') || |
| 88 | OptEnd-OptStart == 6) { |
Chris Lattner | a3b089e | 2009-12-23 18:53:37 +0000 | [diff] [blame] | 89 | Diags.Report(diag::warn_unknown_warning_specifier) |
| 90 | << "-Werror" << ("-W" + Opt); |
Chris Lattner | 9a7e556 | 2009-04-15 22:48:58 +0000 | [diff] [blame] | 91 | continue; |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 92 | } |
| 93 | Specifier = OptStart+6; |
| 94 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 96 | if (Specifier == 0) { |
Shantonu Sen | b1d76bd | 2009-08-14 04:07:15 +0000 | [diff] [blame] | 97 | Diags.setWarningsAsErrors(isPositive); |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 98 | continue; |
| 99 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 1d13a5d | 2009-04-15 04:37:12 +0000 | [diff] [blame] | 101 | // -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] | 102 | Mapping = isPositive ? diag::MAP_ERROR : diag::MAP_WARNING_NO_WERROR; |
Chris Lattner | 1d13a5d | 2009-04-15 04:37:12 +0000 | [diff] [blame] | 103 | OptStart = Specifier; |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 104 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
Chris Lattner | e663c72 | 2009-12-22 23:12:53 +0000 | [diff] [blame] | 106 | // -Wfatal-errors is yet another special case. |
| 107 | if (OptEnd-OptStart >= 12 && memcmp(OptStart, "fatal-errors", 12) == 0) { |
| 108 | const char* Specifier = 0; |
| 109 | if (OptEnd-OptStart != 12) { |
| 110 | if ((OptStart[12] != '=' && OptStart[12] != '-') || |
| 111 | OptEnd-OptStart == 13) { |
Chris Lattner | a3b089e | 2009-12-23 18:53:37 +0000 | [diff] [blame] | 112 | Diags.Report(diag::warn_unknown_warning_specifier) |
| 113 | << "-Wfatal-errors" << ("-W" + Opt); |
Chris Lattner | e663c72 | 2009-12-22 23:12:53 +0000 | [diff] [blame] | 114 | continue; |
| 115 | } |
| 116 | Specifier = OptStart + 13; |
| 117 | } |
| 118 | |
| 119 | if (Specifier == 0) { |
| 120 | Diags.setErrorsAsFatal(isPositive); |
| 121 | continue; |
| 122 | } |
| 123 | |
| 124 | // -Wfatal-errors=foo maps foo to Fatal, -Wno-fatal-errors=foo |
| 125 | // maps it to Error. |
| 126 | Mapping = isPositive ? diag::MAP_FATAL : diag::MAP_ERROR_NO_WFATAL; |
| 127 | OptStart = Specifier; |
| 128 | } |
| 129 | |
Chris Lattner | 3bc172b | 2009-04-19 22:34:23 +0000 | [diff] [blame] | 130 | if (Diags.setDiagnosticGroupMapping(OptStart, Mapping)) |
Daniel Dunbar | 0f9fed7 | 2009-11-10 23:55:23 +0000 | [diff] [blame] | 131 | Diags.Report(diag::warn_unknown_warning_option) << ("-W" + Opt); |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 132 | } |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 133 | } |