1 //===--- Warnings.cpp - C-Language Front-end ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Command line warning options handler.
12 //===----------------------------------------------------------------------===//
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,
17 // -Werror and -Wfatal-errors.
19 // Each warning option controls any number of actual warnings.
20 // Given a warning option 'foo', the following are valid:
21 // -Wfoo, -Wno-foo, -Werror=foo, -Wfatal-errors=foo
23 #include "clang/Frontend/Utils.h"
24 #include "clang/Basic/Diagnostic.h"
25 #include "clang/Sema/SemaDiagnostic.h"
26 #include "clang/Lex/LexDiagnostic.h"
27 #include "clang/Frontend/DiagnosticOptions.h"
28 #include "clang/Frontend/FrontendDiagnostic.h"
32 using namespace clang;
34 // EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning
36 static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags,
37 StringRef Prefix, StringRef Opt,
39 StringRef Suggestion = DiagnosticIDs::getNearestWarningOption(Opt);
40 if (!Suggestion.empty())
41 Diags.Report(isPositive? diag::warn_unknown_warning_option_suggest :
42 diag::warn_unknown_negative_warning_option_suggest)
43 << (Prefix.str() += Opt) << (Prefix.str() += Suggestion);
45 Diags.Report(isPositive? diag::warn_unknown_warning_option :
46 diag::warn_unknown_negative_warning_option)
47 << (Prefix.str() += Opt);
50 void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
51 const DiagnosticOptions &Opts) {
52 Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
53 Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
54 Diags.setShowOverloads(
55 static_cast<DiagnosticsEngine::OverloadsShown>(Opts.ShowOverloads));
57 // Handle -ferror-limit
59 Diags.setErrorLimit(Opts.ErrorLimit);
60 if (Opts.TemplateBacktraceLimit)
61 Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
62 if (Opts.ConstexprBacktraceLimit)
63 Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit);
65 // If -pedantic or -pedantic-errors was specified, then we want to map all
66 // extension diagnostics onto WARNING or ERROR unless the user has futz'd
67 // around with them explicitly.
68 if (Opts.PedanticErrors)
69 Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Error);
70 else if (Opts.Pedantic)
71 Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Warn);
73 Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Ignore);
75 llvm::SmallVector<diag::kind, 10> _Diags;
76 const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs =
77 Diags.getDiagnosticIDs();
78 // We parse the warning options twice. The first pass sets diagnostic state,
79 // while the second pass reports warnings/errors. This has the effect that
80 // we follow the more canonical "last option wins" paradigm when there are
81 // conflicting options.
82 for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; ++Report) {
83 bool SetDiagnostic = (Report == 0);
84 for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
85 StringRef Opt = Opts.Warnings[i];
87 // Treat -Wformat=0 as an alias for -Wno-format.
88 if (Opt == "format=0")
91 // Check to see if this warning starts with "no-", if so, this is a
92 // negative form of the option.
93 bool isPositive = true;
94 if (Opt.startswith("no-")) {
99 // Figure out how this option affects the warning. If -Wfoo, map the
100 // diagnostic to a warning, if -Wno-foo, map it to ignore.
101 diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE;
103 // -Wsystem-headers is a special case, not driven by the option table. It
104 // cannot be controlled with -Werror.
105 if (Opt == "system-headers") {
107 Diags.setSuppressSystemWarnings(!isPositive);
111 // -Weverything is a special case as well. It implicitly enables all
112 // warnings, including ones not explicitly in a warning group.
113 if (Opt == "everything") {
116 Diags.setEnableAllWarnings(true);
118 Diags.setEnableAllWarnings(false);
119 Diags.setMappingToAllDiagnostics(diag::MAP_IGNORE);
125 // -Werror/-Wno-error is a special case, not controlled by the option
126 // table. It also has the "specifier" form of -Werror=foo and -Werror-foo.
127 if (Opt.startswith("error")) {
129 if (Opt.size() > 5) { // Specifier must be present.
130 if ((Opt[5] != '=' && Opt[5] != '-') || Opt.size() == 6) {
132 Diags.Report(diag::warn_unknown_warning_specifier)
133 << "-Werror" << ("-W" + Opt.str());
136 Specifier = Opt.substr(6);
139 if (Specifier.empty()) {
141 Diags.setWarningsAsErrors(isPositive);
146 // Set the warning as error flag for this specifier.
147 Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive);
148 } else if (DiagIDs->getDiagnosticsInGroup(Specifier, _Diags)) {
149 EmitUnknownDiagWarning(Diags, "-Werror=", Specifier, isPositive);
154 // -Wfatal-errors is yet another special case.
155 if (Opt.startswith("fatal-errors")) {
157 if (Opt.size() != 12) {
158 if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) {
160 Diags.Report(diag::warn_unknown_warning_specifier)
161 << "-Wfatal-errors" << ("-W" + Opt.str());
164 Specifier = Opt.substr(13);
167 if (Specifier.empty()) {
169 Diags.setErrorsAsFatal(isPositive);
174 // Set the error as fatal flag for this specifier.
175 Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive);
176 } else if (DiagIDs->getDiagnosticsInGroup(Specifier, _Diags)) {
177 EmitUnknownDiagWarning(Diags, "-Wfatal-errors=", Specifier,
184 if (DiagIDs->getDiagnosticsInGroup(Opt, _Diags))
185 EmitUnknownDiagWarning(Diags, "-W", Opt, isPositive);
187 Diags.setDiagnosticGroupMapping(Opt, Mapping);