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/DiagnosticDriver.h"
27#include "clang/Basic/DiagnosticIDs.h"
28#include "clang/Basic/DiagnosticOptions.h"
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
36static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags,
37 diag::Flavor Flavor, StringRef Prefix,
38 StringRef Opt) {
39 StringRef Suggestion = DiagnosticIDs::getNearestOption(Flavor, Group: 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
46void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
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 SmallVector<diag::kind, 10> _Diags;
77 const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs =
78 Diags.getDiagnosticIDs();
79 // We parse the warning options twice. The first pass sets diagnostic state,
80 // while the second pass reports warnings/errors. This has the effect that
81 // we follow the more canonical "last option wins" paradigm when there are
82 // conflicting options.
83 for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; ++Report) {
84 bool SetDiagnostic = (Report == 0);
85
86 // If we've set the diagnostic state and are not reporting diagnostics then
87 // we're done.
88 if (!SetDiagnostic && !ReportDiags)
89 break;
90
91 for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
92 const auto Flavor = diag::Flavor::WarningOrError;
93 StringRef Opt = Opts.Warnings[i];
94 StringRef OrigOpt = Opts.Warnings[i];
95
96 // Treat -Wformat=0 as an alias for -Wno-format.
97 if (Opt == "format=0")
98 Opt = "no-format";
99
100 // Check to see if this warning starts with "no-", if so, this is a
101 // negative form of the option.
102 bool isPositive = !Opt.consume_front(Prefix: "no-");
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, Map: 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. GCC supports
133 // the deprecated -Werror-implicit-function-declaration which is used by
134 // a few projects.
135 if (Opt.starts_with(Prefix: "error")) {
136 StringRef Specifier;
137 if (Opt.size() > 5) { // Specifier must be present.
138 if (Opt[5] != '=' &&
139 Opt.substr(Start: 5) != "-implicit-function-declaration") {
140 if (Report)
141 Diags.Report(diag::warn_unknown_warning_specifier)
142 << "-Werror" << ("-W" + OrigOpt.str());
143 continue;
144 }
145 Specifier = Opt.substr(Start: 6);
146 }
147
148 if (Specifier.empty()) {
149 if (SetDiagnostic)
150 Diags.setWarningsAsErrors(isPositive);
151 continue;
152 }
153
154 if (SetDiagnostic) {
155 // Set the warning as error flag for this specifier.
156 Diags.setDiagnosticGroupWarningAsError(Group: Specifier, Enabled: isPositive);
157 } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Group: Specifier, Diags&: _Diags)) {
158 EmitUnknownDiagWarning(Diags, Flavor, Prefix: "-Werror=", Opt: Specifier);
159 }
160 continue;
161 }
162
163 // -Wfatal-errors is yet another special case.
164 if (Opt.starts_with(Prefix: "fatal-errors")) {
165 StringRef Specifier;
166 if (Opt.size() != 12) {
167 if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) {
168 if (Report)
169 Diags.Report(diag::warn_unknown_warning_specifier)
170 << "-Wfatal-errors" << ("-W" + OrigOpt.str());
171 continue;
172 }
173 Specifier = Opt.substr(Start: 13);
174 }
175
176 if (Specifier.empty()) {
177 if (SetDiagnostic)
178 Diags.setErrorsAsFatal(isPositive);
179 continue;
180 }
181
182 if (SetDiagnostic) {
183 // Set the error as fatal flag for this specifier.
184 Diags.setDiagnosticGroupErrorAsFatal(Group: Specifier, Enabled: isPositive);
185 } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Group: Specifier, Diags&: _Diags)) {
186 EmitUnknownDiagWarning(Diags, Flavor, Prefix: "-Wfatal-errors=", Opt: Specifier);
187 }
188 continue;
189 }
190
191 if (Report) {
192 if (DiagIDs->getDiagnosticsInGroup(Flavor, Group: Opt, Diags&: _Diags))
193 EmitUnknownDiagWarning(Diags, Flavor, Prefix: isPositive ? "-W" : "-Wno-",
194 Opt);
195 } else {
196 Diags.setSeverityForGroup(Flavor, Group: Opt, Map: Mapping);
197 }
198 }
199
200 for (StringRef Opt : Opts.Remarks) {
201 const auto Flavor = diag::Flavor::Remark;
202
203 // Check to see if this warning starts with "no-", if so, this is a
204 // negative form of the option.
205 bool IsPositive = !Opt.consume_front(Prefix: "no-");
206
207 auto Severity = IsPositive ? diag::Severity::Remark
208 : diag::Severity::Ignored;
209
210 // -Reverything sets the state of all remarks. Note that all remarks are
211 // in remark groups, so we don't need a separate 'all remarks enabled'
212 // flag.
213 if (Opt == "everything") {
214 if (SetDiagnostic)
215 Diags.setSeverityForAll(Flavor, Map: Severity);
216 continue;
217 }
218
219 if (Report) {
220 if (DiagIDs->getDiagnosticsInGroup(Flavor, Group: Opt, Diags&: _Diags))
221 EmitUnknownDiagWarning(Diags, Flavor, Prefix: IsPositive ? "-R" : "-Rno-",
222 Opt);
223 } else {
224 Diags.setSeverityForGroup(Flavor, Group: Opt,
225 Map: IsPositive ? diag::Severity::Remark
226 : diag::Severity::Ignored);
227 }
228 }
229 }
230
231 // Process suppression mappings file after processing other warning flags
232 // (like -Wno-unknown-warning-option) as we can emit extra warnings during
233 // processing.
234 if (!Opts.DiagnosticSuppressionMappingsFile.empty()) {
235 if (auto FileContents =
236 VFS.getBufferForFile(Name: Opts.DiagnosticSuppressionMappingsFile)) {
237 Diags.setDiagSuppressionMapping(**FileContents);
238 } else if (ReportDiags) {
239 Diags.Report(diag::err_drv_no_such_file)
240 << Opts.DiagnosticSuppressionMappingsFile;
241 }
242 }
243}
244

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of clang/lib/Basic/Warnings.cpp