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" 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 // Check that DiagnosticErrorTrap works with SuppressAllDiagnostics. 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 // Diag that would set UncompilableErrorOccurred and ErrorOccurred. 30 Diags.Report(diag::err_target_unknown_triple) << "unknown"; 31 32 // Diag that would set UnrecoverableErrorOccurred and ErrorOccurred. 33 Diags.Report(diag::err_cannot_open_file) << "file" << "error"; 34 35 // Diag that would set FatalErrorOccurred 36 // (via non-note following a fatal error). 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 // Check that FatalsAsErrors works as intended 50 TEST(DiagnosticTest, fatalsAsErrors) { 51 DiagnosticsEngine Diags(new DiagnosticIDs(), 52 new DiagnosticOptions, 53 new IgnoringDiagConsumer()); 54 Diags.setFatalsAsError(true); 55 56 // Diag that would set UncompilableErrorOccurred and ErrorOccurred. 57 Diags.Report(diag::err_target_unknown_triple) << "unknown"; 58 59 // Diag that would set UnrecoverableErrorOccurred and ErrorOccurred. 60 Diags.Report(diag::err_cannot_open_file) << "file" << "error"; 61 62 // Diag that would set FatalErrorOccurred 63 // (via non-note following a fatal error). 64 Diags.Report(diag::warn_mt_message) << "warning"; 65 66 EXPECT_TRUE(Diags.hasErrorOccurred()); 67 EXPECT_FALSE(Diags.hasFatalErrorOccurred()); 68 EXPECT_TRUE(Diags.hasUncompilableErrorOccurred()); 69 EXPECT_TRUE(Diags.hasUnrecoverableErrorOccurred()); 70 } 71 72 } 73