blob: 1ae122dc7a36d14fe81314729b1e5fd1a14eeda0 [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,
Chris Lattnerb54b2762009-04-16 05:04:32 +000068 Diagnostic &Diags) {
Chris Lattner8ac3af92009-04-16 00:53:55 +000069 // Option exists, poke all the members of its diagnostic set.
70 if (const short *Member = Group->Members) {
Chris Lattnerb54b2762009-04-16 05:04:32 +000071 for (; *Member != -1; ++Member)
Chris Lattner8ac3af92009-04-16 00:53:55 +000072 Diags.setDiagnosticMapping(*Member, Mapping);
Chris Lattner8ac3af92009-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 Lattnerb54b2762009-04-16 05:04:32 +000078 MapGroupMembers(&OptionTable[(unsigned char)*SubGroups], Mapping, Diags);
Chris Lattner8ac3af92009-04-16 00:53:55 +000079 }
80}
81
Chris Lattnerd613c3d2009-04-08 22:37:15 +000082bool clang::ProcessWarningOptions(Diagnostic &Diags) {
Chris Lattner27ceb9d2009-04-15 07:01:18 +000083 Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000084 Diags.setIgnoreAllWarnings(OptNoWarnings);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000085
Chris Lattnerb54b2762009-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 Lattner6280dbc2009-04-15 04:51:48 +000096 // FIXME: -Wfatal-errors / -Wfatal-errors=foo
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000097
Chris Lattner5147e8e2009-04-15 04:27:38 +000098 for (unsigned i = 0, e = OptWarnings.size(); i != e; ++i) {
99 const std::string &Opt = OptWarnings[i];
100 const char *OptStart = &Opt[0];
101 const char *OptEnd = OptStart+Opt.size();
102 assert(*OptEnd == 0 && "Expect null termination for lower-bound search");
103
104 // Check to see if this warning starts with "no-", if so, this is a negative
105 // form of the option.
106 bool isPositive = true;
107 if (OptEnd-OptStart > 3 && memcmp(OptStart, "no-", 3) == 0) {
108 isPositive = false;
109 OptStart += 3;
110 }
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000111
Chris Lattner1d13a5d2009-04-15 04:37:12 +0000112 // Figure out how this option affects the warning. If -Wfoo, map the
113 // diagnostic to a warning, if -Wno-foo, map it to ignore.
114 diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE;
115
116 // -Wsystem-headers is a special case, not driven by the option table. It
117 // cannot be controlled with -Werror.
Chris Lattner5147e8e2009-04-15 04:27:38 +0000118 if (OptEnd-OptStart == 14 && memcmp(OptStart, "system-headers", 14) == 0) {
119 Diags.setSuppressSystemWarnings(!isPositive);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000120 continue;
121 }
Chris Lattner5147e8e2009-04-15 04:27:38 +0000122
123 // -Werror/-Wno-error is a special case, not controlled by the option table.
Chris Lattner5b912d92009-04-19 22:07:21 +0000124 // It also has the "specifier" form of -Werror=foo and -Werror-foo.
Chris Lattner5147e8e2009-04-15 04:27:38 +0000125 if (OptEnd-OptStart >= 5 && memcmp(OptStart, "error", 5) == 0) {
126 const char *Specifier = 0;
127 if (OptEnd-OptStart != 5) { // Specifier must be present.
Chris Lattner5b912d92009-04-19 22:07:21 +0000128 if ((OptStart[5] != '=' && OptStart[5] != '-') ||
129 OptEnd-OptStart == 6) {
Chris Lattner9a7e5562009-04-15 22:48:58 +0000130 fprintf(stderr, "warning: unknown -Werror warning specifier: -W%s\n",
131 Opt.c_str());
132 continue;
Chris Lattner5147e8e2009-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 Lattner1d13a5d2009-04-15 04:37:12 +0000142 // -Werror=foo maps foo to Error, -Wno-error=foo maps it to Warning.
Chris Lattner2b07d8f2009-04-16 04:32:54 +0000143 Mapping = isPositive ? diag::MAP_ERROR : diag::MAP_WARNING_NO_WERROR;
Chris Lattner1d13a5d2009-04-15 04:37:12 +0000144 OptStart = Specifier;
Chris Lattner5147e8e2009-04-15 04:27:38 +0000145 }
146
Chris Lattner8ac3af92009-04-16 00:53:55 +0000147 WarningOption Key = { OptStart, 0, 0 };
Chris Lattnerd613c3d2009-04-08 22:37:15 +0000148 const WarningOption *Found =
149 std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
150 WarningOptionCompare);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000151 if (Found == OptionTable + OptionTableSize ||
Chris Lattner1d13a5d2009-04-15 04:37:12 +0000152 strcmp(Found->Name, OptStart) != 0) {
Chris Lattner9a7e5562009-04-15 22:48:58 +0000153 fprintf(stderr, "warning: unknown warning option: -W%s\n", Opt.c_str());
154 continue;
Sebastian Redlc5613db2009-03-07 12:09:25 +0000155 }
Chris Lattner5147e8e2009-04-15 04:27:38 +0000156
Chris Lattnerb54b2762009-04-16 05:04:32 +0000157 MapGroupMembers(Found, Mapping, Diags);
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000158 }
159
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000160 return false;
161}