blob: eda398be24efcb5f05110799255abf79d971243d [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//
25// Clang is parsed warning options. Each warning option controls any number of
26// actual warnings.
27// Given a warning option 'foo', the following are valid:
28// -Wfoo=ignore -> Ignore the controlled warnings.
29// -Wfoo=warn -> Warn about the controlled warnings.
30// -Wfoo=error -> Fail on the controlled warnings.
31// -Wfoo -> alias of -Wfoo=warn
32// -Wno-foo -> alias of -Wfoo=ignore
33// -Werror=foo -> alias of -Wfoo=error
34//
35// Because of this complex handling of options, the default parser is replaced.
36
37#include "clang/Basic/Diagnostic.h"
38#include "clang/Sema/SemaDiagnostic.h"
39#include "clang/Lex/LexDiagnostic.h"
40#include "llvm/Support/CommandLine.h"
41#include <vector>
42#include <string>
43#include <utility>
44#include <algorithm>
45#include <string.h>
46
47using namespace clang;
48
49namespace {
50 struct ParsedOption {
51 std::string Name;
52 diag::Mapping Mapping;
53
54 ParsedOption() {}
55 // Used by -Werror, implicitly.
56 ParsedOption(const std::string& name) : Name(name), Mapping(diag::MAP_ERROR)
57 {}
58 };
59
60 typedef std::vector<ParsedOption> OptionsList;
61
62 OptionsList Options;
63
64 struct WarningParser : public llvm::cl::basic_parser<ParsedOption> {
65 diag::Mapping StrToMapping(const std::string &S) {
66 if (S == "ignore")
67 return diag::MAP_IGNORE;
68 if (S == "warn")
69 return diag::MAP_WARNING;
70 if (S == "error")
71 return diag::MAP_ERROR;
72 return diag::MAP_DEFAULT;
73 }
74 bool parse(llvm::cl::Option &O, const char *ArgName,
75 const std::string &ArgValue, ParsedOption &Val)
76 {
77 size_t Eq = ArgValue.find("=");
78 if (Eq == std::string::npos) {
79 // Could be -Wfoo or -Wno-foo
80 if (ArgValue.compare(0, 3, "no-") == 0) {
81 Val.Name = ArgValue.substr(3);
82 Val.Mapping = diag::MAP_IGNORE;
83 } else {
84 Val.Name = ArgValue;
85 Val.Mapping = diag::MAP_WARNING;
86 }
87 } else {
88 Val.Name = ArgValue.substr(0, Eq);
89 Val.Mapping = StrToMapping(ArgValue.substr(Eq+1));
90 if (Val.Mapping == diag::MAP_DEFAULT)
91 return true;
92 }
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>
109OptSuppressSystemWarnings("suppress-system-warnings",
110 llvm::cl::desc("Suppress warnings issued in system headers"),
111 llvm::cl::init(true));
112
113namespace {
114 struct WarningOption {
115 const char *Name;
116 const diag::kind *Members;
117 size_t NumMembers;
118 };
119 bool operator <(const WarningOption& lhs, const WarningOption& rhs) {
120 return strcmp(lhs.Name, rhs.Name) < 0;
121 }
122}
123#define DIAGS(a) a, (sizeof(a) / sizeof(a[0]))
124// These tables will be TableGenerated later.
125// First the table sets describing the diagnostics controlled by each option.
126static const diag::kind UnusedMacrosDiags[] = { diag::pp_macro_not_used };
127static const diag::kind FloatEqualDiags[] = { diag::warn_floatingpoint_eq };
128static const diag::kind ReadOnlySetterAttrsDiags[] = {
129 diag::warn_objc_property_attr_mutually_exclusive
130};
131static const diag::kind FormatNonLiteralDiags[] = {
132 diag::warn_printf_not_string_constant
133};
134static const diag::kind UndefDiags[] = { diag::warn_pp_undef_identifier };
135static const diag::kind ImplicitFunctionDeclarationDiags[] = {
136 diag::ext_implicit_function_decl, diag::warn_implicit_function_decl
137};
138// Hmm ... this option is currently actually completely ignored.
139//static const diag::kind StrictSelectorMatchDiags[] = { };
140// Second the table of options. MUST be sorted by name! Binary lookup is done.
141static const WarningOption OptionTable[] = {
142 { "float-equal", DIAGS(FloatEqualDiags) },
143 { "format-nonliteral", DIAGS(FormatNonLiteralDiags) },
144 { "implicit-function-declaration", DIAGS(ImplicitFunctionDeclarationDiags) },
145 { "readonly-setter-attrs", DIAGS(ReadOnlySetterAttrsDiags) },
146 { "undef", DIAGS(UndefDiags) },
147 { "unused-macros", DIAGS(UnusedMacrosDiags) },
148// { "strict-selector-match", DIAGS(StrictSelectorMatchDiags) }
149};
150static const size_t OptionTableSize =
151 sizeof(OptionTable) / sizeof(OptionTable[0]);
152
153namespace clang {
154
155bool ProcessWarningOptions(Diagnostic &Diags) {
156 // FIXME: These should be mapped to group options.
157 Diags.setIgnoreAllWarnings(OptNoWarnings);
158 Diags.setWarnOnExtensions(OptPedantic);
159 Diags.setErrorOnExtensions(OptPedanticErrors);
160
161 // Set some defaults that are currently set manually. This, too, should
162 // be in the tablegen stuff later.
163 Diags.setDiagnosticMapping(diag::pp_macro_not_used, diag::MAP_IGNORE);
164 Diags.setDiagnosticMapping(diag::warn_floatingpoint_eq, diag::MAP_IGNORE);
165 Diags.setDiagnosticMapping(diag::warn_objc_property_attr_mutually_exclusive,
166 diag::MAP_IGNORE);
167 Diags.setDiagnosticMapping(diag::warn_pp_undef_identifier, diag::MAP_IGNORE);
168 Diags.setDiagnosticMapping(diag::warn_implicit_function_decl,
169 diag::MAP_IGNORE);
170
171 Diags.setDiagnosticMapping(diag::err_pp_file_not_found, diag::MAP_FATAL);
172 Diags.setSuppressSystemWarnings(OptSuppressSystemWarnings);
173
174 for (OptionsList::iterator it = Options.begin(), e = Options.end();
175 it != e; ++it) {
176 if (it->Name.empty()) {
177 // Empty string is "everything". This way, -Werror does the right thing.
178 // FIXME: These flags do not participate in proper option overriding.
179 switch(it->Mapping) {
180 default:
181 assert(false && "Illegal mapping");
182 break;
183
184 case diag::MAP_IGNORE:
185 Diags.setIgnoreAllWarnings(true);
186 Diags.setWarningsAsErrors(false);
187 break;
188
189 case diag::MAP_WARNING:
190 Diags.setIgnoreAllWarnings(false);
191 Diags.setWarningsAsErrors(false);
192 break;
193
194 case diag::MAP_ERROR:
195 Diags.setIgnoreAllWarnings(false);
196 Diags.setWarningsAsErrors(true);
197 break;
198 }
199 continue;
200 }
201 WarningOption Key = { it->Name.c_str(), 0, 0 };
202 const WarningOption *Found = std::lower_bound(OptionTable,
203 OptionTable + OptionTableSize,
204 Key);
205 if (Found == OptionTable + OptionTableSize ||
206 strcmp(Found->Name, Key.Name) != 0)
207 return true;
208
209 // Option exists.
210 for (size_t i = 0; i < Found->NumMembers; ++i) {
211 Diags.setDiagnosticMapping(Found->Members[i], it->Mapping);
212 }
213 }
214 return false;
215}
216
217}