Warnings.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. // EmitUnknownDiagWarning - Emit a warning and typo hint for unknown warning
  33. // opts
  34. static void EmitUnknownDiagWarning(DiagnosticsEngine &Diags,
  35. diag::Flavor Flavor, StringRef Prefix,
  36. StringRef Opt) {
  37. StringRef Suggestion = DiagnosticIDs::getNearestOption(Flavor, Opt);
  38. Diags.Report(diag::warn_unknown_diag_option)
  39. << (Flavor == diag::Flavor::WarningOrError ? 0 : 1) << (Prefix.str() += Opt)
  40. << !Suggestion.empty() << (Prefix.str() += 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 and -Werror-foo.
  120. if (Opt.startswith("error")) {
  121. StringRef Specifier;
  122. if (Opt.size() > 5) { // Specifier must be present.
  123. if ((Opt[5] != '=' && Opt[5] != '-') || Opt.size() == 6) {
  124. if (Report)
  125. Diags.Report(diag::warn_unknown_warning_specifier)
  126. << "-Werror" << ("-W" + OrigOpt.str());
  127. continue;
  128. }
  129. Specifier = Opt.substr(6);
  130. }
  131. if (Specifier.empty()) {
  132. if (SetDiagnostic)
  133. Diags.setWarningsAsErrors(isPositive);
  134. continue;
  135. }
  136. if (SetDiagnostic) {
  137. // Set the warning as error flag for this specifier.
  138. Diags.setDiagnosticGroupWarningAsError(Specifier, isPositive);
  139. } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
  140. EmitUnknownDiagWarning(Diags, Flavor, "-Werror=", Specifier);
  141. }
  142. continue;
  143. }
  144. // -Wfatal-errors is yet another special case.
  145. if (Opt.startswith("fatal-errors")) {
  146. StringRef Specifier;
  147. if (Opt.size() != 12) {
  148. if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) {
  149. if (Report)
  150. Diags.Report(diag::warn_unknown_warning_specifier)
  151. << "-Wfatal-errors" << ("-W" + OrigOpt.str());
  152. continue;
  153. }
  154. Specifier = Opt.substr(13);
  155. }
  156. if (Specifier.empty()) {
  157. if (SetDiagnostic)
  158. Diags.setErrorsAsFatal(isPositive);
  159. continue;
  160. }
  161. if (SetDiagnostic) {
  162. // Set the error as fatal flag for this specifier.
  163. Diags.setDiagnosticGroupErrorAsFatal(Specifier, isPositive);
  164. } else if (DiagIDs->getDiagnosticsInGroup(Flavor, Specifier, _Diags)) {
  165. EmitUnknownDiagWarning(Diags, Flavor, "-Wfatal-errors=", Specifier);
  166. }
  167. continue;
  168. }
  169. if (Report) {
  170. if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
  171. EmitUnknownDiagWarning(Diags, Flavor, isPositive ? "-W" : "-Wno-",
  172. Opt);
  173. } else {
  174. Diags.setSeverityForGroup(Flavor, Opt, Mapping);
  175. }
  176. }
  177. for (unsigned i = 0, e = Opts.Remarks.size(); i != e; ++i) {
  178. StringRef Opt = Opts.Remarks[i];
  179. const auto Flavor = diag::Flavor::Remark;
  180. // Check to see if this warning starts with "no-", if so, this is a
  181. // negative form of the option.
  182. bool IsPositive = !Opt.startswith("no-");
  183. if (!IsPositive) Opt = Opt.substr(3);
  184. auto Severity = IsPositive ? diag::Severity::Remark
  185. : diag::Severity::Ignored;
  186. // -Reverything sets the state of all remarks. Note that all remarks are
  187. // in remark groups, so we don't need a separate 'all remarks enabled'
  188. // flag.
  189. if (Opt == "everything") {
  190. if (SetDiagnostic)
  191. Diags.setSeverityForAll(Flavor, Severity);
  192. continue;
  193. }
  194. if (Report) {
  195. if (DiagIDs->getDiagnosticsInGroup(Flavor, Opt, _Diags))
  196. EmitUnknownDiagWarning(Diags, Flavor, IsPositive ? "-R" : "-Rno-",
  197. Opt);
  198. } else {
  199. Diags.setSeverityForGroup(Flavor, Opt,
  200. IsPositive ? diag::Severity::Remark
  201. : diag::Severity::Ignored);
  202. }
  203. }
  204. }
  205. }