Warnings.cpp revision 99643d951e56359c1bead5ce70be3883a06b520a
163a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl//===--- Warnings.cpp - C-Language Front-end ------------------------------===// 263a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl// 363a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl// The LLVM Compiler Infrastructure 463a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl// 563a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl// This file is distributed under the University of Illinois Open Source 663a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl// License. See LICENSE.TXT for details. 763a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl// 863a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl//===----------------------------------------------------------------------===// 963a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl// 1063a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl// Command line warning options handler. 1163a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl// 1263a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl//===----------------------------------------------------------------------===// 1363a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl// 1463a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl// This file is responsible for handling all warning options. This includes 1563a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl// a number of -Wfoo options and their variants, which are driven by TableGen- 16a3b089e062133bdc0e5c39ea03f8b78230953f6cChris Lattner// generated data, and the special cases -pedantic, -pedantic-errors, -w, 17a3b089e062133bdc0e5c39ea03f8b78230953f6cChris Lattner// -Werror and -Wfatal-errors. 1863a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl// 19c5613db921c87067660b262af379b38a2791e412Sebastian Redl// Each warning option controls any number of actual warnings. 2063a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl// Given a warning option 'foo', the following are valid: 21a3b089e062133bdc0e5c39ea03f8b78230953f6cChris Lattner// -Wfoo, -Wno-foo, -Werror=foo, -Wfatal-errors=foo 2263a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl// 23b09f6e15c59b89d5820db8ef40598eb1d1323c1fEli Friedman#include "clang/Frontend/Utils.h" 2463a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl#include "clang/Basic/Diagnostic.h" 2563a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl#include "clang/Sema/SemaDiagnostic.h" 2663a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl#include "clang/Lex/LexDiagnostic.h" 276907943901e0aae5be7618c36c0f8275634e6ab5Daniel Dunbar#include "clang/Frontend/DiagnosticOptions.h" 288ea841b1152308ae11c23a7836dead36e47d96e3Chris Lattner#include "clang/Frontend/FrontendDiagnostic.h" 29ac8d6298a8ae9276412d8c054957dfa65a69eb14Eli Friedman#include <cstring> 3063a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl#include <utility> 3163a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl#include <algorithm> 3263a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redlusing namespace clang; 3363a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl 3499643d951e56359c1bead5ce70be3883a06b520aChad Rosier// EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning opts 3599643d951e56359c1bead5ce70be3883a06b520aChad Rosierstatic void EmitUnknownDiagWarning(DiagnosticsEngine &Diags, 36dce6327160a6e333137b34cce77e2dfc2cd5aab6Benjamin Kramer StringRef Prefix, StringRef Opt, 37dce6327160a6e333137b34cce77e2dfc2cd5aab6Benjamin Kramer bool isPositive) { 38dce6327160a6e333137b34cce77e2dfc2cd5aab6Benjamin Kramer StringRef Suggestion = DiagnosticIDs::getNearestWarningOption(Opt); 39dce6327160a6e333137b34cce77e2dfc2cd5aab6Benjamin Kramer if (!Suggestion.empty()) 40dce6327160a6e333137b34cce77e2dfc2cd5aab6Benjamin Kramer Diags.Report(isPositive? diag::warn_unknown_warning_option_suggest : 41dce6327160a6e333137b34cce77e2dfc2cd5aab6Benjamin Kramer diag::warn_unknown_negative_warning_option_suggest) 42dce6327160a6e333137b34cce77e2dfc2cd5aab6Benjamin Kramer << (Prefix.str() += Opt) << (Prefix.str() += Suggestion); 43dce6327160a6e333137b34cce77e2dfc2cd5aab6Benjamin Kramer else 44dce6327160a6e333137b34cce77e2dfc2cd5aab6Benjamin Kramer Diags.Report(isPositive? diag::warn_unknown_warning_option : 45dce6327160a6e333137b34cce77e2dfc2cd5aab6Benjamin Kramer diag::warn_unknown_negative_warning_option) 46dce6327160a6e333137b34cce77e2dfc2cd5aab6Benjamin Kramer << (Prefix.str() += Opt); 47dce6327160a6e333137b34cce77e2dfc2cd5aab6Benjamin Kramer} 48dce6327160a6e333137b34cce77e2dfc2cd5aab6Benjamin Kramer 49d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikievoid clang::ProcessWarningOptions(DiagnosticsEngine &Diags, 506907943901e0aae5be7618c36c0f8275634e6ab5Daniel Dunbar const DiagnosticOptions &Opts) { 5127ceb9d77d929f02a8a811d189a96885629c7c0cChris Lattner Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers 526907943901e0aae5be7618c36c0f8275634e6ab5Daniel Dunbar Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings); 535edbdcc62098e305cd55654814dcf783a3f3c477Jeffrey Yasskin Diags.setShowOverloads( 54d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie static_cast<DiagnosticsEngine::OverloadsShown>(Opts.ShowOverloads)); 55c100214fdc41a7ea215f75d433eb1cb829fd4330Chris Lattner 56c100214fdc41a7ea215f75d433eb1cb829fd4330Chris Lattner // Handle -ferror-limit 57c100214fdc41a7ea215f75d433eb1cb829fd4330Chris Lattner if (Opts.ErrorLimit) 58c100214fdc41a7ea215f75d433eb1cb829fd4330Chris Lattner Diags.setErrorLimit(Opts.ErrorLimit); 59575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor if (Opts.TemplateBacktraceLimit) 60575cf3791216c33770ba950430493cdd43099f8fDouglas Gregor Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit); 6163a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl 62b54b276a920246c595a0498da281821eb9d22996Chris Lattner // If -pedantic or -pedantic-errors was specified, then we want to map all 63b54b276a920246c595a0498da281821eb9d22996Chris Lattner // extension diagnostics onto WARNING or ERROR unless the user has futz'd 64b54b276a920246c595a0498da281821eb9d22996Chris Lattner // around with them explicitly. 656907943901e0aae5be7618c36c0f8275634e6ab5Daniel Dunbar if (Opts.PedanticErrors) 66d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Error); 676907943901e0aae5be7618c36c0f8275634e6ab5Daniel Dunbar else if (Opts.Pedantic) 68d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Warn); 69b54b276a920246c595a0498da281821eb9d22996Chris Lattner else 70d6471f7c1921c7802804ce3ff6fe9768310f72b9David Blaikie Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Ignore); 711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 7205272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier llvm::SmallVector<diag::kind, 10> _Diags; 7305272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier const llvm::IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs = 7405272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier Diags.getDiagnosticIDs(); 7505272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier // We parse the warning options twice. The first pass sets diagnostic state, 7605272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier // while the second pass reports warnings/errors. This has the effect that 7705272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier // we follow the more canonical "last option wins" paradigm when there are 7805272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier // conflicting options. 7905272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; ++Report) { 8005272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier bool SetDiagnostic = (Report == 0); 8105272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) { 8205272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier StringRef Opt = Opts.Warnings[i]; 831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8405272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier // Check to see if this warning starts with "no-", if so, this is a 8505272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier // negative form of the option. 8605272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier bool isPositive = true; 8705272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier if (Opt.startswith("no-")) { 8805272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier isPositive = false; 8905272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier Opt = Opt.substr(3); 905147e8e0186ec7d4c2c2aeb348c0354c1c3302f9Chris Lattner } 911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9205272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier // Figure out how this option affects the warning. If -Wfoo, map the 9305272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier // diagnostic to a warning, if -Wno-foo, map it to ignore. 9405272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE; 9505272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier 9605272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier // -Wsystem-headers is a special case, not driven by the option table. It 9705272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier // cannot be controlled with -Werror. 9805272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier if (Opt == "system-headers") { 9905272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier if (SetDiagnostic) 10005272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier Diags.setSuppressSystemWarnings(!isPositive); 1015147e8e0186ec7d4c2c2aeb348c0354c1c3302f9Chris Lattner continue; 10263a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl } 10305272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier 10405272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier // -Weverything is a special case as well. It implicitly enables all 10505272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier // warnings, including ones not explicitly in a warning group. 10605272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier if (Opt == "everything") { 10705272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier if (SetDiagnostic) 10805272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier Diags.setEnableAllWarnings(true); 10905272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier continue; 1104aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar } 11105272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier 11205272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier // -Werror/-Wno-error is a special case, not controlled by the option 11305272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier // table. It also has the "specifier" form of -Werror=foo and -Werror-foo. 11405272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier if (Opt.startswith("error")) { 11505272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier StringRef Specifier; 11605272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier if (Opt.size() > 5) { // Specifier must be present. 11705272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier if ((Opt[5] != '=' && Opt[5] != '-') || Opt.size() == 6) { 11805272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier if (Report) 11905272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier Diags.Report(diag::warn_unknown_warning_specifier) 12005272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier << "-Werror" << ("-W" + Opt.str()); 12105272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier continue; 12205272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier } 12305272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier Specifier = Opt.substr(6); 12405272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier } 12505272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier 12605272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier if (Specifier.empty()) { 12705272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier if (SetDiagnostic) 12805272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier Diags.setWarningsAsErrors(isPositive); 129e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner continue; 130e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner } 13105272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier 13205272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier if (SetDiagnostic) { 13305272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier // Set the warning as error flag for this specifier. 13405272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive); 13505272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier } else if (DiagIDs->getDiagnosticsInGroup(Specifier, _Diags)) { 13699643d951e56359c1bead5ce70be3883a06b520aChad Rosier EmitUnknownDiagWarning(Diags, "-Werror", Specifier, isPositive); 13705272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier } 13805272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier continue; 139e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner } 14005272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier 14105272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier // -Wfatal-errors is yet another special case. 14205272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier if (Opt.startswith("fatal-errors")) { 14305272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier StringRef Specifier; 14405272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier if (Opt.size() != 12) { 14505272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) { 14605272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier if (Report) 14705272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier Diags.Report(diag::warn_unknown_warning_specifier) 14805272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier << "-Wfatal-errors" << ("-W" + Opt.str()); 14905272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier continue; 15005272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier } 15105272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier Specifier = Opt.substr(13); 15205272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier } 153e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner 15405272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier if (Specifier.empty()) { 15505272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier if (SetDiagnostic) 15605272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier Diags.setErrorsAsFatal(isPositive); 15705272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier continue; 15805272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier } 15905272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier 16005272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier if (SetDiagnostic) { 16105272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier // Set the error as fatal flag for this specifier. 16205272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive); 16305272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier } else if (DiagIDs->getDiagnosticsInGroup(Specifier, _Diags)) { 16499643d951e56359c1bead5ce70be3883a06b520aChad Rosier EmitUnknownDiagWarning(Diags, "-Wfatal-errors", Specifier, isPositive); 16505272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier } 166e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner continue; 167e663c720063fc9ff9f75bcbe38cd070c73c78b0eChris Lattner } 16805272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier 169496cc8eae2c69b8fd36a2b3e815dd0e8207ec493Chad Rosier if (Report) { 170496cc8eae2c69b8fd36a2b3e815dd0e8207ec493Chad Rosier if (DiagIDs->getDiagnosticsInGroup(Opt, _Diags)) 17199643d951e56359c1bead5ce70be3883a06b520aChad Rosier EmitUnknownDiagWarning(Diags, "-W", Opt, isPositive); 17205272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier } else { 17305272a659437fc6ec7fa5a7c3f3fc6eb220a6fa2Chad Rosier Diags.setDiagnosticGroupMapping(Opt, Mapping); 1744aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar } 1754aa8f2bce0f498152d624f748712a991adc23fddDaniel Dunbar } 17663a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl } 17763a9e0ff79f01a542afaf9b912e3dee3d395ebc5Sebastian Redl} 178