[SCCP] Avoid modifying AdditionalUsers while iterating over it
[llvm-project.git] / clang / unittests / Basic / DiagnosticTest.cpp
blob8d018a8b9cef439cb771a78644c2e3e98f03aded
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 //===----------------------------------------------------------------------===//
9 #include "clang/Basic/Diagnostic.h"
10 #include "clang/Basic/DiagnosticError.h"
11 #include "clang/Basic/DiagnosticIDs.h"
12 #include "gtest/gtest.h"
14 using namespace llvm;
15 using namespace clang;
17 namespace {
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);
27 DiagnosticErrorTrap trap(Diags);
29 // Diag that would set UncompilableErrorOccurred and ErrorOccurred.
30 Diags.Report(diag::err_target_unknown_triple) << "unknown";
32 // Diag that would set UnrecoverableErrorOccurred and ErrorOccurred.
33 Diags.Report(diag::err_cannot_open_file) << "file" << "error";
35 // Diag that would set FatalErrorOccurred
36 // (via non-note following a fatal error).
37 Diags.Report(diag::warn_mt_message) << "warning";
39 EXPECT_TRUE(trap.hasErrorOccurred());
40 EXPECT_TRUE(trap.hasUnrecoverableErrorOccurred());
43 EXPECT_FALSE(Diags.hasErrorOccurred());
44 EXPECT_FALSE(Diags.hasFatalErrorOccurred());
45 EXPECT_FALSE(Diags.hasUncompilableErrorOccurred());
46 EXPECT_FALSE(Diags.hasUnrecoverableErrorOccurred());
49 // Check that FatalsAsError works as intended
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);
57 // Diag that would set UnrecoverableErrorOccurred and ErrorOccurred.
58 Diags.Report(diag::err_cannot_open_file) << "file" << "error";
60 // Diag that would set FatalErrorOccurred
61 // (via non-note following a fatal error).
62 Diags.Report(diag::warn_mt_message) << "warning";
64 EXPECT_TRUE(Diags.hasErrorOccurred());
65 EXPECT_EQ(Diags.hasFatalErrorOccurred(), FatalsAsError ? 0u : 1u);
66 EXPECT_TRUE(Diags.hasUncompilableErrorOccurred());
67 EXPECT_TRUE(Diags.hasUnrecoverableErrorOccurred());
69 // The warning should be emitted and counted only if we're not suppressing
70 // after fatal errors.
71 EXPECT_EQ(Diags.getNumWarnings(), FatalsAsError);
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);
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);