Debian Codegraph @

llvm-project/clang/unittests/Basic/DiagnosticTest.cpp

raw
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 #include <optional>
14
15 using namespace llvm;
16 using namespace clang;
17
18 void clang::DiagnosticsTestHelper(DiagnosticsEngine &diag) {
19 unsigned delayedDiagID = 0U;
20
21 EXPECT_EQ(diag.DelayedDiagID, delayedDiagID);
22 EXPECT_FALSE(diag.DiagStates.empty());
23 EXPECT_TRUE(diag.DiagStatesByLoc.empty());
24 EXPECT_TRUE(diag.DiagStateOnPushStack.empty());
25 }
26
27 namespace {
28
29 // Check that DiagnosticErrorTrap works with SuppressAllDiagnostics.
30 TEST(DiagnosticTest, suppressAndTrap) {
31 DiagnosticsEngine Diags(new DiagnosticIDs(),
32 new DiagnosticOptions,
33 new IgnoringDiagConsumer());
34 Diags.setSuppressAllDiagnostics(true);
35
36 {
37 DiagnosticErrorTrap trap(Diags);
38
39 // Diag that would set UncompilableErrorOccurred and ErrorOccurred.
40 Diags.Report(diag::err_target_unknown_triple) << "unknown";
41
42 // Diag that would set UnrecoverableErrorOccurred and ErrorOccurred.
43 Diags.Report(diag::err_cannot_open_file) << "file" << "error";
44
45 // Diag that would set FatalErrorOccurred
46 // (via non-note following a fatal error).
47 Diags.Report(diag::warn_mt_message) << "warning";
48
49 EXPECT_TRUE(trap.hasErrorOccurred());
50 EXPECT_TRUE(trap.hasUnrecoverableErrorOccurred());
51 }
52
53 EXPECT_FALSE(Diags.hasErrorOccurred());
54 EXPECT_FALSE(Diags.hasFatalErrorOccurred());
55 EXPECT_FALSE(Diags.hasUncompilableErrorOccurred());
56 EXPECT_FALSE(Diags.hasUnrecoverableErrorOccurred());
57 }
58
59 // Check that FatalsAsError works as intended
60 TEST(DiagnosticTest, fatalsAsError) {
61 for (unsigned FatalsAsError = 0; FatalsAsError != 2; ++FatalsAsError) {
62 DiagnosticsEngine Diags(new DiagnosticIDs(),
63 new DiagnosticOptions,
64 new IgnoringDiagConsumer());
65 Diags.setFatalsAsError(FatalsAsError);
66
67 // Diag that would set UnrecoverableErrorOccurred and ErrorOccurred.
68 Diags.Report(diag::err_cannot_open_file) << "file" << "error";
69
70 // Diag that would set FatalErrorOccurred
71 // (via non-note following a fatal error).
72 Diags.Report(diag::warn_mt_message) << "warning";
73
74 EXPECT_TRUE(Diags.hasErrorOccurred());
75 EXPECT_EQ(Diags.hasFatalErrorOccurred(), FatalsAsError ? 0u : 1u);
76 EXPECT_TRUE(Diags.hasUncompilableErrorOccurred());
77 EXPECT_TRUE(Diags.hasUnrecoverableErrorOccurred());
78
79 // The warning should be emitted and counted only if we're not suppressing
80 // after fatal errors.
81 EXPECT_EQ(Diags.getNumWarnings(), FatalsAsError);
82 }
83 }
84
85 // Check that soft RESET works as intended
86 TEST(DiagnosticTest, softReset) {
87 DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions,
88 new IgnoringDiagConsumer());
89
90 unsigned numWarnings = 0U, numErrors = 0U;
91
92 Diags.Reset(true);
93 // Check For ErrorOccurred and TrapNumErrorsOccurred
94 EXPECT_FALSE(Diags.hasErrorOccurred());
95 EXPECT_FALSE(Diags.hasFatalErrorOccurred());
96 EXPECT_FALSE(Diags.hasUncompilableErrorOccurred());
97 // Check for UnrecoverableErrorOccurred and TrapNumUnrecoverableErrorsOccurred
98 EXPECT_FALSE(Diags.hasUnrecoverableErrorOccurred());
99
100 EXPECT_EQ(Diags.getNumWarnings(), numWarnings);
101 EXPECT_EQ(Diags.getNumErrors(), numErrors);
102
103 // Check for private variables of DiagnosticsEngine differentiating soft reset
104 DiagnosticsTestHelper(Diags);
105
106 EXPECT_FALSE(Diags.isDiagnosticInFlight());
107 EXPECT_TRUE(Diags.isLastDiagnosticIgnored());
108 }
109
110 TEST(DiagnosticTest, diagnosticError) {
111 DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions,
112 new IgnoringDiagConsumer());
113 PartialDiagnostic::DiagStorageAllocator Alloc;
114 llvm::Expected<std::pair<int, int>> Value = DiagnosticError::create(
115 SourceLocation(), PartialDiagnostic(diag::err_cannot_open_file, Alloc)
116 << "file"
117 << "error");
118 ASSERT_TRUE(!Value);
119 llvm::Error Err = Value.takeError();
120 std::optional<PartialDiagnosticAt> ErrDiag = DiagnosticError::take(Err);
121 llvm::cantFail(std::move(Err));
122 ASSERT_FALSE(!ErrDiag);
123 EXPECT_EQ(ErrDiag->first, SourceLocation());
124 EXPECT_EQ(ErrDiag->second.getDiagID(), diag::err_cannot_open_file);
125
126 Value = std::make_pair(20, 1);
127 ASSERT_FALSE(!Value);
128 EXPECT_EQ(*Value, std::make_pair(20, 1));
129 EXPECT_EQ(Value->first, 20);
130 }
131 }