clang 10.0.0git

Warnings.cpp
Go to the documentation of this file.
1 //===--- Warnings.cpp - C-Language Front-end ------------------------------===//
2 //
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
6 //
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-
15 // generated data, and the special cases -pedantic, -pedantic-errors, -w,
16 // -Werror and -Wfatal-errors.
17 //
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
21 //
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"
27 #include <algorithm>
28 #include <cstring>
29 #include <utility>
30 using namespace clang;
31
32 // EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning
33 // opts
34 static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags,
35 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);
41 }
42
43 void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
44 const DiagnosticOptions &Opts,
45 bool ReportDiags) {
46 Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
47 Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
48 Diags.setShowOverloads(Opts.getShowOverloads());
49
50 Diags.setElideType(Opts.ElideType);
51 Diags.setPrintTemplateTree(Opts.ShowTemplateTree);
52 Diags.setShowColors(Opts.ShowColors);
53
54 // Handle -ferror-limit
55 if (Opts.ErrorLimit)
56 Diags.setErrorLimit(Opts.ErrorLimit);
57 if (Opts.TemplateBacktraceLimit)
58 Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
59 if (Opts.ConstexprBacktraceLimit)
60 Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit);
61
62 // 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.
65 if (Opts.PedanticErrors)
66 Diags.setExtensionHandlingBehavior(diag::Severity::Error);
67 else if (Opts.Pedantic)
68 Diags.setExtensionHandlingBehavior(diag::Severity::Warning);
69 else
70 Diags.setExtensionHandlingBehavior(diag::Severity::Ignored);
71
72 SmallVector<diag::kind, 10> _Diags;
73 const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs =
74 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
77 // we follow the more canonical "last option wins" paradigm when there are
78 // conflicting options.
79 for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; ++Report) {
80 bool SetDiagnostic = (Report == 0);
81
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
87 for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
88 const auto Flavor = diag::Flavor::WarningOrError;
89 StringRef Opt = Opts.Warnings[i];
90 StringRef OrigOpt = Opts.Warnings[i];
91
92 // Treat -Wformat=0 as an alias for -Wno-format.
93 if (Opt == "format=0")
94 Opt = "no-format";
95
96 // 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);
102 }
103
104 // Figure out how this option affects the warning. If -Wfoo, map the
105 // diagnostic to a warning, if -Wno-foo, map it to ignore.
106 diag::Severity Mapping =
107 isPositive ? diag::Severity::Warning : diag::Severity::Ignored;
108
109 // -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);
114 continue;
115 }
116
117 // -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") {
120 if (SetDiagnostic) {
121 if (isPositive) {
122 Diags.setEnableAllWarnings(true);
123 } else {
124 Diags.setEnableAllWarnings(false);
125 Diags.setSeverityForAll(Flavor, diag::Severity::Ignored);
126 }
127 }
128 continue;
129 }
130
131 // -Werror/-Wno-error is a special case, not controlled by the option
132 // 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)
139 << "-Werror" << ("-W" + OrigOpt.str());
140 continue;
141 }
142 Specifier = Opt.substr(6);
143 }
144
145 if (Specifier.empty()) {
146 if (SetDiagnostic)
147 Diags.setWarningsAsErrors(isPositive);
148 continue;
149 }
150
151 if (SetDiagnostic) {
152 // Set the warning as error flag for this specifier.
153 Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive);
154 } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
155 EmitUnknownDiagWarning(Diags, Flavor, "-Werror=", Specifier);
156 }
157 continue;
158 }
159
160 // -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)
167 << "-Wfatal-errors" << ("-W" + OrigOpt.str());
168 continue;
169 }
170 Specifier = Opt.substr(13);
171 }
172
173 if (Specifier.empty()) {
174 if (SetDiagnostic)
175 Diags.setErrorsAsFatal(isPositive);
176 continue;
177 }
178
179 if (SetDiagnostic) {
180 // Set the error as fatal flag for this specifier.
181 Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive);
182 } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
183 EmitUnknownDiagWarning(Diags, Flavor, "-Wfatal-errors=", Specifier);
184 }
185 continue;
186 }
187
188 if (Report) {
189 if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
190 EmitUnknownDiagWarning(Diags, Flavor, isPositive ? "-W" : "-Wno-",
191 Opt);
192 } else {
193 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);
226 }
227 }
228 }
229 }
clang::diag::Flavor::WarningOrError
A diagnostic that indicates a problem or potential problem.
clang::DiagnosticsEngine::setSeverityForAll
void setSeverityForAll(diag::Flavor Flavor, diag::Severity Map, SourceLocation Loc=SourceLocation())
Add the specified mapping to all diagnostics of the specified flavor.
Definition: Diagnostic.cpp:459
clang::DiagnosticsEngine::setShowOverloads
void setShowOverloads(OverloadsShown Val)
Specify which overload candidates to show when overload resolution fails.
Definition: Diagnostic.h:663
clang::DiagnosticsEngine::Report
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1300
Specifier
const NestedNameSpecifier * Specifier
Definition: USRLocFinder.cpp:172
clang::DiagnosticsEngine::setDiagnosticGroupWarningAsError
bool setDiagnosticGroupWarningAsError(StringRef Group, bool Enabled)
Set the warning-as-error flag for the given diagnostic group.
Definition: Diagnostic.cpp:398
clang::diag::Severity
Severity
Enum values that allow the client to map NOTEs, WARNINGs, and EXTENSIONs to either Ignore (nothing)...
Definition: DiagnosticIDs.h:79
clang::DiagnosticsEngine::setTemplateBacktraceLimit
void setTemplateBacktraceLimit(unsigned Limit)
Specify the maximum number of template instantiation notes to emit along with a given diagnostic...
Definition: Diagnostic.h:569
AllDiagnostics.h
Includes all the separate Diagnostic headers & some related helpers.
clang::DiagnosticsEngine::setElideType
void setElideType(bool Val)
Set type eliding, to skip outputting same types occurring in template types.
Definition: Diagnostic.h:646
clang::DiagnosticsEngine::setDiagnosticGroupErrorAsFatal
bool setDiagnosticGroupErrorAsFatal(StringRef Group, bool Enabled)
Set the error-as-fatal flag for the given diagnostic group.
Definition: Diagnostic.cpp:429
clang::DiagnosticsEngine::setExtensionHandlingBehavior
void setExtensionHandlingBehavior(diag::Severity H)
Controls whether otherwise-unmapped extension diagnostics are mapped onto ignore/warning/error.
Definition: Diagnostic.h:691
clang::DiagnosticIDs::getNearestOption
static StringRef getNearestOption(diag::Flavor Flavor, StringRef Group)
Get the diagnostic option with the closest edit distance to the given group name. ...
Definition: DiagnosticIDs.cpp:596
clang::DiagnosticsEngine
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:149
clang::DiagnosticsEngine::setSeverityForGroup
bool setSeverityForGroup(diag::Flavor Flavor, StringRef Group, diag::Severity Map, SourceLocation Loc=SourceLocation())
Change an entire diagnostic group (e.g.
Definition: Diagnostic.cpp:383
Diagnostic.h
Defines the Diagnostic-related interfaces.
clang::diag::Severity::Error
Present this diagnostic as an error.
clang::DiagnosticOptions::Warnings
std::vector< std::string > Warnings
The list of -W...
Definition: DiagnosticOptions.h:99
clang::DiagnosticsEngine::setEnableAllWarnings
void setEnableAllWarnings(bool Val)
When set to true, any unmapped ignored warnings are no longer ignored.
Definition: Diagnostic.h:605
clang::DiagnosticsEngine::setSuppressSystemWarnings
void setSuppressSystemWarnings(bool Val)
When set to true mask warnings that come from system headers.
Definition: Diagnostic.h:631
llvm::IntrusiveRefCntPtr
Definition: LLVM.h:44
clang::DiagnosticOptions
Options for controlling the compiler diagnostics engine.
Definition: DiagnosticOptions.h:63
clang::ProcessWarningOptions
void ProcessWarningOptions(DiagnosticsEngine &Diags, const DiagnosticOptions &Opts, bool ReportDiags=true)
ProcessWarningOptions - Initialize the diagnostic client and process the warning options specified on...
Definition: Warnings.cpp:43
clang::DiagnosticOptions::Remarks
std::vector< std::string > Remarks
The list of -R...
Definition: DiagnosticOptions.h:103
llvm::SmallVector
Definition: LLVM.h:35
clang::diag::Flavor
Flavor
Flavors of diagnostics we can emit.
Definition: DiagnosticIDs.h:90
clang::DiagnosticsEngine::setErrorLimit
void setErrorLimit(unsigned Limit)
Specify a limit for the number of errors we should emit before giving up.
Definition: Diagnostic.h:565
clang::DiagnosticsEngine::setIgnoreAllWarnings
void setIgnoreAllWarnings(bool Val)
When set to true, any unmapped warnings are ignored.
Definition: Diagnostic.h:594
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:21
clang::diag::Severity::Remark
Present this diagnostic as a remark.
clang::DiagnosticsEngine::setErrorsAsFatal
void setErrorsAsFatal(bool Val)
When set to true, any error reported is made a fatal error.
Definition: Diagnostic.h:621
clang::DiagnosticsEngine::setPrintTemplateTree
void setPrintTemplateTree(bool Val)
Set tree printing, to outputting the template difference in a tree format.
Definition: Diagnostic.h:651
clang::DiagnosticsEngine::setShowColors
void setShowColors(bool Val)
Set color printing, so the type diffing will inject color markers into the output.
Definition: Diagnostic.h:656
clang::DiagnosticsEngine::setWarningsAsErrors
void setWarningsAsErrors(bool Val)
When set to true, any warnings reported are issued as errors.
Definition: Diagnostic.h:613
clang::DiagnosticsEngine::setConstexprBacktraceLimit
void setConstexprBacktraceLimit(unsigned Limit)
Specify the maximum number of constexpr evaluation notes to emit along with a given diagnostic...
Definition: Diagnostic.h:581
clang::DiagnosticsEngine::getDiagnosticIDs
const IntrusiveRefCntPtr< DiagnosticIDs > & getDiagnosticIDs() const
Definition: Diagnostic.h:501
clang::diag::Severity::Ignored
Do not present this diagnostic, ignore it.
clang::diag::Flavor::Remark
A diagnostic that indicates normal progress through compilation.
DiagnosticOptions.h
clang::diag::Severity::Warning
Present this diagnostic as a warning.
EmitUnknownDiagWarning
static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags, diag::Flavor Flavor, StringRef Prefix, StringRef Opt)
Definition: Warnings.cpp:34