blob: 88ef2eaa6589182db6c9b02f0616b688c9516e2b [file] [log] [blame]
Sebastian Redl48350102009-03-06 17:41:35 +00001//===--- Warnings.cpp - C-Language Front-end ------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Sebastian Redl48350102009-03-06 17:41:35 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Command line warning options handler.
10//
11//===----------------------------------------------------------------------===//
12//
13// This file is responsible for handling all warning options. This includes
14// a number of -Wfoo options and their variants, which are driven by TableGen-
Chris Lattnerb089c1d2009-12-23 18:53:37 +000015// generated data, and the special cases -pedantic, -pedantic-errors, -w,
16// -Werror and -Wfatal-errors.
Sebastian Redl48350102009-03-06 17:41:35 +000017//
Sebastian Redlecc54442009-03-07 12:09:25 +000018// Each warning option controls any number of actual warnings.
Sebastian Redl48350102009-03-06 17:41:35 +000019// Given a warning option 'foo', the following are valid:
Chris Lattnerb089c1d2009-12-23 18:53:37 +000020// -Wfoo, -Wno-foo, -Werror=foo, -Wfatal-errors=foo
Sebastian Redl48350102009-03-06 17:41:35 +000021//
Richard Smith3be1cb22014-08-07 00:24:21 +000022// Remark options are also handled here, analogously, except that they are much
23// simpler because a remark can't be promoted to an error.
Ben Langmuirb92de022014-04-29 16:25:26 +000024#include "clang/Basic/AllDiagnostics.h"
Sebastian Redl48350102009-03-06 17:41:35 +000025#include "clang/Basic/Diagnostic.h"
Douglas Gregor811db4e2012-10-23 22:26:28 +000026#include "clang/Basic/DiagnosticOptions.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000027#include <algorithm>
Eli Friedmancec35d72009-05-19 04:30:57 +000028#include <cstring>
Sebastian Redl48350102009-03-06 17:41:35 +000029#include <utility>
Sebastian Redl48350102009-03-06 17:41:35 +000030using namespace clang;
31
Chad Rosierebaefe72012-01-31 19:31:12 +000032// EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning
33// opts
Chad Rosier1fede192011-11-15 19:03:03 +000034static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags,
Richard Smith3be1cb22014-08-07 00:24:21 +000035 diag::Flavor Flavor, StringRef Prefix,
36 StringRef Opt) {
37 StringRef Suggestion = DiagnosticIDs::getNearestOption(Flavor, Opt);
38 Diags.Report(diag::warn_unknown_diag_option)
39 << (Flavor == diag::Flavor::WarningOrError ? 0 : 1) << (Prefix.str() += Opt)
40 << !Suggestion.empty() << (Prefix.str() += Suggestion);
Benjamin Kramer176a5cb2011-11-15 12:26:39 +000041}
42
David Blaikie9c902b52011-09-25 23:23:43 +000043void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
Chad Rosier5f15a352013-01-15 01:21:53 +000044 const DiagnosticOptions &Opts,
45 bool ReportDiags) {
Chris Lattnere007de32009-04-15 07:01:18 +000046 Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
Daniel Dunbar4c0e8272009-11-12 07:28:44 +000047 Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
Douglas Gregor79591782012-10-23 23:11:23 +000048 Diags.setShowOverloads(Opts.getShowOverloads());
Richard Trieu91844232012-06-26 18:18:47 +000049
50 Diags.setElideType(Opts.ElideType);
51 Diags.setPrintTemplateTree(Opts.ShowTemplateTree);
52 Diags.setShowColors(Opts.ShowColors);
Fangrui Song6907ce22018-07-30 19:24:48 +000053
Chris Lattnerdec49e72010-04-07 20:37:06 +000054 // Handle -ferror-limit
55 if (Opts.ErrorLimit)
56 Diags.setErrorLimit(Opts.ErrorLimit);
Douglas Gregorffed1cb2010-04-20 07:18:24 +000057 if (Opts.TemplateBacktraceLimit)
58 Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
Richard Smithf6f003a2011-12-16 19:06:07 +000059 if (Opts.ConstexprBacktraceLimit)
60 Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit);
Sebastian Redl48350102009-03-06 17:41:35 +000061
Chris Lattnerb8e73152009-04-16 05:04:32 +000062 // If -pedantic or -pedantic-errors was specified, then we want to map all
63 // extension diagnostics onto WARNING or ERROR unless the user has futz'd
64 // around with them explicitly.
Daniel Dunbar4c0e8272009-11-12 07:28:44 +000065 if (Opts.PedanticErrors)
Alp Tokerac4e8e52014-06-22 21:58:33 +000066 Diags.setExtensionHandlingBehavior(diag::Severity::Error);
Daniel Dunbar4c0e8272009-11-12 07:28:44 +000067 else if (Opts.Pedantic)
Alp Tokerac4e8e52014-06-22 21:58:33 +000068 Diags.setExtensionHandlingBehavior(diag::Severity::Warning);
Chris Lattnerb8e73152009-04-16 05:04:32 +000069 else
Alp Tokerac4e8e52014-06-22 21:58:33 +000070 Diags.setExtensionHandlingBehavior(diag::Severity::Ignored);
Mike Stump11289f42009-09-09 15:08:12 +000071
Dmitri Gribenkof8579502013-01-12 19:30:44 +000072 SmallVector<diag::kind, 10> _Diags;
Dylan Noblesmithc95d8192012-02-20 14:00:23 +000073 const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs =
Chad Rosier52d00e42011-11-03 21:23:39 +000074 Diags.getDiagnosticIDs();
75 // We parse the warning options twice. The first pass sets diagnostic state,
76 // while the second pass reports warnings/errors. This has the effect that
Fangrui Song6907ce22018-07-30 19:24:48 +000077 // we follow the more canonical "last option wins" paradigm when there are
Chad Rosier52d00e42011-11-03 21:23:39 +000078 // conflicting options.
79 for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; ++Report) {
80 bool SetDiagnostic = (Report == 0);
Chad Rosier5f15a352013-01-15 01:21:53 +000081
82 // If we've set the diagnostic state and are not reporting diagnostics then
83 // we're done.
84 if (!SetDiagnostic && !ReportDiags)
85 break;
86
Chad Rosier52d00e42011-11-03 21:23:39 +000087 for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
Richard Smith3be1cb22014-08-07 00:24:21 +000088 const auto Flavor = diag::Flavor::WarningOrError;
Chad Rosier52d00e42011-11-03 21:23:39 +000089 StringRef Opt = Opts.Warnings[i];
Chad Rosier286852a2012-05-16 19:28:02 +000090 StringRef OrigOpt = Opts.Warnings[i];
Mike Stump11289f42009-09-09 15:08:12 +000091
Hans Wennborg507b5672012-01-17 09:30:38 +000092 // Treat -Wformat=0 as an alias for -Wno-format.
93 if (Opt == "format=0")
94 Opt = "no-format";
95
Chad Rosier52d00e42011-11-03 21:23:39 +000096 // Check to see if this warning starts with "no-", if so, this is a
97 // negative form of the option.
98 bool isPositive = true;
99 if (Opt.startswith("no-")) {
100 isPositive = false;
101 Opt = Opt.substr(3);
Chris Lattner4c73b672009-04-15 04:27:38 +0000102 }
Mike Stump11289f42009-09-09 15:08:12 +0000103
Chad Rosier52d00e42011-11-03 21:23:39 +0000104 // Figure out how this option affects the warning. If -Wfoo, map the
105 // diagnostic to a warning, if -Wno-foo, map it to ignore.
Alp Tokerc726c362014-06-10 09:31:37 +0000106 diag::Severity Mapping =
Alp Toker46df1c02014-06-12 10:15:20 +0000107 isPositive ? diag::Severity::Warning : diag::Severity::Ignored;
Alp Tokerc726c362014-06-10 09:31:37 +0000108
Chad Rosier52d00e42011-11-03 21:23:39 +0000109 // -Wsystem-headers is a special case, not driven by the option table. It
110 // cannot be controlled with -Werror.
111 if (Opt == "system-headers") {
112 if (SetDiagnostic)
113 Diags.setSuppressSystemWarnings(!isPositive);
Chris Lattner4c73b672009-04-15 04:27:38 +0000114 continue;
115 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000116
Chad Rosier52d00e42011-11-03 21:23:39 +0000117 // -Weverything is a special case as well. It implicitly enables all
118 // warnings, including ones not explicitly in a warning group.
119 if (Opt == "everything") {
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000120 if (SetDiagnostic) {
121 if (isPositive) {
122 Diags.setEnableAllWarnings(true);
123 } else {
124 Diags.setEnableAllWarnings(false);
Richard Smith3be1cb22014-08-07 00:24:21 +0000125 Diags.setSeverityForAll(Flavor, diag::Severity::Ignored);
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000126 }
127 }
Chad Rosier52d00e42011-11-03 21:23:39 +0000128 continue;
129 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000130
131 // -Werror/-Wno-error is a special case, not controlled by the option
Chad Rosier52d00e42011-11-03 21:23:39 +0000132 // table. It also has the "specifier" form of -Werror=foo and -Werror-foo.
133 if (Opt.startswith("error")) {
134 StringRef Specifier;
135 if (Opt.size() > 5) { // Specifier must be present.
136 if ((Opt[5] != '=' && Opt[5] != '-') || Opt.size() == 6) {
137 if (Report)
138 Diags.Report(diag::warn_unknown_warning_specifier)
Chad Rosier286852a2012-05-16 19:28:02 +0000139 << "-Werror" << ("-W" + OrigOpt.str());
Chad Rosier52d00e42011-11-03 21:23:39 +0000140 continue;
141 }
142 Specifier = Opt.substr(6);
143 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000144
Chad Rosier52d00e42011-11-03 21:23:39 +0000145 if (Specifier.empty()) {
146 if (SetDiagnostic)
147 Diags.setWarningsAsErrors(isPositive);
148 continue;
149 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000150
Chad Rosier52d00e42011-11-03 21:23:39 +0000151 if (SetDiagnostic) {
152 // Set the warning as error flag for this specifier.
153 Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive);
Richard Smith3be1cb22014-08-07 00:24:21 +0000154 } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
155 EmitUnknownDiagWarning(Diags, Flavor, "-Werror=", Specifier);
Chad Rosier52d00e42011-11-03 21:23:39 +0000156 }
157 continue;
158 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000159
Chad Rosier52d00e42011-11-03 21:23:39 +0000160 // -Wfatal-errors is yet another special case.
161 if (Opt.startswith("fatal-errors")) {
162 StringRef Specifier;
163 if (Opt.size() != 12) {
164 if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) {
165 if (Report)
166 Diags.Report(diag::warn_unknown_warning_specifier)
Chad Rosier286852a2012-05-16 19:28:02 +0000167 << "-Wfatal-errors" << ("-W" + OrigOpt.str());
Chad Rosier52d00e42011-11-03 21:23:39 +0000168 continue;
169 }
170 Specifier = Opt.substr(13);
171 }
Mike Stump11289f42009-09-09 15:08:12 +0000172
Chad Rosier52d00e42011-11-03 21:23:39 +0000173 if (Specifier.empty()) {
174 if (SetDiagnostic)
175 Diags.setErrorsAsFatal(isPositive);
176 continue;
177 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000178
Chad Rosier52d00e42011-11-03 21:23:39 +0000179 if (SetDiagnostic) {
180 // Set the error as fatal flag for this specifier.
181 Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive);
Richard Smith3be1cb22014-08-07 00:24:21 +0000182 } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
183 EmitUnknownDiagWarning(Diags, Flavor, "-Wfatal-errors=", Specifier);
Chad Rosier52d00e42011-11-03 21:23:39 +0000184 }
185 continue;
186 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000187
Chad Rosiere35f9dd2011-11-15 18:57:32 +0000188 if (Report) {
Richard Smith3be1cb22014-08-07 00:24:21 +0000189 if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
190 EmitUnknownDiagWarning(Diags, Flavor, isPositive ? "-W" : "-Wno-",
191 Opt);
Chad Rosier52d00e42011-11-03 21:23:39 +0000192 } else {
Richard Smith3be1cb22014-08-07 00:24:21 +0000193 Diags.setSeverityForGroup(Flavor, Opt, Mapping);
194 }
195 }
196
197 for (unsigned i = 0, e = Opts.Remarks.size(); i != e; ++i) {
198 StringRef Opt = Opts.Remarks[i];
199 const auto Flavor = diag::Flavor::Remark;
200
201 // Check to see if this warning starts with "no-", if so, this is a
202 // negative form of the option.
203 bool IsPositive = !Opt.startswith("no-");
204 if (!IsPositive) Opt = Opt.substr(3);
205
206 auto Severity = IsPositive ? diag::Severity::Remark
207 : diag::Severity::Ignored;
208
209 // -Reverything sets the state of all remarks. Note that all remarks are
210 // in remark groups, so we don't need a separate 'all remarks enabled'
211 // flag.
212 if (Opt == "everything") {
213 if (SetDiagnostic)
214 Diags.setSeverityForAll(Flavor, Severity);
215 continue;
216 }
217
218 if (Report) {
219 if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
220 EmitUnknownDiagWarning(Diags, Flavor, IsPositive ? "-R" : "-Rno-",
221 Opt);
222 } else {
223 Diags.setSeverityForGroup(Flavor, Opt,
224 IsPositive ? diag::Severity::Remark
225 : diag::Severity::Ignored);
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000226 }
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000227 }
Chris Lattnere007de32009-04-15 07:01:18 +0000228 }
Sebastian Redl48350102009-03-06 17:41:35 +0000229}