/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/clang/lib/Basic/Warnings.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- Warnings.cpp - C-Language Front-end ------------------------------===// |
2 | | // |
3 | | // The LLVM Compiler Infrastructure |
4 | | // |
5 | | // This file is distributed under the University of Illinois Open Source |
6 | | // License. See LICENSE.TXT for details. |
7 | | // |
8 | | //===----------------------------------------------------------------------===// |
9 | | // |
10 | | // Command line warning options handler. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | // |
14 | | // This file is responsible for handling all warning options. This includes |
15 | | // a number of -Wfoo options and their variants, which are driven by TableGen- |
16 | | // generated data, and the special cases -pedantic, -pedantic-errors, -w, |
17 | | // -Werror and -Wfatal-errors. |
18 | | // |
19 | | // Each warning option controls any number of actual warnings. |
20 | | // Given a warning option 'foo', the following are valid: |
21 | | // -Wfoo, -Wno-foo, -Werror=foo, -Wfatal-errors=foo |
22 | | // |
23 | | // Remark options are also handled here, analogously, except that they are much |
24 | | // simpler because a remark can't be promoted to an error. |
25 | | #include "clang/Basic/AllDiagnostics.h" |
26 | | #include "clang/Basic/Diagnostic.h" |
27 | | #include "clang/Basic/DiagnosticOptions.h" |
28 | | #include <algorithm> |
29 | | #include <cstring> |
30 | | #include <utility> |
31 | | using namespace clang; |
32 | | |
33 | | // EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning |
34 | | // opts |
35 | | static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags, |
36 | | diag::Flavor Flavor, StringRef Prefix, |
37 | 15 | StringRef Opt) { |
38 | 15 | StringRef Suggestion = DiagnosticIDs::getNearestOption(Flavor, Opt); |
39 | 15 | Diags.Report(diag::warn_unknown_diag_option) |
40 | 15 | << (Flavor == diag::Flavor::WarningOrError ? 013 : 12 ) << (Prefix.str() += Opt) |
41 | 15 | << !Suggestion.empty() << (Prefix.str() += Suggestion); |
42 | 15 | } |
43 | | |
44 | | void clang::ProcessWarningOptions(DiagnosticsEngine &Diags, |
45 | | const DiagnosticOptions &Opts, |
46 | 71.0k | bool ReportDiags) { |
47 | 71.0k | Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers |
48 | 71.0k | Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings); |
49 | 71.0k | Diags.setShowOverloads(Opts.getShowOverloads()); |
50 | 71.0k | |
51 | 71.0k | Diags.setElideType(Opts.ElideType); |
52 | 71.0k | Diags.setPrintTemplateTree(Opts.ShowTemplateTree); |
53 | 71.0k | Diags.setShowColors(Opts.ShowColors); |
54 | 71.0k | |
55 | 71.0k | // Handle -ferror-limit |
56 | 71.0k | if (Opts.ErrorLimit) |
57 | 24.8k | Diags.setErrorLimit(Opts.ErrorLimit); |
58 | 71.0k | if (Opts.TemplateBacktraceLimit) |
59 | 71.0k | Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit); |
60 | 71.0k | if (Opts.ConstexprBacktraceLimit) |
61 | 71.0k | Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit); |
62 | 71.0k | |
63 | 71.0k | // If -pedantic or -pedantic-errors was specified, then we want to map all |
64 | 71.0k | // extension diagnostics onto WARNING or ERROR unless the user has futz'd |
65 | 71.0k | // around with them explicitly. |
66 | 71.0k | if (Opts.PedanticErrors) |
67 | 188 | Diags.setExtensionHandlingBehavior(diag::Severity::Error); |
68 | 70.9k | else if (70.9k Opts.Pedantic70.9k ) |
69 | 375 | Diags.setExtensionHandlingBehavior(diag::Severity::Warning); |
70 | 70.9k | else |
71 | 70.5k | Diags.setExtensionHandlingBehavior(diag::Severity::Ignored); |
72 | 71.0k | |
73 | 71.0k | SmallVector<diag::kind, 10> _Diags; |
74 | 71.0k | const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs = |
75 | 71.0k | Diags.getDiagnosticIDs(); |
76 | 71.0k | // We parse the warning options twice. The first pass sets diagnostic state, |
77 | 71.0k | // while the second pass reports warnings/errors. This has the effect that |
78 | 71.0k | // we follow the more canonical "last option wins" paradigm when there are |
79 | 71.0k | // conflicting options. |
80 | 183k | for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd183k ; ++Report112k ) { |
81 | 142k | bool SetDiagnostic = (Report == 0); |
82 | 142k | |
83 | 142k | // If we've set the diagnostic state and are not reporting diagnostics then |
84 | 142k | // we're done. |
85 | 142k | if (!SetDiagnostic && 142k !ReportDiags71.0k ) |
86 | 29.5k | break; |
87 | 112k | |
88 | 291k | for (unsigned i = 0, e = Opts.Warnings.size(); 112k i != e291k ; ++i178k ) { |
89 | 178k | const auto Flavor = diag::Flavor::WarningOrError; |
90 | 178k | StringRef Opt = Opts.Warnings[i]; |
91 | 178k | StringRef OrigOpt = Opts.Warnings[i]; |
92 | 178k | |
93 | 178k | // Treat -Wformat=0 as an alias for -Wno-format. |
94 | 178k | if (Opt == "format=0") |
95 | 2 | Opt = "no-format"; |
96 | 178k | |
97 | 178k | // Check to see if this warning starts with "no-", if so, this is a |
98 | 178k | // negative form of the option. |
99 | 178k | bool isPositive = true; |
100 | 178k | if (Opt.startswith("no-")178k ) { |
101 | 15.0k | isPositive = false; |
102 | 15.0k | Opt = Opt.substr(3); |
103 | 15.0k | } |
104 | 178k | |
105 | 178k | // Figure out how this option affects the warning. If -Wfoo, map the |
106 | 178k | // diagnostic to a warning, if -Wno-foo, map it to ignore. |
107 | 178k | diag::Severity Mapping = |
108 | 178k | isPositive ? diag::Severity::Warning163k : diag::Severity::Ignored15.0k ; |
109 | 178k | |
110 | 178k | // -Wsystem-headers is a special case, not driven by the option table. It |
111 | 178k | // cannot be controlled with -Werror. |
112 | 178k | if (Opt == "system-headers"178k ) { |
113 | 31 | if (SetDiagnostic) |
114 | 18 | Diags.setSuppressSystemWarnings(!isPositive); |
115 | 31 | continue; |
116 | 31 | } |
117 | 178k | |
118 | 178k | // -Weverything is a special case as well. It implicitly enables all |
119 | 178k | // warnings, including ones not explicitly in a warning group. |
120 | 178k | if (178k Opt == "everything"178k ) { |
121 | 64 | if (SetDiagnostic64 ) { |
122 | 34 | if (isPositive34 ) { |
123 | 18 | Diags.setEnableAllWarnings(true); |
124 | 34 | } else { |
125 | 16 | Diags.setEnableAllWarnings(false); |
126 | 16 | Diags.setSeverityForAll(Flavor, diag::Severity::Ignored); |
127 | 16 | } |
128 | 34 | } |
129 | 64 | continue; |
130 | 64 | } |
131 | 178k | |
132 | 178k | // -Werror/-Wno-error is a special case, not controlled by the option |
133 | 178k | // table. It also has the "specifier" form of -Werror=foo and -Werror-foo. |
134 | 178k | if (178k Opt.startswith("error")178k ) { |
135 | 105k | StringRef Specifier; |
136 | 105k | if (Opt.size() > 5105k ) { // Specifier must be present. |
137 | 104k | if ((Opt[5] != '=' && 104k Opt[5] != '-'10 ) || Opt.size() == 6104k ) { |
138 | 0 | if (Report) |
139 | 0 | Diags.Report(diag::warn_unknown_warning_specifier) |
140 | 0 | << "-Werror" << ("-W" + OrigOpt.str()); |
141 | 0 | continue; |
142 | 0 | } |
143 | 104k | Specifier = Opt.substr(6); |
144 | 104k | } |
145 | 105k | |
146 | 105k | if (105k Specifier.empty()105k ) { |
147 | 924 | if (SetDiagnostic) |
148 | 532 | Diags.setWarningsAsErrors(isPositive); |
149 | 924 | continue; |
150 | 924 | } |
151 | 104k | |
152 | 104k | if (104k SetDiagnostic104k ) { |
153 | 58.9k | // Set the warning as error flag for this specifier. |
154 | 58.9k | Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive); |
155 | 104k | } else if (45.7k DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)45.7k ) { |
156 | 0 | EmitUnknownDiagWarning(Diags, Flavor, "-Werror=", Specifier); |
157 | 0 | } |
158 | 105k | continue; |
159 | 105k | } |
160 | 73.1k | |
161 | 73.1k | // -Wfatal-errors is yet another special case. |
162 | 73.1k | if (73.1k Opt.startswith("fatal-errors")73.1k ) { |
163 | 6 | StringRef Specifier; |
164 | 6 | if (Opt.size() != 126 ) { |
165 | 2 | if ((Opt[12] != '=' && 2 Opt[12] != '-'0 ) || Opt.size() == 132 ) { |
166 | 0 | if (Report) |
167 | 0 | Diags.Report(diag::warn_unknown_warning_specifier) |
168 | 0 | << "-Wfatal-errors" << ("-W" + OrigOpt.str()); |
169 | 0 | continue; |
170 | 0 | } |
171 | 2 | Specifier = Opt.substr(13); |
172 | 2 | } |
173 | 6 | |
174 | 6 | if (6 Specifier.empty()6 ) { |
175 | 4 | if (SetDiagnostic) |
176 | 2 | Diags.setErrorsAsFatal(isPositive); |
177 | 4 | continue; |
178 | 4 | } |
179 | 2 | |
180 | 2 | if (2 SetDiagnostic2 ) { |
181 | 1 | // Set the error as fatal flag for this specifier. |
182 | 1 | Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive); |
183 | 2 | } else if (1 DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)1 ) { |
184 | 0 | EmitUnknownDiagWarning(Diags, Flavor, "-Wfatal-errors=", Specifier); |
185 | 0 | } |
186 | 6 | continue; |
187 | 6 | } |
188 | 73.1k | |
189 | 73.1k | if (73.1k Report73.1k ) { |
190 | 33.0k | if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags)) |
191 | 13 | EmitUnknownDiagWarning(Diags, Flavor, isPositive ? 13 "-W"11 : "-Wno-"2 , |
192 | 13 | Opt); |
193 | 73.1k | } else { |
194 | 40.0k | Diags.setSeverityForGroup(Flavor, Opt, Mapping); |
195 | 40.0k | } |
196 | 178k | } |
197 | 112k | |
198 | 112k | for (unsigned i = 0, e = Opts.Remarks.size(); i != e112k ; ++i318 ) { |
199 | 318 | StringRef Opt = Opts.Remarks[i]; |
200 | 318 | const auto Flavor = diag::Flavor::Remark; |
201 | 318 | |
202 | 318 | // Check to see if this warning starts with "no-", if so, this is a |
203 | 318 | // negative form of the option. |
204 | 318 | bool IsPositive = !Opt.startswith("no-"); |
205 | 318 | if (!IsPositive318 ) Opt = Opt.substr(3)13 ; |
206 | 318 | |
207 | 305 | auto Severity = IsPositive ? diag::Severity::Remark |
208 | 13 | : diag::Severity::Ignored; |
209 | 318 | |
210 | 318 | // -Reverything sets the state of all remarks. Note that all remarks are |
211 | 318 | // in remark groups, so we don't need a separate 'all remarks enabled' |
212 | 318 | // flag. |
213 | 318 | if (Opt == "everything"318 ) { |
214 | 24 | if (SetDiagnostic) |
215 | 15 | Diags.setSeverityForAll(Flavor, Severity); |
216 | 24 | continue; |
217 | 24 | } |
218 | 294 | |
219 | 294 | if (294 Report294 ) { |
220 | 121 | if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags)) |
221 | 2 | EmitUnknownDiagWarning(Diags, Flavor, IsPositive ? 2 "-R"1 : "-Rno-"1 , |
222 | 2 | Opt); |
223 | 294 | } else { |
224 | 173 | Diags.setSeverityForGroup(Flavor, Opt, |
225 | 171 | IsPositive ? diag::Severity::Remark |
226 | 2 | : diag::Severity::Ignored); |
227 | 173 | } |
228 | 318 | } |
229 | 142k | } |
230 | 71.0k | } |