2009-03-07 01:41:35 +08:00
|
|
|
//===--- Warnings.cpp - C-Language Front-end ------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Command line warning options handler.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is responsible for handling all warning options. This includes
|
|
|
|
// a number of -Wfoo options and their variants, which are driven by TableGen-
|
2009-12-24 02:53:37 +08:00
|
|
|
// generated data, and the special cases -pedantic, -pedantic-errors, -w,
|
|
|
|
// -Werror and -Wfatal-errors.
|
2009-03-07 01:41:35 +08:00
|
|
|
//
|
2009-03-07 20:09:25 +08:00
|
|
|
// Each warning option controls any number of actual warnings.
|
2009-03-07 01:41:35 +08:00
|
|
|
// Given a warning option 'foo', the following are valid:
|
2009-12-24 02:53:37 +08:00
|
|
|
// -Wfoo, -Wno-foo, -Werror=foo, -Wfatal-errors=foo
|
2009-03-07 01:41:35 +08:00
|
|
|
//
|
Use -Rblah, not -Wblah, to control remark diagnostics. This was always the
intent when we added remark support, but was never implemented in the general
case, because the first -R flags didn't need it. (-Rpass= had special handling
to accomodate its argument.)
-Rno-foo, -Reverything, and -Rno-everything can be used to turn off a remark,
or to turn on or off all remarks. Per discussion on cfe-commits, -Weverything
does not affect remarks, and -Reverything does not affect warnings or errors.
The only "real" -R flag we have right now is -Rmodule-build; that flag is
effectively renamed from -Wmodule-build to -Rmodule-build by this change.
-Wpass and -Wno-pass (and their friends) are also renamed to -Rpass and
-Rno-pass by this change; it's not completely clear whether we intended to have
a -Rpass (with no =pattern), but that is unchanged by this commit, other than
the flag name. The default pattern is effectively one which matches no passes.
In future, we may want to make the default pattern be .*, so that -Reverything
works for -Rpass properly.
llvm-svn: 215046
2014-08-07 08:24:21 +08:00
|
|
|
// Remark options are also handled here, analogously, except that they are much
|
|
|
|
// simpler because a remark can't be promoted to an error.
|
2014-04-30 00:25:26 +08:00
|
|
|
#include "clang/Basic/AllDiagnostics.h"
|
2009-03-07 01:41:35 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2012-10-24 06:26:28 +08:00
|
|
|
#include "clang/Basic/DiagnosticOptions.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include <algorithm>
|
2009-05-19 12:30:57 +08:00
|
|
|
#include <cstring>
|
2009-03-07 01:41:35 +08:00
|
|
|
#include <utility>
|
|
|
|
using namespace clang;
|
|
|
|
|
2012-02-01 03:31:12 +08:00
|
|
|
// EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning
|
|
|
|
// opts
|
2011-11-16 03:03:03 +08:00
|
|
|
static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags,
|
Use -Rblah, not -Wblah, to control remark diagnostics. This was always the
intent when we added remark support, but was never implemented in the general
case, because the first -R flags didn't need it. (-Rpass= had special handling
to accomodate its argument.)
-Rno-foo, -Reverything, and -Rno-everything can be used to turn off a remark,
or to turn on or off all remarks. Per discussion on cfe-commits, -Weverything
does not affect remarks, and -Reverything does not affect warnings or errors.
The only "real" -R flag we have right now is -Rmodule-build; that flag is
effectively renamed from -Wmodule-build to -Rmodule-build by this change.
-Wpass and -Wno-pass (and their friends) are also renamed to -Rpass and
-Rno-pass by this change; it's not completely clear whether we intended to have
a -Rpass (with no =pattern), but that is unchanged by this commit, other than
the flag name. The default pattern is effectively one which matches no passes.
In future, we may want to make the default pattern be .*, so that -Reverything
works for -Rpass properly.
llvm-svn: 215046
2014-08-07 08:24:21 +08:00
|
|
|
diag::Flavor Flavor, StringRef Prefix,
|
|
|
|
StringRef Opt) {
|
|
|
|
StringRef Suggestion = DiagnosticIDs::getNearestOption(Flavor, Opt);
|
|
|
|
Diags.Report(diag::warn_unknown_diag_option)
|
|
|
|
<< (Flavor == diag::Flavor::WarningOrError ? 0 : 1) << (Prefix.str() += Opt)
|
|
|
|
<< !Suggestion.empty() << (Prefix.str() += Suggestion);
|
2011-11-15 20:26:39 +08:00
|
|
|
}
|
|
|
|
|
2011-09-26 07:23:43 +08:00
|
|
|
void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
|
2013-01-15 09:21:53 +08:00
|
|
|
const DiagnosticOptions &Opts,
|
|
|
|
bool ReportDiags) {
|
2009-04-15 15:01:18 +08:00
|
|
|
Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
|
2009-11-12 15:28:44 +08:00
|
|
|
Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
|
2012-10-24 07:11:23 +08:00
|
|
|
Diags.setShowOverloads(Opts.getShowOverloads());
|
2012-06-27 02:18:47 +08:00
|
|
|
|
|
|
|
Diags.setElideType(Opts.ElideType);
|
|
|
|
Diags.setPrintTemplateTree(Opts.ShowTemplateTree);
|
|
|
|
Diags.setShowColors(Opts.ShowColors);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2010-04-08 04:37:06 +08:00
|
|
|
// Handle -ferror-limit
|
|
|
|
if (Opts.ErrorLimit)
|
|
|
|
Diags.setErrorLimit(Opts.ErrorLimit);
|
2010-04-20 15:18:24 +08:00
|
|
|
if (Opts.TemplateBacktraceLimit)
|
|
|
|
Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
|
2011-12-17 03:06:07 +08:00
|
|
|
if (Opts.ConstexprBacktraceLimit)
|
|
|
|
Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit);
|
2009-03-07 01:41:35 +08:00
|
|
|
|
2009-04-16 13:04:32 +08:00
|
|
|
// If -pedantic or -pedantic-errors was specified, then we want to map all
|
|
|
|
// extension diagnostics onto WARNING or ERROR unless the user has futz'd
|
|
|
|
// around with them explicitly.
|
2009-11-12 15:28:44 +08:00
|
|
|
if (Opts.PedanticErrors)
|
2014-06-23 05:58:33 +08:00
|
|
|
Diags.setExtensionHandlingBehavior(diag::Severity::Error);
|
2009-11-12 15:28:44 +08:00
|
|
|
else if (Opts.Pedantic)
|
2014-06-23 05:58:33 +08:00
|
|
|
Diags.setExtensionHandlingBehavior(diag::Severity::Warning);
|
2009-04-16 13:04:32 +08:00
|
|
|
else
|
2014-06-23 05:58:33 +08:00
|
|
|
Diags.setExtensionHandlingBehavior(diag::Severity::Ignored);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-01-13 03:30:44 +08:00
|
|
|
SmallVector<diag::kind, 10> _Diags;
|
2012-02-20 22:00:23 +08:00
|
|
|
const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs =
|
2011-11-04 05:23:39 +08:00
|
|
|
Diags.getDiagnosticIDs();
|
|
|
|
// We parse the warning options twice. The first pass sets diagnostic state,
|
|
|
|
// while the second pass reports warnings/errors. This has the effect that
|
2018-07-31 03:24:48 +08:00
|
|
|
// we follow the more canonical "last option wins" paradigm when there are
|
2011-11-04 05:23:39 +08:00
|
|
|
// conflicting options.
|
|
|
|
for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; ++Report) {
|
|
|
|
bool SetDiagnostic = (Report == 0);
|
2013-01-15 09:21:53 +08:00
|
|
|
|
|
|
|
// If we've set the diagnostic state and are not reporting diagnostics then
|
|
|
|
// we're done.
|
|
|
|
if (!SetDiagnostic && !ReportDiags)
|
|
|
|
break;
|
|
|
|
|
2011-11-04 05:23:39 +08:00
|
|
|
for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
|
Use -Rblah, not -Wblah, to control remark diagnostics. This was always the
intent when we added remark support, but was never implemented in the general
case, because the first -R flags didn't need it. (-Rpass= had special handling
to accomodate its argument.)
-Rno-foo, -Reverything, and -Rno-everything can be used to turn off a remark,
or to turn on or off all remarks. Per discussion on cfe-commits, -Weverything
does not affect remarks, and -Reverything does not affect warnings or errors.
The only "real" -R flag we have right now is -Rmodule-build; that flag is
effectively renamed from -Wmodule-build to -Rmodule-build by this change.
-Wpass and -Wno-pass (and their friends) are also renamed to -Rpass and
-Rno-pass by this change; it's not completely clear whether we intended to have
a -Rpass (with no =pattern), but that is unchanged by this commit, other than
the flag name. The default pattern is effectively one which matches no passes.
In future, we may want to make the default pattern be .*, so that -Reverything
works for -Rpass properly.
llvm-svn: 215046
2014-08-07 08:24:21 +08:00
|
|
|
const auto Flavor = diag::Flavor::WarningOrError;
|
2011-11-04 05:23:39 +08:00
|
|
|
StringRef Opt = Opts.Warnings[i];
|
2012-05-17 03:28:02 +08:00
|
|
|
StringRef OrigOpt = Opts.Warnings[i];
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-01-17 17:30:38 +08:00
|
|
|
// Treat -Wformat=0 as an alias for -Wno-format.
|
|
|
|
if (Opt == "format=0")
|
|
|
|
Opt = "no-format";
|
|
|
|
|
2011-11-04 05:23:39 +08:00
|
|
|
// Check to see if this warning starts with "no-", if so, this is a
|
|
|
|
// negative form of the option.
|
|
|
|
bool isPositive = true;
|
|
|
|
if (Opt.startswith("no-")) {
|
|
|
|
isPositive = false;
|
|
|
|
Opt = Opt.substr(3);
|
2009-04-15 12:27:38 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-11-04 05:23:39 +08:00
|
|
|
// Figure out how this option affects the warning. If -Wfoo, map the
|
|
|
|
// diagnostic to a warning, if -Wno-foo, map it to ignore.
|
2014-06-10 17:31:37 +08:00
|
|
|
diag::Severity Mapping =
|
2014-06-12 18:15:20 +08:00
|
|
|
isPositive ? diag::Severity::Warning : diag::Severity::Ignored;
|
2014-06-10 17:31:37 +08:00
|
|
|
|
2011-11-04 05:23:39 +08:00
|
|
|
// -Wsystem-headers is a special case, not driven by the option table. It
|
|
|
|
// cannot be controlled with -Werror.
|
|
|
|
if (Opt == "system-headers") {
|
|
|
|
if (SetDiagnostic)
|
|
|
|
Diags.setSuppressSystemWarnings(!isPositive);
|
2009-04-15 12:27:38 +08:00
|
|
|
continue;
|
2009-03-07 01:41:35 +08:00
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-11-04 05:23:39 +08:00
|
|
|
// -Weverything is a special case as well. It implicitly enables all
|
|
|
|
// warnings, including ones not explicitly in a warning group.
|
|
|
|
if (Opt == "everything") {
|
2012-01-27 14:15:43 +08:00
|
|
|
if (SetDiagnostic) {
|
|
|
|
if (isPositive) {
|
|
|
|
Diags.setEnableAllWarnings(true);
|
|
|
|
} else {
|
|
|
|
Diags.setEnableAllWarnings(false);
|
Use -Rblah, not -Wblah, to control remark diagnostics. This was always the
intent when we added remark support, but was never implemented in the general
case, because the first -R flags didn't need it. (-Rpass= had special handling
to accomodate its argument.)
-Rno-foo, -Reverything, and -Rno-everything can be used to turn off a remark,
or to turn on or off all remarks. Per discussion on cfe-commits, -Weverything
does not affect remarks, and -Reverything does not affect warnings or errors.
The only "real" -R flag we have right now is -Rmodule-build; that flag is
effectively renamed from -Wmodule-build to -Rmodule-build by this change.
-Wpass and -Wno-pass (and their friends) are also renamed to -Rpass and
-Rno-pass by this change; it's not completely clear whether we intended to have
a -Rpass (with no =pattern), but that is unchanged by this commit, other than
the flag name. The default pattern is effectively one which matches no passes.
In future, we may want to make the default pattern be .*, so that -Reverything
works for -Rpass properly.
llvm-svn: 215046
2014-08-07 08:24:21 +08:00
|
|
|
Diags.setSeverityForAll(Flavor, diag::Severity::Ignored);
|
2012-01-27 14:15:43 +08:00
|
|
|
}
|
|
|
|
}
|
2011-11-04 05:23:39 +08:00
|
|
|
continue;
|
2011-09-29 08:53:47 +08:00
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
|
|
|
// -Werror/-Wno-error is a special case, not controlled by the option
|
2011-11-04 05:23:39 +08:00
|
|
|
// table. It also has the "specifier" form of -Werror=foo and -Werror-foo.
|
|
|
|
if (Opt.startswith("error")) {
|
|
|
|
StringRef Specifier;
|
|
|
|
if (Opt.size() > 5) { // Specifier must be present.
|
|
|
|
if ((Opt[5] != '=' && Opt[5] != '-') || Opt.size() == 6) {
|
|
|
|
if (Report)
|
|
|
|
Diags.Report(diag::warn_unknown_warning_specifier)
|
2012-05-17 03:28:02 +08:00
|
|
|
<< "-Werror" << ("-W" + OrigOpt.str());
|
2011-11-04 05:23:39 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Specifier = Opt.substr(6);
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-11-04 05:23:39 +08:00
|
|
|
if (Specifier.empty()) {
|
|
|
|
if (SetDiagnostic)
|
|
|
|
Diags.setWarningsAsErrors(isPositive);
|
2009-12-23 07:12:53 +08:00
|
|
|
continue;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-11-04 05:23:39 +08:00
|
|
|
if (SetDiagnostic) {
|
|
|
|
// Set the warning as error flag for this specifier.
|
|
|
|
Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive);
|
Use -Rblah, not -Wblah, to control remark diagnostics. This was always the
intent when we added remark support, but was never implemented in the general
case, because the first -R flags didn't need it. (-Rpass= had special handling
to accomodate its argument.)
-Rno-foo, -Reverything, and -Rno-everything can be used to turn off a remark,
or to turn on or off all remarks. Per discussion on cfe-commits, -Weverything
does not affect remarks, and -Reverything does not affect warnings or errors.
The only "real" -R flag we have right now is -Rmodule-build; that flag is
effectively renamed from -Wmodule-build to -Rmodule-build by this change.
-Wpass and -Wno-pass (and their friends) are also renamed to -Rpass and
-Rno-pass by this change; it's not completely clear whether we intended to have
a -Rpass (with no =pattern), but that is unchanged by this commit, other than
the flag name. The default pattern is effectively one which matches no passes.
In future, we may want to make the default pattern be .*, so that -Reverything
works for -Rpass properly.
llvm-svn: 215046
2014-08-07 08:24:21 +08:00
|
|
|
} else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
|
|
|
|
EmitUnknownDiagWarning(Diags, Flavor, "-Werror=", Specifier);
|
2011-11-04 05:23:39 +08:00
|
|
|
}
|
|
|
|
continue;
|
2009-12-23 07:12:53 +08:00
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-11-04 05:23:39 +08:00
|
|
|
// -Wfatal-errors is yet another special case.
|
|
|
|
if (Opt.startswith("fatal-errors")) {
|
|
|
|
StringRef Specifier;
|
|
|
|
if (Opt.size() != 12) {
|
|
|
|
if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) {
|
|
|
|
if (Report)
|
|
|
|
Diags.Report(diag::warn_unknown_warning_specifier)
|
2012-05-17 03:28:02 +08:00
|
|
|
<< "-Wfatal-errors" << ("-W" + OrigOpt.str());
|
2011-11-04 05:23:39 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Specifier = Opt.substr(13);
|
|
|
|
}
|
2009-12-23 07:12:53 +08:00
|
|
|
|
2011-11-04 05:23:39 +08:00
|
|
|
if (Specifier.empty()) {
|
|
|
|
if (SetDiagnostic)
|
|
|
|
Diags.setErrorsAsFatal(isPositive);
|
|
|
|
continue;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-11-04 05:23:39 +08:00
|
|
|
if (SetDiagnostic) {
|
|
|
|
// Set the error as fatal flag for this specifier.
|
|
|
|
Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive);
|
Use -Rblah, not -Wblah, to control remark diagnostics. This was always the
intent when we added remark support, but was never implemented in the general
case, because the first -R flags didn't need it. (-Rpass= had special handling
to accomodate its argument.)
-Rno-foo, -Reverything, and -Rno-everything can be used to turn off a remark,
or to turn on or off all remarks. Per discussion on cfe-commits, -Weverything
does not affect remarks, and -Reverything does not affect warnings or errors.
The only "real" -R flag we have right now is -Rmodule-build; that flag is
effectively renamed from -Wmodule-build to -Rmodule-build by this change.
-Wpass and -Wno-pass (and their friends) are also renamed to -Rpass and
-Rno-pass by this change; it's not completely clear whether we intended to have
a -Rpass (with no =pattern), but that is unchanged by this commit, other than
the flag name. The default pattern is effectively one which matches no passes.
In future, we may want to make the default pattern be .*, so that -Reverything
works for -Rpass properly.
llvm-svn: 215046
2014-08-07 08:24:21 +08:00
|
|
|
} else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
|
|
|
|
EmitUnknownDiagWarning(Diags, Flavor, "-Wfatal-errors=", Specifier);
|
2011-11-04 05:23:39 +08:00
|
|
|
}
|
2009-12-23 07:12:53 +08:00
|
|
|
continue;
|
|
|
|
}
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2011-11-16 02:57:32 +08:00
|
|
|
if (Report) {
|
Use -Rblah, not -Wblah, to control remark diagnostics. This was always the
intent when we added remark support, but was never implemented in the general
case, because the first -R flags didn't need it. (-Rpass= had special handling
to accomodate its argument.)
-Rno-foo, -Reverything, and -Rno-everything can be used to turn off a remark,
or to turn on or off all remarks. Per discussion on cfe-commits, -Weverything
does not affect remarks, and -Reverything does not affect warnings or errors.
The only "real" -R flag we have right now is -Rmodule-build; that flag is
effectively renamed from -Wmodule-build to -Rmodule-build by this change.
-Wpass and -Wno-pass (and their friends) are also renamed to -Rpass and
-Rno-pass by this change; it's not completely clear whether we intended to have
a -Rpass (with no =pattern), but that is unchanged by this commit, other than
the flag name. The default pattern is effectively one which matches no passes.
In future, we may want to make the default pattern be .*, so that -Reverything
works for -Rpass properly.
llvm-svn: 215046
2014-08-07 08:24:21 +08:00
|
|
|
if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
|
|
|
|
EmitUnknownDiagWarning(Diags, Flavor, isPositive ? "-W" : "-Wno-",
|
|
|
|
Opt);
|
|
|
|
} else {
|
|
|
|
Diags.setSeverityForGroup(Flavor, Opt, Mapping);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Opts.Remarks.size(); i != e; ++i) {
|
|
|
|
StringRef Opt = Opts.Remarks[i];
|
|
|
|
const auto Flavor = diag::Flavor::Remark;
|
|
|
|
|
|
|
|
// Check to see if this warning starts with "no-", if so, this is a
|
|
|
|
// negative form of the option.
|
|
|
|
bool IsPositive = !Opt.startswith("no-");
|
|
|
|
if (!IsPositive) Opt = Opt.substr(3);
|
|
|
|
|
|
|
|
auto Severity = IsPositive ? diag::Severity::Remark
|
|
|
|
: diag::Severity::Ignored;
|
|
|
|
|
|
|
|
// -Reverything sets the state of all remarks. Note that all remarks are
|
|
|
|
// in remark groups, so we don't need a separate 'all remarks enabled'
|
|
|
|
// flag.
|
|
|
|
if (Opt == "everything") {
|
|
|
|
if (SetDiagnostic)
|
|
|
|
Diags.setSeverityForAll(Flavor, Severity);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Report) {
|
|
|
|
if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
|
|
|
|
EmitUnknownDiagWarning(Diags, Flavor, IsPositive ? "-R" : "-Rno-",
|
|
|
|
Opt);
|
2011-11-04 05:23:39 +08:00
|
|
|
} else {
|
Use -Rblah, not -Wblah, to control remark diagnostics. This was always the
intent when we added remark support, but was never implemented in the general
case, because the first -R flags didn't need it. (-Rpass= had special handling
to accomodate its argument.)
-Rno-foo, -Reverything, and -Rno-everything can be used to turn off a remark,
or to turn on or off all remarks. Per discussion on cfe-commits, -Weverything
does not affect remarks, and -Reverything does not affect warnings or errors.
The only "real" -R flag we have right now is -Rmodule-build; that flag is
effectively renamed from -Wmodule-build to -Rmodule-build by this change.
-Wpass and -Wno-pass (and their friends) are also renamed to -Rpass and
-Rno-pass by this change; it's not completely clear whether we intended to have
a -Rpass (with no =pattern), but that is unchanged by this commit, other than
the flag name. The default pattern is effectively one which matches no passes.
In future, we may want to make the default pattern be .*, so that -Reverything
works for -Rpass properly.
llvm-svn: 215046
2014-08-07 08:24:21 +08:00
|
|
|
Diags.setSeverityForGroup(Flavor, Opt,
|
|
|
|
IsPositive ? diag::Severity::Remark
|
|
|
|
: diag::Severity::Ignored);
|
2011-09-29 08:53:47 +08:00
|
|
|
}
|
|
|
|
}
|
2009-03-07 01:41:35 +08:00
|
|
|
}
|
|
|
|
}
|