| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 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 | |
| 14 | using namespace llvm; |
| 15 | using namespace clang; |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | |
| 20 | TEST(DiagnosticTest, suppressAndTrap) { |
| 21 | DiagnosticsEngine Diags(new DiagnosticIDs(), |
| 22 | new DiagnosticOptions, |
| 23 | new IgnoringDiagConsumer()); |
| 24 | Diags.setSuppressAllDiagnostics(true); |
| 25 | |
| 26 | { |
| 27 | DiagnosticErrorTrap trap(Diags); |
| 28 | |
| 29 | |
| 30 | Diags.Report(diag::err_target_unknown_triple) << "unknown"; |
| 31 | |
| 32 | |
| 33 | Diags.Report(diag::err_cannot_open_file) << "file" << "error"; |
| 34 | |
| 35 | |
| 36 | |
| 37 | Diags.Report(diag::warn_mt_message) << "warning"; |
| 38 | |
| 39 | EXPECT_TRUE(trap.hasErrorOccurred()); |
| 40 | EXPECT_TRUE(trap.hasUnrecoverableErrorOccurred()); |
| 41 | } |
| 42 | |
| 43 | EXPECT_FALSE(Diags.hasErrorOccurred()); |
| 44 | EXPECT_FALSE(Diags.hasFatalErrorOccurred()); |
| 45 | EXPECT_FALSE(Diags.hasUncompilableErrorOccurred()); |
| 46 | EXPECT_FALSE(Diags.hasUnrecoverableErrorOccurred()); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | TEST(DiagnosticTest, fatalsAsError) { |
| 51 | for (unsigned FatalsAsError = 0; FatalsAsError != 2; ++FatalsAsError) { |
| 52 | DiagnosticsEngine Diags(new DiagnosticIDs(), |
| 53 | new DiagnosticOptions, |
| 54 | new IgnoringDiagConsumer()); |
| 55 | Diags.setFatalsAsError(FatalsAsError); |
| 56 | |
| 57 | |
| 58 | Diags.Report(diag::err_cannot_open_file) << "file" << "error"; |
| 59 | |
| 60 | |
| 61 | |
| 62 | Diags.Report(diag::warn_mt_message) << "warning"; |
| 63 | |
| 64 | EXPECT_TRUE(Diags.hasErrorOccurred()); |
| 65 | EXPECT_EQ(Diags.hasFatalErrorOccurred(), FatalsAsError ? 0u : 1u); |
| 66 | EXPECT_TRUE(Diags.hasUncompilableErrorOccurred()); |
| 67 | EXPECT_TRUE(Diags.hasUnrecoverableErrorOccurred()); |
| 68 | |
| 69 | |
| 70 | |
| 71 | EXPECT_EQ(Diags.getNumWarnings(), FatalsAsError); |
| 72 | } |
| 73 | } |
| 74 | TEST(DiagnosticTest, diagnosticError) { |
| 75 | DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions, |
| 76 | new IgnoringDiagConsumer()); |
| 77 | PartialDiagnostic::StorageAllocator Alloc; |
| 78 | llvm::Expected<std::pair<int, int>> Value = DiagnosticError::create( |
| 79 | SourceLocation(), PartialDiagnostic(diag::err_cannot_open_file, Alloc) |
| 80 | << "file" |
| 81 | << "error"); |
| 82 | ASSERT_TRUE(!Value); |
| 83 | llvm::Error Err = Value.takeError(); |
| 84 | Optional<PartialDiagnosticAt> ErrDiag = DiagnosticError::take(Err); |
| 85 | llvm::cantFail(std::move(Err)); |
| 86 | ASSERT_FALSE(!ErrDiag); |
| 87 | EXPECT_EQ(ErrDiag->first, SourceLocation()); |
| 88 | EXPECT_EQ(ErrDiag->second.getDiagID(), diag::err_cannot_open_file); |
| 89 | |
| 90 | Value = std::make_pair(20, 1); |
| 91 | ASSERT_FALSE(!Value); |
| 92 | EXPECT_EQ(*Value, std::make_pair(20, 1)); |
| 93 | EXPECT_EQ(Value->first, 20); |
| 94 | } |
| 95 | } |
| 96 | |