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