blob: ccaa6533e1cd58a29c4f01cb22fd872a02f75a47 [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 Lattner8ac3af92009-04-16 00:53:55 +000021// -Wfoo, -Wno-foo, -Werror=foo
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000022//
Chris Lattnerd613c3d2009-04-08 22:37:15 +000023#include "clang-cc.h"
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000024#include "clang/Basic/Diagnostic.h"
25#include "clang/Sema/SemaDiagnostic.h"
26#include "clang/Lex/LexDiagnostic.h"
27#include "llvm/Support/CommandLine.h"
Chris Lattner3c304bd2009-04-11 18:40:46 +000028#include <cstdio>
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000029#include <utility>
30#include <algorithm>
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000031using namespace clang;
32
Chris Lattner5147e8e2009-04-15 04:27:38 +000033// This gets all -W options, including -Werror, -W[no-]system-headers, etc. The
Chris Lattner1d13a5d2009-04-15 04:37:12 +000034// driver has stripped off -Wa,foo etc. The driver has also translated -W to
35// -Wextra, so we don't need to worry about it.
Chris Lattner5147e8e2009-04-15 04:27:38 +000036static llvm::cl::list<std::string>
Chris Lattnerc83b60d2009-04-15 22:38:06 +000037OptWarnings("W", llvm::cl::Prefix, llvm::cl::ValueOptional);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000038
39static llvm::cl::opt<bool> OptPedantic("pedantic");
40static llvm::cl::opt<bool> OptPedanticErrors("pedantic-errors");
41static llvm::cl::opt<bool> OptNoWarnings("w");
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000042
Chris Lattnerd7492c42009-04-15 20:58:49 +000043struct WarningOption {
Chris Lattner8ac3af92009-04-16 00:53:55 +000044 const char *Name;
Chris Lattnerd7492c42009-04-15 20:58:49 +000045 const short *Members;
Chris Lattner8ac3af92009-04-16 00:53:55 +000046 const char *SubGroups;
Chris Lattnerd613c3d2009-04-08 22:37:15 +000047};
Steve Naroff17f689f2009-03-31 15:00:11 +000048
Chris Lattnerd7492c42009-04-15 20:58:49 +000049#define GET_DIAG_ARRAYS
50#include "clang/Basic/DiagnosticGroups.inc"
51#undef GET_DIAG_ARRAYS
52
53// Second the table of options, sorted by name for fast binary lookup.
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000054static const WarningOption OptionTable[] = {
Chris Lattnerd7492c42009-04-15 20:58:49 +000055#define GET_DIAG_TABLE
56#include "clang/Basic/DiagnosticGroups.inc"
57#undef GET_DIAG_TABLE
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000058};
59static const size_t OptionTableSize =
60 sizeof(OptionTable) / sizeof(OptionTable[0]);
61
Chris Lattnerd613c3d2009-04-08 22:37:15 +000062static bool WarningOptionCompare(const WarningOption &LHS,
63 const WarningOption &RHS) {
64 return strcmp(LHS.Name, RHS.Name) < 0;
65}
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000066
Chris Lattner8ac3af92009-04-16 00:53:55 +000067static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping,
68 Diagnostic &Diags,
69 llvm::SmallVectorImpl<unsigned short> &ControlledDiags) {
70 // Option exists, poke all the members of its diagnostic set.
71 if (const short *Member = Group->Members) {
72 for (; *Member != -1; ++Member) {
73 Diags.setDiagnosticMapping(*Member, Mapping);
74 ControlledDiags.push_back(*Member);
75 }
76 }
77
78 // Enable/disable all subgroups along with this one.
79 if (const char *SubGroups = Group->SubGroups) {
80 for (; *SubGroups != (char)-1; ++SubGroups)
81 MapGroupMembers(&OptionTable[(unsigned char)*SubGroups], Mapping,
82 Diags, ControlledDiags);
83 }
84}
85
Chris Lattnerd613c3d2009-04-08 22:37:15 +000086bool clang::ProcessWarningOptions(Diagnostic &Diags) {
Chris Lattner27ceb9d2009-04-15 07:01:18 +000087 Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000088 Diags.setIgnoreAllWarnings(OptNoWarnings);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000089
Chris Lattner6280dbc2009-04-15 04:51:48 +000090 // FIXME: -fdiagnostics-show-option
91 // FIXME: -Wfatal-errors / -Wfatal-errors=foo
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000092
Chris Lattner9dbbdbf2009-04-15 14:42:02 +000093 /// ControlledDiags - Keep track of the options that the user explicitly
Chris Lattner27ceb9d2009-04-15 07:01:18 +000094 /// poked with -Wfoo, -Wno-foo, or -Werror=foo.
95 llvm::SmallVector<unsigned short, 256> ControlledDiags;
96
Chris Lattner5147e8e2009-04-15 04:27:38 +000097 for (unsigned i = 0, e = OptWarnings.size(); i != e; ++i) {
98 const std::string &Opt = OptWarnings[i];
99 const char *OptStart = &Opt[0];
100 const char *OptEnd = OptStart+Opt.size();
101 assert(*OptEnd == 0 && "Expect null termination for lower-bound search");
102
103 // Check to see if this warning starts with "no-", if so, this is a negative
104 // form of the option.
105 bool isPositive = true;
106 if (OptEnd-OptStart > 3 && memcmp(OptStart, "no-", 3) == 0) {
107 isPositive = false;
108 OptStart += 3;
109 }
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000110
Chris Lattner1d13a5d2009-04-15 04:37:12 +0000111 // Figure out how this option affects the warning. If -Wfoo, map the
112 // diagnostic to a warning, if -Wno-foo, map it to ignore.
113 diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE;
114
115 // -Wsystem-headers is a special case, not driven by the option table. It
116 // cannot be controlled with -Werror.
Chris Lattner5147e8e2009-04-15 04:27:38 +0000117 if (OptEnd-OptStart == 14 && memcmp(OptStart, "system-headers", 14) == 0) {
118 Diags.setSuppressSystemWarnings(!isPositive);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000119 continue;
120 }
Chris Lattner5147e8e2009-04-15 04:27:38 +0000121
122 // -Werror/-Wno-error is a special case, not controlled by the option table.
123 // It also has the "specifier" form of -Werror=foo.
124 if (OptEnd-OptStart >= 5 && memcmp(OptStart, "error", 5) == 0) {
125 const char *Specifier = 0;
126 if (OptEnd-OptStart != 5) { // Specifier must be present.
127 if (OptStart[5] != '=' || OptEnd-OptStart == 6) {
Chris Lattner9a7e5562009-04-15 22:48:58 +0000128 fprintf(stderr, "warning: unknown -Werror warning specifier: -W%s\n",
129 Opt.c_str());
130 continue;
Chris Lattner5147e8e2009-04-15 04:27:38 +0000131 }
132 Specifier = OptStart+6;
133 }
134
135 if (Specifier == 0) {
136 Diags.setWarningsAsErrors(true);
137 continue;
138 }
139
Chris Lattner1d13a5d2009-04-15 04:37:12 +0000140 // -Werror=foo maps foo to Error, -Wno-error=foo maps it to Warning.
141 Mapping = isPositive ? diag::MAP_ERROR : diag::MAP_WARNING;
142 OptStart = Specifier;
Chris Lattner5147e8e2009-04-15 04:27:38 +0000143 }
144
Chris Lattner8ac3af92009-04-16 00:53:55 +0000145 WarningOption Key = { OptStart, 0, 0 };
Chris Lattnerd613c3d2009-04-08 22:37:15 +0000146 const WarningOption *Found =
147 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
148 WarningOptionCompare);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000149 if (Found == OptionTable + OptionTableSize ||
Chris Lattner1d13a5d2009-04-15 04:37:12 +0000150 strcmp(Found->Name, OptStart) != 0) {
Chris Lattner9a7e5562009-04-15 22:48:58 +0000151 fprintf(stderr, "warning: unknown warning option: -W%s\n", Opt.c_str());
152 continue;
Sebastian Redlc5613db2009-03-07 12:09:25 +0000153 }
Chris Lattner5147e8e2009-04-15 04:27:38 +0000154
Chris Lattner8ac3af92009-04-16 00:53:55 +0000155 MapGroupMembers(Found, Mapping, Diags, ControlledDiags);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000156 }
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000157
158 // If -pedantic or -pedantic-errors was specified, then we want to map all
159 // extension diagnostics onto WARNING or ERROR unless the user has futz'd
160 // around with them explicitly.
161 if (OptPedantic || OptPedanticErrors) {
162 // Sort the array of options that has been poked at directly so we can do
163 // efficient queries.
164 std::sort(ControlledDiags.begin(), ControlledDiags.end());
165
166 // Don't worry about iteration off the end down below.
167 ControlledDiags.push_back(diag::DIAG_UPPER_LIMIT);
168
169 diag::Mapping Mapping =
170 OptPedanticErrors ? diag::MAP_ERROR : diag::MAP_WARNING;
171
172 // Loop over all of the extension diagnostics. Unless they were explicitly
173 // controlled, reset their mapping to Mapping. We walk through the
Chris Lattner9dbbdbf2009-04-15 14:42:02 +0000174 // ControlledDiags in parallel with this walk, which is faster than
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000175 // repeatedly binary searching it.
176 //
177 llvm::SmallVectorImpl<unsigned short>::iterator ControlledDiagsIt =
178 ControlledDiags.begin();
179
180 // TODO: if it matters, we could make tblgen produce a list of just the
181 // extension diags to avoid skipping ones that don't matter.
182 for (unsigned short i = 0; i != diag::DIAG_UPPER_LIMIT; ++i) {
183 // If this diagnostic was controlled, ignore it.
184 if (i == *ControlledDiagsIt) {
185 ++ControlledDiagsIt;
186 while (i == *ControlledDiagsIt) // ControlledDiags can have dupes.
187 ++ControlledDiagsIt;
188 // Do not map this diagnostic ID#.
189 continue;
190 }
191
192 // Okay, the user didn't control this ID. If it is an example, map it.
193 if (Diagnostic::isBuiltinExtensionDiag(i))
194 Diags.setDiagnosticMapping(i, Mapping);
195 }
196 }
197
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000198 return false;
199}