Warnings.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #include "clang/Basic/AllDiagnostics.h"
  25. #include "clang/Basic/Diagnostic.h"
  26. #include "clang/Basic/DiagnosticOptions.h"
  27. #include <algorithm>
  28. #include <cstring>
  29. #include <utility>
  30. using namespace clang;
  31. // EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning
  32. // opts
  33. static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags,
  34. diag::Flavor Flavor, StringRef Prefix,
  35. StringRef Opt) {
  36. StringRef Suggestion = DiagnosticIDs::getNearestOption(Flavor, Opt);
  37. Diags.Report(diag::warn_unknown_diag_option)
  38. << (Flavor == diag::Flavor::WarningOrError ? 0 : 1)
  39. << (Prefix.str() += std::string(Opt)) << !Suggestion.empty()
  40. << (Prefix.str() += std::string(Suggestion));
  41. }
  42. void clang::ProcessWarningOptions(DiagnosticsEngine &Diags,
  43. const DiagnosticOptions &Opts,
  44. bool ReportDiags) {
  45. Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers
  46. Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings);
  47. Diags.setShowOverloads(Opts.getShowOverloads());
  48. Diags.setElideType(Opts.ElideType);
  49. Diags.setPrintTemplateTree(Opts.ShowTemplateTree);
  50. Diags.setShowColors(Opts.ShowColors);
  51. // Handle -ferror-limit
  52. if (Opts.ErrorLimit)
  53. Diags.setErrorLimit(Opts.ErrorLimit);
  54. if (Opts.TemplateBacktraceLimit)
  55. Diags.setTemplateBacktraceLimit(Opts.TemplateBacktraceLimit);
  56. if (Opts.ConstexprBacktraceLimit)
  57. Diags.setConstexprBacktraceLimit(Opts.ConstexprBacktraceLimit);
  58. // If -pedantic or -pedantic-errors was specified, then we want to map all
  59. // extension diagnostics onto WARNING or ERROR unless the user has futz'd
  60. // around with them explicitly.
  61. if (Opts.PedanticErrors)
  62. Diags.setExtensionHandlingBehavior(diag::Severity::Error);
  63. else if (Opts.Pedantic)
  64. Diags.setExtensionHandlingBehavior(diag::Severity::Warning);
  65. else
  66. Diags.setExtensionHandlingBehavior(diag::Severity::Ignored);
  67. SmallVector<diag::kind, 10> _Diags;
  68. const IntrusiveRefCntPtr< DiagnosticIDs > DiagIDs =
  69. Diags.getDiagnosticIDs();
  70. // We parse the warning options twice. The first pass sets diagnostic state,
  71. // while the second pass reports warnings/errors. This has the effect that
  72. // we follow the more canonical "last option wins" paradigm when there are
  73. // conflicting options.
  74. for (unsigned Report = 0, ReportEnd = 2; Report != ReportEnd; ++Report) {
  75. bool SetDiagnostic = (Report == 0);
  76. // If we've set the diagnostic state and are not reporting diagnostics then
  77. // we're done.
  78. if (!SetDiagnostic && !ReportDiags)
  79. break;
  80. for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
  81. const auto Flavor = diag::Flavor::WarningOrError;
  82. StringRef Opt = Opts.Warnings[i];
  83. StringRef OrigOpt = Opts.Warnings[i];
  84. // Treat -Wformat=0 as an alias for -Wno-format.
  85. if (Opt == "format=0")
  86. Opt = "no-format";
  87. // Check to see if this warning starts with "no-", if so, this is a
  88. // negative form of the option.
  89. bool isPositive = true;
  90. if (Opt.startswith("no-")) {
  91. isPositive = false;
  92. Opt = Opt.substr(3);
  93. }
  94. // Figure out how this option affects the warning. If -Wfoo, map the
  95. // diagnostic to a warning, if -Wno-foo, map it to ignore.
  96. diag::Severity Mapping =
  97. isPositive ? diag::Severity::Warning : diag::Severity::Ignored;
  98. // -Wsystem-headers is a special case, not driven by the option table. It
  99. // cannot be controlled with -Werror.
  100. if (Opt == "system-headers") {
  101. if (SetDiagnostic)
  102. Diags.setSuppressSystemWarnings(!isPositive);
  103. continue;
  104. }
  105. // -Weverything is a special case as well. It implicitly enables all
  106. // warnings, including ones not explicitly in a warning group.
  107. if (Opt == "everything") {
  108. if (SetDiagnostic) {
  109. if (isPositive) {
  110. Diags.setEnableAllWarnings(true);
  111. } else {
  112. Diags.setEnableAllWarnings(false);
  113. Diags.setSeverityForAll(Flavor, diag::Severity::Ignored);
  114. }
  115. }
  116. continue;
  117. }
  118. // -Werror/-Wno-error is a special case, not controlled by the option
  119. // table. It also has the "specifier" form of -Werror=foo. GCC supports
  120. // the deprecated -Werror-implicit-function-declaration which is used by
  121. // a few projects.
  122. if (Opt.startswith("error")) {
  123. StringRef Specifier;
  124. if (Opt.size() > 5) { // Specifier must be present.
  125. if (Opt[5] != '=' &&
  126. Opt.substr(5) != "-implicit-function-declaration") {
  127. if (Report)
  128. Diags.Report(diag::warn_unknown_warning_specifier)
  129. << "-Werror" << ("-W" + OrigOpt.str());
  130. continue;
  131. }
  132. Specifier = Opt.substr(6);
  133. }
  134. if (Specifier.empty()) {
  135. if (SetDiagnostic)
  136. Diags.setWarningsAsErrors(isPositive);
  137. continue;
  138. }
  139. if (SetDiagnostic) {
  140. // Set the warning as error flag for this specifier.
  141. Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive);
  142. } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
  143. EmitUnknownDiagWarning(Diags, Flavor, "-Werror=", Specifier);
  144. }
  145. continue;
  146. }
  147. // -Wfatal-errors is yet another special case.
  148. if (Opt.startswith("fatal-errors")) {
  149. StringRef Specifier;
  150. if (Opt.size() != 12) {
  151. if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) {
  152. if (Report)
  153. Diags.Report(diag::warn_unknown_warning_specifier)
  154. << "-Wfatal-errors" << ("-W" + OrigOpt.str());
  155. continue;
  156. }
  157. Specifier = Opt.substr(13);
  158. }
  159. if (Specifier.empty()) {
  160. if (SetDiagnostic)
  161. Diags.setErrorsAsFatal(isPositive);
  162. continue;
  163. }
  164. if (SetDiagnostic) {
  165. // Set the error as fatal flag for this specifier.
  166. Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive);
  167. } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
  168. EmitUnknownDiagWarning(Diags, Flavor, "-Wfatal-errors=", Specifier);
  169. }
  170. continue;
  171. }
  172. if (Report) {
  173. if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
  174. EmitUnknownDiagWarning(Diags, Flavor, isPositive ? "-W" : "-Wno-",
  175. Opt);
  176. } else {
  177. Diags.setSeverityForGroup(Flavor, Opt, Mapping);
  178. }
  179. }
  180. for (unsigned i = 0, e = Opts.Remarks.size(); i != e; ++i) {
  181. StringRef Opt = Opts.Remarks[i];
  182. const auto Flavor = diag::Flavor::Remark;
  183. // Check to see if this warning starts with "no-", if so, this is a
  184. // negative form of the option.
  185. bool IsPositive = !Opt.startswith("no-");
  186. if (!IsPositive) Opt = Opt.substr(3);
  187. auto Severity = IsPositive ? diag::Severity::Remark
  188. : diag::Severity::Ignored;
  189. // -Reverything sets the state of all remarks. Note that all remarks are
  190. // in remark groups, so we don't need a separate 'all remarks enabled'
  191. // flag.
  192. if (Opt == "everything") {
  193. if (SetDiagnostic)
  194. Diags.setSeverityForAll(Flavor, Severity);
  195. continue;
  196. }
  197. if (Report) {
  198. if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
  199. EmitUnknownDiagWarning(Diags, Flavor, IsPositive ? "-R" : "-Rno-",
  200. Opt);
  201. } else {
  202. Diags.setSeverityForGroup(Flavor, Opt,
  203. IsPositive ? diag::Severity::Remark
  204. : diag::Severity::Ignored);
  205. }
  206. }
  207. }
  208. }