blob: ca540a39983c2a5a1202f787becaabc526669479 [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//
Sebastian Redlc5613db2009-03-07 12:09:25 +000019// Each warning option controls any number of actual warnings.
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000020// Given a warning option 'foo', the following are valid:
Chris Lattner5147e8e2009-04-15 04:27:38 +000021//
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000022// -Wfoo -> alias of -Wfoo=warn
23// -Wno-foo -> alias of -Wfoo=ignore
24// -Werror=foo -> alias of -Wfoo=error
25//
Chris Lattnerd613c3d2009-04-08 22:37:15 +000026#include "clang-cc.h"
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000027#include "clang/Basic/Diagnostic.h"
28#include "clang/Sema/SemaDiagnostic.h"
29#include "clang/Lex/LexDiagnostic.h"
30#include "llvm/Support/CommandLine.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000031#include <cstdio>
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000032#include <utility>
33#include <algorithm>
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000034using namespace clang;
35
Chris Lattner5147e8e2009-04-15 04:27:38 +000036// This gets all -W options, including -Werror, -W[no-]system-headers, etc. The
Chris Lattner1d13a5d2009-04-15 04:37:12 +000037// driver has stripped off -Wa,foo etc. The driver has also translated -W to
38// -Wextra, so we don't need to worry about it.
Chris Lattner5147e8e2009-04-15 04:27:38 +000039static llvm::cl::list<std::string>
40OptWarnings("W", llvm::cl::Prefix);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000041
42static llvm::cl::opt<bool> OptPedantic("pedantic");
43static llvm::cl::opt<bool> OptPedanticErrors("pedantic-errors");
44static llvm::cl::opt<bool> OptNoWarnings("w");
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000045
46namespace {
47 struct WarningOption {
48 const char *Name;
49 const diag::kind *Members;
Chris Lattner5147e8e2009-04-15 04:27:38 +000050 unsigned NumMembers;
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000051 };
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000052}
Chris Lattner5147e8e2009-04-15 04:27:38 +000053#define DIAGS(a) a, unsigned(sizeof(a) / sizeof(a[0]))
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000054// These tables will be TableGenerated later.
55// First the table sets describing the diagnostics controlled by each option.
56static const diag::kind UnusedMacrosDiags[] = { diag::pp_macro_not_used };
57static const diag::kind FloatEqualDiags[] = { diag::warn_floatingpoint_eq };
Chris Lattner7eba5c92009-04-14 20:36:01 +000058static const diag::kind ExtraTokens[] = { diag::ext_pp_extra_tokens_at_eol };
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000059static const diag::kind ReadOnlySetterAttrsDiags[] = {
60 diag::warn_objc_property_attr_mutually_exclusive
61};
62static const diag::kind FormatNonLiteralDiags[] = {
63 diag::warn_printf_not_string_constant
64};
65static const diag::kind UndefDiags[] = { diag::warn_pp_undef_identifier };
66static const diag::kind ImplicitFunctionDeclarationDiags[] = {
67 diag::ext_implicit_function_decl, diag::warn_implicit_function_decl
68};
Eli Friedman757e0dd2009-03-30 21:19:48 +000069static const diag::kind PointerSignDiags[] = {
70 diag::ext_typecheck_convert_incompatible_pointer_sign
71};
Steve Naroff17f689f2009-03-31 15:00:11 +000072static const diag::kind DeprecatedDeclarations[] = { diag::warn_deprecated };
Douglas Gregor8499f3f2009-03-31 16:35:03 +000073static const diag::kind MissingPrototypesDiags[] = {
74 diag::warn_missing_prototype
75};
Chris Lattnerd613c3d2009-04-08 22:37:15 +000076static const diag::kind TrigraphsDiags[] = {
77 diag::trigraph_ignored, diag::trigraph_ignored_block_comment,
78 diag::trigraph_ends_block_comment, diag::trigraph_converted
79};
Steve Naroff17f689f2009-03-31 15:00:11 +000080
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000081// Second the table of options. MUST be sorted by name! Binary lookup is done.
82static const WarningOption OptionTable[] = {
Chris Lattner7eba5c92009-04-14 20:36:01 +000083 { "deprecated-declarations", DIAGS(DeprecatedDeclarations) },
84 { "extra-tokens", DIAGS(ExtraTokens) },
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000085 { "float-equal", DIAGS(FloatEqualDiags) },
86 { "format-nonliteral", DIAGS(FormatNonLiteralDiags) },
87 { "implicit-function-declaration", DIAGS(ImplicitFunctionDeclarationDiags) },
Douglas Gregor8499f3f2009-03-31 16:35:03 +000088 { "missing-prototypes", DIAGS(MissingPrototypesDiags) },
Eli Friedman757e0dd2009-03-30 21:19:48 +000089 { "pointer-sign", DIAGS(PointerSignDiags) },
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000090 { "readonly-setter-attrs", DIAGS(ReadOnlySetterAttrsDiags) },
Chris Lattnerd613c3d2009-04-08 22:37:15 +000091 { "trigraphs", DIAGS(TrigraphsDiags) },
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000092 { "undef", DIAGS(UndefDiags) },
93 { "unused-macros", DIAGS(UnusedMacrosDiags) },
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000094};
95static const size_t OptionTableSize =
96 sizeof(OptionTable) / sizeof(OptionTable[0]);
97
Chris Lattnerd613c3d2009-04-08 22:37:15 +000098static bool WarningOptionCompare(const WarningOption &LHS,
99 const WarningOption &RHS) {
100 return strcmp(LHS.Name, RHS.Name) < 0;
101}
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000102
Chris Lattnerd613c3d2009-04-08 22:37:15 +0000103bool clang::ProcessWarningOptions(Diagnostic &Diags) {
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000104 Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000105 Diags.setIgnoreAllWarnings(OptNoWarnings);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000106
Chris Lattner6280dbc2009-04-15 04:51:48 +0000107 // FIXME: -fdiagnostics-show-option
108 // FIXME: -Wfatal-errors / -Wfatal-errors=foo
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000109
Chris Lattner9dbbdbf2009-04-15 14:42:02 +0000110 /// ControlledDiags - Keep track of the options that the user explicitly
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000111 /// poked with -Wfoo, -Wno-foo, or -Werror=foo.
112 llvm::SmallVector<unsigned short, 256> ControlledDiags;
113
Chris Lattner5147e8e2009-04-15 04:27:38 +0000114 for (unsigned i = 0, e = OptWarnings.size(); i != e; ++i) {
115 const std::string &Opt = OptWarnings[i];
116 const char *OptStart = &Opt[0];
117 const char *OptEnd = OptStart+Opt.size();
118 assert(*OptEnd == 0 && "Expect null termination for lower-bound search");
119
120 // Check to see if this warning starts with "no-", if so, this is a negative
121 // form of the option.
122 bool isPositive = true;
123 if (OptEnd-OptStart > 3 && memcmp(OptStart, "no-", 3) == 0) {
124 isPositive = false;
125 OptStart += 3;
126 }
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000127
Chris Lattner1d13a5d2009-04-15 04:37:12 +0000128 // Figure out how this option affects the warning. If -Wfoo, map the
129 // diagnostic to a warning, if -Wno-foo, map it to ignore.
130 diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE;
131
132 // -Wsystem-headers is a special case, not driven by the option table. It
133 // cannot be controlled with -Werror.
Chris Lattner5147e8e2009-04-15 04:27:38 +0000134 if (OptEnd-OptStart == 14 && memcmp(OptStart, "system-headers", 14) == 0) {
135 Diags.setSuppressSystemWarnings(!isPositive);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000136 continue;
137 }
Chris Lattner5147e8e2009-04-15 04:27:38 +0000138
139 // -Werror/-Wno-error is a special case, not controlled by the option table.
140 // It also has the "specifier" form of -Werror=foo.
141 if (OptEnd-OptStart >= 5 && memcmp(OptStart, "error", 5) == 0) {
142 const char *Specifier = 0;
143 if (OptEnd-OptStart != 5) { // Specifier must be present.
144 if (OptStart[5] != '=' || OptEnd-OptStart == 6) {
Chris Lattner6280dbc2009-04-15 04:51:48 +0000145 fprintf(stderr, "error: unknown warning option: -W%s\n", Opt.c_str());
Chris Lattner5147e8e2009-04-15 04:27:38 +0000146 return true;
147 }
148 Specifier = OptStart+6;
149 }
150
151 if (Specifier == 0) {
152 Diags.setWarningsAsErrors(true);
153 continue;
154 }
155
Chris Lattner1d13a5d2009-04-15 04:37:12 +0000156 // -Werror=foo maps foo to Error, -Wno-error=foo maps it to Warning.
157 Mapping = isPositive ? diag::MAP_ERROR : diag::MAP_WARNING;
158 OptStart = Specifier;
Chris Lattner5147e8e2009-04-15 04:27:38 +0000159 }
160
161 WarningOption Key = { OptStart, 0, 0 };
Chris Lattnerd613c3d2009-04-08 22:37:15 +0000162 const WarningOption *Found =
163 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
164 WarningOptionCompare);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000165 if (Found == OptionTable + OptionTableSize ||
Chris Lattner1d13a5d2009-04-15 04:37:12 +0000166 strcmp(Found->Name, OptStart) != 0) {
Chris Lattner6280dbc2009-04-15 04:51:48 +0000167 fprintf(stderr, "error: unknown warning option: -W%s\n", Opt.c_str());
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000168 return true;
Sebastian Redlc5613db2009-03-07 12:09:25 +0000169 }
Chris Lattner5147e8e2009-04-15 04:27:38 +0000170
Chris Lattner6280dbc2009-04-15 04:51:48 +0000171 // Option exists, poke all the members of its diagnostic set.
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000172 for (const diag::kind *Member = Found->Members,
173 *E = Found->Members+Found->NumMembers; Member != E; ++Member) {
174 Diags.setDiagnosticMapping(*Member, Mapping);
Chris Lattner9dbbdbf2009-04-15 14:42:02 +0000175 assert(*Member < 65536 && "ControlledDiags element too small");
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000176 ControlledDiags.push_back(*Member);
177 }
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000178 }
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000179
180 // If -pedantic or -pedantic-errors was specified, then we want to map all
181 // extension diagnostics onto WARNING or ERROR unless the user has futz'd
182 // around with them explicitly.
183 if (OptPedantic || OptPedanticErrors) {
184 // Sort the array of options that has been poked at directly so we can do
185 // efficient queries.
186 std::sort(ControlledDiags.begin(), ControlledDiags.end());
187
188 // Don't worry about iteration off the end down below.
189 ControlledDiags.push_back(diag::DIAG_UPPER_LIMIT);
190
191 diag::Mapping Mapping =
192 OptPedanticErrors ? diag::MAP_ERROR : diag::MAP_WARNING;
193
194 // Loop over all of the extension diagnostics. Unless they were explicitly
195 // controlled, reset their mapping to Mapping. We walk through the
Chris Lattner9dbbdbf2009-04-15 14:42:02 +0000196 // ControlledDiags in parallel with this walk, which is faster than
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000197 // repeatedly binary searching it.
198 //
199 llvm::SmallVectorImpl<unsigned short>::iterator ControlledDiagsIt =
200 ControlledDiags.begin();
201
202 // TODO: if it matters, we could make tblgen produce a list of just the
203 // extension diags to avoid skipping ones that don't matter.
204 for (unsigned short i = 0; i != diag::DIAG_UPPER_LIMIT; ++i) {
205 // If this diagnostic was controlled, ignore it.
206 if (i == *ControlledDiagsIt) {
207 ++ControlledDiagsIt;
208 while (i == *ControlledDiagsIt) // ControlledDiags can have dupes.
209 ++ControlledDiagsIt;
210 // Do not map this diagnostic ID#.
211 continue;
212 }
213
214 // Okay, the user didn't control this ID. If it is an example, map it.
215 if (Diagnostic::isBuiltinExtensionDiag(i))
216 Diags.setDiagnosticMapping(i, Mapping);
217 }
218 }
219
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000220 return false;
221}