blob: edbccb62be6e34d7876e86a6395fcc03c5634dd8 [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-
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 Redlc5613db2009-03-07 12:09:25 +000025// Each warning option controls any number of actual warnings.
Sebastian Redl63a9e0f2009-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 Lattnerd613c3d2009-04-08 22:37:15 +000036#include "clang-cc.h"
Sebastian Redl63a9e0f2009-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"
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000041#include <utility>
42#include <algorithm>
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000043using namespace clang;
44
45namespace {
46 struct ParsedOption {
47 std::string Name;
48 diag::Mapping Mapping;
49
50 ParsedOption() {}
51 // Used by -Werror, implicitly.
52 ParsedOption(const std::string& name) : Name(name), Mapping(diag::MAP_ERROR)
53 {}
54 };
55
56 typedef std::vector<ParsedOption> OptionsList;
57
58 OptionsList Options;
59
60 struct WarningParser : public llvm::cl::basic_parser<ParsedOption> {
61 diag::Mapping StrToMapping(const std::string &S) {
62 if (S == "ignore")
63 return diag::MAP_IGNORE;
64 if (S == "warn")
65 return diag::MAP_WARNING;
66 if (S == "error")
67 return diag::MAP_ERROR;
68 return diag::MAP_DEFAULT;
69 }
70 bool parse(llvm::cl::Option &O, const char *ArgName,
71 const std::string &ArgValue, ParsedOption &Val)
72 {
73 size_t Eq = ArgValue.find("=");
74 if (Eq == std::string::npos) {
75 // Could be -Wfoo or -Wno-foo
76 if (ArgValue.compare(0, 3, "no-") == 0) {
77 Val.Name = ArgValue.substr(3);
78 Val.Mapping = diag::MAP_IGNORE;
79 } else {
80 Val.Name = ArgValue;
81 Val.Mapping = diag::MAP_WARNING;
82 }
83 } else {
84 Val.Name = ArgValue.substr(0, Eq);
85 Val.Mapping = StrToMapping(ArgValue.substr(Eq+1));
Sebastian Redlc5613db2009-03-07 12:09:25 +000086 if (Val.Mapping == diag::MAP_DEFAULT) {
87 fprintf(stderr, "Illegal warning option value: %s\n",
88 ArgValue.substr(Eq+1).c_str());
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000089 return true;
Sebastian Redlc5613db2009-03-07 12:09:25 +000090 }
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000091 }
92 return false;
93 }
94 };
95}
96
97static llvm::cl::list<ParsedOption, OptionsList, WarningParser>
98OptWarnings("W", llvm::cl::location(Options), llvm::cl::Prefix);
99
100static llvm::cl::list<ParsedOption, OptionsList, llvm::cl::parser<std::string> >
101OptWError("Werror", llvm::cl::location(Options), llvm::cl::CommaSeparated,
102 llvm::cl::ValueOptional);
103
104static llvm::cl::opt<bool> OptPedantic("pedantic");
105static llvm::cl::opt<bool> OptPedanticErrors("pedantic-errors");
106static llvm::cl::opt<bool> OptNoWarnings("w");
107static llvm::cl::opt<bool>
Chris Lattnerdb7bc582009-03-09 20:44:22 +0000108OptWarnInSystemHeaders("Wsystem-headers",
109 llvm::cl::desc("Do not suppress warnings issued in system headers"));
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000110
111namespace {
112 struct WarningOption {
113 const char *Name;
114 const diag::kind *Members;
115 size_t NumMembers;
116 };
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000117}
118#define DIAGS(a) a, (sizeof(a) / sizeof(a[0]))
119// These tables will be TableGenerated later.
120// First the table sets describing the diagnostics controlled by each option.
121static const diag::kind UnusedMacrosDiags[] = { diag::pp_macro_not_used };
122static const diag::kind FloatEqualDiags[] = { diag::warn_floatingpoint_eq };
123static const diag::kind ReadOnlySetterAttrsDiags[] = {
124 diag::warn_objc_property_attr_mutually_exclusive
125};
126static const diag::kind FormatNonLiteralDiags[] = {
127 diag::warn_printf_not_string_constant
128};
129static const diag::kind UndefDiags[] = { diag::warn_pp_undef_identifier };
130static const diag::kind ImplicitFunctionDeclarationDiags[] = {
131 diag::ext_implicit_function_decl, diag::warn_implicit_function_decl
132};
Eli Friedman757e0dd2009-03-30 21:19:48 +0000133static const diag::kind PointerSignDiags[] = {
134 diag::ext_typecheck_convert_incompatible_pointer_sign
135};
Steve Naroff17f689f2009-03-31 15:00:11 +0000136static const diag::kind DeprecatedDeclarations[] = { diag::warn_deprecated };
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000137static const diag::kind MissingPrototypesDiags[] = {
138 diag::warn_missing_prototype
139};
Chris Lattnerd613c3d2009-04-08 22:37:15 +0000140static const diag::kind TrigraphsDiags[] = {
141 diag::trigraph_ignored, diag::trigraph_ignored_block_comment,
142 diag::trigraph_ends_block_comment, diag::trigraph_converted
143};
Steve Naroff17f689f2009-03-31 15:00:11 +0000144
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000145// Hmm ... this option is currently actually completely ignored.
146//static const diag::kind StrictSelectorMatchDiags[] = { };
147// Second the table of options. MUST be sorted by name! Binary lookup is done.
148static const WarningOption OptionTable[] = {
Steve Naroff17f689f2009-03-31 15:00:11 +0000149 { "deprecated-declarations", DIAGS(DeprecatedDeclarations) },
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000150 { "float-equal", DIAGS(FloatEqualDiags) },
151 { "format-nonliteral", DIAGS(FormatNonLiteralDiags) },
152 { "implicit-function-declaration", DIAGS(ImplicitFunctionDeclarationDiags) },
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000153 { "missing-prototypes", DIAGS(MissingPrototypesDiags) },
Eli Friedman757e0dd2009-03-30 21:19:48 +0000154 { "pointer-sign", DIAGS(PointerSignDiags) },
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000155 { "readonly-setter-attrs", DIAGS(ReadOnlySetterAttrsDiags) },
Chris Lattnerd613c3d2009-04-08 22:37:15 +0000156 { "trigraphs", DIAGS(TrigraphsDiags) },
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000157 { "undef", DIAGS(UndefDiags) },
158 { "unused-macros", DIAGS(UnusedMacrosDiags) },
159// { "strict-selector-match", DIAGS(StrictSelectorMatchDiags) }
160};
161static const size_t OptionTableSize =
162 sizeof(OptionTable) / sizeof(OptionTable[0]);
163
Chris Lattnerd613c3d2009-04-08 22:37:15 +0000164static bool WarningOptionCompare(const WarningOption &LHS,
165 const WarningOption &RHS) {
166 return strcmp(LHS.Name, RHS.Name) < 0;
167}
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000168
Chris Lattnerd613c3d2009-04-08 22:37:15 +0000169bool clang::ProcessWarningOptions(Diagnostic &Diags) {
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000170 // FIXME: These should be mapped to group options.
171 Diags.setIgnoreAllWarnings(OptNoWarnings);
172 Diags.setWarnOnExtensions(OptPedantic);
173 Diags.setErrorOnExtensions(OptPedanticErrors);
174
175 // Set some defaults that are currently set manually. This, too, should
176 // be in the tablegen stuff later.
177 Diags.setDiagnosticMapping(diag::pp_macro_not_used, diag::MAP_IGNORE);
178 Diags.setDiagnosticMapping(diag::warn_floatingpoint_eq, diag::MAP_IGNORE);
179 Diags.setDiagnosticMapping(diag::warn_objc_property_attr_mutually_exclusive,
180 diag::MAP_IGNORE);
181 Diags.setDiagnosticMapping(diag::warn_pp_undef_identifier, diag::MAP_IGNORE);
182 Diags.setDiagnosticMapping(diag::warn_implicit_function_decl,
183 diag::MAP_IGNORE);
184
185 Diags.setDiagnosticMapping(diag::err_pp_file_not_found, diag::MAP_FATAL);
Douglas Gregor525c4b02009-03-19 18:55:06 +0000186 Diags.setDiagnosticMapping(diag::err_template_recursion_depth_exceeded,
187 diag::MAP_FATAL);
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000188 Diags.setDiagnosticMapping(diag::warn_missing_prototype, diag::MAP_IGNORE);
Chris Lattnerdb7bc582009-03-09 20:44:22 +0000189 Diags.setSuppressSystemWarnings(!OptWarnInSystemHeaders);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000190
191 for (OptionsList::iterator it = Options.begin(), e = Options.end();
192 it != e; ++it) {
193 if (it->Name.empty()) {
194 // Empty string is "everything". This way, -Werror does the right thing.
195 // FIXME: These flags do not participate in proper option overriding.
196 switch(it->Mapping) {
197 default:
198 assert(false && "Illegal mapping");
199 break;
200
201 case diag::MAP_IGNORE:
202 Diags.setIgnoreAllWarnings(true);
203 Diags.setWarningsAsErrors(false);
204 break;
205
206 case diag::MAP_WARNING:
207 Diags.setIgnoreAllWarnings(false);
208 Diags.setWarningsAsErrors(false);
209 break;
210
211 case diag::MAP_ERROR:
212 Diags.setIgnoreAllWarnings(false);
213 Diags.setWarningsAsErrors(true);
214 break;
215 }
216 continue;
217 }
218 WarningOption Key = { it->Name.c_str(), 0, 0 };
Chris Lattnerd613c3d2009-04-08 22:37:15 +0000219 const WarningOption *Found =
220 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
221 WarningOptionCompare);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000222 if (Found == OptionTable + OptionTableSize ||
Sebastian Redlc5613db2009-03-07 12:09:25 +0000223 strcmp(Found->Name, Key.Name) != 0) {
224 fprintf(stderr, "Unknown warning option: -W%s\n", Key.Name);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000225 return true;
Sebastian Redlc5613db2009-03-07 12:09:25 +0000226 }
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000227
228 // Option exists.
Chris Lattnerd613c3d2009-04-08 22:37:15 +0000229 for (size_t i = 0, e = Found->NumMembers; i != e; ++i)
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000230 Diags.setDiagnosticMapping(Found->Members[i], it->Mapping);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000231 }
232 return false;
233}