10b57cec5SDimitry Andric //===--- Warnings.cpp - C-Language Front-end ------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Command line warning options handler.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric // This file is responsible for handling all warning options. This includes
140b57cec5SDimitry Andric // a number of -Wfoo options and their variants, which are driven by TableGen-
150b57cec5SDimitry Andric // generated data, and the special cases -pedantic, -pedantic-errors, -w,
160b57cec5SDimitry Andric // -Werror and -Wfatal-errors.
170b57cec5SDimitry Andric //
180b57cec5SDimitry Andric // Each warning option controls any number of actual warnings.
190b57cec5SDimitry Andric // Given a warning option 'foo', the following are valid:
200b57cec5SDimitry Andric //    -Wfoo, -Wno-foo, -Werror=foo, -Wfatal-errors=foo
210b57cec5SDimitry Andric //
220b57cec5SDimitry Andric // Remark options are also handled here, analogously, except that they are much
230b57cec5SDimitry Andric // simpler because a remark can't be promoted to an error.
240b57cec5SDimitry Andric #include "clang/Basic/AllDiagnostics.h"
250b57cec5SDimitry Andric #include "clang/Basic/Diagnostic.h"
260b57cec5SDimitry Andric #include "clang/Basic/DiagnosticOptions.h"
270b57cec5SDimitry Andric #include <algorithm>
280b57cec5SDimitry Andric #include <cstring>
290b57cec5SDimitry Andric #include <utility>
300b57cec5SDimitry Andric using namespace clang;
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric // EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning
330b57cec5SDimitry Andric // opts
EmitUnknownDiagWarning(DiagnosticsEngine & Diags,diag::Flavor Flavor,StringRef Prefix,StringRef Opt)340b57cec5SDimitry Andric static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags,
350b57cec5SDimitry Andric                                    diag::Flavor Flavor, StringRef Prefix,
360b57cec5SDimitry Andric                                    StringRef Opt) {
370b57cec5SDimitry Andric   StringRef Suggestion = DiagnosticIDs::getNearestOption(Flavor, Opt);
380b57cec5SDimitry Andric   Diags.Report(diag::warn_unknown_diag_option)
395ffd83dbSDimitry Andric       << (Flavor == diag::Flavor::WarningOrError ? 0 : 1)
405ffd83dbSDimitry Andric       << (Prefix.str() += std::string(Opt)) << !Suggestion.empty()
415ffd83dbSDimitry Andric       << (Prefix.str() += std::string(Suggestion));
420b57cec5SDimitry Andric }
430b57cec5SDimitry Andric 
ProcessWarningOptions(DiagnosticsEngine & Diags,const DiagnosticOptions & Opts,bool ReportDiags)440b57cec5SDimitry Andric void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
450b57cec5SDimitry Andric                                   const DiagnosticOptions &Opts,
460b57cec5SDimitry Andric                                   bool ReportDiags) {
470b57cec5SDimitry Andric   Diags.setSuppressSystemWarnings(true);  // Default to -Wno-system-headers
480b57cec5SDimitry Andric   Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
490b57cec5SDimitry Andric   Diags.setShowOverloads(Opts.getShowOverloads());
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric   Diags.setElideType(Opts.ElideType);
520b57cec5SDimitry Andric   Diags.setPrintTemplateTree(Opts.ShowTemplateTree);
530b57cec5SDimitry Andric   Diags.setShowColors(Opts.ShowColors);
540b57cec5SDimitry Andric 
550b57cec5SDimitry Andric   // Handle -ferror-limit
560b57cec5SDimitry Andric   if (Opts.ErrorLimit)
570b57cec5SDimitry Andric     Diags.setErrorLimit(Opts.ErrorLimit);
580b57cec5SDimitry Andric   if (Opts.TemplateBacktraceLimit)
590b57cec5SDimitry Andric     Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
600b57cec5SDimitry Andric   if (Opts.ConstexprBacktraceLimit)
610b57cec5SDimitry Andric     Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit);
620b57cec5SDimitry Andric 
630b57cec5SDimitry Andric   // If -pedantic or -pedantic-errors was specified, then we want to map all
640b57cec5SDimitry Andric   // extension diagnostics onto WARNING or ERROR unless the user has futz'd
650b57cec5SDimitry Andric   // around with them explicitly.
660b57cec5SDimitry Andric   if (Opts.PedanticErrors)
670b57cec5SDimitry Andric     Diags.setExtensionHandlingBehavior(diag::Severity::Error);
680b57cec5SDimitry Andric   else if (Opts.Pedantic)
690b57cec5SDimitry Andric     Diags.setExtensionHandlingBehavior(diag::Severity::Warning);
700b57cec5SDimitry Andric   else
710b57cec5SDimitry Andric     Diags.setExtensionHandlingBehavior(diag::Severity::Ignored);
720b57cec5SDimitry Andric 
730b57cec5SDimitry Andric   SmallVector<diag::kind, 10> _Diags;
740b57cec5SDimitry Andric   const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs =
750b57cec5SDimitry Andric     Diags.getDiagnosticIDs();
760b57cec5SDimitry Andric   // We parse the warning options twice.  The first pass sets diagnostic state,
770b57cec5SDimitry Andric   // while the second pass reports warnings/errors.  This has the effect that
780b57cec5SDimitry Andric   // we follow the more canonical "last option wins" paradigm when there are
790b57cec5SDimitry Andric   // conflicting options.
800b57cec5SDimitry Andric   for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; ++Report) {
810b57cec5SDimitry Andric     bool SetDiagnostic = (Report == 0);
820b57cec5SDimitry Andric 
830b57cec5SDimitry Andric     // If we've set the diagnostic state and are not reporting diagnostics then
840b57cec5SDimitry Andric     // we're done.
850b57cec5SDimitry Andric     if (!SetDiagnostic && !ReportDiags)
860b57cec5SDimitry Andric       break;
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric     for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
890b57cec5SDimitry Andric       const auto Flavor = diag::Flavor::WarningOrError;
900b57cec5SDimitry Andric       StringRef Opt = Opts.Warnings[i];
910b57cec5SDimitry Andric       StringRef OrigOpt = Opts.Warnings[i];
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric       // Treat -Wformat=0 as an alias for -Wno-format.
940b57cec5SDimitry Andric       if (Opt == "format=0")
950b57cec5SDimitry Andric         Opt = "no-format";
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric       // Check to see if this warning starts with "no-", if so, this is a
980b57cec5SDimitry Andric       // negative form of the option.
99647cbc5dSDimitry Andric       bool isPositive = !Opt.consume_front("no-");
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric       // Figure out how this option affects the warning.  If -Wfoo, map the
1020b57cec5SDimitry Andric       // diagnostic to a warning, if -Wno-foo, map it to ignore.
1030b57cec5SDimitry Andric       diag::Severity Mapping =
1040b57cec5SDimitry Andric           isPositive ? diag::Severity::Warning : diag::Severity::Ignored;
1050b57cec5SDimitry Andric 
1060b57cec5SDimitry Andric       // -Wsystem-headers is a special case, not driven by the option table.  It
1070b57cec5SDimitry Andric       // cannot be controlled with -Werror.
1080b57cec5SDimitry Andric       if (Opt == "system-headers") {
1090b57cec5SDimitry Andric         if (SetDiagnostic)
1100b57cec5SDimitry Andric           Diags.setSuppressSystemWarnings(!isPositive);
1110b57cec5SDimitry Andric         continue;
1120b57cec5SDimitry Andric       }
1130b57cec5SDimitry Andric 
1140b57cec5SDimitry Andric       // -Weverything is a special case as well.  It implicitly enables all
1150b57cec5SDimitry Andric       // warnings, including ones not explicitly in a warning group.
1160b57cec5SDimitry Andric       if (Opt == "everything") {
1170b57cec5SDimitry Andric         if (SetDiagnostic) {
1180b57cec5SDimitry Andric           if (isPositive) {
1190b57cec5SDimitry Andric             Diags.setEnableAllWarnings(true);
1200b57cec5SDimitry Andric           } else {
1210b57cec5SDimitry Andric             Diags.setEnableAllWarnings(false);
1220b57cec5SDimitry Andric             Diags.setSeverityForAll(Flavor, diag::Severity::Ignored);
1230b57cec5SDimitry Andric           }
1240b57cec5SDimitry Andric         }
1250b57cec5SDimitry Andric         continue;
1260b57cec5SDimitry Andric       }
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric       // -Werror/-Wno-error is a special case, not controlled by the option
129e8d8bef9SDimitry Andric       // table. It also has the "specifier" form of -Werror=foo. GCC supports
130e8d8bef9SDimitry Andric       // the deprecated -Werror-implicit-function-declaration which is used by
131e8d8bef9SDimitry Andric       // a few projects.
1325f757f3fSDimitry Andric       if (Opt.starts_with("error")) {
1330b57cec5SDimitry Andric         StringRef Specifier;
1340b57cec5SDimitry Andric         if (Opt.size() > 5) {  // Specifier must be present.
135e8d8bef9SDimitry Andric           if (Opt[5] != '=' &&
136e8d8bef9SDimitry Andric               Opt.substr(5) != "-implicit-function-declaration") {
1370b57cec5SDimitry Andric             if (Report)
1380b57cec5SDimitry Andric               Diags.Report(diag::warn_unknown_warning_specifier)
1390b57cec5SDimitry Andric                 << "-Werror" << ("-W" + OrigOpt.str());
1400b57cec5SDimitry Andric             continue;
1410b57cec5SDimitry Andric           }
1420b57cec5SDimitry Andric           Specifier = Opt.substr(6);
1430b57cec5SDimitry Andric         }
1440b57cec5SDimitry Andric 
1450b57cec5SDimitry Andric         if (Specifier.empty()) {
1460b57cec5SDimitry Andric           if (SetDiagnostic)
1470b57cec5SDimitry Andric             Diags.setWarningsAsErrors(isPositive);
1480b57cec5SDimitry Andric           continue;
1490b57cec5SDimitry Andric         }
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric         if (SetDiagnostic) {
1520b57cec5SDimitry Andric           // Set the warning as error flag for this specifier.
1530b57cec5SDimitry Andric           Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive);
1540b57cec5SDimitry Andric         } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
1550b57cec5SDimitry Andric           EmitUnknownDiagWarning(Diags, Flavor, "-Werror=", Specifier);
1560b57cec5SDimitry Andric         }
1570b57cec5SDimitry Andric         continue;
1580b57cec5SDimitry Andric       }
1590b57cec5SDimitry Andric 
1600b57cec5SDimitry Andric       // -Wfatal-errors is yet another special case.
1615f757f3fSDimitry Andric       if (Opt.starts_with("fatal-errors")) {
1620b57cec5SDimitry Andric         StringRef Specifier;
1630b57cec5SDimitry Andric         if (Opt.size() != 12) {
1640b57cec5SDimitry Andric           if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) {
1650b57cec5SDimitry Andric             if (Report)
1660b57cec5SDimitry Andric               Diags.Report(diag::warn_unknown_warning_specifier)
1670b57cec5SDimitry Andric                 << "-Wfatal-errors" << ("-W" + OrigOpt.str());
1680b57cec5SDimitry Andric             continue;
1690b57cec5SDimitry Andric           }
1700b57cec5SDimitry Andric           Specifier = Opt.substr(13);
1710b57cec5SDimitry Andric         }
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric         if (Specifier.empty()) {
1740b57cec5SDimitry Andric           if (SetDiagnostic)
1750b57cec5SDimitry Andric             Diags.setErrorsAsFatal(isPositive);
1760b57cec5SDimitry Andric           continue;
1770b57cec5SDimitry Andric         }
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric         if (SetDiagnostic) {
1800b57cec5SDimitry Andric           // Set the error as fatal flag for this specifier.
1810b57cec5SDimitry Andric           Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive);
1820b57cec5SDimitry Andric         } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
1830b57cec5SDimitry Andric           EmitUnknownDiagWarning(Diags, Flavor, "-Wfatal-errors=", Specifier);
1840b57cec5SDimitry Andric         }
1850b57cec5SDimitry Andric         continue;
1860b57cec5SDimitry Andric       }
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric       if (Report) {
1890b57cec5SDimitry Andric         if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
1900b57cec5SDimitry Andric           EmitUnknownDiagWarning(Diags, Flavor, isPositive ? "-W" : "-Wno-",
1910b57cec5SDimitry Andric                                  Opt);
1920b57cec5SDimitry Andric       } else {
1930b57cec5SDimitry Andric         Diags.setSeverityForGroup(Flavor, Opt, Mapping);
1940b57cec5SDimitry Andric       }
1950b57cec5SDimitry Andric     }
1960b57cec5SDimitry Andric 
197cb14a3feSDimitry Andric     for (StringRef Opt : Opts.Remarks) {
1980b57cec5SDimitry Andric       const auto Flavor = diag::Flavor::Remark;
1990b57cec5SDimitry Andric 
2000b57cec5SDimitry Andric       // Check to see if this warning starts with "no-", if so, this is a
2010b57cec5SDimitry Andric       // negative form of the option.
2027a6dacacSDimitry Andric       bool IsPositive = !Opt.consume_front("no-");
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric       auto Severity = IsPositive ? diag::Severity::Remark
2050b57cec5SDimitry Andric                                  : diag::Severity::Ignored;
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric       // -Reverything sets the state of all remarks. Note that all remarks are
2080b57cec5SDimitry Andric       // in remark groups, so we don't need a separate 'all remarks enabled'
2090b57cec5SDimitry Andric       // flag.
2100b57cec5SDimitry Andric       if (Opt == "everything") {
2110b57cec5SDimitry Andric         if (SetDiagnostic)
2120b57cec5SDimitry Andric           Diags.setSeverityForAll(Flavor, Severity);
2130b57cec5SDimitry Andric         continue;
2140b57cec5SDimitry Andric       }
2150b57cec5SDimitry Andric 
2160b57cec5SDimitry Andric       if (Report) {
2170b57cec5SDimitry Andric         if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
2180b57cec5SDimitry Andric           EmitUnknownDiagWarning(Diags, Flavor, IsPositive ? "-R" : "-Rno-",
2190b57cec5SDimitry Andric                                  Opt);
2200b57cec5SDimitry Andric       } else {
2210b57cec5SDimitry Andric         Diags.setSeverityForGroup(Flavor, Opt,
2220b57cec5SDimitry Andric                                   IsPositive ? diag::Severity::Remark
2230b57cec5SDimitry Andric                                              : diag::Severity::Ignored);
2240b57cec5SDimitry Andric       }
2250b57cec5SDimitry Andric     }
2260b57cec5SDimitry Andric   }
2270b57cec5SDimitry Andric }
228