| 1 | //===--- Warnings.cpp - C-Language Front-end ------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Command line warning options handler. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | // |
| 13 | // This file is responsible for handling all warning options. This includes |
| 14 | // a number of -Wfoo options and their variants, which are driven by TableGen- |
| 15 | // generated data, and the special cases -pedantic, -pedantic-errors, -w, |
| 16 | // -Werror and -Wfatal-errors. |
| 17 | // |
| 18 | // Each warning option controls any number of actual warnings. |
| 19 | // Given a warning option 'foo', the following are valid: |
| 20 | // -Wfoo, -Wno-foo, -Werror=foo, -Wfatal-errors=foo |
| 21 | // |
| 22 | // Remark options are also handled here, analogously, except that they are much |
| 23 | // simpler because a remark can't be promoted to an error. |
| 24 | |
| 25 | #include "clang/Basic/AllDiagnostics.h" |
| 26 | #include "clang/Basic/Diagnostic.h" |
| 27 | #include "clang/Basic/DiagnosticDriver.h" |
| 28 | #include "clang/Basic/DiagnosticIDs.h" |
| 29 | #include "clang/Basic/DiagnosticOptions.h" |
| 30 | #include "llvm/ADT/StringRef.h" |
| 31 | #include "llvm/Support/Process.h" |
| 32 | #include "llvm/Support/VirtualFileSystem.h" |
| 33 | #include "llvm/Support/raw_ostream.h" |
| 34 | #include <cstring> |
| 35 | |
| 36 | using namespace clang; |
| 37 | |
| 38 | // EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning |
| 39 | // opts |
| 40 | static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags, |
| 41 | diag::Flavor Flavor, StringRef Prefix, |
| 42 | StringRef Opt) { |
| 43 | StringRef Suggestion = DiagnosticIDs::getNearestOption(Flavor, Group: Opt); |
| 44 | Diags.Report(DiagID: diag::warn_unknown_diag_option) |
| 45 | << (Flavor == diag::Flavor::WarningOrError ? 0 : 1) |
| 46 | << (Prefix.str() += std::string(Opt)) << !Suggestion.empty() |
| 47 | << (Prefix.str() += std::string(Suggestion)); |
| 48 | } |
| 49 | |
| 50 | void clang::ProcessWarningOptions(DiagnosticsEngine &Diags, |
| 51 | const DiagnosticOptions &Opts, |
| 52 | llvm::vfs::FileSystem &VFS, |
| 53 | bool ReportDiags) { |
| 54 | Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers |
| 55 | Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings); |
| 56 | Diags.setShowOverloads(Opts.getShowOverloads()); |
| 57 | |
| 58 | Diags.setElideType(Opts.ElideType); |
| 59 | Diags.setPrintTemplateTree(Opts.ShowTemplateTree); |
| 60 | Diags.setShowColors( |
| 61 | Opts.showColors(StreamHasColors: llvm::sys::Process::StandardErrHasColors())); |
| 62 | |
| 63 | // Handle -ferror-limit |
| 64 | if (Opts.ErrorLimit) |
| 65 | Diags.setErrorLimit(Opts.ErrorLimit); |
| 66 | if (Opts.TemplateBacktraceLimit) |
| 67 | Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit); |
| 68 | if (Opts.ConstexprBacktraceLimit) |
| 69 | Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit); |
| 70 | |
| 71 | // If -pedantic or -pedantic-errors was specified, then we want to map all |
| 72 | // extension diagnostics onto WARNING or ERROR unless the user has futz'd |
| 73 | // around with them explicitly. |
| 74 | if (Opts.PedanticErrors) |
| 75 | Diags.setExtensionHandlingBehavior(diag::Severity::Error); |
| 76 | else if (Opts.Pedantic) |
| 77 | Diags.setExtensionHandlingBehavior(diag::Severity::Warning); |
| 78 | else |
| 79 | Diags.setExtensionHandlingBehavior(diag::Severity::Ignored); |
| 80 | |
| 81 | SmallVector<diag::kind, 10> _Diags; |
| 82 | const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs = |
| 83 | Diags.getDiagnosticIDs(); |
| 84 | // We parse the warning options twice. The first pass sets diagnostic state, |
| 85 | // while the second pass reports warnings/errors. This has the effect that |
| 86 | // we follow the more canonical "last option wins" paradigm when there are |
| 87 | // conflicting options. |
| 88 | for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; ++Report) { |
| 89 | bool SetDiagnostic = (Report == 0); |
| 90 | |
| 91 | // If we've set the diagnostic state and are not reporting diagnostics then |
| 92 | // we're done. |
| 93 | if (!SetDiagnostic && !ReportDiags) |
| 94 | break; |
| 95 | |
| 96 | for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) { |
| 97 | const auto Flavor = diag::Flavor::WarningOrError; |
| 98 | StringRef Opt = Opts.Warnings[i]; |
| 99 | StringRef OrigOpt = Opts.Warnings[i]; |
| 100 | |
| 101 | // Treat -Wformat=0 as an alias for -Wno-format. |
| 102 | if (Opt == "format=0" ) |
| 103 | Opt = "no-format" ; |
| 104 | |
| 105 | // Check to see if this warning starts with "no-", if so, this is a |
| 106 | // negative form of the option. |
| 107 | bool isPositive = !Opt.consume_front(Prefix: "no-" ); |
| 108 | |
| 109 | // Figure out how this option affects the warning. If -Wfoo, map the |
| 110 | // diagnostic to a warning, if -Wno-foo, map it to ignore. |
| 111 | diag::Severity Mapping = |
| 112 | isPositive ? diag::Severity::Warning : diag::Severity::Ignored; |
| 113 | |
| 114 | // -Wsystem-headers is a special case, not driven by the option table. It |
| 115 | // cannot be controlled with -Werror. |
| 116 | if (Opt == "system-headers" ) { |
| 117 | if (SetDiagnostic) |
| 118 | Diags.setSuppressSystemWarnings(!isPositive); |
| 119 | continue; |
| 120 | } |
| 121 | |
| 122 | // -Weverything is a special case as well. It implicitly enables all |
| 123 | // warnings, including ones not explicitly in a warning group. |
| 124 | if (Opt == "everything" ) { |
| 125 | if (SetDiagnostic) { |
| 126 | if (isPositive) { |
| 127 | Diags.setEnableAllWarnings(true); |
| 128 | } else { |
| 129 | Diags.setEnableAllWarnings(false); |
| 130 | Diags.setSeverityForAll(Flavor, Map: diag::Severity::Ignored); |
| 131 | } |
| 132 | } |
| 133 | continue; |
| 134 | } |
| 135 | |
| 136 | // -Werror/-Wno-error is a special case, not controlled by the option |
| 137 | // table. It also has the "specifier" form of -Werror=foo. GCC supports |
| 138 | // the deprecated -Werror-implicit-function-declaration which is used by |
| 139 | // a few projects. |
| 140 | if (Opt.starts_with(Prefix: "error" )) { |
| 141 | StringRef Specifier; |
| 142 | if (Opt.size() > 5) { // Specifier must be present. |
| 143 | if (Opt[5] != '=' && |
| 144 | Opt.substr(Start: 5) != "-implicit-function-declaration" ) { |
| 145 | if (Report) |
| 146 | Diags.Report(DiagID: diag::warn_unknown_warning_specifier) |
| 147 | << "-Werror" << ("-W" + OrigOpt.str()); |
| 148 | continue; |
| 149 | } |
| 150 | Specifier = Opt.substr(Start: 6); |
| 151 | } |
| 152 | |
| 153 | if (Specifier.empty()) { |
| 154 | if (SetDiagnostic) |
| 155 | Diags.setWarningsAsErrors(isPositive); |
| 156 | continue; |
| 157 | } |
| 158 | |
| 159 | if (SetDiagnostic) { |
| 160 | // Set the warning as error flag for this specifier. |
| 161 | Diags.setDiagnosticGroupWarningAsError(Group: Specifier, Enabled: isPositive); |
| 162 | } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Group: Specifier, Diags&: _Diags)) { |
| 163 | EmitUnknownDiagWarning(Diags, Flavor, Prefix: "-Werror=" , Opt: Specifier); |
| 164 | } |
| 165 | continue; |
| 166 | } |
| 167 | |
| 168 | // -Wfatal-errors is yet another special case. |
| 169 | if (Opt.starts_with(Prefix: "fatal-errors" )) { |
| 170 | StringRef Specifier; |
| 171 | if (Opt.size() != 12) { |
| 172 | if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) { |
| 173 | if (Report) |
| 174 | Diags.Report(DiagID: diag::warn_unknown_warning_specifier) |
| 175 | << "-Wfatal-errors" << ("-W" + OrigOpt.str()); |
| 176 | continue; |
| 177 | } |
| 178 | Specifier = Opt.substr(Start: 13); |
| 179 | } |
| 180 | |
| 181 | if (Specifier.empty()) { |
| 182 | if (SetDiagnostic) |
| 183 | Diags.setErrorsAsFatal(isPositive); |
| 184 | continue; |
| 185 | } |
| 186 | |
| 187 | if (SetDiagnostic) { |
| 188 | // Set the error as fatal flag for this specifier. |
| 189 | Diags.setDiagnosticGroupErrorAsFatal(Group: Specifier, Enabled: isPositive); |
| 190 | } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Group: Specifier, Diags&: _Diags)) { |
| 191 | EmitUnknownDiagWarning(Diags, Flavor, Prefix: "-Wfatal-errors=" , Opt: Specifier); |
| 192 | } |
| 193 | continue; |
| 194 | } |
| 195 | |
| 196 | if (Report) { |
| 197 | if (DiagIDs->getDiagnosticsInGroup(Flavor, Group: Opt, Diags&: _Diags)) |
| 198 | EmitUnknownDiagWarning(Diags, Flavor, Prefix: isPositive ? "-W" : "-Wno-" , |
| 199 | Opt); |
| 200 | } else { |
| 201 | Diags.setSeverityForGroup(Flavor, Group: Opt, Map: Mapping); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | for (StringRef Opt : Opts.Remarks) { |
| 206 | const auto Flavor = diag::Flavor::Remark; |
| 207 | |
| 208 | // Check to see if this warning starts with "no-", if so, this is a |
| 209 | // negative form of the option. |
| 210 | bool IsPositive = !Opt.consume_front(Prefix: "no-" ); |
| 211 | |
| 212 | auto Severity = IsPositive ? diag::Severity::Remark |
| 213 | : diag::Severity::Ignored; |
| 214 | |
| 215 | // -Reverything sets the state of all remarks. Note that all remarks are |
| 216 | // in remark groups, so we don't need a separate 'all remarks enabled' |
| 217 | // flag. |
| 218 | if (Opt == "everything" ) { |
| 219 | if (SetDiagnostic) |
| 220 | Diags.setSeverityForAll(Flavor, Map: Severity); |
| 221 | continue; |
| 222 | } |
| 223 | |
| 224 | if (Report) { |
| 225 | if (DiagIDs->getDiagnosticsInGroup(Flavor, Group: Opt, Diags&: _Diags)) |
| 226 | EmitUnknownDiagWarning(Diags, Flavor, Prefix: IsPositive ? "-R" : "-Rno-" , |
| 227 | Opt); |
| 228 | } else { |
| 229 | Diags.setSeverityForGroup(Flavor, Group: Opt, |
| 230 | Map: IsPositive ? diag::Severity::Remark |
| 231 | : diag::Severity::Ignored); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | // Process suppression mappings file after processing other warning flags |
| 237 | // (like -Wno-unknown-warning-option) as we can emit extra warnings during |
| 238 | // processing. |
| 239 | if (!Opts.DiagnosticSuppressionMappingsFile.empty()) { |
| 240 | if (auto FileContents = |
| 241 | VFS.getBufferForFile(Name: Opts.DiagnosticSuppressionMappingsFile)) { |
| 242 | Diags.setDiagSuppressionMapping(**FileContents); |
| 243 | } else if (ReportDiags) { |
| 244 | Diags.Report(DiagID: diag::err_drv_no_such_file) |
| 245 | << Opts.DiagnosticSuppressionMappingsFile; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |