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 | |
Chad Rosier | 588e83b | 2012-01-31 19:31:12 +0000 | [diff] [blame] | 34 | // EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning |
| 35 | // opts |
Chad Rosier | 99643d9 | 2011-11-15 19:03:03 +0000 | [diff] [blame] | 36 | static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags, |
Benjamin Kramer | dce6327 | 2011-11-15 12:26:39 +0000 | [diff] [blame] | 37 | StringRef Prefix, StringRef Opt, |
| 38 | bool isPositive) { |
| 39 | StringRef Suggestion = DiagnosticIDs::getNearestWarningOption(Opt); |
| 40 | if (!Suggestion.empty()) |
| 41 | Diags.Report(isPositive? diag::warn_unknown_warning_option_suggest : |
| 42 | diag::warn_unknown_negative_warning_option_suggest) |
| 43 | << (Prefix.str() += Opt) << (Prefix.str() += Suggestion); |
| 44 | else |
| 45 | Diags.Report(isPositive? diag::warn_unknown_warning_option : |
| 46 | diag::warn_unknown_negative_warning_option) |
| 47 | << (Prefix.str() += Opt); |
| 48 | } |
| 49 | |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 50 | void clang::ProcessWarningOptions(DiagnosticsEngine &Diags, |
Daniel Dunbar | 6907943 | 2009-11-12 07:28:44 +0000 | [diff] [blame] | 51 | const DiagnosticOptions &Opts) { |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 52 | Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers |
Daniel Dunbar | 6907943 | 2009-11-12 07:28:44 +0000 | [diff] [blame] | 53 | Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings); |
Jeffrey Yasskin | 5edbdcc | 2010-06-11 05:57:47 +0000 | [diff] [blame] | 54 | Diags.setShowOverloads( |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 55 | static_cast<DiagnosticsEngine::OverloadsShown>(Opts.ShowOverloads)); |
Chris Lattner | c100214 | 2010-04-07 20:37:06 +0000 | [diff] [blame] | 56 | |
| 57 | // Handle -ferror-limit |
| 58 | if (Opts.ErrorLimit) |
| 59 | Diags.setErrorLimit(Opts.ErrorLimit); |
Douglas Gregor | 575cf37 | 2010-04-20 07:18:24 +0000 | [diff] [blame] | 60 | if (Opts.TemplateBacktraceLimit) |
| 61 | Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit); |
Richard Smith | 08d6e03 | 2011-12-16 19:06:07 +0000 | [diff] [blame] | 62 | if (Opts.ConstexprBacktraceLimit) |
| 63 | Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit); |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 64 | |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 65 | // If -pedantic or -pedantic-errors was specified, then we want to map all |
| 66 | // extension diagnostics onto WARNING or ERROR unless the user has futz'd |
| 67 | // around with them explicitly. |
Daniel Dunbar | 6907943 | 2009-11-12 07:28:44 +0000 | [diff] [blame] | 68 | if (Opts.PedanticErrors) |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 69 | Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Error); |
Daniel Dunbar | 6907943 | 2009-11-12 07:28:44 +0000 | [diff] [blame] | 70 | else if (Opts.Pedantic) |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 71 | Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Warn); |
Chris Lattner | b54b276 | 2009-04-16 05:04:32 +0000 | [diff] [blame] | 72 | else |
David Blaikie | d6471f7 | 2011-09-25 23:23:43 +0000 | [diff] [blame] | 73 | Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Ignore); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 75 | llvm::SmallVector<diag::kind, 10> _Diags; |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 76 | const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs = |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 77 | Diags.getDiagnosticIDs(); |
| 78 | // We parse the warning options twice. The first pass sets diagnostic state, |
| 79 | // while the second pass reports warnings/errors. This has the effect that |
| 80 | // we follow the more canonical "last option wins" paradigm when there are |
| 81 | // conflicting options. |
| 82 | for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; ++Report) { |
| 83 | bool SetDiagnostic = (Report == 0); |
| 84 | for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) { |
| 85 | StringRef Opt = Opts.Warnings[i]; |
Chad Rosier | ff1affe | 2012-05-16 19:28:02 +0000 | [diff] [blame^] | 86 | StringRef OrigOpt = Opts.Warnings[i]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 87 | |
Hans Wennborg | c876946 | 2012-01-17 09:30:38 +0000 | [diff] [blame] | 88 | // Treat -Wformat=0 as an alias for -Wno-format. |
| 89 | if (Opt == "format=0") |
| 90 | Opt = "no-format"; |
| 91 | |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 92 | // Check to see if this warning starts with "no-", if so, this is a |
| 93 | // negative form of the option. |
| 94 | bool isPositive = true; |
| 95 | if (Opt.startswith("no-")) { |
| 96 | isPositive = false; |
| 97 | Opt = Opt.substr(3); |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 98 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 100 | // Figure out how this option affects the warning. If -Wfoo, map the |
| 101 | // diagnostic to a warning, if -Wno-foo, map it to ignore. |
| 102 | diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE; |
| 103 | |
| 104 | // -Wsystem-headers is a special case, not driven by the option table. It |
| 105 | // cannot be controlled with -Werror. |
| 106 | if (Opt == "system-headers") { |
| 107 | if (SetDiagnostic) |
| 108 | Diags.setSuppressSystemWarnings(!isPositive); |
Chris Lattner | 5147e8e | 2009-04-15 04:27:38 +0000 | [diff] [blame] | 109 | continue; |
| 110 | } |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 111 | |
| 112 | // -Weverything is a special case as well. It implicitly enables all |
| 113 | // warnings, including ones not explicitly in a warning group. |
| 114 | if (Opt == "everything") { |
Argyrios Kyrtzidis | 11583c7 | 2012-01-27 06:15:43 +0000 | [diff] [blame] | 115 | if (SetDiagnostic) { |
| 116 | if (isPositive) { |
| 117 | Diags.setEnableAllWarnings(true); |
| 118 | } else { |
| 119 | Diags.setEnableAllWarnings(false); |
| 120 | Diags.setMappingToAllDiagnostics(diag::MAP_IGNORE); |
| 121 | } |
| 122 | } |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 123 | continue; |
| 124 | } |
| 125 | |
| 126 | // -Werror/-Wno-error is a special case, not controlled by the option |
| 127 | // table. It also has the "specifier" form of -Werror=foo and -Werror-foo. |
| 128 | if (Opt.startswith("error")) { |
| 129 | StringRef Specifier; |
| 130 | if (Opt.size() > 5) { // Specifier must be present. |
| 131 | if ((Opt[5] != '=' && Opt[5] != '-') || Opt.size() == 6) { |
| 132 | if (Report) |
| 133 | Diags.Report(diag::warn_unknown_warning_specifier) |
Chad Rosier | ff1affe | 2012-05-16 19:28:02 +0000 | [diff] [blame^] | 134 | << "-Werror" << ("-W" + OrigOpt.str()); |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 135 | continue; |
| 136 | } |
| 137 | Specifier = Opt.substr(6); |
| 138 | } |
| 139 | |
| 140 | if (Specifier.empty()) { |
| 141 | if (SetDiagnostic) |
| 142 | Diags.setWarningsAsErrors(isPositive); |
| 143 | continue; |
| 144 | } |
| 145 | |
| 146 | if (SetDiagnostic) { |
| 147 | // Set the warning as error flag for this specifier. |
| 148 | Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive); |
| 149 | } else if (DiagIDs->getDiagnosticsInGroup(Specifier, _Diags)) { |
Benjamin Kramer | 4e96944 | 2011-11-28 22:17:09 +0000 | [diff] [blame] | 150 | EmitUnknownDiagWarning(Diags, "-Werror=", Specifier, isPositive); |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 151 | } |
| 152 | continue; |
| 153 | } |
| 154 | |
| 155 | // -Wfatal-errors is yet another special case. |
| 156 | if (Opt.startswith("fatal-errors")) { |
| 157 | StringRef Specifier; |
| 158 | if (Opt.size() != 12) { |
| 159 | if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) { |
| 160 | if (Report) |
| 161 | Diags.Report(diag::warn_unknown_warning_specifier) |
Chad Rosier | ff1affe | 2012-05-16 19:28:02 +0000 | [diff] [blame^] | 162 | << "-Wfatal-errors" << ("-W" + OrigOpt.str()); |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 163 | continue; |
| 164 | } |
| 165 | Specifier = Opt.substr(13); |
| 166 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 168 | if (Specifier.empty()) { |
| 169 | if (SetDiagnostic) |
| 170 | Diags.setErrorsAsFatal(isPositive); |
| 171 | continue; |
| 172 | } |
| 173 | |
| 174 | if (SetDiagnostic) { |
| 175 | // Set the error as fatal flag for this specifier. |
| 176 | Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive); |
| 177 | } else if (DiagIDs->getDiagnosticsInGroup(Specifier, _Diags)) { |
Benjamin Kramer | 4e96944 | 2011-11-28 22:17:09 +0000 | [diff] [blame] | 178 | EmitUnknownDiagWarning(Diags, "-Wfatal-errors=", Specifier, |
| 179 | isPositive); |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 180 | } |
| 181 | continue; |
| 182 | } |
| 183 | |
Chad Rosier | 496cc8e | 2011-11-15 18:57:32 +0000 | [diff] [blame] | 184 | if (Report) { |
| 185 | if (DiagIDs->getDiagnosticsInGroup(Opt, _Diags)) |
Chad Rosier | ff1affe | 2012-05-16 19:28:02 +0000 | [diff] [blame^] | 186 | EmitUnknownDiagWarning(Diags, "-W", OrigOpt, isPositive); |
Chad Rosier | 05272a6 | 2011-11-03 21:23:39 +0000 | [diff] [blame] | 187 | } else { |
| 188 | Diags.setDiagnosticGroupMapping(Opt, Mapping); |
Daniel Dunbar | 4aa8f2b | 2011-09-29 00:53:47 +0000 | [diff] [blame] | 189 | } |
Daniel Dunbar | 4aa8f2b | 2011-09-29 00:53:47 +0000 | [diff] [blame] | 190 | } |
Chris Lattner | 27ceb9d | 2009-04-15 07:01:18 +0000 | [diff] [blame] | 191 | } |
Sebastian Redl | 63a9e0f | 2009-03-06 17:41:35 +0000 | [diff] [blame] | 192 | } |