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#include "llvm/Support/raw_ostream.h"
34#include <cstring>
35
36using namespace clang;
37
38// EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning
39// opts
41 diag::Flavor Flavor, StringRef Prefix,
42 StringRef Opt) {
43 StringRef Suggestion = DiagnosticIDs::getNearestOption(Flavor, Opt);
44 Diags.Report(diag::warn_unknown_diag_option)
45 << (Flavor == diag::Flavor::WarningOrError ? 0 : 1)
46 << (Prefix.str() += std::string(Opt)) << !Suggestion.empty()
47 << (Prefix.str() += std::string(Suggestion));
48}
49
51 const DiagnosticOptions &Opts,
52 llvm::vfs::FileSystem &VFS,
53 bool ReportDiags) {
54 Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
55 Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
56 Diags.setShowOverloads(Opts.getShowOverloads());
57
58 Diags.setElideType(Opts.ElideType);
59 Diags.setPrintTemplateTree(Opts.ShowTemplateTree);
60 Diags.setShowColors(
61 Opts.showColors(llvm::sys::Process::StandardErrHasColors()));
62
63 // Handle -ferror-limit
64 if (Opts.ErrorLimit)
65 Diags.setErrorLimit(Opts.ErrorLimit);
66 if (Opts.TemplateBacktraceLimit)
67 Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
68 if (Opts.ConstexprBacktraceLimit)
69 Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit);
70
71 // If -pedantic or -pedantic-errors was specified, then we want to map all
72 // extension diagnostics onto WARNING or ERROR unless the user has futz'd
73 // around with them explicitly.
74 if (Opts.PedanticErrors)
76 else if (Opts.Pedantic)
78 else
80
83 Diags.getDiagnosticIDs();
84 // We parse the warning options twice. The first pass sets diagnostic state,
85 // while the second pass reports warnings/errors. This has the effect that
86 // we follow the more canonical "last option wins" paradigm when there are
87 // conflicting options.
88 for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; ++Report) {
89 bool SetDiagnostic = (Report == 0);
90
91 // If we've set the diagnostic state and are not reporting diagnostics then
92 // we're done.
93 if (!SetDiagnostic && !ReportDiags)
94 break;
95
96 for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
97 const auto Flavor = diag::Flavor::WarningOrError;
98 StringRef Opt = Opts.Warnings[i];
99 StringRef OrigOpt = Opts.Warnings[i];
100
101 // Treat -Wformat=0 as an alias for -Wno-format.
102 if (Opt == "format=0")
103 Opt = "no-format";
104
105 // Check to see if this warning starts with "no-", if so, this is a
106 // negative form of the option.
107 bool isPositive = !Opt.consume_front("no-");
108
109 // Figure out how this option affects the warning. If -Wfoo, map the
110 // diagnostic to a warning, if -Wno-foo, map it to ignore.
111 diag::Severity Mapping =
113
114 // -Wsystem-headers is a special case, not driven by the option table. It
115 // cannot be controlled with -Werror.
116 if (Opt == "system-headers") {
117 if (SetDiagnostic)
118 Diags.setSuppressSystemWarnings(!isPositive);
119 continue;
120 }
121
122 // -Weverything is a special case as well. It implicitly enables all
123 // warnings, including ones not explicitly in a warning group.
124 if (Opt == "everything") {
125 if (SetDiagnostic) {
126 if (isPositive) {
127 Diags.setEnableAllWarnings(true);
128 } else {
129 Diags.setEnableAllWarnings(false);
131 }
132 }
133 continue;
134 }
135
136 // -Werror/-Wno-error is a special case, not controlled by the option
137 // table. It also has the "specifier" form of -Werror=foo. GCC supports
138 // the deprecated -Werror-implicit-function-declaration which is used by
139 // a few projects.
140 if (Opt.starts_with("error")) {
141 StringRef Specifier;
142 if (Opt.size() > 5) { // Specifier must be present.
143 if (Opt[5] != '=' &&
144 Opt.substr(5) != "-implicit-function-declaration") {
145 if (Report)
146 Diags.Report(diag::warn_unknown_warning_specifier)
147 << "-Werror" << ("-W" + OrigOpt.str());
148 continue;
149 }
150 Specifier = Opt.substr(6);
151 }
152
153 if (Specifier.empty()) {
154 if (SetDiagnostic)
155 Diags.setWarningsAsErrors(isPositive);
156 continue;
157 }
158
159 if (SetDiagnostic) {
160 // Set the warning as error flag for this specifier.
161 Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive);
162 } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
163 EmitUnknownDiagWarning(Diags, Flavor, "-Werror=", 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)) {
191 EmitUnknownDiagWarning(Diags, Flavor, "-Wfatal-errors=", Specifier);
192 }
193 continue;
194 }
195
196 if (Report) {
197 if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
198 EmitUnknownDiagWarning(Diags, Flavor, isPositive ? "-W" : "-Wno-",
199 Opt);
200 } else {
201 Diags.setSeverityForGroup(Flavor, Opt, Mapping);
202 }
203 }
204
205 for (StringRef Opt : Opts.Remarks) {
206 const auto Flavor = diag::Flavor::Remark;
207
208 // Check to see if this warning starts with "no-", if so, this is a
209 // negative form of the option.
210 bool IsPositive = !Opt.consume_front("no-");
211
212 auto Severity = IsPositive ? diag::Severity::Remark
214
215 // -Reverything sets the state of all remarks. Note that all remarks are
216 // in remark groups, so we don't need a separate 'all remarks enabled'
217 // flag.
218 if (Opt == "everything") {
219 if (SetDiagnostic)
220 Diags.setSeverityForAll(Flavor, Severity);
221 continue;
222 }
223
224 if (Report) {
225 if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
226 EmitUnknownDiagWarning(Diags, Flavor, IsPositive ? "-R" : "-Rno-",
227 Opt);
228 } else {
229 Diags.setSeverityForGroup(Flavor, Opt,
230 IsPositive ? diag::Severity::Remark
232 }
233 }
234 }
235
236 // Process suppression mappings file after processing other warning flags
237 // (like -Wno-unknown-warning-option) as we can emit extra warnings during
238 // processing.
239 if (!Opts.DiagnosticSuppressionMappingsFile.empty()) {
240 if (auto FileContents =
241 VFS.getBufferForFile(Opts.DiagnosticSuppressionMappingsFile)) {
242 Diags.setDiagSuppressionMapping(**FileContents);
243 } else if (ReportDiags) {
244 Diags.Report(diag::err_drv_no_such_file)
246 }
247 }
248}
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:40
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:234
void setErrorsAsFatal(bool Val)
When set to true, any error reported is made a fatal error.
Definition Diagnostic.h:717
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:750
void setSuppressSystemWarnings(bool Val)
When set to true mask warnings that come from system headers.
Definition Diagnostic.h:727
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:690
void setExtensionHandlingBehavior(diag::Severity H)
Controls whether otherwise-unmapped extension diagnostics are mapped onto ignore/warning/error.
Definition Diagnostic.h:817
void setTemplateBacktraceLimit(unsigned Limit)
Specify the maximum number of template instantiation notes to emit along with a given diagnostic.
Definition Diagnostic.h:667
void setErrorLimit(unsigned Limit)
Specify a limit for the number of errors we should emit before giving up.
Definition Diagnostic.h:663
void setWarningsAsErrors(bool Val)
When set to true, any warnings reported are issued as errors.
Definition Diagnostic.h:709
void setShowOverloads(OverloadsShown Val)
Specify which overload candidates to show when overload resolution fails.
Definition Diagnostic.h:762
void setEnableAllWarnings(bool Val)
When set to true, any unmapped ignored warnings are no longer ignored.
Definition Diagnostic.h:701
void setConstexprBacktraceLimit(unsigned Limit)
Specify the maximum number of constexpr evaluation notes to emit along with a given diagnostic.
Definition Diagnostic.h:677
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:755
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:745
const IntrusiveRefCntPtr< DiagnosticIDs > & getDiagnosticIDs() const
Definition Diagnostic.h:599
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.
The JSON file list parser is used to communicate input to InstallAPI.
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:50