• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "clang/Frontend/Utils.h"
24 #include "clang/Basic/Diagnostic.h"
25 #include "clang/Basic/DiagnosticOptions.h"
26 #include "clang/Frontend/FrontendDiagnostic.h"
27 #include "clang/Lex/LexDiagnostic.h"
28 #include "clang/Sema/SemaDiagnostic.h"
29 #include <algorithm>
30 #include <cstring>
31 #include <utility>
32 using namespace clang;
33 
34 // EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning
35 // opts
EmitUnknownDiagWarning(DiagnosticsEngine & Diags,StringRef Prefix,StringRef Opt,bool isPositive)36 static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags,
37                                   StringRef Prefix, StringRef Opt,
38                                   bool isPositive) {
39   StringRef Suggestion = DiagnosticIDs::getNearestWarningOption(Opt);
40   if (!Suggestion.empty())
41     Diags.Report(isPositive? diag::warn_unknown_warning_option_suggest :
42                              diag::warn_unknown_negative_warning_option_suggest)
43       << (Prefix.str() += Opt) << (Prefix.str() += Suggestion);
44   else
45     Diags.Report(isPositive? diag::warn_unknown_warning_option :
46                              diag::warn_unknown_negative_warning_option)
47       << (Prefix.str() += Opt);
48 }
49 
ProcessWarningOptions(DiagnosticsEngine & Diags,const DiagnosticOptions & Opts,bool ReportDiags)50 void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
51                                   const DiagnosticOptions &Opts,
52                                   bool ReportDiags) {
53   Diags.setSuppressSystemWarnings(true);  // Default to -Wno-system-headers
54   Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
55   Diags.setShowOverloads(Opts.getShowOverloads());
56 
57   Diags.setElideType(Opts.ElideType);
58   Diags.setPrintTemplateTree(Opts.ShowTemplateTree);
59   Diags.setWarnOnSpellCheck(Opts.WarnOnSpellCheck);
60   Diags.setShowColors(Opts.ShowColors);
61 
62   // Handle -ferror-limit
63   if (Opts.ErrorLimit)
64     Diags.setErrorLimit(Opts.ErrorLimit);
65   if (Opts.TemplateBacktraceLimit)
66     Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
67   if (Opts.ConstexprBacktraceLimit)
68     Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit);
69 
70   // If -pedantic or -pedantic-errors was specified, then we want to map all
71   // extension diagnostics onto WARNING or ERROR unless the user has futz'd
72   // around with them explicitly.
73   if (Opts.PedanticErrors)
74     Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Error);
75   else if (Opts.Pedantic)
76     Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Warn);
77   else
78     Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Ignore);
79 
80   SmallVector<diag::kind, 10> _Diags;
81   const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs =
82     Diags.getDiagnosticIDs();
83   // We parse the warning options twice.  The first pass sets diagnostic state,
84   // while the second pass reports warnings/errors.  This has the effect that
85   // we follow the more canonical "last option wins" paradigm when there are
86   // conflicting options.
87   for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; ++Report) {
88     bool SetDiagnostic = (Report == 0);
89 
90     // If we've set the diagnostic state and are not reporting diagnostics then
91     // we're done.
92     if (!SetDiagnostic && !ReportDiags)
93       break;
94 
95     for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
96       StringRef Opt = Opts.Warnings[i];
97       StringRef OrigOpt = Opts.Warnings[i];
98 
99       // Treat -Wformat=0 as an alias for -Wno-format.
100       if (Opt == "format=0")
101         Opt = "no-format";
102 
103       // Check to see if this warning starts with "no-", if so, this is a
104       // negative form of the option.
105       bool isPositive = true;
106       if (Opt.startswith("no-")) {
107         isPositive = false;
108         Opt = Opt.substr(3);
109       }
110 
111       // Figure out how this option affects the warning.  If -Wfoo, map the
112       // diagnostic to a warning, if -Wno-foo, map it to ignore.
113       diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE;
114 
115       // -Wsystem-headers is a special case, not driven by the option table.  It
116       // cannot be controlled with -Werror.
117       if (Opt == "system-headers") {
118         if (SetDiagnostic)
119           Diags.setSuppressSystemWarnings(!isPositive);
120         continue;
121       }
122 
123       // -Weverything is a special case as well.  It implicitly enables all
124       // warnings, including ones not explicitly in a warning group.
125       if (Opt == "everything") {
126         if (SetDiagnostic) {
127           if (isPositive) {
128             Diags.setEnableAllWarnings(true);
129           } else {
130             Diags.setEnableAllWarnings(false);
131             Diags.setMappingToAllDiagnostics(diag::MAP_IGNORE);
132           }
133         }
134         continue;
135       }
136 
137       // -Werror/-Wno-error is a special case, not controlled by the option
138       // table. It also has the "specifier" form of -Werror=foo and -Werror-foo.
139       if (Opt.startswith("error")) {
140         StringRef Specifier;
141         if (Opt.size() > 5) {  // Specifier must be present.
142           if ((Opt[5] != '=' && Opt[5] != '-') || Opt.size() == 6) {
143             if (Report)
144               Diags.Report(diag::warn_unknown_warning_specifier)
145                 << "-Werror" << ("-W" + OrigOpt.str());
146             continue;
147           }
148           Specifier = Opt.substr(6);
149         }
150 
151         if (Specifier.empty()) {
152           if (SetDiagnostic)
153             Diags.setWarningsAsErrors(isPositive);
154           continue;
155         }
156 
157         if (SetDiagnostic) {
158           // Set the warning as error flag for this specifier.
159           Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive);
160         } else if (DiagIDs->getDiagnosticsInGroup(Specifier, _Diags)) {
161           EmitUnknownDiagWarning(Diags, "-Werror=", Specifier, isPositive);
162         }
163         continue;
164       }
165 
166       // -Wfatal-errors is yet another special case.
167       if (Opt.startswith("fatal-errors")) {
168         StringRef Specifier;
169         if (Opt.size() != 12) {
170           if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) {
171             if (Report)
172               Diags.Report(diag::warn_unknown_warning_specifier)
173                 << "-Wfatal-errors" << ("-W" + OrigOpt.str());
174             continue;
175           }
176           Specifier = Opt.substr(13);
177         }
178 
179         if (Specifier.empty()) {
180           if (SetDiagnostic)
181             Diags.setErrorsAsFatal(isPositive);
182           continue;
183         }
184 
185         if (SetDiagnostic) {
186           // Set the error as fatal flag for this specifier.
187           Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive);
188         } else if (DiagIDs->getDiagnosticsInGroup(Specifier, _Diags)) {
189           EmitUnknownDiagWarning(Diags, "-Wfatal-errors=", Specifier,
190                                  isPositive);
191         }
192         continue;
193       }
194 
195       if (Report) {
196         if (DiagIDs->getDiagnosticsInGroup(Opt, _Diags))
197           EmitUnknownDiagWarning(Diags, isPositive ? "-W" : "-Wno-", Opt,
198                                  isPositive);
199       } else {
200         Diags.setDiagnosticGroupMapping(Opt, Mapping);
201       }
202     }
203   }
204 }
205