1 //===--- Warnings.cpp - C-Language Front-end ------------------------------===//
3 // 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
7 //===----------------------------------------------------------------------===//
9 // Command line warning options handler.
11 //===----------------------------------------------------------------------===//
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-
15 // generated data, and the special cases -pedantic, -pedantic-errors, -w,
16 // -Werror and -Wfatal-errors.
18 // Each warning option controls any number of actual warnings.
19 // Given a warning option 'foo', the following are valid:
20 // -Wfoo, -Wno-foo, -Werror=foo, -Wfatal-errors=foo
22 // Remark options are also handled here, analogously, except that they are much
23 // simpler because a remark can't be promoted to an error.
24 #include "clang/Basic/AllDiagnostics.h"
25 #include "clang/Basic/Diagnostic.h"
26 #include "clang/Basic/DiagnosticOptions.h"
30 using namespace clang
;
32 // EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning
34 static void EmitUnknownDiagWarning(DiagnosticsEngine
&Diags
,
35 diag::Flavor Flavor
, StringRef Prefix
,
37 StringRef Suggestion
= DiagnosticIDs::getNearestOption(Flavor
, Opt
);
38 Diags
.Report(diag::warn_unknown_diag_option
)
39 << (Flavor
== diag::Flavor::WarningOrError
? 0 : 1)
40 << (Prefix
.str() += std::string(Opt
)) << !Suggestion
.empty()
41 << (Prefix
.str() += std::string(Suggestion
));
44 void clang::ProcessWarningOptions(DiagnosticsEngine
&Diags
,
45 const DiagnosticOptions
&Opts
,
47 Diags
.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
48 Diags
.setIgnoreAllWarnings(Opts
.IgnoreWarnings
);
49 Diags
.setShowOverloads(Opts
.getShowOverloads());
51 Diags
.setElideType(Opts
.ElideType
);
52 Diags
.setPrintTemplateTree(Opts
.ShowTemplateTree
);
53 Diags
.setShowColors(Opts
.ShowColors
);
55 // Handle -ferror-limit
57 Diags
.setErrorLimit(Opts
.ErrorLimit
);
58 if (Opts
.TemplateBacktraceLimit
)
59 Diags
.setTemplateBacktraceLimit(Opts
.TemplateBacktraceLimit
);
60 if (Opts
.ConstexprBacktraceLimit
)
61 Diags
.setConstexprBacktraceLimit(Opts
.ConstexprBacktraceLimit
);
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.
66 if (Opts
.PedanticErrors
)
67 Diags
.setExtensionHandlingBehavior(diag::Severity::Error
);
68 else if (Opts
.Pedantic
)
69 Diags
.setExtensionHandlingBehavior(diag::Severity::Warning
);
71 Diags
.setExtensionHandlingBehavior(diag::Severity::Ignored
);
73 SmallVector
<diag::kind
, 10> _Diags
;
74 const IntrusiveRefCntPtr
< DiagnosticIDs
> DiagIDs
=
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);
83 // If we've set the diagnostic state and are not reporting diagnostics then
85 if (!SetDiagnostic
&& !ReportDiags
)
88 for (unsigned i
= 0, e
= Opts
.Warnings
.size(); i
!= e
; ++i
) {
89 const auto Flavor
= diag::Flavor::WarningOrError
;
90 StringRef Opt
= Opts
.Warnings
[i
];
91 StringRef OrigOpt
= Opts
.Warnings
[i
];
93 // Treat -Wformat=0 as an alias for -Wno-format.
94 if (Opt
== "format=0")
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-")) {
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.
107 diag::Severity Mapping
=
108 isPositive
? diag::Severity::Warning
: diag::Severity::Ignored
;
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") {
114 Diags
.setSuppressSystemWarnings(!isPositive
);
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") {
123 Diags
.setEnableAllWarnings(true);
125 Diags
.setEnableAllWarnings(false);
126 Diags
.setSeverityForAll(Flavor
, diag::Severity::Ignored
);
132 // -Werror/-Wno-error is a special case, not controlled by the option
133 // table. It also has the "specifier" form of -Werror=foo. GCC supports
134 // the deprecated -Werror-implicit-function-declaration which is used by
136 if (Opt
.startswith("error")) {
138 if (Opt
.size() > 5) { // Specifier must be present.
140 Opt
.substr(5) != "-implicit-function-declaration") {
142 Diags
.Report(diag::warn_unknown_warning_specifier
)
143 << "-Werror" << ("-W" + OrigOpt
.str());
146 Specifier
= Opt
.substr(6);
149 if (Specifier
.empty()) {
151 Diags
.setWarningsAsErrors(isPositive
);
156 // Set the warning as error flag for this specifier.
157 Diags
.setDiagnosticGroupWarningAsError(Specifier
, isPositive
);
158 } else if (DiagIDs
->getDiagnosticsInGroup(Flavor
, Specifier
, _Diags
)) {
159 EmitUnknownDiagWarning(Diags
, Flavor
, "-Werror=", Specifier
);
164 // -Wfatal-errors is yet another special case.
165 if (Opt
.startswith("fatal-errors")) {
167 if (Opt
.size() != 12) {
168 if ((Opt
[12] != '=' && Opt
[12] != '-') || Opt
.size() == 13) {
170 Diags
.Report(diag::warn_unknown_warning_specifier
)
171 << "-Wfatal-errors" << ("-W" + OrigOpt
.str());
174 Specifier
= Opt
.substr(13);
177 if (Specifier
.empty()) {
179 Diags
.setErrorsAsFatal(isPositive
);
184 // Set the error as fatal flag for this specifier.
185 Diags
.setDiagnosticGroupErrorAsFatal(Specifier
, isPositive
);
186 } else if (DiagIDs
->getDiagnosticsInGroup(Flavor
, Specifier
, _Diags
)) {
187 EmitUnknownDiagWarning(Diags
, Flavor
, "-Wfatal-errors=", Specifier
);
193 if (DiagIDs
->getDiagnosticsInGroup(Flavor
, Opt
, _Diags
))
194 EmitUnknownDiagWarning(Diags
, Flavor
, isPositive
? "-W" : "-Wno-",
197 Diags
.setSeverityForGroup(Flavor
, Opt
, Mapping
);
201 for (unsigned i
= 0, e
= Opts
.Remarks
.size(); i
!= e
; ++i
) {
202 StringRef Opt
= Opts
.Remarks
[i
];
203 const auto Flavor
= diag::Flavor::Remark
;
205 // Check to see if this warning starts with "no-", if so, this is a
206 // negative form of the option.
207 bool IsPositive
= !Opt
.startswith("no-");
208 if (!IsPositive
) Opt
= Opt
.substr(3);
210 auto Severity
= IsPositive
? diag::Severity::Remark
211 : diag::Severity::Ignored
;
213 // -Reverything sets the state of all remarks. Note that all remarks are
214 // in remark groups, so we don't need a separate 'all remarks enabled'
216 if (Opt
== "everything") {
218 Diags
.setSeverityForAll(Flavor
, Severity
);
223 if (DiagIDs
->getDiagnosticsInGroup(Flavor
, Opt
, _Diags
))
224 EmitUnknownDiagWarning(Diags
, Flavor
, IsPositive
? "-R" : "-Rno-",
227 Diags
.setSeverityForGroup(Flavor
, Opt
,
228 IsPositive
? diag::Severity::Remark
229 : diag::Severity::Ignored
);