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 #include "clang/Frontend/Utils.h"
00024 #include "clang/Basic/Diagnostic.h"
00025 #include "clang/Sema/SemaDiagnostic.h"
00026 #include "clang/Lex/LexDiagnostic.h"
00027 #include "clang/Frontend/DiagnosticOptions.h"
00028 #include "clang/Frontend/FrontendDiagnostic.h"
00029 #include <cstring>
00030 #include <utility>
00031 #include <algorithm>
00032 using namespace clang;
00033 
00034 // EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning
00035 // opts
00036 static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags,
00037                                   StringRef Prefix, StringRef Opt,
00038                                   bool isPositive) {
00039   StringRef Suggestion = DiagnosticIDs::getNearestWarningOption(Opt);
00040   if (!Suggestion.empty())
00041     Diags.Report(isPositive? diag::warn_unknown_warning_option_suggest :
00042                              diag::warn_unknown_negative_warning_option_suggest)
00043       << (Prefix.str() += Opt) << (Prefix.str() += Suggestion);
00044   else
00045     Diags.Report(isPositive? diag::warn_unknown_warning_option :
00046                              diag::warn_unknown_negative_warning_option)
00047       << (Prefix.str() += Opt);
00048 }
00049 
00050 void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
00051                                   const DiagnosticOptions &Opts) {
00052   Diags.setSuppressSystemWarnings(true);  // Default to -Wno-system-headers
00053   Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
00054   Diags.setShowOverloads(
00055     static_cast<DiagnosticsEngine::OverloadsShown>(Opts.ShowOverloads));
00056   
00057   // Handle -ferror-limit
00058   if (Opts.ErrorLimit)
00059     Diags.setErrorLimit(Opts.ErrorLimit);
00060   if (Opts.TemplateBacktraceLimit)
00061     Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
00062   if (Opts.ConstexprBacktraceLimit)
00063     Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit);
00064 
00065   // If -pedantic or -pedantic-errors was specified, then we want to map all
00066   // extension diagnostics onto WARNING or ERROR unless the user has futz'd
00067   // around with them explicitly.
00068   if (Opts.PedanticErrors)
00069     Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Error);
00070   else if (Opts.Pedantic)
00071     Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Warn);
00072   else
00073     Diags.setExtensionHandlingBehavior(DiagnosticsEngine::Ext_Ignore);
00074 
00075   llvm::SmallVector<diag::kind, 10> _Diags;
00076   const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs =
00077     Diags.getDiagnosticIDs();
00078   // We parse the warning options twice.  The first pass sets diagnostic state,
00079   // while the second pass reports warnings/errors.  This has the effect that
00080   // we follow the more canonical "last option wins" paradigm when there are 
00081   // conflicting options.
00082   for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; ++Report) {
00083     bool SetDiagnostic = (Report == 0);
00084     for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
00085       StringRef Opt = Opts.Warnings[i];
00086       StringRef OrigOpt = Opts.Warnings[i];
00087 
00088       // Treat -Wformat=0 as an alias for -Wno-format.
00089       if (Opt == "format=0")
00090         Opt = "no-format";
00091 
00092       // Check to see if this warning starts with "no-", if so, this is a
00093       // negative form of the option.
00094       bool isPositive = true;
00095       if (Opt.startswith("no-")) {
00096         isPositive = false;
00097         Opt = Opt.substr(3);
00098       }
00099 
00100       // Figure out how this option affects the warning.  If -Wfoo, map the
00101       // diagnostic to a warning, if -Wno-foo, map it to ignore.
00102       diag::Mapping Mapping = isPositive ? diag::MAP_WARNING : diag::MAP_IGNORE;
00103       
00104       // -Wsystem-headers is a special case, not driven by the option table.  It
00105       // cannot be controlled with -Werror.
00106       if (Opt == "system-headers") {
00107         if (SetDiagnostic)
00108           Diags.setSuppressSystemWarnings(!isPositive);
00109         continue;
00110       }
00111       
00112       // -Weverything is a special case as well.  It implicitly enables all
00113       // warnings, including ones not explicitly in a warning group.
00114       if (Opt == "everything") {
00115         if (SetDiagnostic) {
00116           if (isPositive) {
00117             Diags.setEnableAllWarnings(true);
00118           } else {
00119             Diags.setEnableAllWarnings(false);
00120             Diags.setMappingToAllDiagnostics(diag::MAP_IGNORE);
00121           }
00122         }
00123         continue;
00124       }
00125       
00126       // -Werror/-Wno-error is a special case, not controlled by the option 
00127       // table. It also has the "specifier" form of -Werror=foo and -Werror-foo.
00128       if (Opt.startswith("error")) {
00129         StringRef Specifier;
00130         if (Opt.size() > 5) {  // Specifier must be present.
00131           if ((Opt[5] != '=' && Opt[5] != '-') || Opt.size() == 6) {
00132             if (Report)
00133               Diags.Report(diag::warn_unknown_warning_specifier)
00134                 << "-Werror" << ("-W" + OrigOpt.str());
00135             continue;
00136           }
00137           Specifier = Opt.substr(6);
00138         }
00139         
00140         if (Specifier.empty()) {
00141           if (SetDiagnostic)
00142             Diags.setWarningsAsErrors(isPositive);
00143           continue;
00144         }
00145         
00146         if (SetDiagnostic) {
00147           // Set the warning as error flag for this specifier.
00148           Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive);
00149         } else if (DiagIDs->getDiagnosticsInGroup(Specifier, _Diags)) {
00150           EmitUnknownDiagWarning(Diags, "-Werror=", Specifier, isPositive);
00151         }
00152         continue;
00153       }
00154       
00155       // -Wfatal-errors is yet another special case.
00156       if (Opt.startswith("fatal-errors")) {
00157         StringRef Specifier;
00158         if (Opt.size() != 12) {
00159           if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) {
00160             if (Report)
00161               Diags.Report(diag::warn_unknown_warning_specifier)
00162                 << "-Wfatal-errors" << ("-W" + OrigOpt.str());
00163             continue;
00164           }
00165           Specifier = Opt.substr(13);
00166         }
00167 
00168         if (Specifier.empty()) {
00169           if (SetDiagnostic)
00170             Diags.setErrorsAsFatal(isPositive);
00171           continue;
00172         }
00173         
00174         if (SetDiagnostic) {
00175           // Set the error as fatal flag for this specifier.
00176           Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive);
00177         } else if (DiagIDs->getDiagnosticsInGroup(Specifier, _Diags)) {
00178           EmitUnknownDiagWarning(Diags, "-Wfatal-errors=", Specifier,
00179                                  isPositive);
00180         }
00181         continue;
00182       }
00183       
00184       if (Report) {
00185         if (DiagIDs->getDiagnosticsInGroup(Opt, _Diags))
00186           EmitUnknownDiagWarning(Diags, isPositive ? "-W" : "-Wno-", Opt,
00187                                  isPositive);
00188       } else {
00189         Diags.setDiagnosticGroupMapping(Opt, Mapping);
00190       }
00191     }
00192   }
00193 }