blob: ec5fde0c3eff8b8f9fcf62068f9d40d293aa1796 [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//
Eli Friedmanb09f6e12009-05-19 04:14:29 +000023#include "clang/Frontend/Utils.h"
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000024#include "clang/Basic/Diagnostic.h"
25#include "clang/Sema/SemaDiagnostic.h"
26#include "clang/Lex/LexDiagnostic.h"
Daniel Dunbar69079432009-11-12 07:28:44 +000027#include "clang/Frontend/DiagnosticOptions.h"
Chris Lattner8ea841b2009-06-23 01:20:39 +000028#include "clang/Frontend/FrontendDiagnostic.h"
Eli Friedmanac8d6292009-05-19 04:30:57 +000029#include <cstring>
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000030#include <utility>
31#include <algorithm>
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000032using namespace clang;
33
Chad Rosier588e83b2012-01-31 19:31:12 +000034// EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning
35// opts
Chad Rosier99643d92011-11-15 19:03:03 +000036static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags,
Benjamin Kramerdce63272011-11-15 12:26:39 +000037 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
David Blaikied6471f72011-09-25 23:23:43 +000050void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
Daniel Dunbar69079432009-11-12 07:28:44 +000051 const DiagnosticOptions &Opts) {
Chris Lattner27ceb9d2009-04-15 07:01:18 +000052 Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
Daniel Dunbar69079432009-11-12 07:28:44 +000053 Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
Jeffrey Yasskin5edbdcc2010-06-11 05:57:47 +000054 Diags.setShowOverloads(
David Blaikied6471f72011-09-25 23:23:43 +000055 static_cast<DiagnosticsEngine::OverloadsShown>(Opts.ShowOverloads));
Chris Lattnerc1002142010-04-07 20:37:06 +000056
57 // Handle -ferror-limit
58 if (Opts.ErrorLimit)
59 Diags.setErrorLimit(Opts.ErrorLimit);
Douglas Gregor575cf372010-04-20 07:18:24 +000060 if (Opts.TemplateBacktraceLimit)
61 Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
Richard Smith08d6e032011-12-16 19:06:07 +000062 if (Opts.ConstexprBacktraceLimit)
63 Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000064
Chris Lattnerb54b2762009-04-16 05:04:32 +000065 // If -pedantic or -pedantic-errors was specified, then we want to map all
66 // extension diagnostics onto WARNING or ERROR unless the user has futz'd
67 // around with them explicitly.
Daniel Dunbar69079432009-11-12 07:28:44 +000068 if (Opts.PedanticErrors)
David Blaikied6471f72011-09-25 23:23:43 +000069 Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Error);
Daniel Dunbar69079432009-11-12 07:28:44 +000070 else if (Opts.Pedantic)
David Blaikied6471f72011-09-25 23:23:43 +000071 Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Warn);
Chris Lattnerb54b2762009-04-16 05:04:32 +000072 else
David Blaikied6471f72011-09-25 23:23:43 +000073 Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Ignore);
Mike Stump1eb44332009-09-09 15:08:12 +000074
Chad Rosier05272a62011-11-03 21:23:39 +000075 llvm::SmallVector<diag::kind, 10> _Diags;
Dylan Noblesmithc93dc782012-02-20 14:00:23 +000076 const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs =
Chad Rosier05272a62011-11-03 21:23:39 +000077 Diags.getDiagnosticIDs();
78 // We parse the warning options twice. The first pass sets diagnostic state,
79 // while the second pass reports warnings/errors. This has the effect that
80 // we follow the more canonical "last option wins" paradigm when there are
81 // conflicting options.
82 for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; ++Report) {
83 bool SetDiagnostic = (Report == 0);
84 for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
85 StringRef Opt = Opts.Warnings[i];
Mike Stump1eb44332009-09-09 15:08:12 +000086
Hans Wennborgc8769462012-01-17 09:30:38 +000087 // Treat -Wformat=0 as an alias for -Wno-format.
88 if (Opt == "format=0")
89 Opt = "no-format";
90
Chad Rosier05272a62011-11-03 21:23:39 +000091 // Check to see if this warning starts with "no-", if so, this is a
92 // negative form of the option.
93 bool isPositive = true;
94 if (Opt.startswith("no-")) {
95 isPositive = false;
96 Opt = Opt.substr(3);
Chris Lattner5147e8e2009-04-15 04:27:38 +000097 }
Mike Stump1eb44332009-09-09 15:08:12 +000098
Chad Rosier05272a62011-11-03 21:23:39 +000099 // Figure out how this option affects the warning. If -Wfoo, map the
100 // diagnostic to a warning, if -Wno-foo, map it to ignore.
101 diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE;
102
103 // -Wsystem-headers is a special case, not driven by the option table. It
104 // cannot be controlled with -Werror.
105 if (Opt == "system-headers") {
106 if (SetDiagnostic)
107 Diags.setSuppressSystemWarnings(!isPositive);
Chris Lattner5147e8e2009-04-15 04:27:38 +0000108 continue;
109 }
Chad Rosier05272a62011-11-03 21:23:39 +0000110
111 // -Weverything is a special case as well. It implicitly enables all
112 // warnings, including ones not explicitly in a warning group.
113 if (Opt == "everything") {
Argyrios Kyrtzidis11583c72012-01-27 06:15:43 +0000114 if (SetDiagnostic) {
115 if (isPositive) {
116 Diags.setEnableAllWarnings(true);
117 } else {
118 Diags.setEnableAllWarnings(false);
119 Diags.setMappingToAllDiagnostics(diag::MAP_IGNORE);
120 }
121 }
Chad Rosier05272a62011-11-03 21:23:39 +0000122 continue;
123 }
124
125 // -Werror/-Wno-error is a special case, not controlled by the option
126 // table. It also has the "specifier" form of -Werror=foo and -Werror-foo.
127 if (Opt.startswith("error")) {
128 StringRef Specifier;
129 if (Opt.size() > 5) { // Specifier must be present.
130 if ((Opt[5] != '=' && Opt[5] != '-') || Opt.size() == 6) {
131 if (Report)
132 Diags.Report(diag::warn_unknown_warning_specifier)
133 << "-Werror" << ("-W" + Opt.str());
134 continue;
135 }
136 Specifier = Opt.substr(6);
137 }
138
139 if (Specifier.empty()) {
140 if (SetDiagnostic)
141 Diags.setWarningsAsErrors(isPositive);
142 continue;
143 }
144
145 if (SetDiagnostic) {
146 // Set the warning as error flag for this specifier.
147 Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive);
148 } else if (DiagIDs->getDiagnosticsInGroup(Specifier, _Diags)) {
Benjamin Kramer4e969442011-11-28 22:17:09 +0000149 EmitUnknownDiagWarning(Diags, "-Werror=", Specifier, isPositive);
Chad Rosier05272a62011-11-03 21:23:39 +0000150 }
151 continue;
152 }
153
154 // -Wfatal-errors is yet another special case.
155 if (Opt.startswith("fatal-errors")) {
156 StringRef Specifier;
157 if (Opt.size() != 12) {
158 if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) {
159 if (Report)
160 Diags.Report(diag::warn_unknown_warning_specifier)
161 << "-Wfatal-errors" << ("-W" + Opt.str());
162 continue;
163 }
164 Specifier = Opt.substr(13);
165 }
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Chad Rosier05272a62011-11-03 21:23:39 +0000167 if (Specifier.empty()) {
168 if (SetDiagnostic)
169 Diags.setErrorsAsFatal(isPositive);
170 continue;
171 }
172
173 if (SetDiagnostic) {
174 // Set the error as fatal flag for this specifier.
175 Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive);
176 } else if (DiagIDs->getDiagnosticsInGroup(Specifier, _Diags)) {
Benjamin Kramer4e969442011-11-28 22:17:09 +0000177 EmitUnknownDiagWarning(Diags, "-Wfatal-errors=", Specifier,
178 isPositive);
Chad Rosier05272a62011-11-03 21:23:39 +0000179 }
180 continue;
181 }
182
Chad Rosier496cc8e2011-11-15 18:57:32 +0000183 if (Report) {
184 if (DiagIDs->getDiagnosticsInGroup(Opt, _Diags))
Chad Rosier99643d92011-11-15 19:03:03 +0000185 EmitUnknownDiagWarning(Diags, "-W", Opt, isPositive);
Chad Rosier05272a62011-11-03 21:23:39 +0000186 } else {
187 Diags.setDiagnosticGroupMapping(Opt, Mapping);
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000188 }
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000189 }
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000190 }
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000191}