blob: b09e69aeca64b7b4e24ade650adcbfb1b6e0076f [file] [log] [blame]
Sebastian Redl63a9e0f2009-03-06 17:41:35 +00001//===--- 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-
Chris Lattnera3b089e2009-12-23 18:53:37 +000016// generated data, and the special cases -pedantic, -pedantic-errors, -w,
17// -Werror and -Wfatal-errors.
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000018//
Sebastian Redlc5613db2009-03-07 12:09:25 +000019// Each warning option controls any number of actual warnings.
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000020// Given a warning option 'foo', the following are valid:
Chris Lattnera3b089e2009-12-23 18:53:37 +000021// -Wfoo, -Wno-foo, -Werror=foo, -Wfatal-errors=foo
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000022//
Stephen Hines6bcf27b2014-05-29 04:14:42 -070023#include "clang/Basic/AllDiagnostics.h"
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000024#include "clang/Basic/Diagnostic.h"
Douglas Gregor02c23eb2012-10-23 22:26:28 +000025#include "clang/Basic/DiagnosticOptions.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000026#include <algorithm>
Eli Friedmanac8d6292009-05-19 04:30:57 +000027#include <cstring>
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000028#include <utility>
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000029using namespace clang;
30
Chad Rosier588e83b2012-01-31 19:31:12 +000031// EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning
32// opts
Chad Rosier99643d92011-11-15 19:03:03 +000033static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags,
Benjamin Kramerdce63272011-11-15 12:26:39 +000034 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
David Blaikied6471f72011-09-25 23:23:43 +000047void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
Chad Rosier08e79d22013-01-15 01:21:53 +000048 const DiagnosticOptions &Opts,
49 bool ReportDiags) {
Chris Lattner27ceb9d2009-04-15 07:01:18 +000050 Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
Daniel Dunbar69079432009-11-12 07:28:44 +000051 Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
Douglas Gregordc7b6412012-10-23 23:11:23 +000052 Diags.setShowOverloads(Opts.getShowOverloads());
Richard Trieu246b6aa2012-06-26 18:18:47 +000053
54 Diags.setElideType(Opts.ElideType);
55 Diags.setPrintTemplateTree(Opts.ShowTemplateTree);
56 Diags.setShowColors(Opts.ShowColors);
57
Chris Lattnerc1002142010-04-07 20:37:06 +000058 // Handle -ferror-limit
59 if (Opts.ErrorLimit)
60 Diags.setErrorLimit(Opts.ErrorLimit);
Douglas Gregor575cf372010-04-20 07:18:24 +000061 if (Opts.TemplateBacktraceLimit)
62 Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
Richard Smith08d6e032011-12-16 19:06:07 +000063 if (Opts.ConstexprBacktraceLimit)
64 Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000065
Chris Lattnerb54b2762009-04-16 05:04:32 +000066 // 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.
Daniel Dunbar69079432009-11-12 07:28:44 +000069 if (Opts.PedanticErrors)
David Blaikied6471f72011-09-25 23:23:43 +000070 Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Error);
Daniel Dunbar69079432009-11-12 07:28:44 +000071 else if (Opts.Pedantic)
David Blaikied6471f72011-09-25 23:23:43 +000072 Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Warn);
Chris Lattnerb54b2762009-04-16 05:04:32 +000073 else
David Blaikied6471f72011-09-25 23:23:43 +000074 Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Ignore);
Mike Stump1eb44332009-09-09 15:08:12 +000075
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +000076 SmallVector<diag::kind, 10> _Diags;
Dylan Noblesmithc93dc782012-02-20 14:00:23 +000077 const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs =
Chad Rosier05272a62011-11-03 21:23:39 +000078 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);
Chad Rosier08e79d22013-01-15 01:21:53 +000085
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
Chad Rosier05272a62011-11-03 21:23:39 +000091 for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
92 StringRef Opt = Opts.Warnings[i];
Chad Rosierff1affe2012-05-16 19:28:02 +000093 StringRef OrigOpt = Opts.Warnings[i];
Mike Stump1eb44332009-09-09 15:08:12 +000094
Hans Wennborgc8769462012-01-17 09:30:38 +000095 // Treat -Wformat=0 as an alias for -Wno-format.
96 if (Opt == "format=0")
97 Opt = "no-format";
98
Chad Rosier05272a62011-11-03 21:23:39 +000099 // 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);
Chris Lattner5147e8e2009-04-15 04:27:38 +0000105 }
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Chad Rosier05272a62011-11-03 21:23:39 +0000107 // 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::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE;
110
111 // -Wsystem-headers is a special case, not driven by the option table. It
112 // cannot be controlled with -Werror.
113 if (Opt == "system-headers") {
114 if (SetDiagnostic)
115 Diags.setSuppressSystemWarnings(!isPositive);
Chris Lattner5147e8e2009-04-15 04:27:38 +0000116 continue;
117 }
Chad Rosier05272a62011-11-03 21:23:39 +0000118
119 // -Weverything is a special case as well. It implicitly enables all
120 // warnings, including ones not explicitly in a warning group.
121 if (Opt == "everything") {
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000122 if (SetDiagnostic) {
123 if (isPositive) {
124 Diags.setEnableAllWarnings(true);
125 } else {
126 Diags.setEnableAllWarnings(false);
127 Diags.setMappingToAllDiagnostics(diag::MAP_IGNORE);
128 }
129 }
Chad Rosier05272a62011-11-03 21:23:39 +0000130 continue;
131 }
132
133 // -Werror/-Wno-error is a special case, not controlled by the option
134 // table. It also has the "specifier" form of -Werror=foo and -Werror-foo.
135 if (Opt.startswith("error")) {
136 StringRef Specifier;
137 if (Opt.size() > 5) { // Specifier must be present.
138 if ((Opt[5] != '=' && Opt[5] != '-') || Opt.size() == 6) {
139 if (Report)
140 Diags.Report(diag::warn_unknown_warning_specifier)
Chad Rosierff1affe2012-05-16 19:28:02 +0000141 << "-Werror" << ("-W" + OrigOpt.str());
Chad Rosier05272a62011-11-03 21:23:39 +0000142 continue;
143 }
144 Specifier = Opt.substr(6);
145 }
146
147 if (Specifier.empty()) {
148 if (SetDiagnostic)
149 Diags.setWarningsAsErrors(isPositive);
150 continue;
151 }
152
153 if (SetDiagnostic) {
154 // Set the warning as error flag for this specifier.
155 Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive);
156 } else if (DiagIDs->getDiagnosticsInGroup(Specifier, _Diags)) {
Benjamin Kramer4e969442011-11-28 22:17:09 +0000157 EmitUnknownDiagWarning(Diags, "-Werror=", Specifier, isPositive);
Chad Rosier05272a62011-11-03 21:23:39 +0000158 }
159 continue;
160 }
161
162 // -Wfatal-errors is yet another special case.
163 if (Opt.startswith("fatal-errors")) {
164 StringRef Specifier;
165 if (Opt.size() != 12) {
166 if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) {
167 if (Report)
168 Diags.Report(diag::warn_unknown_warning_specifier)
Chad Rosierff1affe2012-05-16 19:28:02 +0000169 << "-Wfatal-errors" << ("-W" + OrigOpt.str());
Chad Rosier05272a62011-11-03 21:23:39 +0000170 continue;
171 }
172 Specifier = Opt.substr(13);
173 }
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Chad Rosier05272a62011-11-03 21:23:39 +0000175 if (Specifier.empty()) {
176 if (SetDiagnostic)
177 Diags.setErrorsAsFatal(isPositive);
178 continue;
179 }
180
181 if (SetDiagnostic) {
182 // Set the error as fatal flag for this specifier.
183 Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive);
184 } else if (DiagIDs->getDiagnosticsInGroup(Specifier, _Diags)) {
Benjamin Kramer4e969442011-11-28 22:17:09 +0000185 EmitUnknownDiagWarning(Diags, "-Wfatal-errors=", Specifier,
186 isPositive);
Chad Rosier05272a62011-11-03 21:23:39 +0000187 }
188 continue;
189 }
190
Chad Rosier496cc8e2011-11-15 18:57:32 +0000191 if (Report) {
192 if (DiagIDs->getDiagnosticsInGroup(Opt, _Diags))
Chad Rosier6a1fd4a2012-05-16 19:42:03 +0000193 EmitUnknownDiagWarning(Diags, isPositive ? "-W" : "-Wno-", Opt,
194 isPositive);
Chad Rosier05272a62011-11-03 21:23:39 +0000195 } else {
196 Diags.setDiagnosticGroupMapping(Opt, Mapping);
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000197 }
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000198 }
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000199 }
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000200}