Warnings.cpp 8.8 KB

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