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 | // |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 23 | // Remark options are also handled here, analogously, except that they are much |
| 24 | // simpler because a remark can't be promoted to an error. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame] | 25 | #include "clang/Basic/AllDiagnostics.h" |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 26 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 27 | #include "clang/Basic/DiagnosticOptions.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 28 | #include <algorithm> |
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> |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 31 | using namespace clang; |
| 32 | |
Chad Rosier | 588e83b | 2012-01-31 19:31:12 +0000 | [diff] [blame] | 33 | // EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning |
| 34 | // opts |
Chad Rosier | 99643d9 | 2011-11-15 19:03:03 +0000 | [diff] [blame] | 35 | static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags, |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 36 | diag::Flavor Flavor, StringRef Prefix, |
| 37 | StringRef Opt) { |
| 38 | StringRef Suggestion = DiagnosticIDs::getNearestOption(Flavor, Opt); |
| 39 | Diags.Report(diag::warn_unknown_diag_option) |
| 40 | << (Flavor == diag::Flavor::WarningOrError ? 0 : 1) << (Prefix.str() += Opt) |
| 41 | << !Suggestion.empty() << (Prefix.str() += Suggestion); |
Benjamin Kramer | dce6327 | 2011-11-15 12:26:39 +0000 | [diff] [blame] | 42 | } |
| 43 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 44 | void clang::ProcessWarningOptions(DiagnosticsEngine &Diags, |
Chad Rosier | 08e79d2 | 2013-01-15 01:21:53 +0000 | [diff] [blame] | 45 | const DiagnosticOptions &Opts, |
| 46 | bool ReportDiags) { |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 47 | Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers |
Daniel Dunbar | 6907943 | 2009-11-12 07:28:44 +0000 | [diff] [blame] | 48 | Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings); |
Douglas Gregor | dc7b641 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 49 | Diags.setShowOverloads(Opts.getShowOverloads()); |
Richard Trieu | 246b6aa | 2012-06-26 18:18:47 +0000 | [diff] [blame] | 50 | |
| 51 | Diags.setElideType(Opts.ElideType); |
| 52 | Diags.setPrintTemplateTree(Opts.ShowTemplateTree); |
| 53 | Diags.setShowColors(Opts.ShowColors); |
| 54 | |
Chris Lattner | c100214 | 2010-04-07 20:37:06 +0000 | [diff] [blame] | 55 | // Handle -ferror-limit |
| 56 | if (Opts.ErrorLimit) |
| 57 | Diags.setErrorLimit(Opts.ErrorLimit); |
Douglas Gregor | 575cf37 | 2010-04-20 07:18:24 +0000 | [diff] [blame] | 58 | if (Opts.TemplateBacktraceLimit) |
| 59 | Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 60 | if (Opts.ConstexprBacktraceLimit) |
| 61 | Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit); |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 62 | |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 63 | // If -pedantic or -pedantic-errors was specified, then we want to map all |
| 64 | // extension diagnostics onto WARNING or ERROR unless the user has futz'd |
| 65 | // around with them explicitly. |
Daniel Dunbar | 6907943 | 2009-11-12 07:28:44 +0000 | [diff] [blame] | 66 | if (Opts.PedanticErrors) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 67 | Diags.setExtensionHandlingBehavior(diag::Severity::Error); |
Daniel Dunbar | 6907943 | 2009-11-12 07:28:44 +0000 | [diff] [blame] | 68 | else if (Opts.Pedantic) |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 69 | Diags.setExtensionHandlingBehavior(diag::Severity::Warning); |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 70 | else |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 71 | Diags.setExtensionHandlingBehavior(diag::Severity::Ignored); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 72 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 73 | SmallVector<diag::kind, 10> _Diags; |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 74 | const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs = |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 75 | Diags.getDiagnosticIDs(); |
| 76 | // We parse the warning options twice. The first pass sets diagnostic state, |
| 77 | // while the second pass reports warnings/errors. This has the effect that |
| 78 | // we follow the more canonical "last option wins" paradigm when there are |
| 79 | // conflicting options. |
| 80 | for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; ++Report) { |
| 81 | bool SetDiagnostic = (Report == 0); |
Chad Rosier | 08e79d2 | 2013-01-15 01:21:53 +0000 | [diff] [blame] | 82 | |
| 83 | // If we've set the diagnostic state and are not reporting diagnostics then |
| 84 | // we're done. |
| 85 | if (!SetDiagnostic && !ReportDiags) |
| 86 | break; |
| 87 | |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 88 | for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 89 | const auto Flavor = diag::Flavor::WarningOrError; |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 90 | StringRef Opt = Opts.Warnings[i]; |
Chad Rosier | ff1affe | 2012-05-16 19:28:02 +0000 | [diff] [blame] | 91 | StringRef OrigOpt = Opts.Warnings[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 92 | |
Hans Wennborg | c876946 | 2012-01-17 09:30:38 +0000 | [diff] [blame] | 93 | // Treat -Wformat=0 as an alias for -Wno-format. |
| 94 | if (Opt == "format=0") |
| 95 | Opt = "no-format"; |
| 96 | |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 97 | // Check to see if this warning starts with "no-", if so, this is a |
| 98 | // negative form of the option. |
| 99 | bool isPositive = true; |
| 100 | if (Opt.startswith("no-")) { |
| 101 | isPositive = false; |
| 102 | Opt = Opt.substr(3); |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 103 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 105 | // Figure out how this option affects the warning. If -Wfoo, map the |
| 106 | // diagnostic to a warning, if -Wno-foo, map it to ignore. |
Stephen Hines | c568f1e | 2014-07-21 00:47:37 -0700 | [diff] [blame] | 107 | diag::Severity Mapping = |
| 108 | isPositive ? diag::Severity::Warning : diag::Severity::Ignored; |
| 109 | |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 110 | // -Wsystem-headers is a special case, not driven by the option table. It |
| 111 | // cannot be controlled with -Werror. |
| 112 | if (Opt == "system-headers") { |
| 113 | if (SetDiagnostic) |
| 114 | Diags.setSuppressSystemWarnings(!isPositive); |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 115 | continue; |
| 116 | } |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 117 | |
| 118 | // -Weverything is a special case as well. It implicitly enables all |
| 119 | // warnings, including ones not explicitly in a warning group. |
| 120 | if (Opt == "everything") { |
Argyrios Kyrtzidis | 11583c7 | 2012-01-27 06:15:43 +0000 | [diff] [blame] | 121 | if (SetDiagnostic) { |
| 122 | if (isPositive) { |
| 123 | Diags.setEnableAllWarnings(true); |
| 124 | } else { |
| 125 | Diags.setEnableAllWarnings(false); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 126 | Diags.setSeverityForAll(Flavor, diag::Severity::Ignored); |
Argyrios Kyrtzidis | 11583c7 | 2012-01-27 06:15:43 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 129 | continue; |
| 130 | } |
| 131 | |
| 132 | // -Werror/-Wno-error is a special case, not controlled by the option |
| 133 | // table. It also has the "specifier" form of -Werror=foo and -Werror-foo. |
| 134 | if (Opt.startswith("error")) { |
| 135 | StringRef Specifier; |
| 136 | if (Opt.size() > 5) { // Specifier must be present. |
| 137 | if ((Opt[5] != '=' && Opt[5] != '-') || Opt.size() == 6) { |
| 138 | if (Report) |
| 139 | Diags.Report(diag::warn_unknown_warning_specifier) |
Chad Rosier | ff1affe | 2012-05-16 19:28:02 +0000 | [diff] [blame] | 140 | << "-Werror" << ("-W" + OrigOpt.str()); |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 141 | continue; |
| 142 | } |
| 143 | Specifier = Opt.substr(6); |
| 144 | } |
| 145 | |
| 146 | if (Specifier.empty()) { |
| 147 | if (SetDiagnostic) |
| 148 | Diags.setWarningsAsErrors(isPositive); |
| 149 | continue; |
| 150 | } |
| 151 | |
| 152 | if (SetDiagnostic) { |
| 153 | // Set the warning as error flag for this specifier. |
| 154 | Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 155 | } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) { |
| 156 | EmitUnknownDiagWarning(Diags, Flavor, "-Werror=", Specifier); |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 157 | } |
| 158 | continue; |
| 159 | } |
| 160 | |
| 161 | // -Wfatal-errors is yet another special case. |
| 162 | if (Opt.startswith("fatal-errors")) { |
| 163 | StringRef Specifier; |
| 164 | if (Opt.size() != 12) { |
| 165 | if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) { |
| 166 | if (Report) |
| 167 | Diags.Report(diag::warn_unknown_warning_specifier) |
Chad Rosier | ff1affe | 2012-05-16 19:28:02 +0000 | [diff] [blame] | 168 | << "-Wfatal-errors" << ("-W" + OrigOpt.str()); |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 169 | continue; |
| 170 | } |
| 171 | Specifier = Opt.substr(13); |
| 172 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 173 | |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 174 | if (Specifier.empty()) { |
| 175 | if (SetDiagnostic) |
| 176 | Diags.setErrorsAsFatal(isPositive); |
| 177 | continue; |
| 178 | } |
| 179 | |
| 180 | if (SetDiagnostic) { |
| 181 | // Set the error as fatal flag for this specifier. |
| 182 | Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive); |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 183 | } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) { |
| 184 | EmitUnknownDiagWarning(Diags, Flavor, "-Wfatal-errors=", Specifier); |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 185 | } |
| 186 | continue; |
| 187 | } |
| 188 | |
Chad Rosier | 496cc8e | 2011-11-15 18:57:32 +0000 | [diff] [blame] | 189 | if (Report) { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 190 | if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags)) |
| 191 | EmitUnknownDiagWarning(Diags, Flavor, isPositive ? "-W" : "-Wno-", |
| 192 | Opt); |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 193 | } else { |
Stephen Hines | 176edba | 2014-12-01 14:53:08 -0800 | [diff] [blame] | 194 | Diags.setSeverityForGroup(Flavor, Opt, Mapping); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | for (unsigned i = 0, e = Opts.Remarks.size(); i != e; ++i) { |
| 199 | StringRef Opt = Opts.Remarks[i]; |
| 200 | const auto Flavor = diag::Flavor::Remark; |
| 201 | |
| 202 | // Check to see if this warning starts with "no-", if so, this is a |
| 203 | // negative form of the option. |
| 204 | bool IsPositive = !Opt.startswith("no-"); |
| 205 | if (!IsPositive) Opt = Opt.substr(3); |
| 206 | |
| 207 | auto Severity = IsPositive ? diag::Severity::Remark |
| 208 | : diag::Severity::Ignored; |
| 209 | |
| 210 | // -Reverything sets the state of all remarks. Note that all remarks are |
| 211 | // in remark groups, so we don't need a separate 'all remarks enabled' |
| 212 | // flag. |
| 213 | if (Opt == "everything") { |
| 214 | if (SetDiagnostic) |
| 215 | Diags.setSeverityForAll(Flavor, Severity); |
| 216 | continue; |
| 217 | } |
| 218 | |
| 219 | if (Report) { |
| 220 | if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags)) |
| 221 | EmitUnknownDiagWarning(Diags, Flavor, IsPositive ? "-R" : "-Rno-", |
| 222 | Opt); |
| 223 | } else { |
| 224 | Diags.setSeverityForGroup(Flavor, Opt, |
| 225 | IsPositive ? diag::Severity::Remark |
| 226 | : diag::Severity::Ignored); |
Daniel Dunbar | 4aa8f2b | 2011-09-29 00:53:47 +0000 | [diff] [blame] | 227 | } |
Daniel Dunbar | 4aa8f2b | 2011-09-29 00:53:47 +0000 | [diff] [blame] | 228 | } |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 229 | } |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 230 | } |