blob: a0a2164423c16e74a5f5f3747091b332d9e2f637 [file] [log] [blame]
Sebastian Redl48350102009-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 Lattnerb089c1d2009-12-23 18:53:37 +000016// generated data, and the special cases -pedantic, -pedantic-errors, -w,
17// -Werror and -Wfatal-errors.
Sebastian Redl48350102009-03-06 17:41:35 +000018//
Sebastian Redlecc54442009-03-07 12:09:25 +000019// Each warning option controls any number of actual warnings.
Sebastian Redl48350102009-03-06 17:41:35 +000020// Given a warning option 'foo', the following are valid:
Chris Lattnerb089c1d2009-12-23 18:53:37 +000021// -Wfoo, -Wno-foo, -Werror=foo, -Wfatal-errors=foo
Sebastian Redl48350102009-03-06 17:41:35 +000022//
Ben Langmuirb92de022014-04-29 16:25:26 +000023#include "clang/Basic/AllDiagnostics.h"
Sebastian Redl48350102009-03-06 17:41:35 +000024#include "clang/Basic/Diagnostic.h"
Douglas Gregor811db4e2012-10-23 22:26:28 +000025#include "clang/Basic/DiagnosticOptions.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000026#include <algorithm>
Eli Friedmancec35d72009-05-19 04:30:57 +000027#include <cstring>
Sebastian Redl48350102009-03-06 17:41:35 +000028#include <utility>
Sebastian Redl48350102009-03-06 17:41:35 +000029using namespace clang;
30
Chad Rosierebaefe72012-01-31 19:31:12 +000031// EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning
32// opts
Chad Rosier1fede192011-11-15 19:03:03 +000033static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags,
Benjamin Kramer176a5cb2011-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 Blaikie9c902b52011-09-25 23:23:43 +000047void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
Chad Rosier5f15a352013-01-15 01:21:53 +000048 const DiagnosticOptions &Opts,
49 bool ReportDiags) {
Chris Lattnere007de32009-04-15 07:01:18 +000050 Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
Daniel Dunbar4c0e8272009-11-12 07:28:44 +000051 Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
Douglas Gregor79591782012-10-23 23:11:23 +000052 Diags.setShowOverloads(Opts.getShowOverloads());
Richard Trieu91844232012-06-26 18:18:47 +000053
54 Diags.setElideType(Opts.ElideType);
55 Diags.setPrintTemplateTree(Opts.ShowTemplateTree);
56 Diags.setShowColors(Opts.ShowColors);
57
Chris Lattnerdec49e72010-04-07 20:37:06 +000058 // Handle -ferror-limit
59 if (Opts.ErrorLimit)
60 Diags.setErrorLimit(Opts.ErrorLimit);
Douglas Gregorffed1cb2010-04-20 07:18:24 +000061 if (Opts.TemplateBacktraceLimit)
62 Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
Richard Smithf6f003a2011-12-16 19:06:07 +000063 if (Opts.ConstexprBacktraceLimit)
64 Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit);
Sebastian Redl48350102009-03-06 17:41:35 +000065
Chris Lattnerb8e73152009-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 Dunbar4c0e8272009-11-12 07:28:44 +000069 if (Opts.PedanticErrors)
David Blaikie9c902b52011-09-25 23:23:43 +000070 Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Error);
Daniel Dunbar4c0e8272009-11-12 07:28:44 +000071 else if (Opts.Pedantic)
David Blaikie9c902b52011-09-25 23:23:43 +000072 Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Warn);
Chris Lattnerb8e73152009-04-16 05:04:32 +000073 else
David Blaikie9c902b52011-09-25 23:23:43 +000074 Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Ignore);
Mike Stump11289f42009-09-09 15:08:12 +000075
Dmitri Gribenkof8579502013-01-12 19:30:44 +000076 SmallVector<diag::kind, 10> _Diags;
Dylan Noblesmithc95d8192012-02-20 14:00:23 +000077 const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs =
Chad Rosier52d00e42011-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 Rosier5f15a352013-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 Rosier52d00e42011-11-03 21:23:39 +000091 for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
92 StringRef Opt = Opts.Warnings[i];
Chad Rosier286852a2012-05-16 19:28:02 +000093 StringRef OrigOpt = Opts.Warnings[i];
Mike Stump11289f42009-09-09 15:08:12 +000094
Hans Wennborg507b5672012-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 Rosier52d00e42011-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 Lattner4c73b672009-04-15 04:27:38 +0000105 }
Mike Stump11289f42009-09-09 15:08:12 +0000106
Chad Rosier52d00e42011-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.
Alp Tokerc726c362014-06-10 09:31:37 +0000109 diag::Severity Mapping =
Alp Toker46df1c02014-06-12 10:15:20 +0000110 isPositive ? diag::Severity::Warning : diag::Severity::Ignored;
Alp Tokerc726c362014-06-10 09:31:37 +0000111
Chad Rosier52d00e42011-11-03 21:23:39 +0000112 // -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);
Chris Lattner4c73b672009-04-15 04:27:38 +0000117 continue;
118 }
Chad Rosier52d00e42011-11-03 21:23:39 +0000119
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") {
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000123 if (SetDiagnostic) {
124 if (isPositive) {
125 Diags.setEnableAllWarnings(true);
126 } else {
127 Diags.setEnableAllWarnings(false);
Alp Toker46df1c02014-06-12 10:15:20 +0000128 Diags.setMappingForAllDiagnostics(diag::Severity::Ignored);
Argyrios Kyrtzidis9ffada92012-01-27 06:15:43 +0000129 }
130 }
Chad Rosier52d00e42011-11-03 21:23:39 +0000131 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)
Chad Rosier286852a2012-05-16 19:28:02 +0000142 << "-Werror" << ("-W" + OrigOpt.str());
Chad Rosier52d00e42011-11-03 21:23:39 +0000143 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)) {
Benjamin Kramerc7ccd612011-11-28 22:17:09 +0000158 EmitUnknownDiagWarning(Diags, "-Werror=", Specifier, isPositive);
Chad Rosier52d00e42011-11-03 21:23:39 +0000159 }
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)
Chad Rosier286852a2012-05-16 19:28:02 +0000170 << "-Wfatal-errors" << ("-W" + OrigOpt.str());
Chad Rosier52d00e42011-11-03 21:23:39 +0000171 continue;
172 }
173 Specifier = Opt.substr(13);
174 }
Mike Stump11289f42009-09-09 15:08:12 +0000175
Chad Rosier52d00e42011-11-03 21:23:39 +0000176 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)) {
Benjamin Kramerc7ccd612011-11-28 22:17:09 +0000186 EmitUnknownDiagWarning(Diags, "-Wfatal-errors=", Specifier,
187 isPositive);
Chad Rosier52d00e42011-11-03 21:23:39 +0000188 }
189 continue;
190 }
191
Chad Rosiere35f9dd2011-11-15 18:57:32 +0000192 if (Report) {
193 if (DiagIDs->getDiagnosticsInGroup(Opt, _Diags))
Chad Rosiere2d8fbb2012-05-16 19:42:03 +0000194 EmitUnknownDiagWarning(Diags, isPositive ? "-W" : "-Wno-", Opt,
195 isPositive);
Chad Rosier52d00e42011-11-03 21:23:39 +0000196 } else {
197 Diags.setDiagnosticGroupMapping(Opt, Mapping);
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000198 }
Daniel Dunbarc2e5ca62011-09-29 00:53:47 +0000199 }
Chris Lattnere007de32009-04-15 07:01:18 +0000200 }
Sebastian Redl48350102009-03-06 17:41:35 +0000201}