blob: e63bc21cb4bd3d4e7e6008920b0297bce17e21fa [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
Chad Rosier99643d92011-11-15 19:03:03 +000034// EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning opts
35static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags,
Benjamin Kramerdce63272011-11-15 12:26:39 +000036 StringRef Prefix, StringRef Opt,
37 bool isPositive) {
38 StringRef Suggestion = DiagnosticIDs::getNearestWarningOption(Opt);
39 if (!Suggestion.empty())
40 Diags.Report(isPositive? diag::warn_unknown_warning_option_suggest :
41 diag::warn_unknown_negative_warning_option_suggest)
42 << (Prefix.str() += Opt) << (Prefix.str() += Suggestion);
43 else
44 Diags.Report(isPositive? diag::warn_unknown_warning_option :
45 diag::warn_unknown_negative_warning_option)
46 << (Prefix.str() += Opt);
47}
48
David Blaikied6471f72011-09-25 23:23:43 +000049void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
Daniel Dunbar69079432009-11-12 07:28:44 +000050 const DiagnosticOptions &Opts) {
Chris Lattner27ceb9d2009-04-15 07:01:18 +000051 Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
Daniel Dunbar69079432009-11-12 07:28:44 +000052 Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
Jeffrey Yasskin5edbdcc2010-06-11 05:57:47 +000053 Diags.setShowOverloads(
David Blaikied6471f72011-09-25 23:23:43 +000054 static_cast<DiagnosticsEngine::OverloadsShown>(Opts.ShowOverloads));
Chris Lattnerc1002142010-04-07 20:37:06 +000055
56 // Handle -ferror-limit
57 if (Opts.ErrorLimit)
58 Diags.setErrorLimit(Opts.ErrorLimit);
Douglas Gregor575cf372010-04-20 07:18:24 +000059 if (Opts.TemplateBacktraceLimit)
60 Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
Sebastian Redl63a9e0f2009-03-06 17:41:35 +000061
Chris Lattnerb54b2762009-04-16 05:04:32 +000062 // If -pedantic or -pedantic-errors was specified, then we want to map all
63 // extension diagnostics onto WARNING or ERROR unless the user has futz'd
64 // around with them explicitly.
Daniel Dunbar69079432009-11-12 07:28:44 +000065 if (Opts.PedanticErrors)
David Blaikied6471f72011-09-25 23:23:43 +000066 Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Error);
Daniel Dunbar69079432009-11-12 07:28:44 +000067 else if (Opts.Pedantic)
David Blaikied6471f72011-09-25 23:23:43 +000068 Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Warn);
Chris Lattnerb54b2762009-04-16 05:04:32 +000069 else
David Blaikied6471f72011-09-25 23:23:43 +000070 Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Ignore);
Mike Stump1eb44332009-09-09 15:08:12 +000071
Chad Rosier05272a62011-11-03 21:23:39 +000072 llvm::SmallVector<diag::kind, 10> _Diags;
73 const llvm::IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs =
74 Diags.getDiagnosticIDs();
75 // We parse the warning options twice. The first pass sets diagnostic state,
76 // while the second pass reports warnings/errors. This has the effect that
77 // we follow the more canonical "last option wins" paradigm when there are
78 // conflicting options.
79 for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; ++Report) {
80 bool SetDiagnostic = (Report == 0);
81 for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
82 StringRef Opt = Opts.Warnings[i];
Mike Stump1eb44332009-09-09 15:08:12 +000083
Chad Rosier05272a62011-11-03 21:23:39 +000084 // Check to see if this warning starts with "no-", if so, this is a
85 // negative form of the option.
86 bool isPositive = true;
87 if (Opt.startswith("no-")) {
88 isPositive = false;
89 Opt = Opt.substr(3);
Chris Lattner5147e8e2009-04-15 04:27:38 +000090 }
Mike Stump1eb44332009-09-09 15:08:12 +000091
Chad Rosier05272a62011-11-03 21:23:39 +000092 // Figure out how this option affects the warning. If -Wfoo, map the
93 // diagnostic to a warning, if -Wno-foo, map it to ignore.
94 diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE;
95
96 // -Wsystem-headers is a special case, not driven by the option table. It
97 // cannot be controlled with -Werror.
98 if (Opt == "system-headers") {
99 if (SetDiagnostic)
100 Diags.setSuppressSystemWarnings(!isPositive);
Chris Lattner5147e8e2009-04-15 04:27:38 +0000101 continue;
102 }
Chad Rosier05272a62011-11-03 21:23:39 +0000103
104 // -Weverything is a special case as well. It implicitly enables all
105 // warnings, including ones not explicitly in a warning group.
106 if (Opt == "everything") {
107 if (SetDiagnostic)
108 Diags.setEnableAllWarnings(true);
109 continue;
110 }
111
112 // -Werror/-Wno-error is a special case, not controlled by the option
113 // table. It also has the "specifier" form of -Werror=foo and -Werror-foo.
114 if (Opt.startswith("error")) {
115 StringRef Specifier;
116 if (Opt.size() > 5) { // Specifier must be present.
117 if ((Opt[5] != '=' && Opt[5] != '-') || Opt.size() == 6) {
118 if (Report)
119 Diags.Report(diag::warn_unknown_warning_specifier)
120 << "-Werror" << ("-W" + Opt.str());
121 continue;
122 }
123 Specifier = Opt.substr(6);
124 }
125
126 if (Specifier.empty()) {
127 if (SetDiagnostic)
128 Diags.setWarningsAsErrors(isPositive);
129 continue;
130 }
131
132 if (SetDiagnostic) {
133 // Set the warning as error flag for this specifier.
134 Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive);
135 } else if (DiagIDs->getDiagnosticsInGroup(Specifier, _Diags)) {
Benjamin Kramer4e969442011-11-28 22:17:09 +0000136 EmitUnknownDiagWarning(Diags, "-Werror=", Specifier, isPositive);
Chad Rosier05272a62011-11-03 21:23:39 +0000137 }
138 continue;
139 }
140
141 // -Wfatal-errors is yet another special case.
142 if (Opt.startswith("fatal-errors")) {
143 StringRef Specifier;
144 if (Opt.size() != 12) {
145 if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) {
146 if (Report)
147 Diags.Report(diag::warn_unknown_warning_specifier)
148 << "-Wfatal-errors" << ("-W" + Opt.str());
149 continue;
150 }
151 Specifier = Opt.substr(13);
152 }
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Chad Rosier05272a62011-11-03 21:23:39 +0000154 if (Specifier.empty()) {
155 if (SetDiagnostic)
156 Diags.setErrorsAsFatal(isPositive);
157 continue;
158 }
159
160 if (SetDiagnostic) {
161 // Set the error as fatal flag for this specifier.
162 Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive);
163 } else if (DiagIDs->getDiagnosticsInGroup(Specifier, _Diags)) {
Benjamin Kramer4e969442011-11-28 22:17:09 +0000164 EmitUnknownDiagWarning(Diags, "-Wfatal-errors=", Specifier,
165 isPositive);
Chad Rosier05272a62011-11-03 21:23:39 +0000166 }
167 continue;
168 }
169
Chad Rosier496cc8e2011-11-15 18:57:32 +0000170 if (Report) {
171 if (DiagIDs->getDiagnosticsInGroup(Opt, _Diags))
Chad Rosier99643d92011-11-15 19:03:03 +0000172 EmitUnknownDiagWarning(Diags, "-W", Opt, isPositive);
Chad Rosier05272a62011-11-03 21:23:39 +0000173 } else {
174 Diags.setDiagnosticGroupMapping(Opt, Mapping);
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000175 }
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000176 }
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000177 }
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000178}