clang API Documentation

Warnings.cpp
Go to the documentation of this file.
00001 //===--- Warnings.cpp - C-Language Front-end ------------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // Command line warning options handler.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 //
00014 // This file is responsible for handling all warning options. This includes
00015 // a number of -Wfoo options and their variants, which are driven by TableGen-
00016 // generated data, and the special cases -pedantic, -pedantic-errors, -w,
00017 // -Werror and -Wfatal-errors.
00018 //
00019 // Each warning option controls any number of actual warnings.
00020 // Given a warning option 'foo', the following are valid:
00021 //    -Wfoo, -Wno-foo, -Werror=foo, -Wfatal-errors=foo
00022 //
00023 // Remark options are also handled here, analogously, except that they are much
00024 // simpler because a remark can't be promoted to an error.
00025 #include "clang/Basic/AllDiagnostics.h"
00026 #include "clang/Basic/Diagnostic.h"
00027 #include "clang/Basic/DiagnosticOptions.h"
00028 #include <algorithm>
00029 #include <cstring>
00030 #include <utility>
00031 using namespace clang;
00032 
00033 // EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning
00034 // opts
00035 static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags,
00036                                    diag::Flavor Flavor, StringRef Prefix,
00037                                    StringRef Opt) {
00038   StringRef Suggestion = DiagnosticIDs::getNearestOption(Flavor, Opt);
00039   Diags.Report(diag::warn_unknown_diag_option)
00040     << (Flavor == diag::Flavor::WarningOrError ? 0 : 1) << (Prefix.str() += Opt)
00041     << !Suggestion.empty() << (Prefix.str() += Suggestion);
00042 }
00043 
00044 void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
00045                                   const DiagnosticOptions &Opts,
00046                                   bool ReportDiags) {
00047   Diags.setSuppressSystemWarnings(true);  // Default to -Wno-system-headers
00048   Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
00049   Diags.setShowOverloads(Opts.getShowOverloads());
00050 
00051   Diags.setElideType(Opts.ElideType);
00052   Diags.setPrintTemplateTree(Opts.ShowTemplateTree);
00053   Diags.setShowColors(Opts.ShowColors);
00054  
00055   // Handle -ferror-limit
00056   if (Opts.ErrorLimit)
00057     Diags.setErrorLimit(Opts.ErrorLimit);
00058   if (Opts.TemplateBacktraceLimit)
00059     Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
00060   if (Opts.ConstexprBacktraceLimit)
00061     Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit);
00062 
00063   // If -pedantic or -pedantic-errors was specified, then we want to map all
00064   // extension diagnostics onto WARNING or ERROR unless the user has futz'd
00065   // around with them explicitly.
00066   if (Opts.PedanticErrors)
00067     Diags.setExtensionHandlingBehavior(diag::Severity::Error);
00068   else if (Opts.Pedantic)
00069     Diags.setExtensionHandlingBehavior(diag::Severity::Warning);
00070   else
00071     Diags.setExtensionHandlingBehavior(diag::Severity::Ignored);
00072 
00073   SmallVector<diag::kind, 10> _Diags;
00074   const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs =
00075     Diags.getDiagnosticIDs();
00076   // We parse the warning options twice.  The first pass sets diagnostic state,
00077   // while the second pass reports warnings/errors.  This has the effect that
00078   // we follow the more canonical "last option wins" paradigm when there are 
00079   // conflicting options.
00080   for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; ++Report) {
00081     bool SetDiagnostic = (Report == 0);
00082 
00083     // If we've set the diagnostic state and are not reporting diagnostics then
00084     // we're done.
00085     if (!SetDiagnostic && !ReportDiags)
00086       break;
00087 
00088     for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
00089       const auto Flavor = diag::Flavor::WarningOrError;
00090       StringRef Opt = Opts.Warnings[i];
00091       StringRef OrigOpt = Opts.Warnings[i];
00092 
00093       // Treat -Wformat=0 as an alias for -Wno-format.
00094       if (Opt == "format=0")
00095         Opt = "no-format";
00096 
00097       // Check to see if this warning starts with "no-", if so, this is a
00098       // negative form of the option.
00099       bool isPositive = true;
00100       if (Opt.startswith("no-")) {
00101         isPositive = false;
00102         Opt = Opt.substr(3);
00103       }
00104 
00105       // Figure out how this option affects the warning.  If -Wfoo, map the
00106       // diagnostic to a warning, if -Wno-foo, map it to ignore.
00107       diag::Severity Mapping =
00108           isPositive ? diag::Severity::Warning : diag::Severity::Ignored;
00109 
00110       // -Wsystem-headers is a special case, not driven by the option table.  It
00111       // cannot be controlled with -Werror.
00112       if (Opt == "system-headers") {
00113         if (SetDiagnostic)
00114           Diags.setSuppressSystemWarnings(!isPositive);
00115         continue;
00116       }
00117       
00118       // -Weverything is a special case as well.  It implicitly enables all
00119       // warnings, including ones not explicitly in a warning group.
00120       if (Opt == "everything") {
00121         if (SetDiagnostic) {
00122           if (isPositive) {
00123             Diags.setEnableAllWarnings(true);
00124           } else {
00125             Diags.setEnableAllWarnings(false);
00126             Diags.setSeverityForAll(Flavor, diag::Severity::Ignored);
00127           }
00128         }
00129         continue;
00130       }
00131       
00132       // -Werror/-Wno-error is a special case, not controlled by the option 
00133       // table. It also has the "specifier" form of -Werror=foo and -Werror-foo.
00134       if (Opt.startswith("error")) {
00135         StringRef Specifier;
00136         if (Opt.size() > 5) {  // Specifier must be present.
00137           if ((Opt[5] != '=' && Opt[5] != '-') || Opt.size() == 6) {
00138             if (Report)
00139               Diags.Report(diag::warn_unknown_warning_specifier)
00140                 << "-Werror" << ("-W" + OrigOpt.str());
00141             continue;
00142           }
00143           Specifier = Opt.substr(6);
00144         }
00145         
00146         if (Specifier.empty()) {
00147           if (SetDiagnostic)
00148             Diags.setWarningsAsErrors(isPositive);
00149           continue;
00150         }
00151         
00152         if (SetDiagnostic) {
00153           // Set the warning as error flag for this specifier.
00154           Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive);
00155         } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
00156           EmitUnknownDiagWarning(Diags, Flavor, "-Werror=", Specifier);
00157         }
00158         continue;
00159       }
00160       
00161       // -Wfatal-errors is yet another special case.
00162       if (Opt.startswith("fatal-errors")) {
00163         StringRef Specifier;
00164         if (Opt.size() != 12) {
00165           if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) {
00166             if (Report)
00167               Diags.Report(diag::warn_unknown_warning_specifier)
00168                 << "-Wfatal-errors" << ("-W" + OrigOpt.str());
00169             continue;
00170           }
00171           Specifier = Opt.substr(13);
00172         }
00173 
00174         if (Specifier.empty()) {
00175           if (SetDiagnostic)
00176             Diags.setErrorsAsFatal(isPositive);
00177           continue;
00178         }
00179         
00180         if (SetDiagnostic) {
00181           // Set the error as fatal flag for this specifier.
00182           Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive);
00183         } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
00184           EmitUnknownDiagWarning(Diags, Flavor, "-Wfatal-errors=", Specifier);
00185         }
00186         continue;
00187       }
00188       
00189       if (Report) {
00190         if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
00191           EmitUnknownDiagWarning(Diags, Flavor, isPositive ? "-W" : "-Wno-",
00192                                  Opt);
00193       } else {
00194         Diags.setSeverityForGroup(Flavor, Opt, Mapping);
00195       }
00196     }
00197 
00198     for (unsigned i = 0, e = Opts.Remarks.size(); i != e; ++i) {
00199       StringRef Opt = Opts.Remarks[i];
00200       const auto Flavor = diag::Flavor::Remark;
00201 
00202       // Check to see if this warning starts with "no-", if so, this is a
00203       // negative form of the option.
00204       bool IsPositive = !Opt.startswith("no-");
00205       if (!IsPositive) Opt = Opt.substr(3);
00206 
00207       auto Severity = IsPositive ? diag::Severity::Remark
00208                                  : diag::Severity::Ignored;
00209 
00210       // -Reverything sets the state of all remarks. Note that all remarks are
00211       // in remark groups, so we don't need a separate 'all remarks enabled'
00212       // flag.
00213       if (Opt == "everything") {
00214         if (SetDiagnostic)
00215           Diags.setSeverityForAll(Flavor, Severity);
00216         continue;
00217       }
00218 
00219       if (Report) {
00220         if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
00221           EmitUnknownDiagWarning(Diags, Flavor, IsPositive ? "-R" : "-Rno-",
00222                                  Opt);
00223       } else {
00224         Diags.setSeverityForGroup(Flavor, Opt,
00225                                   IsPositive ? diag::Severity::Remark
00226                                              : diag::Severity::Ignored);
00227       }
00228     }
00229   }
00230 }