Warnings.cpp 8.6 KB

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