blob: bab2ab7a140e85331f142af718f42c1523142eeb [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//
Sebastian Redlf10cbca2009-03-07 12:09:25 +000019// Each warning option controls any number of actual warnings.
Sebastian Redl44ff86c2009-03-06 17:41:35 +000020// Given a warning option 'foo', the following are valid:
Chris Lattner6cf5f352009-04-16 00:53:55 +000021// -Wfoo, -Wno-foo, -Werror=foo
Sebastian Redl44ff86c2009-03-06 17:41:35 +000022//
Chris Lattnerae057262009-04-08 22:37:15 +000023#include "clang-cc.h"
Sebastian Redl44ff86c2009-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 Lattner64b65f82009-04-11 18:40:46 +000028#include <cstdio>
Sebastian Redl44ff86c2009-03-06 17:41:35 +000029#include <utility>
30#include <algorithm>
Sebastian Redl44ff86c2009-03-06 17:41:35 +000031using namespace clang;
32
Chris Lattnera38dd3c2009-04-15 04:27:38 +000033// This gets all -W options, including -Werror, -W[no-]system-headers, etc. The
Chris Lattnere8ad1c52009-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 Lattnera38dd3c2009-04-15 04:27:38 +000036static llvm::cl::list<std::string>
Chris Lattnera577ce32009-04-15 22:38:06 +000037OptWarnings("W", llvm::cl::Prefix, llvm::cl::ValueOptional);
Sebastian Redl44ff86c2009-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 Redl44ff86c2009-03-06 17:41:35 +000042
Chris Lattner70f6b1d2009-04-15 20:58:49 +000043struct WarningOption {
Chris Lattner6cf5f352009-04-16 00:53:55 +000044 const char *Name;
Chris Lattner70f6b1d2009-04-15 20:58:49 +000045 const short *Members;
Chris Lattner6cf5f352009-04-16 00:53:55 +000046 const char *SubGroups;
Chris Lattnerae057262009-04-08 22:37:15 +000047};
Steve Naroff82eff5c2009-03-31 15:00:11 +000048
Chris Lattner70f6b1d2009-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 Redl44ff86c2009-03-06 17:41:35 +000054static const WarningOption OptionTable[] = {
Chris Lattner70f6b1d2009-04-15 20:58:49 +000055#define GET_DIAG_TABLE
56#include "clang/Basic/DiagnosticGroups.inc"
57#undef GET_DIAG_TABLE
Sebastian Redl44ff86c2009-03-06 17:41:35 +000058};
59static const size_t OptionTableSize =
60 sizeof(OptionTable) / sizeof(OptionTable[0]);
61
Chris Lattnerae057262009-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 Redl44ff86c2009-03-06 17:41:35 +000066
Chris Lattner6cf5f352009-04-16 00:53:55 +000067static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping,
Chris Lattner85b1ac82009-04-16 05:04:32 +000068 Diagnostic &Diags) {
Chris Lattner6cf5f352009-04-16 00:53:55 +000069 // Option exists, poke all the members of its diagnostic set.
70 if (const short *Member = Group->Members) {
Chris Lattner85b1ac82009-04-16 05:04:32 +000071 for (; *Member != -1; ++Member)
Chris Lattner6cf5f352009-04-16 00:53:55 +000072 Diags.setDiagnosticMapping(*Member, Mapping);
Chris Lattner6cf5f352009-04-16 00:53:55 +000073 }
74
75 // Enable/disable all subgroups along with this one.
76 if (const char *SubGroups = Group->SubGroups) {
77 for (; *SubGroups != (char)-1; ++SubGroups)
Chris Lattner85b1ac82009-04-16 05:04:32 +000078 MapGroupMembers(&OptionTable[(unsigned char)*SubGroups], Mapping, Diags);
Chris Lattner6cf5f352009-04-16 00:53:55 +000079 }
80}
81
Chris Lattnerae057262009-04-08 22:37:15 +000082bool clang::ProcessWarningOptions(Diagnostic &Diags) {
Chris Lattner015b4e12009-04-15 07:01:18 +000083 Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
Sebastian Redl44ff86c2009-03-06 17:41:35 +000084 Diags.setIgnoreAllWarnings(OptNoWarnings);
Sebastian Redl44ff86c2009-03-06 17:41:35 +000085
Chris Lattner85b1ac82009-04-16 05:04:32 +000086 // If -pedantic or -pedantic-errors was specified, then we want to map all
87 // extension diagnostics onto WARNING or ERROR unless the user has futz'd
88 // around with them explicitly.
89 if (OptPedanticErrors)
90 Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Error);
91 else if (OptPedantic)
92 Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Warn);
93 else
94 Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Ignore);
95
Chris Lattnerd6f65e82009-04-15 04:51:48 +000096 // FIXME: -fdiagnostics-show-option
97 // FIXME: -Wfatal-errors / -Wfatal-errors=foo
Sebastian Redl44ff86c2009-03-06 17:41:35 +000098
Chris Lattnera38dd3c2009-04-15 04:27:38 +000099 for (unsigned i = 0, e = OptWarnings.size(); i != e; ++i) {
100 const std::string &Opt = OptWarnings[i];
101 const char *OptStart = &Opt[0];
102 const char *OptEnd = OptStart+Opt.size();
103 assert(*OptEnd == 0 && "Expect null termination for lower-bound search");
104
105 // Check to see if this warning starts with "no-", if so, this is a negative
106 // form of the option.
107 bool isPositive = true;
108 if (OptEnd-OptStart > 3 && memcmp(OptStart, "no-", 3) == 0) {
109 isPositive = false;
110 OptStart += 3;
111 }
Sebastian Redl44ff86c2009-03-06 17:41:35 +0000112
Chris Lattnere8ad1c52009-04-15 04:37:12 +0000113 // Figure out how this option affects the warning. If -Wfoo, map the
114 // diagnostic to a warning, if -Wno-foo, map it to ignore.
115 diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE;
116
117 // -Wsystem-headers is a special case, not driven by the option table. It
118 // cannot be controlled with -Werror.
Chris Lattnera38dd3c2009-04-15 04:27:38 +0000119 if (OptEnd-OptStart == 14 && memcmp(OptStart, "system-headers", 14) == 0) {
120 Diags.setSuppressSystemWarnings(!isPositive);
Sebastian Redl44ff86c2009-03-06 17:41:35 +0000121 continue;
122 }
Chris Lattnera38dd3c2009-04-15 04:27:38 +0000123
124 // -Werror/-Wno-error is a special case, not controlled by the option table.
125 // It also has the "specifier" form of -Werror=foo.
126 if (OptEnd-OptStart >= 5 && memcmp(OptStart, "error", 5) == 0) {
127 const char *Specifier = 0;
128 if (OptEnd-OptStart != 5) { // Specifier must be present.
129 if (OptStart[5] != '=' || OptEnd-OptStart == 6) {
Chris Lattner6e650102009-04-15 22:48:58 +0000130 fprintf(stderr, "warning: unknown -Werror warning specifier: -W%s\n",
131 Opt.c_str());
132 continue;
Chris Lattnera38dd3c2009-04-15 04:27:38 +0000133 }
134 Specifier = OptStart+6;
135 }
136
137 if (Specifier == 0) {
138 Diags.setWarningsAsErrors(true);
139 continue;
140 }
141
Chris Lattnere8ad1c52009-04-15 04:37:12 +0000142 // -Werror=foo maps foo to Error, -Wno-error=foo maps it to Warning.
Chris Lattner8444dd12009-04-16 04:32:54 +0000143 Mapping = isPositive ? diag::MAP_ERROR : diag::MAP_WARNING_NO_WERROR;
Chris Lattnere8ad1c52009-04-15 04:37:12 +0000144 OptStart = Specifier;
Chris Lattnera38dd3c2009-04-15 04:27:38 +0000145 }
146
Chris Lattner6cf5f352009-04-16 00:53:55 +0000147 WarningOption Key = { OptStart, 0, 0 };
Chris Lattnerae057262009-04-08 22:37:15 +0000148 const WarningOption *Found =
149 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
150 WarningOptionCompare);
Sebastian Redl44ff86c2009-03-06 17:41:35 +0000151 if (Found == OptionTable + OptionTableSize ||
Chris Lattnere8ad1c52009-04-15 04:37:12 +0000152 strcmp(Found->Name, OptStart) != 0) {
Chris Lattner6e650102009-04-15 22:48:58 +0000153 fprintf(stderr, "warning: unknown warning option: -W%s\n", Opt.c_str());
154 continue;
Sebastian Redlf10cbca2009-03-07 12:09:25 +0000155 }
Chris Lattnera38dd3c2009-04-15 04:27:38 +0000156
Chris Lattner85b1ac82009-04-16 05:04:32 +0000157 MapGroupMembers(Found, Mapping, Diags);
Chris Lattner015b4e12009-04-15 07:01:18 +0000158 }
159
Sebastian Redl44ff86c2009-03-06 17:41:35 +0000160 return false;
161}