clang 24.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
30#include "llvm/ADT/StringRef.h"
31#include "llvm/Support/Process.h"
32#include "llvm/Support/VirtualFileSystem.h"
33
34using namespace clang;
35
36// EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning
37// opts
39 diag::Flavor Flavor, StringRef Prefix,
40 StringRef Opt) {
41 StringRef Suggestion = DiagnosticIDs::getNearestOption(Flavor, Opt);
42 Diags.Report(diag::warn_unknown_diag_option)
43 << (Flavor == diag::Flavor::WarningOrError ? 0 : 1)
44 << (Prefix.str() += std::string(Opt)) << !Suggestion.empty()
45 << (Prefix.str() += std::string(Suggestion));
46}
47
49 const DiagnosticOptions &Opts,
50 llvm::vfs::FileSystem &VFS,
51 bool ReportDiags) {
52 Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
53 Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
54 Diags.setShowOverloads(Opts.getShowOverloads());
55
56 Diags.setElideType(Opts.ElideType);
57 Diags.setPrintTemplateTree(Opts.ShowTemplateTree);
58 Diags.setShowColors(
59 Opts.showColors(llvm::sys::Process::StandardErrHasColors()));
60
61 // Handle -ferror-limit
62 if (Opts.ErrorLimit)
63 Diags.setErrorLimit(Opts.ErrorLimit);
64 if (Opts.TemplateBacktraceLimit)
65 Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
66 if (Opts.ConstexprBacktraceLimit)
67 Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit);
68
69 // If -pedantic or -pedantic-errors was specified, then we want to map all
70 // extension diagnostics onto WARNING or ERROR unless the user has futz'd
71 // around with them explicitly.
72 if (Opts.PedanticErrors)
74 else if (Opts.Pedantic)
76 else
78
81 Diags.getDiagnosticIDs();
82 // We parse the warning options twice. The first pass sets diagnostic state,
83 // while the second pass reports warnings/errors. This has the effect that
84 // we follow the more canonical "last option wins" paradigm when there are
85 // conflicting options.
86 for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; ++Report) {
87 bool SetDiagnostic = (Report == 0);
88
89 // If we've set the diagnostic state and are not reporting diagnostics then
90 // we're done.
91 if (!SetDiagnostic && !ReportDiags)
92 break;
93
94 for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
95 const auto Flavor = diag::Flavor::WarningOrError;
96 StringRef Opt = Opts.Warnings[i];
97 StringRef OrigOpt = Opts.Warnings[i];
98
99 // Treat -Wformat=0 as an alias for -Wno-format.
100 if (Opt == "format=0")
101 Opt = "no-format";
102
103 // Check to see if this warning starts with "no-", if so, this is a
104 // negative form of the option.
105 bool isPositive = !Opt.consume_front("no-");
106
107 // Figure out how this option affects the warning. If -Wfoo, map the
108 // diagnostic to a warning, if -Wno-foo, map it to ignore.
109 diag::Severity Mapping =
111
112 // -Wsystem-headers is a special case, not driven by the option table. It
113 // cannot be controlled with -Werror.
114 if (Opt == "system-headers") {
115 if (SetDiagnostic)
116 Diags.setSuppressSystemWarnings(!isPositive);
117 continue;
118 }
119
120 // -Weverything is a special case as well. It implicitly enables all
121 // warnings, including ones not explicitly in a warning group.
122 if (Opt == "everything") {
123 if (SetDiagnostic) {
124 if (isPositive) {
125 Diags.setEnableAllWarnings(true);
126 } else {
127 Diags.setEnableAllWarnings(false);
129 }
130 }
131 continue;
132 }
133
134 // -Werror/-Wno-error is a special case, not controlled by the option
135 // table. It also has the "specifier" form of -Werror=foo. GCC supports
136 // the deprecated -Werror-implicit-function-declaration which is used by
137 // a few projects.
138 if (Opt.starts_with("error")) {
139 StringRef Specifier;
140 if (Opt.size() > 5) { // Specifier must be present.
141 if (Opt[5] != '=' &&
142 Opt.substr(5) != "-implicit-function-declaration") {
143 if (Report)
144 Diags.Report(diag::warn_unknown_warning_specifier)
145 << "-Werror" << ("-W" + OrigOpt.str());
146 continue;
147 }
148 Specifier = Opt.substr(6);
149 }
150
151 if (Specifier.empty()) {
152 if (SetDiagnostic)
153 Diags.setWarningsAsErrors(isPositive);
154 continue;
155 }
156
157 if (SetDiagnostic) {
158 // Set the warning as error flag for this specifier.
159 Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive);
160 } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
162 Diags, Flavor,
163 isPositive ? "-Werror=" : "-Wno-error=", Specifier);
164 }
165 continue;
166 }
167
168 // -Wfatal-errors is yet another special case.
169 if (Opt.starts_with("fatal-errors")) {
170 StringRef Specifier;
171 if (Opt.size() != 12) {
172 if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) {
173 if (Report)
174 Diags.Report(diag::warn_unknown_warning_specifier)
175 << "-Wfatal-errors" << ("-W" + OrigOpt.str());
176 continue;
177 }
178 Specifier = Opt.substr(13);
179 }
180
181 if (Specifier.empty()) {
182 if (SetDiagnostic)
183 Diags.setErrorsAsFatal(isPositive);
184 continue;
185 }
186
187 if (SetDiagnostic) {
188 // Set the error as fatal flag for this specifier.
189 Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive);
190 } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
192 Diags, Flavor,
193 isPositive ? "-Wfatal-errors=" : "-Wno-fatal-errors=", Specifier);
194 }
195 continue;
196 }
197
198 if (Report) {
199 if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
200 EmitUnknownDiagWarning(Diags, Flavor, isPositive ? "-W" : "-Wno-",
201 Opt);
202 } else {
203 Diags.setSeverityForGroup(Flavor, Opt, Mapping);
204 }
205 }
206
207 for (StringRef Opt : Opts.Remarks) {
208 const auto Flavor = diag::Flavor::Remark;
209
210 // Check to see if this warning starts with "no-", if so, this is a
211 // negative form of the option.
212 bool IsPositive = !Opt.consume_front("no-");
213
214 auto Severity = IsPositive ? diag::Severity::Remark
216
217 // -Reverything sets the state of all remarks. Note that all remarks are
218 // in remark groups, so we don't need a separate 'all remarks enabled'
219 // flag.
220 if (Opt == "everything") {
221 if (SetDiagnostic)
222 Diags.setSeverityForAll(Flavor, Severity);
223 continue;
224 }
225
226 if (Report) {
227 if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
228 EmitUnknownDiagWarning(Diags, Flavor, IsPositive ? "-R" : "-Rno-",
229 Opt);
230 } else {
231 Diags.setSeverityForGroup(Flavor, Opt,
232 IsPositive ? diag::Severity::Remark
234 }
235 }
236 }
237
238 // Process suppression mappings file after processing other warning flags
239 // (like -Wno-unknown-warning-option) as we can emit extra warnings during
240 // processing.
241 if (!Opts.DiagnosticSuppressionMappingsFile.empty()) {
242 if (auto FileContents =
243 VFS.getBufferForFile(Opts.DiagnosticSuppressionMappingsFile)) {
244 Diags.setDiagSuppressionMapping(**FileContents);
245 } else if (ReportDiags) {
246 Diags.Report(diag::err_drv_no_such_file)
248 }
249 }
250}
Includes all the separate Diagnostic headers & some related helpers.
Defines the Diagnostic-related interfaces.
Defines the Diagnostic IDs-related interfaces.
static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags, diag::Flavor Flavor, StringRef Prefix, StringRef Opt)
Definition Warnings.cpp:38
static StringRef getNearestOption(diag::Flavor Flavor, StringRef Group)
Get the diagnostic option with the closest edit distance to the given group name.
Options for controlling the compiler diagnostics engine.
std::string DiagnosticSuppressionMappingsFile
Path for the file that defines diagnostic suppression mappings.
std::vector< std::string > Remarks
The list of -R... options used to alter the diagnostic mappings, with the prefixes removed.
std::vector< std::string > Warnings
The list of -W... options used to alter the diagnostic mappings, with the prefixes removed.
bool showColors(bool StreamHasColors) const
Resolve the color mode against a stream's capability.
Concrete class used by the front-end to report problems and issues.
Definition Diagnostic.h:240
void setErrorsAsFatal(bool Val)
When set to true, any error reported is made a fatal error.
Definition Diagnostic.h:758
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
void setDiagSuppressionMapping(llvm::MemoryBuffer &Input)
Diagnostic suppression mappings can be used to suppress specific diagnostics in specific files.
void setPrintTemplateTree(bool Val)
Set tree printing, to outputting the template difference in a tree format.
Definition Diagnostic.h:791
void setSuppressSystemWarnings(bool Val)
When set to true mask warnings that come from system headers.
Definition Diagnostic.h:768
void setSeverityForAll(diag::Flavor Flavor, diag::Severity Map, SourceLocation Loc=SourceLocation())
Add the specified mapping to all diagnostics of the specified flavor.
void setIgnoreAllWarnings(bool Val)
When set to true, any unmapped warnings are ignored.
Definition Diagnostic.h:731
void setExtensionHandlingBehavior(diag::Severity H)
Controls whether otherwise-unmapped extension diagnostics are mapped onto ignore/warning/error.
Definition Diagnostic.h:858
void setTemplateBacktraceLimit(unsigned Limit)
Specify the maximum number of template instantiation notes to emit along with a given diagnostic.
Definition Diagnostic.h:708
void setErrorLimit(unsigned Limit)
Specify a limit for the number of errors we should emit before giving up.
Definition Diagnostic.h:704
void setWarningsAsErrors(bool Val)
When set to true, any warnings reported are issued as errors.
Definition Diagnostic.h:750
void setShowOverloads(OverloadsShown Val)
Specify which overload candidates to show when overload resolution fails.
Definition Diagnostic.h:803
void setEnableAllWarnings(bool Val)
When set to true, any unmapped ignored warnings are no longer ignored.
Definition Diagnostic.h:742
void setConstexprBacktraceLimit(unsigned Limit)
Specify the maximum number of constexpr evaluation notes to emit along with a given diagnostic.
Definition Diagnostic.h:718
bool setDiagnosticGroupErrorAsFatal(StringRef Group, bool Enabled)
Set the error-as-fatal flag for the given diagnostic group.
void setShowColors(bool Val)
Set color printing, so the type diffing will inject color markers into the output.
Definition Diagnostic.h:796
bool setDiagnosticGroupWarningAsError(StringRef Group, bool Enabled)
Set the warning-as-error flag for the given diagnostic group.
bool setSeverityForGroup(diag::Flavor Flavor, StringRef Group, diag::Severity Map, SourceLocation Loc=SourceLocation())
Change an entire diagnostic group (e.g.
void setElideType(bool Val)
Set type eliding, to skip outputting same types occurring in template types.
Definition Diagnostic.h:786
const IntrusiveRefCntPtr< DiagnosticIDs > & getDiagnosticIDs() const
Definition Diagnostic.h:640
Flavor
Flavors of diagnostics we can emit.
@ WarningOrError
A diagnostic that indicates a problem or potential problem.
@ Remark
A diagnostic that indicates normal progress through compilation.
Severity
Enum values that allow the client to map NOTEs, WARNINGs, and EXTENSIONs to either Ignore (nothing),...
@ Warning
Present this diagnostic as a warning.
@ Error
Present this diagnostic as an error.
@ Remark
Present this diagnostic as a remark.
@ Ignored
Do not present this diagnostic, ignore it.
Top level wrappers for InstallAPI frontend operations.
void ProcessWarningOptions(DiagnosticsEngine &Diags, const DiagnosticOptions &Opts, llvm::vfs::FileSystem &VFS, bool ReportDiags=true)
ProcessWarningOptions - Initialize the diagnostic client and process the warning options specified on...
Definition Warnings.cpp:48