xref: /netbsd-src/external/apache2/llvm/dist/clang/lib/Basic/Warnings.cpp (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
17330f729Sjoerg //===--- Warnings.cpp - C-Language Front-end ------------------------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // Command line warning options handler.
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
127330f729Sjoerg //
137330f729Sjoerg // This file is responsible for handling all warning options. This includes
147330f729Sjoerg // a number of -Wfoo options and their variants, which are driven by TableGen-
157330f729Sjoerg // generated data, and the special cases -pedantic, -pedantic-errors, -w,
167330f729Sjoerg // -Werror and -Wfatal-errors.
177330f729Sjoerg //
187330f729Sjoerg // Each warning option controls any number of actual warnings.
197330f729Sjoerg // Given a warning option 'foo', the following are valid:
207330f729Sjoerg //    -Wfoo, -Wno-foo, -Werror=foo, -Wfatal-errors=foo
217330f729Sjoerg //
227330f729Sjoerg // Remark options are also handled here, analogously, except that they are much
237330f729Sjoerg // simpler because a remark can't be promoted to an error.
247330f729Sjoerg #include "clang/Basic/AllDiagnostics.h"
257330f729Sjoerg #include "clang/Basic/Diagnostic.h"
267330f729Sjoerg #include "clang/Basic/DiagnosticOptions.h"
277330f729Sjoerg #include <algorithm>
287330f729Sjoerg #include <cstring>
297330f729Sjoerg #include <utility>
307330f729Sjoerg using namespace clang;
317330f729Sjoerg 
327330f729Sjoerg // EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning
337330f729Sjoerg // opts
EmitUnknownDiagWarning(DiagnosticsEngine & Diags,diag::Flavor Flavor,StringRef Prefix,StringRef Opt)347330f729Sjoerg static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags,
357330f729Sjoerg                                    diag::Flavor Flavor, StringRef Prefix,
367330f729Sjoerg                                    StringRef Opt) {
377330f729Sjoerg   StringRef Suggestion = DiagnosticIDs::getNearestOption(Flavor, Opt);
387330f729Sjoerg   Diags.Report(diag::warn_unknown_diag_option)
39*e038c9c4Sjoerg       << (Flavor == diag::Flavor::WarningOrError ? 0 : 1)
40*e038c9c4Sjoerg       << (Prefix.str() += std::string(Opt)) << !Suggestion.empty()
41*e038c9c4Sjoerg       << (Prefix.str() += std::string(Suggestion));
427330f729Sjoerg }
437330f729Sjoerg 
ProcessWarningOptions(DiagnosticsEngine & Diags,const DiagnosticOptions & Opts,bool ReportDiags)447330f729Sjoerg void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
457330f729Sjoerg                                   const DiagnosticOptions &Opts,
467330f729Sjoerg                                   bool ReportDiags) {
477330f729Sjoerg   Diags.setSuppressSystemWarnings(true);  // Default to -Wno-system-headers
487330f729Sjoerg   Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
497330f729Sjoerg   Diags.setShowOverloads(Opts.getShowOverloads());
507330f729Sjoerg 
517330f729Sjoerg   Diags.setElideType(Opts.ElideType);
527330f729Sjoerg   Diags.setPrintTemplateTree(Opts.ShowTemplateTree);
537330f729Sjoerg   Diags.setShowColors(Opts.ShowColors);
547330f729Sjoerg 
557330f729Sjoerg   // Handle -ferror-limit
567330f729Sjoerg   if (Opts.ErrorLimit)
577330f729Sjoerg     Diags.setErrorLimit(Opts.ErrorLimit);
587330f729Sjoerg   if (Opts.TemplateBacktraceLimit)
597330f729Sjoerg     Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
607330f729Sjoerg   if (Opts.ConstexprBacktraceLimit)
617330f729Sjoerg     Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit);
627330f729Sjoerg 
637330f729Sjoerg   // If -pedantic or -pedantic-errors was specified, then we want to map all
647330f729Sjoerg   // extension diagnostics onto WARNING or ERROR unless the user has futz'd
657330f729Sjoerg   // around with them explicitly.
667330f729Sjoerg   if (Opts.PedanticErrors)
677330f729Sjoerg     Diags.setExtensionHandlingBehavior(diag::Severity::Error);
687330f729Sjoerg   else if (Opts.Pedantic)
697330f729Sjoerg     Diags.setExtensionHandlingBehavior(diag::Severity::Warning);
707330f729Sjoerg   else
717330f729Sjoerg     Diags.setExtensionHandlingBehavior(diag::Severity::Ignored);
727330f729Sjoerg 
737330f729Sjoerg   SmallVector<diag::kind, 10> _Diags;
747330f729Sjoerg   const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs =
757330f729Sjoerg     Diags.getDiagnosticIDs();
767330f729Sjoerg   // We parse the warning options twice.  The first pass sets diagnostic state,
777330f729Sjoerg   // while the second pass reports warnings/errors.  This has the effect that
787330f729Sjoerg   // we follow the more canonical "last option wins" paradigm when there are
797330f729Sjoerg   // conflicting options.
807330f729Sjoerg   for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; ++Report) {
817330f729Sjoerg     bool SetDiagnostic = (Report == 0);
827330f729Sjoerg 
837330f729Sjoerg     // If we've set the diagnostic state and are not reporting diagnostics then
847330f729Sjoerg     // we're done.
857330f729Sjoerg     if (!SetDiagnostic && !ReportDiags)
867330f729Sjoerg       break;
877330f729Sjoerg 
887330f729Sjoerg     for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
897330f729Sjoerg       const auto Flavor = diag::Flavor::WarningOrError;
907330f729Sjoerg       StringRef Opt = Opts.Warnings[i];
917330f729Sjoerg       StringRef OrigOpt = Opts.Warnings[i];
927330f729Sjoerg 
937330f729Sjoerg       // Treat -Wformat=0 as an alias for -Wno-format.
947330f729Sjoerg       if (Opt == "format=0")
957330f729Sjoerg         Opt = "no-format";
967330f729Sjoerg 
977330f729Sjoerg       // Check to see if this warning starts with "no-", if so, this is a
987330f729Sjoerg       // negative form of the option.
997330f729Sjoerg       bool isPositive = true;
1007330f729Sjoerg       if (Opt.startswith("no-")) {
1017330f729Sjoerg         isPositive = false;
1027330f729Sjoerg         Opt = Opt.substr(3);
1037330f729Sjoerg       }
1047330f729Sjoerg 
1057330f729Sjoerg       // Figure out how this option affects the warning.  If -Wfoo, map the
1067330f729Sjoerg       // diagnostic to a warning, if -Wno-foo, map it to ignore.
1077330f729Sjoerg       diag::Severity Mapping =
1087330f729Sjoerg           isPositive ? diag::Severity::Warning : diag::Severity::Ignored;
1097330f729Sjoerg 
1107330f729Sjoerg       // -Wsystem-headers is a special case, not driven by the option table.  It
1117330f729Sjoerg       // cannot be controlled with -Werror.
1127330f729Sjoerg       if (Opt == "system-headers") {
1137330f729Sjoerg         if (SetDiagnostic)
1147330f729Sjoerg           Diags.setSuppressSystemWarnings(!isPositive);
1157330f729Sjoerg         continue;
1167330f729Sjoerg       }
1177330f729Sjoerg 
1187330f729Sjoerg       // -Weverything is a special case as well.  It implicitly enables all
1197330f729Sjoerg       // warnings, including ones not explicitly in a warning group.
1207330f729Sjoerg       if (Opt == "everything") {
1217330f729Sjoerg         if (SetDiagnostic) {
1227330f729Sjoerg           if (isPositive) {
1237330f729Sjoerg             Diags.setEnableAllWarnings(true);
1247330f729Sjoerg           } else {
1257330f729Sjoerg             Diags.setEnableAllWarnings(false);
1267330f729Sjoerg             Diags.setSeverityForAll(Flavor, diag::Severity::Ignored);
1277330f729Sjoerg           }
1287330f729Sjoerg         }
1297330f729Sjoerg         continue;
1307330f729Sjoerg       }
1317330f729Sjoerg 
1327330f729Sjoerg       // -Werror/-Wno-error is a special case, not controlled by the option
133*e038c9c4Sjoerg       // table. It also has the "specifier" form of -Werror=foo. GCC supports
134*e038c9c4Sjoerg       // the deprecated -Werror-implicit-function-declaration which is used by
135*e038c9c4Sjoerg       // a few projects.
1367330f729Sjoerg       if (Opt.startswith("error")) {
1377330f729Sjoerg         StringRef Specifier;
1387330f729Sjoerg         if (Opt.size() > 5) {  // Specifier must be present.
139*e038c9c4Sjoerg           if (Opt[5] != '=' &&
140*e038c9c4Sjoerg               Opt.substr(5) != "-implicit-function-declaration") {
1417330f729Sjoerg             if (Report)
1427330f729Sjoerg               Diags.Report(diag::warn_unknown_warning_specifier)
1437330f729Sjoerg                 << "-Werror" << ("-W" + OrigOpt.str());
1447330f729Sjoerg             continue;
1457330f729Sjoerg           }
1467330f729Sjoerg           Specifier = Opt.substr(6);
1477330f729Sjoerg         }
1487330f729Sjoerg 
1497330f729Sjoerg         if (Specifier.empty()) {
1507330f729Sjoerg           if (SetDiagnostic)
1517330f729Sjoerg             Diags.setWarningsAsErrors(isPositive);
1527330f729Sjoerg           continue;
1537330f729Sjoerg         }
1547330f729Sjoerg 
1557330f729Sjoerg         if (SetDiagnostic) {
1567330f729Sjoerg           // Set the warning as error flag for this specifier.
1577330f729Sjoerg           Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive);
1587330f729Sjoerg         } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
1597330f729Sjoerg           EmitUnknownDiagWarning(Diags, Flavor, "-Werror=", Specifier);
1607330f729Sjoerg         }
1617330f729Sjoerg         continue;
1627330f729Sjoerg       }
1637330f729Sjoerg 
1647330f729Sjoerg       // -Wfatal-errors is yet another special case.
1657330f729Sjoerg       if (Opt.startswith("fatal-errors")) {
1667330f729Sjoerg         StringRef Specifier;
1677330f729Sjoerg         if (Opt.size() != 12) {
1687330f729Sjoerg           if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) {
1697330f729Sjoerg             if (Report)
1707330f729Sjoerg               Diags.Report(diag::warn_unknown_warning_specifier)
1717330f729Sjoerg                 << "-Wfatal-errors" << ("-W" + OrigOpt.str());
1727330f729Sjoerg             continue;
1737330f729Sjoerg           }
1747330f729Sjoerg           Specifier = Opt.substr(13);
1757330f729Sjoerg         }
1767330f729Sjoerg 
1777330f729Sjoerg         if (Specifier.empty()) {
1787330f729Sjoerg           if (SetDiagnostic)
1797330f729Sjoerg             Diags.setErrorsAsFatal(isPositive);
1807330f729Sjoerg           continue;
1817330f729Sjoerg         }
1827330f729Sjoerg 
1837330f729Sjoerg         if (SetDiagnostic) {
1847330f729Sjoerg           // Set the error as fatal flag for this specifier.
1857330f729Sjoerg           Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive);
1867330f729Sjoerg         } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
1877330f729Sjoerg           EmitUnknownDiagWarning(Diags, Flavor, "-Wfatal-errors=", Specifier);
1887330f729Sjoerg         }
1897330f729Sjoerg         continue;
1907330f729Sjoerg       }
1917330f729Sjoerg 
1927330f729Sjoerg       if (Report) {
1937330f729Sjoerg         if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
1947330f729Sjoerg           EmitUnknownDiagWarning(Diags, Flavor, isPositive ? "-W" : "-Wno-",
1957330f729Sjoerg                                  Opt);
1967330f729Sjoerg       } else {
1977330f729Sjoerg         Diags.setSeverityForGroup(Flavor, Opt, Mapping);
1987330f729Sjoerg       }
1997330f729Sjoerg     }
2007330f729Sjoerg 
2017330f729Sjoerg     for (unsigned i = 0, e = Opts.Remarks.size(); i != e; ++i) {
2027330f729Sjoerg       StringRef Opt = Opts.Remarks[i];
2037330f729Sjoerg       const auto Flavor = diag::Flavor::Remark;
2047330f729Sjoerg 
2057330f729Sjoerg       // Check to see if this warning starts with "no-", if so, this is a
2067330f729Sjoerg       // negative form of the option.
2077330f729Sjoerg       bool IsPositive = !Opt.startswith("no-");
2087330f729Sjoerg       if (!IsPositive) Opt = Opt.substr(3);
2097330f729Sjoerg 
2107330f729Sjoerg       auto Severity = IsPositive ? diag::Severity::Remark
2117330f729Sjoerg                                  : diag::Severity::Ignored;
2127330f729Sjoerg 
2137330f729Sjoerg       // -Reverything sets the state of all remarks. Note that all remarks are
2147330f729Sjoerg       // in remark groups, so we don't need a separate 'all remarks enabled'
2157330f729Sjoerg       // flag.
2167330f729Sjoerg       if (Opt == "everything") {
2177330f729Sjoerg         if (SetDiagnostic)
2187330f729Sjoerg           Diags.setSeverityForAll(Flavor, Severity);
2197330f729Sjoerg         continue;
2207330f729Sjoerg       }
2217330f729Sjoerg 
2227330f729Sjoerg       if (Report) {
2237330f729Sjoerg         if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
2247330f729Sjoerg           EmitUnknownDiagWarning(Diags, Flavor, IsPositive ? "-R" : "-Rno-",
2257330f729Sjoerg                                  Opt);
2267330f729Sjoerg       } else {
2277330f729Sjoerg         Diags.setSeverityForGroup(Flavor, Opt,
2287330f729Sjoerg                                   IsPositive ? diag::Severity::Remark
2297330f729Sjoerg                                              : diag::Severity::Ignored);
2307330f729Sjoerg       }
2317330f729Sjoerg     }
2327330f729Sjoerg   }
2337330f729Sjoerg }
234