blob: 829ac9e661c72ce8ed6072993d54c3a14253a7c4 [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-
Chris Lattnera3b089e2009-12-23 18:53:37 +000016// generated data, and the special cases -pedantic, -pedantic-errors, -w,
17// -Werror and -Wfatal-errors.
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000018//
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 Lattnera3b089e2009-12-23 18:53:37 +000021// -Wfoo, -Wno-foo, -Werror=foo, -Wfatal-errors=foo
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000022//
Eli Friedmanb09f6e12009-05-19 04:14:29 +000023#include "clang/Frontend/Utils.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"
Daniel Dunbar69079432009-11-12 07:28:44 +000027#include "clang/Frontend/DiagnosticOptions.h"
Chris Lattner8ea841b2009-06-23 01:20:39 +000028#include "clang/Frontend/FrontendDiagnostic.h"
Eli Friedmanac8d6292009-05-19 04:30:57 +000029#include <cstring>
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000030#include <utility>
31#include <algorithm>
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000032using namespace clang;
33
Kovarththanan Rajaratnam5bf932b2010-03-17 09:36:02 +000034void clang::ProcessWarningOptions(Diagnostic &Diags,
Daniel Dunbar69079432009-11-12 07:28:44 +000035 const DiagnosticOptions &Opts) {
Chris Lattner27ceb9d2009-04-15 07:01:18 +000036 Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
Daniel Dunbar69079432009-11-12 07:28:44 +000037 Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
Jeffrey Yasskin5edbdcc2010-06-11 05:57:47 +000038 Diags.setShowOverloads(
39 static_cast<Diagnostic::OverloadsShown>(Opts.ShowOverloads));
Chris Lattnerc1002142010-04-07 20:37:06 +000040
41 // Handle -ferror-limit
42 if (Opts.ErrorLimit)
43 Diags.setErrorLimit(Opts.ErrorLimit);
Douglas Gregor575cf372010-04-20 07:18:24 +000044 if (Opts.TemplateBacktraceLimit)
45 Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
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.
Daniel Dunbar69079432009-11-12 07:28:44 +000050 if (Opts.PedanticErrors)
Chris Lattnerb54b2762009-04-16 05:04:32 +000051 Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Error);
Daniel Dunbar69079432009-11-12 07:28:44 +000052 else if (Opts.Pedantic)
Chris Lattnerb54b2762009-04-16 05:04:32 +000053 Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Warn);
54 else
55 Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Ignore);
Mike Stump1eb44332009-09-09 15:08:12 +000056
Daniel Dunbar69079432009-11-12 07:28:44 +000057 for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000058 llvm::StringRef Opt = Opts.Warnings[i];
Mike Stump1eb44332009-09-09 15:08:12 +000059
Chris Lattner5147e8e2009-04-15 04:27:38 +000060 // Check to see if this warning starts with "no-", if so, this is a negative
61 // form of the option.
62 bool isPositive = true;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000063 if (Opt.startswith("no-")) {
Chris Lattner5147e8e2009-04-15 04:27:38 +000064 isPositive = false;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000065 Opt = Opt.substr(3);
Chris Lattner5147e8e2009-04-15 04:27:38 +000066 }
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000067
Chris Lattner1d13a5d2009-04-15 04:37:12 +000068 // Figure out how this option affects the warning. If -Wfoo, map the
69 // diagnostic to a warning, if -Wno-foo, map it to ignore.
70 diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE;
71
72 // -Wsystem-headers is a special case, not driven by the option table. It
73 // cannot be controlled with -Werror.
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000074 if (Opt == "system-headers") {
Chris Lattner5147e8e2009-04-15 04:27:38 +000075 Diags.setSuppressSystemWarnings(!isPositive);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000076 continue;
77 }
Mike Stump1eb44332009-09-09 15:08:12 +000078
Chris Lattner5147e8e2009-04-15 04:27:38 +000079 // -Werror/-Wno-error is a special case, not controlled by the option table.
Chris Lattner5b912d92009-04-19 22:07:21 +000080 // It also has the "specifier" form of -Werror=foo and -Werror-foo.
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000081 if (Opt.startswith("error")) {
82 llvm::StringRef Specifier;
83 if (Opt.size() > 5) { // Specifier must be present.
84 if ((Opt[5] != '=' && Opt[5] != '-') || Opt.size() == 6) {
Chris Lattnera3b089e2009-12-23 18:53:37 +000085 Diags.Report(diag::warn_unknown_warning_specifier)
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000086 << "-Werror" << ("-W" + Opt.str());
Chris Lattner9a7e5562009-04-15 22:48:58 +000087 continue;
Chris Lattner5147e8e2009-04-15 04:27:38 +000088 }
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000089 Specifier = Opt.substr(6);
Chris Lattner5147e8e2009-04-15 04:27:38 +000090 }
Mike Stump1eb44332009-09-09 15:08:12 +000091
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000092 if (Specifier.empty()) {
Shantonu Senb1d76bd2009-08-14 04:07:15 +000093 Diags.setWarningsAsErrors(isPositive);
Chris Lattner5147e8e2009-04-15 04:27:38 +000094 continue;
95 }
Mike Stump1eb44332009-09-09 15:08:12 +000096
Chris Lattner1d13a5d2009-04-15 04:37:12 +000097 // -Werror=foo maps foo to Error, -Wno-error=foo maps it to Warning.
Chris Lattner2b07d8f2009-04-16 04:32:54 +000098 Mapping = isPositive ? diag::MAP_ERROR : diag::MAP_WARNING_NO_WERROR;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +000099 Opt = Specifier;
Chris Lattner5147e8e2009-04-15 04:27:38 +0000100 }
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Chris Lattnere663c722009-12-22 23:12:53 +0000102 // -Wfatal-errors is yet another special case.
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000103 if (Opt.startswith("fatal-errors")) {
104 llvm::StringRef Specifier;
105 if (Opt.size() != 12) {
106 if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) {
Chris Lattnera3b089e2009-12-23 18:53:37 +0000107 Diags.Report(diag::warn_unknown_warning_specifier)
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000108 << "-Wfatal-errors" << ("-W" + Opt.str());
Chris Lattnere663c722009-12-22 23:12:53 +0000109 continue;
110 }
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000111 Specifier = Opt.substr(13);
Chris Lattnere663c722009-12-22 23:12:53 +0000112 }
113
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000114 if (Specifier.empty()) {
Chris Lattnere663c722009-12-22 23:12:53 +0000115 Diags.setErrorsAsFatal(isPositive);
116 continue;
117 }
118
119 // -Wfatal-errors=foo maps foo to Fatal, -Wno-fatal-errors=foo
120 // maps it to Error.
121 Mapping = isPositive ? diag::MAP_FATAL : diag::MAP_ERROR_NO_WFATAL;
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000122 Opt = Specifier;
Chris Lattnere663c722009-12-22 23:12:53 +0000123 }
124
Argyrios Kyrtzidis477aab62011-05-25 05:05:01 +0000125 if (Diags.setDiagnosticGroupMapping(Opt, Mapping))
126 Diags.Report(diag::warn_unknown_warning_option) << ("-W" + Opt.str());
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000127 }
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000128}