blob: 1dfe77abd90cf93c9c1bd99aba268411b11c1b87 [file] [log] [blame]
Sebastian Redl44ff86c2009-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-
16// generated data, and the special cases -pedantic, -pedantic-errors, -w and
17// -Werror.
18//
19// Warning options control the handling of the warnings that Clang emits. There
20// are three possible reactions to any given warning:
21// ignore: Do nothing
22// warn: Emit a message, but don't fail the compilation
23// error: Emit a message and fail the compilation
24//
Sebastian Redlf10cbca2009-03-07 12:09:25 +000025// Each warning option controls any number of actual warnings.
Sebastian Redl44ff86c2009-03-06 17:41:35 +000026// Given a warning option 'foo', the following are valid:
27// -Wfoo=ignore -> Ignore the controlled warnings.
28// -Wfoo=warn -> Warn about the controlled warnings.
29// -Wfoo=error -> Fail on the controlled warnings.
30// -Wfoo -> alias of -Wfoo=warn
31// -Wno-foo -> alias of -Wfoo=ignore
32// -Werror=foo -> alias of -Wfoo=error
33//
34// Because of this complex handling of options, the default parser is replaced.
35
Chris Lattnerae057262009-04-08 22:37:15 +000036#include "clang-cc.h"
Sebastian Redl44ff86c2009-03-06 17:41:35 +000037#include "clang/Basic/Diagnostic.h"
38#include "clang/Sema/SemaDiagnostic.h"
39#include "clang/Lex/LexDiagnostic.h"
40#include "llvm/Support/CommandLine.h"
Chris Lattner64b65f82009-04-11 18:40:46 +000041#include <cstdio>
Sebastian Redl44ff86c2009-03-06 17:41:35 +000042#include <utility>
43#include <algorithm>
Sebastian Redl44ff86c2009-03-06 17:41:35 +000044using namespace clang;
45
46namespace {
47 struct ParsedOption {
48 std::string Name;
49 diag::Mapping Mapping;
50
51 ParsedOption() {}
52 // Used by -Werror, implicitly.
53 ParsedOption(const std::string& name) : Name(name), Mapping(diag::MAP_ERROR)
54 {}
55 };
56
57 typedef std::vector<ParsedOption> OptionsList;
58
59 OptionsList Options;
60
61 struct WarningParser : public llvm::cl::basic_parser<ParsedOption> {
62 diag::Mapping StrToMapping(const std::string &S) {
63 if (S == "ignore")
64 return diag::MAP_IGNORE;
65 if (S == "warn")
66 return diag::MAP_WARNING;
67 if (S == "error")
68 return diag::MAP_ERROR;
69 return diag::MAP_DEFAULT;
70 }
71 bool parse(llvm::cl::Option &O, const char *ArgName,
72 const std::string &ArgValue, ParsedOption &Val)
73 {
74 size_t Eq = ArgValue.find("=");
75 if (Eq == std::string::npos) {
76 // Could be -Wfoo or -Wno-foo
77 if (ArgValue.compare(0, 3, "no-") == 0) {
78 Val.Name = ArgValue.substr(3);
79 Val.Mapping = diag::MAP_IGNORE;
80 } else {
81 Val.Name = ArgValue;
82 Val.Mapping = diag::MAP_WARNING;
83 }
84 } else {
85 Val.Name = ArgValue.substr(0, Eq);
86 Val.Mapping = StrToMapping(ArgValue.substr(Eq+1));
Sebastian Redlf10cbca2009-03-07 12:09:25 +000087 if (Val.Mapping == diag::MAP_DEFAULT) {
88 fprintf(stderr, "Illegal warning option value: %s\n",
89 ArgValue.substr(Eq+1).c_str());
Sebastian Redl44ff86c2009-03-06 17:41:35 +000090 return true;
Sebastian Redlf10cbca2009-03-07 12:09:25 +000091 }
Sebastian Redl44ff86c2009-03-06 17:41:35 +000092 }
93 return false;
94 }
95 };
96}
97
98static llvm::cl::list<ParsedOption, OptionsList, WarningParser>
99OptWarnings("W", llvm::cl::location(Options), llvm::cl::Prefix);
100
101static llvm::cl::list<ParsedOption, OptionsList, llvm::cl::parser<std::string> >
102OptWError("Werror", llvm::cl::location(Options), llvm::cl::CommaSeparated,
103 llvm::cl::ValueOptional);
104
105static llvm::cl::opt<bool> OptPedantic("pedantic");
106static llvm::cl::opt<bool> OptPedanticErrors("pedantic-errors");
107static llvm::cl::opt<bool> OptNoWarnings("w");
108static llvm::cl::opt<bool>
Chris Lattner8005e382009-03-09 20:44:22 +0000109OptWarnInSystemHeaders("Wsystem-headers",
110 llvm::cl::desc("Do not suppress warnings issued in system headers"));
Sebastian Redl44ff86c2009-03-06 17:41:35 +0000111
112namespace {
113 struct WarningOption {
114 const char *Name;
115 const diag::kind *Members;
116 size_t NumMembers;
117 };
Sebastian Redl44ff86c2009-03-06 17:41:35 +0000118}
119#define DIAGS(a) a, (sizeof(a) / sizeof(a[0]))
120// These tables will be TableGenerated later.
121// First the table sets describing the diagnostics controlled by each option.
122static const diag::kind UnusedMacrosDiags[] = { diag::pp_macro_not_used };
123static const diag::kind FloatEqualDiags[] = { diag::warn_floatingpoint_eq };
Chris Lattner9a183102009-04-14 20:36:01 +0000124static const diag::kind ExtraTokens[] = { diag::ext_pp_extra_tokens_at_eol };
Sebastian Redl44ff86c2009-03-06 17:41:35 +0000125static const diag::kind ReadOnlySetterAttrsDiags[] = {
126 diag::warn_objc_property_attr_mutually_exclusive
127};
128static const diag::kind FormatNonLiteralDiags[] = {
129 diag::warn_printf_not_string_constant
130};
131static const diag::kind UndefDiags[] = { diag::warn_pp_undef_identifier };
132static const diag::kind ImplicitFunctionDeclarationDiags[] = {
133 diag::ext_implicit_function_decl, diag::warn_implicit_function_decl
134};
Eli Friedmand7065a32009-03-30 21:19:48 +0000135static const diag::kind PointerSignDiags[] = {
136 diag::ext_typecheck_convert_incompatible_pointer_sign
137};
Steve Naroff82eff5c2009-03-31 15:00:11 +0000138static const diag::kind DeprecatedDeclarations[] = { diag::warn_deprecated };
Douglas Gregorcd7ac6f2009-03-31 16:35:03 +0000139static const diag::kind MissingPrototypesDiags[] = {
140 diag::warn_missing_prototype
141};
Chris Lattnerae057262009-04-08 22:37:15 +0000142static const diag::kind TrigraphsDiags[] = {
143 diag::trigraph_ignored, diag::trigraph_ignored_block_comment,
144 diag::trigraph_ends_block_comment, diag::trigraph_converted
145};
Steve Naroff82eff5c2009-03-31 15:00:11 +0000146
Sebastian Redl44ff86c2009-03-06 17:41:35 +0000147// Hmm ... this option is currently actually completely ignored.
148//static const diag::kind StrictSelectorMatchDiags[] = { };
149// Second the table of options. MUST be sorted by name! Binary lookup is done.
150static const WarningOption OptionTable[] = {
Chris Lattner9a183102009-04-14 20:36:01 +0000151 { "deprecated-declarations", DIAGS(DeprecatedDeclarations) },
152 { "extra-tokens", DIAGS(ExtraTokens) },
Sebastian Redl44ff86c2009-03-06 17:41:35 +0000153 { "float-equal", DIAGS(FloatEqualDiags) },
154 { "format-nonliteral", DIAGS(FormatNonLiteralDiags) },
155 { "implicit-function-declaration", DIAGS(ImplicitFunctionDeclarationDiags) },
Douglas Gregorcd7ac6f2009-03-31 16:35:03 +0000156 { "missing-prototypes", DIAGS(MissingPrototypesDiags) },
Eli Friedmand7065a32009-03-30 21:19:48 +0000157 { "pointer-sign", DIAGS(PointerSignDiags) },
Sebastian Redl44ff86c2009-03-06 17:41:35 +0000158 { "readonly-setter-attrs", DIAGS(ReadOnlySetterAttrsDiags) },
Chris Lattnerae057262009-04-08 22:37:15 +0000159 { "trigraphs", DIAGS(TrigraphsDiags) },
Sebastian Redl44ff86c2009-03-06 17:41:35 +0000160 { "undef", DIAGS(UndefDiags) },
161 { "unused-macros", DIAGS(UnusedMacrosDiags) },
162// { "strict-selector-match", DIAGS(StrictSelectorMatchDiags) }
163};
164static const size_t OptionTableSize =
165 sizeof(OptionTable) / sizeof(OptionTable[0]);
166
Chris Lattnerae057262009-04-08 22:37:15 +0000167static bool WarningOptionCompare(const WarningOption &LHS,
168 const WarningOption &RHS) {
169 return strcmp(LHS.Name, RHS.Name) < 0;
170}
Sebastian Redl44ff86c2009-03-06 17:41:35 +0000171
Chris Lattnerae057262009-04-08 22:37:15 +0000172bool clang::ProcessWarningOptions(Diagnostic &Diags) {
Sebastian Redl44ff86c2009-03-06 17:41:35 +0000173 // FIXME: These should be mapped to group options.
174 Diags.setIgnoreAllWarnings(OptNoWarnings);
175 Diags.setWarnOnExtensions(OptPedantic);
176 Diags.setErrorOnExtensions(OptPedanticErrors);
177
178 // Set some defaults that are currently set manually. This, too, should
179 // be in the tablegen stuff later.
180 Diags.setDiagnosticMapping(diag::pp_macro_not_used, diag::MAP_IGNORE);
181 Diags.setDiagnosticMapping(diag::warn_floatingpoint_eq, diag::MAP_IGNORE);
182 Diags.setDiagnosticMapping(diag::warn_objc_property_attr_mutually_exclusive,
183 diag::MAP_IGNORE);
184 Diags.setDiagnosticMapping(diag::warn_pp_undef_identifier, diag::MAP_IGNORE);
185 Diags.setDiagnosticMapping(diag::warn_implicit_function_decl,
186 diag::MAP_IGNORE);
187
188 Diags.setDiagnosticMapping(diag::err_pp_file_not_found, diag::MAP_FATAL);
Douglas Gregor40197492009-03-19 18:55:06 +0000189 Diags.setDiagnosticMapping(diag::err_template_recursion_depth_exceeded,
190 diag::MAP_FATAL);
Douglas Gregorcd7ac6f2009-03-31 16:35:03 +0000191 Diags.setDiagnosticMapping(diag::warn_missing_prototype, diag::MAP_IGNORE);
Chris Lattner8005e382009-03-09 20:44:22 +0000192 Diags.setSuppressSystemWarnings(!OptWarnInSystemHeaders);
Sebastian Redl44ff86c2009-03-06 17:41:35 +0000193
194 for (OptionsList::iterator it = Options.begin(), e = Options.end();
195 it != e; ++it) {
196 if (it->Name.empty()) {
197 // Empty string is "everything". This way, -Werror does the right thing.
198 // FIXME: These flags do not participate in proper option overriding.
199 switch(it->Mapping) {
200 default:
201 assert(false && "Illegal mapping");
202 break;
203
204 case diag::MAP_IGNORE:
205 Diags.setIgnoreAllWarnings(true);
206 Diags.setWarningsAsErrors(false);
207 break;
208
209 case diag::MAP_WARNING:
210 Diags.setIgnoreAllWarnings(false);
211 Diags.setWarningsAsErrors(false);
212 break;
213
214 case diag::MAP_ERROR:
215 Diags.setIgnoreAllWarnings(false);
216 Diags.setWarningsAsErrors(true);
217 break;
218 }
219 continue;
220 }
221 WarningOption Key = { it->Name.c_str(), 0, 0 };
Chris Lattnerae057262009-04-08 22:37:15 +0000222 const WarningOption *Found =
223 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
224 WarningOptionCompare);
Sebastian Redl44ff86c2009-03-06 17:41:35 +0000225 if (Found == OptionTable + OptionTableSize ||
Sebastian Redlf10cbca2009-03-07 12:09:25 +0000226 strcmp(Found->Name, Key.Name) != 0) {
227 fprintf(stderr, "Unknown warning option: -W%s\n", Key.Name);
Sebastian Redl44ff86c2009-03-06 17:41:35 +0000228 return true;
Sebastian Redlf10cbca2009-03-07 12:09:25 +0000229 }
Sebastian Redl44ff86c2009-03-06 17:41:35 +0000230
231 // Option exists.
Chris Lattnerae057262009-04-08 22:37:15 +0000232 for (size_t i = 0, e = Found->NumMembers; i != e; ++i)
Sebastian Redl44ff86c2009-03-06 17:41:35 +0000233 Diags.setDiagnosticMapping(Found->Members[i], it->Mapping);
Sebastian Redl44ff86c2009-03-06 17:41:35 +0000234 }
235 return false;
236}