Richard Smith | a268671 | 2014-12-05 21:52:58 +0000 | [diff] [blame] | 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 | |
| 10 | #include "clang/Basic/Diagnostic.h" |
Alex Lorenz | d0e2726 | 2017-08-25 15:48:00 +0000 | [diff] [blame^] | 11 | #include "clang/Basic/DiagnosticError.h" |
Richard Smith | a268671 | 2014-12-05 21:52:58 +0000 | [diff] [blame] | 12 | #include "clang/Basic/DiagnosticIDs.h" |
| 13 | #include "gtest/gtest.h" |
| 14 | |
| 15 | using namespace llvm; |
| 16 | using namespace clang; |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | // Check that DiagnosticErrorTrap works with SuppressAllDiagnostics. |
| 21 | TEST(DiagnosticTest, suppressAndTrap) { |
| 22 | DiagnosticsEngine Diags(new DiagnosticIDs(), |
| 23 | new DiagnosticOptions, |
| 24 | new IgnoringDiagConsumer()); |
| 25 | Diags.setSuppressAllDiagnostics(true); |
| 26 | |
| 27 | { |
| 28 | DiagnosticErrorTrap trap(Diags); |
| 29 | |
| 30 | // Diag that would set UncompilableErrorOccurred and ErrorOccurred. |
| 31 | Diags.Report(diag::err_target_unknown_triple) << "unknown"; |
| 32 | |
| 33 | // Diag that would set UnrecoverableErrorOccurred and ErrorOccurred. |
| 34 | Diags.Report(diag::err_cannot_open_file) << "file" << "error"; |
| 35 | |
| 36 | // Diag that would set FatalErrorOccurred |
| 37 | // (via non-note following a fatal error). |
| 38 | Diags.Report(diag::warn_mt_message) << "warning"; |
| 39 | |
| 40 | EXPECT_TRUE(trap.hasErrorOccurred()); |
| 41 | EXPECT_TRUE(trap.hasUnrecoverableErrorOccurred()); |
| 42 | } |
| 43 | |
| 44 | EXPECT_FALSE(Diags.hasErrorOccurred()); |
| 45 | EXPECT_FALSE(Diags.hasFatalErrorOccurred()); |
| 46 | EXPECT_FALSE(Diags.hasUncompilableErrorOccurred()); |
| 47 | EXPECT_FALSE(Diags.hasUnrecoverableErrorOccurred()); |
| 48 | } |
| 49 | |
Richard Smith | e37391c | 2017-05-03 00:28:49 +0000 | [diff] [blame] | 50 | // Check that SuppressAfterFatalError works as intended |
| 51 | TEST(DiagnosticTest, suppressAfterFatalError) { |
| 52 | for (unsigned Suppress = 0; Suppress != 2; ++Suppress) { |
| 53 | DiagnosticsEngine Diags(new DiagnosticIDs(), |
| 54 | new DiagnosticOptions, |
| 55 | new IgnoringDiagConsumer()); |
| 56 | Diags.setSuppressAfterFatalError(Suppress); |
Manuel Klimek | 016c024 | 2016-03-01 10:56:19 +0000 | [diff] [blame] | 57 | |
Richard Smith | e37391c | 2017-05-03 00:28:49 +0000 | [diff] [blame] | 58 | // Diag that would set UnrecoverableErrorOccurred and ErrorOccurred. |
| 59 | Diags.Report(diag::err_cannot_open_file) << "file" << "error"; |
Manuel Klimek | 016c024 | 2016-03-01 10:56:19 +0000 | [diff] [blame] | 60 | |
Richard Smith | e37391c | 2017-05-03 00:28:49 +0000 | [diff] [blame] | 61 | // Diag that would set FatalErrorOccurred |
| 62 | // (via non-note following a fatal error). |
| 63 | Diags.Report(diag::warn_mt_message) << "warning"; |
Manuel Klimek | 016c024 | 2016-03-01 10:56:19 +0000 | [diff] [blame] | 64 | |
Richard Smith | e37391c | 2017-05-03 00:28:49 +0000 | [diff] [blame] | 65 | EXPECT_TRUE(Diags.hasErrorOccurred()); |
| 66 | EXPECT_TRUE(Diags.hasFatalErrorOccurred()); |
| 67 | EXPECT_TRUE(Diags.hasUncompilableErrorOccurred()); |
| 68 | EXPECT_TRUE(Diags.hasUnrecoverableErrorOccurred()); |
Manuel Klimek | 016c024 | 2016-03-01 10:56:19 +0000 | [diff] [blame] | 69 | |
Richard Smith | e37391c | 2017-05-03 00:28:49 +0000 | [diff] [blame] | 70 | // The warning should be emitted and counted only if we're not suppressing |
| 71 | // after fatal errors. |
| 72 | EXPECT_EQ(Diags.getNumWarnings(), Suppress ? 0u : 1u); |
| 73 | } |
Manuel Klimek | 016c024 | 2016-03-01 10:56:19 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Alex Lorenz | d0e2726 | 2017-08-25 15:48:00 +0000 | [diff] [blame^] | 76 | TEST(DiagnosticTest, diagnosticError) { |
| 77 | DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions, |
| 78 | new IgnoringDiagConsumer()); |
| 79 | PartialDiagnostic::StorageAllocator Alloc; |
| 80 | llvm::Expected<std::pair<int, int>> Value = DiagnosticError::create( |
| 81 | SourceLocation(), PartialDiagnostic(diag::err_cannot_open_file, Alloc) |
| 82 | << "file" |
| 83 | << "error"); |
| 84 | ASSERT_TRUE(!Value); |
| 85 | llvm::Error Err = Value.takeError(); |
| 86 | Optional<PartialDiagnosticAt> ErrDiag = DiagnosticError::take(Err); |
| 87 | llvm::cantFail(std::move(Err)); |
| 88 | ASSERT_FALSE(!ErrDiag); |
| 89 | EXPECT_EQ(ErrDiag->first, SourceLocation()); |
| 90 | EXPECT_EQ(ErrDiag->second.getDiagID(), diag::err_cannot_open_file); |
| 91 | |
| 92 | Value = std::make_pair(20, 1); |
| 93 | ASSERT_FALSE(!Value); |
| 94 | EXPECT_EQ(*Value, std::make_pair(20, 1)); |
| 95 | EXPECT_EQ(Value->first, 20); |
| 96 | } |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 97 | } |