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