/src/llvm-project/clang/lib/Basic/Warnings.cpp
Line | Count | Source (jump to first uncovered line) |
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 | 0 | StringRef Opt) { |
37 | 0 | StringRef Suggestion = DiagnosticIDs::getNearestOption(Flavor, Opt); |
38 | 0 | Diags.Report(diag::warn_unknown_diag_option) |
39 | 0 | << (Flavor == diag::Flavor::WarningOrError ? 0 : 1) |
40 | 0 | << (Prefix.str() += std::string(Opt)) << !Suggestion.empty() |
41 | 0 | << (Prefix.str() += std::string(Suggestion)); |
42 | 0 | } |
43 | | |
44 | | void clang::ProcessWarningOptions(DiagnosticsEngine &Diags, |
45 | | const DiagnosticOptions &Opts, |
46 | 46 | bool ReportDiags) { |
47 | 46 | Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers |
48 | 46 | Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings); |
49 | 46 | Diags.setShowOverloads(Opts.getShowOverloads()); |
50 | | |
51 | 46 | Diags.setElideType(Opts.ElideType); |
52 | 46 | Diags.setPrintTemplateTree(Opts.ShowTemplateTree); |
53 | 46 | Diags.setShowColors(Opts.ShowColors); |
54 | | |
55 | | // Handle -ferror-limit |
56 | 46 | if (Opts.ErrorLimit) |
57 | 0 | Diags.setErrorLimit(Opts.ErrorLimit); |
58 | 46 | if (Opts.TemplateBacktraceLimit) |
59 | 46 | Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit); |
60 | 46 | if (Opts.ConstexprBacktraceLimit) |
61 | 46 | Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit); |
62 | | |
63 | | // If -pedantic or -pedantic-errors was specified, then we want to map all |
64 | | // extension diagnostics onto WARNING or ERROR unless the user has futz'd |
65 | | // around with them explicitly. |
66 | 46 | if (Opts.PedanticErrors) |
67 | 0 | Diags.setExtensionHandlingBehavior(diag::Severity::Error); |
68 | 46 | else if (Opts.Pedantic) |
69 | 0 | Diags.setExtensionHandlingBehavior(diag::Severity::Warning); |
70 | 46 | else |
71 | 46 | Diags.setExtensionHandlingBehavior(diag::Severity::Ignored); |
72 | | |
73 | 46 | SmallVector<diag::kind, 10> _Diags; |
74 | 46 | const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs = |
75 | 46 | Diags.getDiagnosticIDs(); |
76 | | // We parse the warning options twice. The first pass sets diagnostic state, |
77 | | // while the second pass reports warnings/errors. This has the effect that |
78 | | // we follow the more canonical "last option wins" paradigm when there are |
79 | | // conflicting options. |
80 | 138 | for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; ++Report) { |
81 | 92 | bool SetDiagnostic = (Report == 0); |
82 | | |
83 | | // If we've set the diagnostic state and are not reporting diagnostics then |
84 | | // we're done. |
85 | 92 | if (!SetDiagnostic && !ReportDiags) |
86 | 0 | break; |
87 | | |
88 | 92 | for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) { |
89 | 0 | const auto Flavor = diag::Flavor::WarningOrError; |
90 | 0 | StringRef Opt = Opts.Warnings[i]; |
91 | 0 | StringRef OrigOpt = Opts.Warnings[i]; |
92 | | |
93 | | // Treat -Wformat=0 as an alias for -Wno-format. |
94 | 0 | if (Opt == "format=0") |
95 | 0 | Opt = "no-format"; |
96 | | |
97 | | // Check to see if this warning starts with "no-", if so, this is a |
98 | | // negative form of the option. |
99 | 0 | bool isPositive = !Opt.consume_front("no-"); |
100 | | |
101 | | // Figure out how this option affects the warning. If -Wfoo, map the |
102 | | // diagnostic to a warning, if -Wno-foo, map it to ignore. |
103 | 0 | diag::Severity Mapping = |
104 | 0 | isPositive ? diag::Severity::Warning : diag::Severity::Ignored; |
105 | | |
106 | | // -Wsystem-headers is a special case, not driven by the option table. It |
107 | | // cannot be controlled with -Werror. |
108 | 0 | if (Opt == "system-headers") { |
109 | 0 | if (SetDiagnostic) |
110 | 0 | Diags.setSuppressSystemWarnings(!isPositive); |
111 | 0 | continue; |
112 | 0 | } |
113 | | |
114 | | // -Weverything is a special case as well. It implicitly enables all |
115 | | // warnings, including ones not explicitly in a warning group. |
116 | 0 | if (Opt == "everything") { |
117 | 0 | if (SetDiagnostic) { |
118 | 0 | if (isPositive) { |
119 | 0 | Diags.setEnableAllWarnings(true); |
120 | 0 | } else { |
121 | 0 | Diags.setEnableAllWarnings(false); |
122 | 0 | Diags.setSeverityForAll(Flavor, diag::Severity::Ignored); |
123 | 0 | } |
124 | 0 | } |
125 | 0 | continue; |
126 | 0 | } |
127 | | |
128 | | // -Werror/-Wno-error is a special case, not controlled by the option |
129 | | // table. It also has the "specifier" form of -Werror=foo. GCC supports |
130 | | // the deprecated -Werror-implicit-function-declaration which is used by |
131 | | // a few projects. |
132 | 0 | if (Opt.starts_with("error")) { |
133 | 0 | StringRef Specifier; |
134 | 0 | if (Opt.size() > 5) { // Specifier must be present. |
135 | 0 | if (Opt[5] != '=' && |
136 | 0 | Opt.substr(5) != "-implicit-function-declaration") { |
137 | 0 | if (Report) |
138 | 0 | Diags.Report(diag::warn_unknown_warning_specifier) |
139 | 0 | << "-Werror" << ("-W" + OrigOpt.str()); |
140 | 0 | continue; |
141 | 0 | } |
142 | 0 | Specifier = Opt.substr(6); |
143 | 0 | } |
144 | | |
145 | 0 | if (Specifier.empty()) { |
146 | 0 | if (SetDiagnostic) |
147 | 0 | Diags.setWarningsAsErrors(isPositive); |
148 | 0 | continue; |
149 | 0 | } |
150 | | |
151 | 0 | if (SetDiagnostic) { |
152 | | // Set the warning as error flag for this specifier. |
153 | 0 | Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive); |
154 | 0 | } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) { |
155 | 0 | EmitUnknownDiagWarning(Diags, Flavor, "-Werror=", Specifier); |
156 | 0 | } |
157 | 0 | continue; |
158 | 0 | } |
159 | | |
160 | | // -Wfatal-errors is yet another special case. |
161 | 0 | if (Opt.starts_with("fatal-errors")) { |
162 | 0 | StringRef Specifier; |
163 | 0 | if (Opt.size() != 12) { |
164 | 0 | if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) { |
165 | 0 | if (Report) |
166 | 0 | Diags.Report(diag::warn_unknown_warning_specifier) |
167 | 0 | << "-Wfatal-errors" << ("-W" + OrigOpt.str()); |
168 | 0 | continue; |
169 | 0 | } |
170 | 0 | Specifier = Opt.substr(13); |
171 | 0 | } |
172 | | |
173 | 0 | if (Specifier.empty()) { |
174 | 0 | if (SetDiagnostic) |
175 | 0 | Diags.setErrorsAsFatal(isPositive); |
176 | 0 | continue; |
177 | 0 | } |
178 | | |
179 | 0 | if (SetDiagnostic) { |
180 | | // Set the error as fatal flag for this specifier. |
181 | 0 | Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive); |
182 | 0 | } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) { |
183 | 0 | EmitUnknownDiagWarning(Diags, Flavor, "-Wfatal-errors=", Specifier); |
184 | 0 | } |
185 | 0 | continue; |
186 | 0 | } |
187 | | |
188 | 0 | if (Report) { |
189 | 0 | if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags)) |
190 | 0 | EmitUnknownDiagWarning(Diags, Flavor, isPositive ? "-W" : "-Wno-", |
191 | 0 | Opt); |
192 | 0 | } else { |
193 | 0 | Diags.setSeverityForGroup(Flavor, Opt, Mapping); |
194 | 0 | } |
195 | 0 | } |
196 | | |
197 | 92 | for (StringRef Opt : Opts.Remarks) { |
198 | 0 | const auto Flavor = diag::Flavor::Remark; |
199 | | |
200 | | // Check to see if this warning starts with "no-", if so, this is a |
201 | | // negative form of the option. |
202 | 0 | bool IsPositive = !Opt.consume_front("no-"); |
203 | |
|
204 | 0 | auto Severity = IsPositive ? diag::Severity::Remark |
205 | 0 | : diag::Severity::Ignored; |
206 | | |
207 | | // -Reverything sets the state of all remarks. Note that all remarks are |
208 | | // in remark groups, so we don't need a separate 'all remarks enabled' |
209 | | // flag. |
210 | 0 | if (Opt == "everything") { |
211 | 0 | if (SetDiagnostic) |
212 | 0 | Diags.setSeverityForAll(Flavor, Severity); |
213 | 0 | continue; |
214 | 0 | } |
215 | | |
216 | 0 | if (Report) { |
217 | 0 | if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags)) |
218 | 0 | EmitUnknownDiagWarning(Diags, Flavor, IsPositive ? "-R" : "-Rno-", |
219 | 0 | Opt); |
220 | 0 | } else { |
221 | 0 | Diags.setSeverityForGroup(Flavor, Opt, |
222 | 0 | IsPositive ? diag::Severity::Remark |
223 | 0 | : diag::Severity::Ignored); |
224 | 0 | } |
225 | 0 | } |
226 | 92 | } |
227 | 46 | } |