blob: 7fcbe3acd4bb81121b77b6ad098dc4760d1066fd [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
David Blaikied6471f72011-09-25 23:23:43 +000034void clang::ProcessWarningOptions(DiagnosticsEngine &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(
David Blaikied6471f72011-09-25 23:23:43 +000039 static_cast<DiagnosticsEngine::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)
David Blaikied6471f72011-09-25 23:23:43 +000051 Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Error);
Daniel Dunbar69079432009-11-12 07:28:44 +000052 else if (Opts.Pedantic)
David Blaikied6471f72011-09-25 23:23:43 +000053 Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Warn);
Chris Lattnerb54b2762009-04-16 05:04:32 +000054 else
David Blaikied6471f72011-09-25 23:23:43 +000055 Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Ignore);
Mike Stump1eb44332009-09-09 15:08:12 +000056
Chad Rosier05272a62011-11-03 21:23:39 +000057 llvm::SmallVector<diag::kind, 10> _Diags;
58 const llvm::IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs =
59 Diags.getDiagnosticIDs();
60 // We parse the warning options twice. The first pass sets diagnostic state,
61 // while the second pass reports warnings/errors. This has the effect that
62 // we follow the more canonical "last option wins" paradigm when there are
63 // conflicting options.
64 for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; ++Report) {
65 bool SetDiagnostic = (Report == 0);
66 for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
67 StringRef Opt = Opts.Warnings[i];
Mike Stump1eb44332009-09-09 15:08:12 +000068
Chad Rosier05272a62011-11-03 21:23:39 +000069 // Check to see if this warning starts with "no-", if so, this is a
70 // negative form of the option.
71 bool isPositive = true;
72 if (Opt.startswith("no-")) {
73 isPositive = false;
74 Opt = Opt.substr(3);
Chris Lattner5147e8e2009-04-15 04:27:38 +000075 }
Mike Stump1eb44332009-09-09 15:08:12 +000076
Chad Rosier05272a62011-11-03 21:23:39 +000077 // Figure out how this option affects the warning. If -Wfoo, map the
78 // diagnostic to a warning, if -Wno-foo, map it to ignore.
79 diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE;
80
81 // -Wsystem-headers is a special case, not driven by the option table. It
82 // cannot be controlled with -Werror.
83 if (Opt == "system-headers") {
84 if (SetDiagnostic)
85 Diags.setSuppressSystemWarnings(!isPositive);
Chris Lattner5147e8e2009-04-15 04:27:38 +000086 continue;
87 }
Chad Rosier05272a62011-11-03 21:23:39 +000088
89 // -Weverything is a special case as well. It implicitly enables all
90 // warnings, including ones not explicitly in a warning group.
91 if (Opt == "everything") {
92 if (SetDiagnostic)
93 Diags.setEnableAllWarnings(true);
94 continue;
95 }
96
97 // -Werror/-Wno-error is a special case, not controlled by the option
98 // table. It also has the "specifier" form of -Werror=foo and -Werror-foo.
99 if (Opt.startswith("error")) {
100 StringRef Specifier;
101 if (Opt.size() > 5) { // Specifier must be present.
102 if ((Opt[5] != '=' && Opt[5] != '-') || Opt.size() == 6) {
103 if (Report)
104 Diags.Report(diag::warn_unknown_warning_specifier)
105 << "-Werror" << ("-W" + Opt.str());
106 continue;
107 }
108 Specifier = Opt.substr(6);
109 }
110
111 if (Specifier.empty()) {
112 if (SetDiagnostic)
113 Diags.setWarningsAsErrors(isPositive);
114 continue;
115 }
116
117 if (SetDiagnostic) {
118 // Set the warning as error flag for this specifier.
119 Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive);
120 } else if (DiagIDs->getDiagnosticsInGroup(Specifier, _Diags)) {
121 Diags.Report(isPositive ? diag::warn_unknown_warning_option :
122 diag::warn_unknown_negative_warning_option)
123 << ("-W" + Opt.str());
124 }
125 continue;
126 }
127
128 // -Wfatal-errors is yet another special case.
129 if (Opt.startswith("fatal-errors")) {
130 StringRef Specifier;
131 if (Opt.size() != 12) {
132 if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) {
133 if (Report)
134 Diags.Report(diag::warn_unknown_warning_specifier)
135 << "-Wfatal-errors" << ("-W" + Opt.str());
136 continue;
137 }
138 Specifier = Opt.substr(13);
139 }
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Chad Rosier05272a62011-11-03 21:23:39 +0000141 if (Specifier.empty()) {
142 if (SetDiagnostic)
143 Diags.setErrorsAsFatal(isPositive);
144 continue;
145 }
146
147 if (SetDiagnostic) {
148 // Set the error as fatal flag for this specifier.
149 Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive);
150 } else if (DiagIDs->getDiagnosticsInGroup(Specifier, _Diags)) {
151 Diags.Report(isPositive ? diag::warn_unknown_warning_option :
152 diag::warn_unknown_negative_warning_option)
153 << ("-W" + Opt.str());
154 }
155 continue;
156 }
157
158 if (Report && DiagIDs->getDiagnosticsInGroup(Opt, _Diags)) {
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000159 Diags.Report(isPositive ? diag::warn_unknown_warning_option :
160 diag::warn_unknown_negative_warning_option)
161 << ("-W" + Opt.str());
Chad Rosier05272a62011-11-03 21:23:39 +0000162 } else {
163 Diags.setDiagnosticGroupMapping(Opt, Mapping);
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000164 }
Daniel Dunbar4aa8f2b2011-09-29 00:53:47 +0000165 }
Chris Lattner27ceb9d2009-04-15 07:01:18 +0000166 }
Sebastian Redl63a9e0f2009-03-06 17:41:35 +0000167}