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 
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 // Check that DiagnosticErrorTrap works with SuppressAllDiagnostics.
TEST(DiagnosticTest,suppressAndTrap)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 FatalsAsError works as intended
TEST(DiagnosticTest,fatalsAsError)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     // Diag that would set UnrecoverableErrorOccurred and ErrorOccurred.
58     Diags.Report(diag::err_cannot_open_file) << "file" << "error";
59 
60     // Diag that would set FatalErrorOccurred
61     // (via non-note following a fatal error).
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     // The warning should be emitted and counted only if we're not suppressing
70     // after fatal errors.
71     EXPECT_EQ(Diags.getNumWarnings(), FatalsAsError);
72   }
73 }
TEST(DiagnosticTest,diagnosticError)74 TEST(DiagnosticTest, diagnosticError) {
75   DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions,
76                           new IgnoringDiagConsumer());
77   PartialDiagnostic::DiagStorageAllocator 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