clang 8.0.0

Warnings.cpp
Go to the documentation of this file.
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 StringRef Opt) {
38 StringRef Suggestion = DiagnosticIDs::getNearestOption(Flavor, Opt);
39 Diags.Report(diag::warn_unknown_diag_option)
40 << (Flavor == diag::Flavor::WarningOrError ? 0 : 1) << (Prefix.str() += Opt)
41 << !Suggestion.empty() << (Prefix.str() += Suggestion);
42 }
43
44 void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
45 const DiagnosticOptions &Opts,
46 bool ReportDiags) {
47 Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
48 Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
49 Diags.setShowOverloads(Opts.getShowOverloads());
50
51 Diags.setElideType(Opts.ElideType);
52 Diags.setPrintTemplateTree(Opts.ShowTemplateTree);
53 Diags.setShowColors(Opts.ShowColors);
54
55 // Handle -ferror-limit
56 if (Opts.ErrorLimit)
57 Diags.setErrorLimit(Opts.ErrorLimit);
58 if (Opts.TemplateBacktraceLimit)
59 Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
60 if (Opts.ConstexprBacktraceLimit)
61 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 if (Opts.PedanticErrors)
67 Diags.setExtensionHandlingBehavior(diag::Severity::Error);
68 else if (Opts.Pedantic)
69 Diags.setExtensionHandlingBehavior(diag::Severity::Warning);
70 else
71 Diags.setExtensionHandlingBehavior(diag::Severity::Ignored);
72
73 SmallVector<diag::kind, 10> _Diags;
74 const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs =
75 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 for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; ++Report) {
81 bool SetDiagnostic = (Report == 0);
82
83 // If we've set the diagnostic state and are not reporting diagnostics then
84 // we're done.
85 if (!SetDiagnostic && !ReportDiags)
86 break;
87
88 for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
89 const auto Flavor = diag::Flavor::WarningOrError;
90 StringRef Opt = Opts.Warnings[i];
91 StringRef OrigOpt = Opts.Warnings[i];
92
93 // Treat -Wformat=0 as an alias for -Wno-format.
94 if (Opt == "format=0")
95 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 bool isPositive = true;
100 if (Opt.startswith("no-")) {
101 isPositive = false;
102 Opt = Opt.substr(3);
103 }
104
105 // Figure out how this option affects the warning. If -Wfoo, map the
106 // diagnostic to a warning, if -Wno-foo, map it to ignore.
107 diag::Severity Mapping =
108 isPositive ? diag::Severity::Warning : diag::Severity::Ignored;
109
110 // -Wsystem-headers is a special case, not driven by the option table. It
111 // cannot be controlled with -Werror.
112 if (Opt == "system-headers") {
113 if (SetDiagnostic)
114 Diags.setSuppressSystemWarnings(!isPositive);
115 continue;
116 }
117
118 // -Weverything is a special case as well. It implicitly enables all
119 // warnings, including ones not explicitly in a warning group.
120 if (Opt == "everything") {
121 if (SetDiagnostic) {
122 if (isPositive) {
123 Diags.setEnableAllWarnings(true);
124 } else {
125 Diags.setEnableAllWarnings(false);
126 Diags.setSeverityForAll(Flavor, diag::Severity::Ignored);
127 }
128 }
129 continue;
130 }
131
132 // -Werror/-Wno-error is a special case, not controlled by the option
133 // table. It also has the "specifier" form of -Werror=foo and -Werror-foo.
134 if (Opt.startswith("error")) {
135 StringRef Specifier;
136 if (Opt.size() > 5) { // Specifier must be present.
137 if ((Opt[5] != '=' && Opt[5] != '-') || Opt.size() == 6) {
138 if (Report)
139 Diags.Report(diag::warn_unknown_warning_specifier)
140 << "-Werror" << ("-W" + OrigOpt.str());
141 continue;
142 }
143 Specifier = Opt.substr(6);
144 }
145
146 if (Specifier.empty()) {
147 if (SetDiagnostic)
148 Diags.setWarningsAsErrors(isPositive);
149 continue;
150 }
151
152 if (SetDiagnostic) {
153 // Set the warning as error flag for this specifier.
154 Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive);
155 } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
156 EmitUnknownDiagWarning(Diags, Flavor, "-Werror=", Specifier);
157 }
158 continue;
159 }
160
161 // -Wfatal-errors is yet another special case.
162 if (Opt.startswith("fatal-errors")) {
163 StringRef Specifier;
164 if (Opt.size() != 12) {
165 if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) {
166 if (Report)
167 Diags.Report(diag::warn_unknown_warning_specifier)
168 << "-Wfatal-errors" << ("-W" + OrigOpt.str());
169 continue;
170 }
171 Specifier = Opt.substr(13);
172 }
173
174 if (Specifier.empty()) {
175 if (SetDiagnostic)
176 Diags.setErrorsAsFatal(isPositive);
177 continue;
178 }
179
180 if (SetDiagnostic) {
181 // Set the error as fatal flag for this specifier.
182 Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive);
183 } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
184 EmitUnknownDiagWarning(Diags, Flavor, "-Wfatal-errors=", Specifier);
185 }
186 continue;
187 }
188
189 if (Report) {
190 if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
191 EmitUnknownDiagWarning(Diags, Flavor, isPositive ? "-W" : "-Wno-",
192 Opt);
193 } else {
194 Diags.setSeverityForGroup(Flavor, Opt, Mapping);
195 }
196 }
197
198 for (unsigned i = 0, e = Opts.Remarks.size(); i != e; ++i) {
199 StringRef Opt = Opts.Remarks[i];
200 const auto Flavor = diag::Flavor::Remark;
201
202 // Check to see if this warning starts with "no-", if so, this is a
203 // negative form of the option.
204 bool IsPositive = !Opt.startswith("no-");
205 if (!IsPositive) Opt = Opt.substr(3);
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, Severity);
216 continue;
217 }
218
219 if (Report) {
220 if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
221 EmitUnknownDiagWarning(Diags, Flavor, IsPositive ? "-R" : "-Rno-",
222 Opt);
223 } else {
224 Diags.setSeverityForGroup(Flavor, Opt,
225 IsPositive ? diag::Severity::Remark
226 : diag::Severity::Ignored);
227 }
228 }
229 }
230 }
clang::diag::Flavor::WarningOrError
A diagnostic that indicates a problem or potential problem.
clang::DiagnosticsEngine::setSeverityForAll
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:460
clang::DiagnosticsEngine::setShowColors
void setShowColors(bool Val=false)
Set color printing, so the type diffing will inject color markers into the output.
Definition: Diagnostic.h:650
clang::DiagnosticsEngine::setShowOverloads
void setShowOverloads(OverloadsShown Val)
Specify which overload candidates to show when overload resolution fails.
Definition: Diagnostic.h:657
clang::DiagnosticsEngine::Report
DiagnosticBuilder Report(SourceLocation Loc, unsigned DiagID)
Issue the message to the client.
Definition: Diagnostic.h:1295
Specifier
const NestedNameSpecifier * Specifier
Definition: USRLocFinder.cpp:173
clang::DiagnosticsEngine::setDiagnosticGroupWarningAsError
bool setDiagnosticGroupWarningAsError(StringRef Group, bool Enabled)
Set the warning-as-error flag for the given diagnostic group.
Definition: Diagnostic.cpp:399
clang::diag::Severity
Severity
Enum values that allow the client to map NOTEs, WARNINGs, and EXTENSIONs to either Ignore (nothing)...
Definition: DiagnosticIDs.h:80
clang::DiagnosticsEngine::setTemplateBacktraceLimit
void setTemplateBacktraceLimit(unsigned Limit)
Specify the maximum number of template instantiation notes to emit along with a given diagnostic...
Definition: Diagnostic.h:563
AllDiagnostics.h
Includes all the separate Diagnostic headers & some related helpers.
clang::DiagnosticsEngine::setDiagnosticGroupErrorAsFatal
bool setDiagnosticGroupErrorAsFatal(StringRef Group, bool Enabled)
Set the error-as-fatal flag for the given diagnostic group.
Definition: Diagnostic.cpp:430
clang::DiagnosticsEngine::setExtensionHandlingBehavior
void setExtensionHandlingBehavior(diag::Severity H)
Controls whether otherwise-unmapped extension diagnostics are mapped onto ignore/warning/error.
Definition: Diagnostic.h:685
clang::DiagnosticsEngine::setPrintTemplateTree
void setPrintTemplateTree(bool Val=false)
Set tree printing, to outputting the template difference in a tree format.
Definition: Diagnostic.h:645
clang::DiagnosticIDs::getNearestOption
static StringRef getNearestOption(diag::Flavor Flavor, StringRef Group)
Get the diagnostic option with the closest edit distance to the given group name. ...
Definition: DiagnosticIDs.cpp:592
clang::DiagnosticsEngine
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:149
clang::DiagnosticsEngine::setSeverityForGroup
bool setSeverityForGroup(diag::Flavor Flavor, StringRef Group, diag::Severity Map, SourceLocation Loc=SourceLocation())
Change an entire diagnostic group (e.g.
Definition: Diagnostic.cpp:384
Diagnostic.h
Defines the Diagnostic-related interfaces.
clang::diag::Severity::Error
Present this diagnostic as an error.
clang::DiagnosticOptions::Warnings
std::vector< std::string > Warnings
The list of -W...
Definition: DiagnosticOptions.h:100
clang::DiagnosticsEngine::setElideType
void setElideType(bool Val=true)
Set type eliding, to skip outputting same types occurring in template types.
Definition: Diagnostic.h:640
clang::DiagnosticsEngine::setEnableAllWarnings
void setEnableAllWarnings(bool Val)
When set to true, any unmapped ignored warnings are no longer ignored.
Definition: Diagnostic.h:599
clang::DiagnosticsEngine::setSuppressSystemWarnings
void setSuppressSystemWarnings(bool Val)
When set to true mask warnings that come from system headers.
Definition: Diagnostic.h:623
llvm::IntrusiveRefCntPtr
Definition: LLVM.h:45
clang::DiagnosticOptions
Options for controlling the compiler diagnostics engine.
Definition: DiagnosticOptions.h:64
clang::ProcessWarningOptions
void ProcessWarningOptions(DiagnosticsEngine &Diags, const DiagnosticOptions &Opts, bool ReportDiags=true)
ProcessWarningOptions - Initialize the diagnostic client and process the warning options specified on...
Definition: Warnings.cpp:44
clang::DiagnosticOptions::Remarks
std::vector< std::string > Remarks
The list of -R...
Definition: DiagnosticOptions.h:104
llvm::SmallVector
Definition: LLVM.h:36
clang::diag::Flavor
Flavor
Flavors of diagnostics we can emit.
Definition: DiagnosticIDs.h:91
clang::DiagnosticsEngine::setErrorLimit
void setErrorLimit(unsigned Limit)
Specify a limit for the number of errors we should emit before giving up.
Definition: Diagnostic.h:559
clang::DiagnosticsEngine::setIgnoreAllWarnings
void setIgnoreAllWarnings(bool Val)
When set to true, any unmapped warnings are ignored.
Definition: Diagnostic.h:588
clang
Dataflow Directional Tag Classes.
Definition: CFGReachabilityAnalysis.h:22
clang::diag::Severity::Remark
Present this diagnostic as a remark.
clang::DiagnosticsEngine::setErrorsAsFatal
void setErrorsAsFatal(bool Val)
When set to true, any error reported is made a fatal error.
Definition: Diagnostic.h:615
clang::DiagnosticsEngine::setWarningsAsErrors
void setWarningsAsErrors(bool Val)
When set to true, any warnings reported are issued as errors.
Definition: Diagnostic.h:607
clang::DiagnosticsEngine::setConstexprBacktraceLimit
void setConstexprBacktraceLimit(unsigned Limit)
Specify the maximum number of constexpr evaluation notes to emit along with a given diagnostic...
Definition: Diagnostic.h:575
clang::DiagnosticsEngine::getDiagnosticIDs
const IntrusiveRefCntPtr< DiagnosticIDs > & getDiagnosticIDs() const
Definition: Diagnostic.h:495
clang::diag::Severity::Ignored
Do not present this diagnostic, ignore it.
clang::diag::Flavor::Remark
A diagnostic that indicates normal progress through compilation.
DiagnosticOptions.h
clang::diag::Severity::Warning
Present this diagnostic as a warning.
EmitUnknownDiagWarning
static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags, diag::Flavor Flavor, StringRef Prefix, StringRef Opt)
Definition: Warnings.cpp:35