DiagnosticTest.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. //===- unittests/Basic/DiagnosticTest.cpp -- Diagnostic engine tests ------===//
  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. #include "clang/Basic/Diagnostic.h"
  10. #include "clang/Basic/DiagnosticError.h"
  11. #include "clang/Basic/DiagnosticIDs.h"
  12. #include "gtest/gtest.h"
  13. using namespace llvm;
  14. using namespace clang;
  15. namespace {
  16. // Check that DiagnosticErrorTrap works with SuppressAllDiagnostics.
  17. TEST(DiagnosticTest, suppressAndTrap) {
  18. DiagnosticsEngine Diags(new DiagnosticIDs(),
  19. new DiagnosticOptions,
  20. new IgnoringDiagConsumer());
  21. Diags.setSuppressAllDiagnostics(true);
  22. {
  23. DiagnosticErrorTrap trap(Diags);
  24. // Diag that would set UncompilableErrorOccurred and ErrorOccurred.
  25. Diags.Report(diag::err_target_unknown_triple) << "unknown";
  26. // Diag that would set UnrecoverableErrorOccurred and ErrorOccurred.
  27. Diags.Report(diag::err_cannot_open_file) << "file" << "error";
  28. // Diag that would set FatalErrorOccurred
  29. // (via non-note following a fatal error).
  30. Diags.Report(diag::warn_mt_message) << "warning";
  31. EXPECT_TRUE(trap.hasErrorOccurred());
  32. EXPECT_TRUE(trap.hasUnrecoverableErrorOccurred());
  33. }
  34. EXPECT_FALSE(Diags.hasErrorOccurred());
  35. EXPECT_FALSE(Diags.hasFatalErrorOccurred());
  36. EXPECT_FALSE(Diags.hasUncompilableErrorOccurred());
  37. EXPECT_FALSE(Diags.hasUnrecoverableErrorOccurred());
  38. }
  39. // Check that SuppressAfterFatalError works as intended
  40. TEST(DiagnosticTest, suppressAfterFatalError) {
  41. for (unsigned Suppress = 0; Suppress != 2; ++Suppress) {
  42. DiagnosticsEngine Diags(new DiagnosticIDs(),
  43. new DiagnosticOptions,
  44. new IgnoringDiagConsumer());
  45. Diags.setSuppressAfterFatalError(Suppress);
  46. // Diag that would set UnrecoverableErrorOccurred and ErrorOccurred.
  47. Diags.Report(diag::err_cannot_open_file) << "file" << "error";
  48. // Diag that would set FatalErrorOccurred
  49. // (via non-note following a fatal error).
  50. Diags.Report(diag::warn_mt_message) << "warning";
  51. EXPECT_TRUE(Diags.hasErrorOccurred());
  52. EXPECT_TRUE(Diags.hasFatalErrorOccurred());
  53. EXPECT_TRUE(Diags.hasUncompilableErrorOccurred());
  54. EXPECT_TRUE(Diags.hasUnrecoverableErrorOccurred());
  55. // The warning should be emitted and counted only if we're not suppressing
  56. // after fatal errors.
  57. EXPECT_EQ(Diags.getNumWarnings(), Suppress ? 0u : 1u);
  58. }
  59. }
  60. TEST(DiagnosticTest, diagnosticError) {
  61. DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions,
  62. new IgnoringDiagConsumer());
  63. PartialDiagnostic::StorageAllocator Alloc;
  64. llvm::Expected<std::pair<int, int>> Value = DiagnosticError::create(
  65. SourceLocation(), PartialDiagnostic(diag::err_cannot_open_file, Alloc)
  66. << "file"
  67. << "error");
  68. ASSERT_TRUE(!Value);
  69. llvm::Error Err = Value.takeError();
  70. Optional<PartialDiagnosticAt> ErrDiag = DiagnosticError::take(Err);
  71. llvm::cantFail(std::move(Err));
  72. ASSERT_FALSE(!ErrDiag);
  73. EXPECT_EQ(ErrDiag->first, SourceLocation());
  74. EXPECT_EQ(ErrDiag->second.getDiagID(), diag::err_cannot_open_file);
  75. Value = std::make_pair(20, 1);
  76. ASSERT_FALSE(!Value);
  77. EXPECT_EQ(*Value, std::make_pair(20, 1));
  78. EXPECT_EQ(Value->first, 20);
  79. }
  80. }