blob: 8ef1ff57dbce68d2e49ae20a932f038dfc2a8f8a [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>
111OptSuppressSystemWarnings("suppress-system-warnings",
112 llvm::cl::desc("Suppress warnings issued in system headers"),
113 llvm::cl::init(true));
114
115namespace {
116 struct WarningOption {
117 const char *Name;
118 const diag::kind *Members;
119 size_t NumMembers;
120 };
121 bool operator <(const WarningOption& lhs, const WarningOption& rhs) {
122 return strcmp(lhs.Name, rhs.Name) < 0;
123 }
124}
125#define DIAGS(a) a, (sizeof(a) / sizeof(a[0]))
126// These tables will be TableGenerated later.
127// First the table sets describing the diagnostics controlled by each option.
128static const diag::kind UnusedMacrosDiags[] = { diag::pp_macro_not_used };
129static const diag::kind FloatEqualDiags[] = { diag::warn_floatingpoint_eq };
130static const diag::kind ReadOnlySetterAttrsDiags[] = {
131 diag::warn_objc_property_attr_mutually_exclusive
132};
133static const diag::kind FormatNonLiteralDiags[] = {
134 diag::warn_printf_not_string_constant
135};
136static const diag::kind UndefDiags[] = { diag::warn_pp_undef_identifier };
137static const diag::kind ImplicitFunctionDeclarationDiags[] = {
138 diag::ext_implicit_function_decl, diag::warn_implicit_function_decl
139};
140// Hmm ... this option is currently actually completely ignored.
141//static const diag::kind StrictSelectorMatchDiags[] = { };
142// Second the table of options. MUST be sorted by name! Binary lookup is done.
143static const WarningOption OptionTable[] = {
144 { "float-equal", DIAGS(FloatEqualDiags) },
145 { "format-nonliteral", DIAGS(FormatNonLiteralDiags) },
146 { "implicit-function-declaration", DIAGS(ImplicitFunctionDeclarationDiags) },
147 { "readonly-setter-attrs", DIAGS(ReadOnlySetterAttrsDiags) },
148 { "undef", DIAGS(UndefDiags) },
149 { "unused-macros", DIAGS(UnusedMacrosDiags) },
150// { "strict-selector-match", DIAGS(StrictSelectorMatchDiags) }
151};
152static const size_t OptionTableSize =
153 sizeof(OptionTable) / sizeof(OptionTable[0]);
154
155namespace clang {
156
157bool ProcessWarningOptions(Diagnostic &Diags) {
158 // FIXME: These should be mapped to group options.
159 Diags.setIgnoreAllWarnings(OptNoWarnings);
160 Diags.setWarnOnExtensions(OptPedantic);
161 Diags.setErrorOnExtensions(OptPedanticErrors);
162
163 // Set some defaults that are currently set manually. This, too, should
164 // be in the tablegen stuff later.
165 Diags.setDiagnosticMapping(diag::pp_macro_not_used, diag::MAP_IGNORE);
166 Diags.setDiagnosticMapping(diag::warn_floatingpoint_eq, diag::MAP_IGNORE);
167 Diags.setDiagnosticMapping(diag::warn_objc_property_attr_mutually_exclusive,
168 diag::MAP_IGNORE);
169 Diags.setDiagnosticMapping(diag::warn_pp_undef_identifier, diag::MAP_IGNORE);
170 Diags.setDiagnosticMapping(diag::warn_implicit_function_decl,
171 diag::MAP_IGNORE);
172
173 Diags.setDiagnosticMapping(diag::err_pp_file_not_found, diag::MAP_FATAL);
174 Diags.setSuppressSystemWarnings(OptSuppressSystemWarnings);
175
176 for (OptionsList::iterator it = Options.begin(), e = Options.end();
177 it != e; ++it) {
178 if (it->Name.empty()) {
179 // Empty string is "everything". This way, -Werror does the right thing.
180 // FIXME: These flags do not participate in proper option overriding.
181 switch(it->Mapping) {
182 default:
183 assert(false && "Illegal mapping");
184 break;
185
186 case diag::MAP_IGNORE:
187 Diags.setIgnoreAllWarnings(true);
188 Diags.setWarningsAsErrors(false);
189 break;
190
191 case diag::MAP_WARNING:
192 Diags.setIgnoreAllWarnings(false);
193 Diags.setWarningsAsErrors(false);
194 break;
195
196 case diag::MAP_ERROR:
197 Diags.setIgnoreAllWarnings(false);
198 Diags.setWarningsAsErrors(true);
199 break;
200 }
201 continue;
202 }
203 WarningOption Key = { it->Name.c_str(), 0, 0 };
204 const WarningOption *Found = std::lower_bound(OptionTable,
205 OptionTable + OptionTableSize,
206 Key);
207 if (Found == OptionTable + OptionTableSize ||
Sebastian Redlc5613db2009-03-07 12:09:25 +0000208 strcmp(Found->Name, Key.Name) != 0) {
209 fprintf(stderr, "Unknown warning option: -W%s\n", Key.Name);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000210 return true;
Sebastian Redlc5613db2009-03-07 12:09:25 +0000211 }
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000212
213 // Option exists.
214 for (size_t i = 0; i < Found->NumMembers; ++i) {
215 Diags.setDiagnosticMapping(Found->Members[i], it->Mapping);
216 }
217 }
218 return false;
219}
220
221}