blob: 6306ceabc638a9720a3fc436f53415e0cf833357 [file] [log] [blame]
Sebastian Redl63a9e0f2009-03-06 17:41:35 +00001//===--- 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 Lattnera3b089e2009-12-23 18:53:37 +000016// generated data, and the special cases -pedantic, -pedantic-errors, -w,
17// -Werror and -Wfatal-errors.
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000018//
Sebastian Redlc5613db2009-03-07 12:09:25 +000019// Each warning option controls any number of actual warnings.
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000020// Given a warning option 'foo', the following are valid:
Chris Lattnera3b089e2009-12-23 18:53:37 +000021// -Wfoo, -Wno-foo, -Werror=foo, -Wfatal-errors=foo
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000022//
Stephen Hines176edba2014-12-01 14:53:08 -080023// 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 Hines6bcf27b2014-05-29 04:14:42 -070025#include "clang/Basic/AllDiagnostics.h"
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000026#include "clang/Basic/Diagnostic.h"
Douglas Gregor02c23eb2012-10-23 22:26:28 +000027#include "clang/Basic/DiagnosticOptions.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000028#include <algorithm>
Eli Friedmanac8d6292009-05-19 04:30:57 +000029#include <cstring>
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000030#include <utility>
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000031using namespace clang;
32
Chad Rosier588e83b2012-01-31 19:31:12 +000033// EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning
34// opts
Chad Rosier99643d92011-11-15 19:03:03 +000035static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags,
Stephen Hines176edba2014-12-01 14:53:08 -080036 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 Kramerdce63272011-11-15 12:26:39 +000042}
43
David Blaikied6471f72011-09-25 23:23:43 +000044void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
Chad Rosier08e79d22013-01-15 01:21:53 +000045 const DiagnosticOptions &Opts,
46 bool ReportDiags) {
Chris Lattner27ceb9d2009-04-15 07:01:18 +000047 Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
Daniel Dunbar69079432009-11-12 07:28:44 +000048 Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
Douglas Gregordc7b6412012-10-23 23:11:23 +000049 Diags.setShowOverloads(Opts.getShowOverloads());
Richard Trieu246b6aa2012-06-26 18:18:47 +000050
51 Diags.setElideType(Opts.ElideType);
52 Diags.setPrintTemplateTree(Opts.ShowTemplateTree);
53 Diags.setShowColors(Opts.ShowColors);
54
Chris Lattnerc1002142010-04-07 20:37:06 +000055 // Handle -ferror-limit
56 if (Opts.ErrorLimit)
57 Diags.setErrorLimit(Opts.ErrorLimit);
Douglas Gregor575cf372010-04-20 07:18:24 +000058 if (Opts.TemplateBacktraceLimit)
59 Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
Richard Smith08d6e032011-12-16 19:06:07 +000060 if (Opts.ConstexprBacktraceLimit)
61 Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000062
Chris Lattnerb54b2762009-04-16 05:04:32 +000063 // 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 Dunbar69079432009-11-12 07:28:44 +000066 if (Opts.PedanticErrors)
Stephen Hinesc568f1e2014-07-21 00:47:37 -070067 Diags.setExtensionHandlingBehavior(diag::Severity::Error);
Daniel Dunbar69079432009-11-12 07:28:44 +000068 else if (Opts.Pedantic)
Stephen Hinesc568f1e2014-07-21 00:47:37 -070069 Diags.setExtensionHandlingBehavior(diag::Severity::Warning);
Chris Lattnerb54b2762009-04-16 05:04:32 +000070 else
Stephen Hinesc568f1e2014-07-21 00:47:37 -070071 Diags.setExtensionHandlingBehavior(diag::Severity::Ignored);
Mike Stump1eb44332009-09-09 15:08:12 +000072
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +000073 SmallVector<diag::kind, 10> _Diags;
Dylan Noblesmithc93dc782012-02-20 14:00:23 +000074 const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs =
Chad Rosier05272a62011-11-03 21:23:39 +000075 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 Rosier08e79d22013-01-15 01:21:53 +000082
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 Rosier05272a62011-11-03 21:23:39 +000088 for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
Stephen Hines176edba2014-12-01 14:53:08 -080089 const auto Flavor = diag::Flavor::WarningOrError;
Chad Rosier05272a62011-11-03 21:23:39 +000090 StringRef Opt = Opts.Warnings[i];
Chad Rosierff1affe2012-05-16 19:28:02 +000091 StringRef OrigOpt = Opts.Warnings[i];
Mike Stump1eb44332009-09-09 15:08:12 +000092
Hans Wennborgc8769462012-01-17 09:30:38 +000093 // Treat -Wformat=0 as an alias for -Wno-format.
94 if (Opt == "format=0")
95 Opt = "no-format";
96
Chad Rosier05272a62011-11-03 21:23:39 +000097 // 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 Lattner5147e8e2009-04-15 04:27:38 +0000103 }
Mike Stump1eb44332009-09-09 15:08:12 +0000104
Chad Rosier05272a62011-11-03 21:23:39 +0000105 // 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 Hinesc568f1e2014-07-21 00:47:37 -0700107 diag::Severity Mapping =
108 isPositive ? diag::Severity::Warning : diag::Severity::Ignored;
109
Chad Rosier05272a62011-11-03 21:23:39 +0000110 // -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 Lattner5147e8e2009-04-15 04:27:38 +0000115 continue;
116 }
Chad Rosier05272a62011-11-03 21:23:39 +0000117
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 Kyrtzidis11583c72012-01-27 06:15:43 +0000121 if (SetDiagnostic) {
122 if (isPositive) {
123 Diags.setEnableAllWarnings(true);
124 } else {
125 Diags.setEnableAllWarnings(false);
Stephen Hines176edba2014-12-01 14:53:08 -0800126 Diags.setSeverityForAll(Flavor, diag::Severity::Ignored);
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000127 }
128 }
Chad Rosier05272a62011-11-03 21:23:39 +0000129 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 Rosierff1affe2012-05-16 19:28:02 +0000140 << "-Werror" << ("-W" + OrigOpt.str());
Chad Rosier05272a62011-11-03 21:23:39 +0000141 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 Hines176edba2014-12-01 14:53:08 -0800155 } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
156 EmitUnknownDiagWarning(Diags, Flavor, "-Werror=", Specifier);
Chad Rosier05272a62011-11-03 21:23:39 +0000157 }
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 Rosierff1affe2012-05-16 19:28:02 +0000168 << "-Wfatal-errors" << ("-W" + OrigOpt.str());
Chad Rosier05272a62011-11-03 21:23:39 +0000169 continue;
170 }
171 Specifier = Opt.substr(13);
172 }
Mike Stump1eb44332009-09-09 15:08:12 +0000173
Chad Rosier05272a62011-11-03 21:23:39 +0000174 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 Hines176edba2014-12-01 14:53:08 -0800183 } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
184 EmitUnknownDiagWarning(Diags, Flavor, "-Wfatal-errors=", Specifier);
Chad Rosier05272a62011-11-03 21:23:39 +0000185 }
186 continue;
187 }
188
Chad Rosier496cc8e2011-11-15 18:57:32 +0000189 if (Report) {
Stephen Hines176edba2014-12-01 14:53:08 -0800190 if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
191 EmitUnknownDiagWarning(Diags, Flavor, isPositive ? "-W" : "-Wno-",
192 Opt);
Chad Rosier05272a62011-11-03 21:23:39 +0000193 } else {
Stephen Hines176edba2014-12-01 14:53:08 -0800194 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 Dunbar4aa8f2b2011-09-29 00:53:47 +0000227 }
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000228 }
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000229 }
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000230}