blob: be93c67fa9672a504b9c8ad41ac5538397cb396b [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
36#include "clang/Basic/Diagnostic.h"
37#include "clang/Sema/SemaDiagnostic.h"
38#include "clang/Lex/LexDiagnostic.h"
39#include "llvm/Support/CommandLine.h"
40#include <vector>
41#include <string>
42#include <utility>
43#include <algorithm>
44#include <string.h>
45
46using namespace clang;
47
48namespace {
49 struct ParsedOption {
50 std::string Name;
51 diag::Mapping Mapping;
52
53 ParsedOption() {}
54 // Used by -Werror, implicitly.
55 ParsedOption(const std::string& name) : Name(name), Mapping(diag::MAP_ERROR)
56 {}
57 };
58
59 typedef std::vector<ParsedOption> OptionsList;
60
61 OptionsList Options;
62
63 struct WarningParser : public llvm::cl::basic_parser<ParsedOption> {
64 diag::Mapping StrToMapping(const std::string &S) {
65 if (S == "ignore")
66 return diag::MAP_IGNORE;
67 if (S == "warn")
68 return diag::MAP_WARNING;
69 if (S == "error")
70 return diag::MAP_ERROR;
71 return diag::MAP_DEFAULT;
72 }
73 bool parse(llvm::cl::Option &O, const char *ArgName,
74 const std::string &ArgValue, ParsedOption &Val)
75 {
76 size_t Eq = ArgValue.find("=");
77 if (Eq == std::string::npos) {
78 // Could be -Wfoo or -Wno-foo
79 if (ArgValue.compare(0, 3, "no-") == 0) {
80 Val.Name = ArgValue.substr(3);
81 Val.Mapping = diag::MAP_IGNORE;
82 } else {
83 Val.Name = ArgValue;
84 Val.Mapping = diag::MAP_WARNING;
85 }
86 } else {
87 Val.Name = ArgValue.substr(0, Eq);
88 Val.Mapping = StrToMapping(ArgValue.substr(Eq+1));
Sebastian Redlc5613db2009-03-07 12:09:25 +000089 if (Val.Mapping == diag::MAP_DEFAULT) {
90 fprintf(stderr, "Illegal warning option value: %s\n",
91 ArgValue.substr(Eq+1).c_str());
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000092 return true;
Sebastian Redlc5613db2009-03-07 12:09:25 +000093 }
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000094 }
95 return false;
96 }
97 };
98}
99
100static llvm::cl::list<ParsedOption, OptionsList, WarningParser>
101OptWarnings("W", llvm::cl::location(Options), llvm::cl::Prefix);
102
103static llvm::cl::list<ParsedOption, OptionsList, llvm::cl::parser<std::string> >
104OptWError("Werror", llvm::cl::location(Options), llvm::cl::CommaSeparated,
105 llvm::cl::ValueOptional);
106
107static llvm::cl::opt<bool> OptPedantic("pedantic");
108static llvm::cl::opt<bool> OptPedanticErrors("pedantic-errors");
109static llvm::cl::opt<bool> OptNoWarnings("w");
110static llvm::cl::opt<bool>
Chris Lattnerdb7bc582009-03-09 20:44:22 +0000111OptWarnInSystemHeaders("Wsystem-headers",
112 llvm::cl::desc("Do not suppress warnings issued in system headers"));
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000113
114namespace {
115 struct WarningOption {
116 const char *Name;
117 const diag::kind *Members;
118 size_t NumMembers;
119 };
120 bool operator <(const WarningOption& lhs, const WarningOption& rhs) {
121 return strcmp(lhs.Name, rhs.Name) < 0;
122 }
123}
124#define DIAGS(a) a, (sizeof(a) / sizeof(a[0]))
125// These tables will be TableGenerated later.
126// First the table sets describing the diagnostics controlled by each option.
127static const diag::kind UnusedMacrosDiags[] = { diag::pp_macro_not_used };
128static const diag::kind FloatEqualDiags[] = { diag::warn_floatingpoint_eq };
129static const diag::kind ReadOnlySetterAttrsDiags[] = {
130 diag::warn_objc_property_attr_mutually_exclusive
131};
132static const diag::kind FormatNonLiteralDiags[] = {
133 diag::warn_printf_not_string_constant
134};
135static const diag::kind UndefDiags[] = { diag::warn_pp_undef_identifier };
136static const diag::kind ImplicitFunctionDeclarationDiags[] = {
137 diag::ext_implicit_function_decl, diag::warn_implicit_function_decl
138};
Eli Friedman757e0dd2009-03-30 21:19:48 +0000139static const diag::kind PointerSignDiags[] = {
140 diag::ext_typecheck_convert_incompatible_pointer_sign
141};
Steve Naroff17f689f2009-03-31 15:00:11 +0000142static const diag::kind DeprecatedDeclarations[] = { diag::warn_deprecated };
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000143static const diag::kind MissingPrototypesDiags[] = {
144 diag::warn_missing_prototype
145};
Steve Naroff17f689f2009-03-31 15:00:11 +0000146
Sebastian Redl63a9e0f2009-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[] = {
Steve Naroff17f689f2009-03-31 15:00:11 +0000151 { "deprecated-declarations", DIAGS(DeprecatedDeclarations) },
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000152 { "float-equal", DIAGS(FloatEqualDiags) },
153 { "format-nonliteral", DIAGS(FormatNonLiteralDiags) },
154 { "implicit-function-declaration", DIAGS(ImplicitFunctionDeclarationDiags) },
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000155 { "missing-prototypes", DIAGS(MissingPrototypesDiags) },
Eli Friedman757e0dd2009-03-30 21:19:48 +0000156 { "pointer-sign", DIAGS(PointerSignDiags) },
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000157 { "readonly-setter-attrs", DIAGS(ReadOnlySetterAttrsDiags) },
158 { "undef", DIAGS(UndefDiags) },
159 { "unused-macros", DIAGS(UnusedMacrosDiags) },
160// { "strict-selector-match", DIAGS(StrictSelectorMatchDiags) }
161};
162static const size_t OptionTableSize =
163 sizeof(OptionTable) / sizeof(OptionTable[0]);
164
165namespace clang {
166
167bool ProcessWarningOptions(Diagnostic &Diags) {
168 // FIXME: These should be mapped to group options.
169 Diags.setIgnoreAllWarnings(OptNoWarnings);
170 Diags.setWarnOnExtensions(OptPedantic);
171 Diags.setErrorOnExtensions(OptPedanticErrors);
172
173 // Set some defaults that are currently set manually. This, too, should
174 // be in the tablegen stuff later.
175 Diags.setDiagnosticMapping(diag::pp_macro_not_used, diag::MAP_IGNORE);
176 Diags.setDiagnosticMapping(diag::warn_floatingpoint_eq, diag::MAP_IGNORE);
177 Diags.setDiagnosticMapping(diag::warn_objc_property_attr_mutually_exclusive,
178 diag::MAP_IGNORE);
179 Diags.setDiagnosticMapping(diag::warn_pp_undef_identifier, diag::MAP_IGNORE);
180 Diags.setDiagnosticMapping(diag::warn_implicit_function_decl,
181 diag::MAP_IGNORE);
182
183 Diags.setDiagnosticMapping(diag::err_pp_file_not_found, diag::MAP_FATAL);
Douglas Gregor525c4b02009-03-19 18:55:06 +0000184 Diags.setDiagnosticMapping(diag::err_template_recursion_depth_exceeded,
185 diag::MAP_FATAL);
Douglas Gregor8499f3f2009-03-31 16:35:03 +0000186 Diags.setDiagnosticMapping(diag::warn_missing_prototype, diag::MAP_IGNORE);
Chris Lattnerdb7bc582009-03-09 20:44:22 +0000187 Diags.setSuppressSystemWarnings(!OptWarnInSystemHeaders);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000188
189 for (OptionsList::iterator it = Options.begin(), e = Options.end();
190 it != e; ++it) {
191 if (it->Name.empty()) {
192 // Empty string is "everything". This way, -Werror does the right thing.
193 // FIXME: These flags do not participate in proper option overriding.
194 switch(it->Mapping) {
195 default:
196 assert(false && "Illegal mapping");
197 break;
198
199 case diag::MAP_IGNORE:
200 Diags.setIgnoreAllWarnings(true);
201 Diags.setWarningsAsErrors(false);
202 break;
203
204 case diag::MAP_WARNING:
205 Diags.setIgnoreAllWarnings(false);
206 Diags.setWarningsAsErrors(false);
207 break;
208
209 case diag::MAP_ERROR:
210 Diags.setIgnoreAllWarnings(false);
211 Diags.setWarningsAsErrors(true);
212 break;
213 }
214 continue;
215 }
216 WarningOption Key = { it->Name.c_str(), 0, 0 };
217 const WarningOption *Found = std::lower_bound(OptionTable,
218 OptionTable + OptionTableSize,
219 Key);
220 if (Found == OptionTable + OptionTableSize ||
Sebastian Redlc5613db2009-03-07 12:09:25 +0000221 strcmp(Found->Name, Key.Name) != 0) {
222 fprintf(stderr, "Unknown warning option: -W%s\n", Key.Name);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000223 return true;
Sebastian Redlc5613db2009-03-07 12:09:25 +0000224 }
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000225
226 // Option exists.
227 for (size_t i = 0; i < Found->NumMembers; ++i) {
228 Diags.setDiagnosticMapping(Found->Members[i], it->Mapping);
229 }
230 }
231 return false;
232}
233
234}