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