blob: c3a6b2132a692c907c1bf7d435b91a112e36a1e4 [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 Lattnerd613c3d2009-04-08 22:37:15 +000043bool clang::ProcessWarningOptions(Diagnostic &Diags) {
Chris Lattner27ceb9d2009-04-15 07:01:18 +000044 Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000045 Diags.setIgnoreAllWarnings(OptNoWarnings);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000046
Chris Lattnerb54b2762009-04-16 05:04:32 +000047 // If -pedantic or -pedantic-errors was specified, then we want to map all
48 // extension diagnostics onto WARNING or ERROR unless the user has futz'd
49 // around with them explicitly.
50 if (OptPedanticErrors)
51 Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Error);
52 else if (OptPedantic)
53 Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Warn);
54 else
55 Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Ignore);
56
Chris Lattner6280dbc2009-04-15 04:51:48 +000057 // FIXME: -Wfatal-errors / -Wfatal-errors=foo
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000058
Chris Lattner5147e8e2009-04-15 04:27:38 +000059 for (unsigned i = 0, e = OptWarnings.size(); i != e; ++i) {
60 const std::string &Opt = OptWarnings[i];
61 const char *OptStart = &Opt[0];
62 const char *OptEnd = OptStart+Opt.size();
63 assert(*OptEnd == 0 && "Expect null termination for lower-bound search");
64
65 // Check to see if this warning starts with "no-", if so, this is a negative
66 // form of the option.
67 bool isPositive = true;
68 if (OptEnd-OptStart > 3 && memcmp(OptStart, "no-", 3) == 0) {
69 isPositive = false;
70 OptStart += 3;
71 }
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000072
Chris Lattner1d13a5d2009-04-15 04:37:12 +000073 // Figure out how this option affects the warning. If -Wfoo, map the
74 // diagnostic to a warning, if -Wno-foo, map it to ignore.
75 diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE;
76
77 // -Wsystem-headers is a special case, not driven by the option table. It
78 // cannot be controlled with -Werror.
Chris Lattner5147e8e2009-04-15 04:27:38 +000079 if (OptEnd-OptStart == 14 && memcmp(OptStart, "system-headers", 14) == 0) {
80 Diags.setSuppressSystemWarnings(!isPositive);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000081 continue;
82 }
Chris Lattner5147e8e2009-04-15 04:27:38 +000083
84 // -Werror/-Wno-error is a special case, not controlled by the option table.
Chris Lattner5b912d92009-04-19 22:07:21 +000085 // It also has the "specifier" form of -Werror=foo and -Werror-foo.
Chris Lattner5147e8e2009-04-15 04:27:38 +000086 if (OptEnd-OptStart >= 5 && memcmp(OptStart, "error", 5) == 0) {
87 const char *Specifier = 0;
88 if (OptEnd-OptStart != 5) { // Specifier must be present.
Chris Lattner5b912d92009-04-19 22:07:21 +000089 if ((OptStart[5] != '=' && OptStart[5] != '-') ||
90 OptEnd-OptStart == 6) {
Chris Lattner9a7e5562009-04-15 22:48:58 +000091 fprintf(stderr, "warning: unknown -Werror warning specifier: -W%s\n",
92 Opt.c_str());
93 continue;
Chris Lattner5147e8e2009-04-15 04:27:38 +000094 }
95 Specifier = OptStart+6;
96 }
97
98 if (Specifier == 0) {
99 Diags.setWarningsAsErrors(true);
100 continue;
101 }
102
Chris Lattner1d13a5d2009-04-15 04:37:12 +0000103 // -Werror=foo maps foo to Error, -Wno-error=foo maps it to Warning.
Chris Lattner2b07d8f2009-04-16 04:32:54 +0000104 Mapping = isPositive ? diag::MAP_ERROR : diag::MAP_WARNING_NO_WERROR;
Chris Lattner1d13a5d2009-04-15 04:37:12 +0000105 OptStart = Specifier;
Chris Lattner5147e8e2009-04-15 04:27:38 +0000106 }
107
Chris Lattner3bc172b2009-04-19 22:34:23 +0000108 if (Diags.setDiagnosticGroupMapping(OptStart, Mapping))
Chris Lattner9a7e5562009-04-15 22:48:58 +0000109 fprintf(stderr, "warning: unknown warning option: -W%s\n", Opt.c_str());
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000110 }
111
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000112 return false;
113}