DiagnosticTest.cpp 3.5 KB

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