blob: 6377e57b3557bbbddb4b86dc3d5075e06b50cb16 [file] [log] [blame]
Richard Smitha2686712014-12-05 21:52:58 +00001//===- unittests/Basic/DiagnosticTest.cpp -- Diagnostic engine tests ------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Richard Smitha2686712014-12-05 21:52:58 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "clang/Basic/Diagnostic.h"
Alex Lorenzd0e27262017-08-25 15:48:00 +000010#include "clang/Basic/DiagnosticError.h"
Richard Smitha2686712014-12-05 21:52:58 +000011#include "clang/Basic/DiagnosticIDs.h"
12#include "gtest/gtest.h"
13
14using namespace llvm;
15using namespace clang;
16
17namespace {
18
19// Check that DiagnosticErrorTrap works with SuppressAllDiagnostics.
20TEST(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
Richard Smithe37391c2017-05-03 00:28:49 +000049// Check that SuppressAfterFatalError works as intended
50TEST(DiagnosticTest, suppressAfterFatalError) {
51 for (unsigned Suppress = 0; Suppress != 2; ++Suppress) {
52 DiagnosticsEngine Diags(new DiagnosticIDs(),
53 new DiagnosticOptions,
54 new IgnoringDiagConsumer());
55 Diags.setSuppressAfterFatalError(Suppress);
Manuel Klimek016c0242016-03-01 10:56:19 +000056
Richard Smithe37391c2017-05-03 00:28:49 +000057 // Diag that would set UnrecoverableErrorOccurred and ErrorOccurred.
58 Diags.Report(diag::err_cannot_open_file) << "file" << "error";
Manuel Klimek016c0242016-03-01 10:56:19 +000059
Richard Smithe37391c2017-05-03 00:28:49 +000060 // Diag that would set FatalErrorOccurred
61 // (via non-note following a fatal error).
62 Diags.Report(diag::warn_mt_message) << "warning";
Manuel Klimek016c0242016-03-01 10:56:19 +000063
Richard Smithe37391c2017-05-03 00:28:49 +000064 EXPECT_TRUE(Diags.hasErrorOccurred());
65 EXPECT_TRUE(Diags.hasFatalErrorOccurred());
66 EXPECT_TRUE(Diags.hasUncompilableErrorOccurred());
67 EXPECT_TRUE(Diags.hasUnrecoverableErrorOccurred());
Manuel Klimek016c0242016-03-01 10:56:19 +000068
Richard Smithe37391c2017-05-03 00:28:49 +000069 // The warning should be emitted and counted only if we're not suppressing
70 // after fatal errors.
71 EXPECT_EQ(Diags.getNumWarnings(), Suppress ? 0u : 1u);
72 }
Manuel Klimek016c0242016-03-01 10:56:19 +000073}
74
Alex Lorenzd0e27262017-08-25 15:48:00 +000075TEST(DiagnosticTest, diagnosticError) {
76 DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions,
77 new IgnoringDiagConsumer());
78 PartialDiagnostic::StorageAllocator Alloc;
79 llvm::Expected<std::pair<int, int>> Value = DiagnosticError::create(
80 SourceLocation(), PartialDiagnostic(diag::err_cannot_open_file, Alloc)
81 << "file"
82 << "error");
83 ASSERT_TRUE(!Value);
84 llvm::Error Err = Value.takeError();
85 Optional<PartialDiagnosticAt> ErrDiag = DiagnosticError::take(Err);
86 llvm::cantFail(std::move(Err));
87 ASSERT_FALSE(!ErrDiag);
88 EXPECT_EQ(ErrDiag->first, SourceLocation());
89 EXPECT_EQ(ErrDiag->second.getDiagID(), diag::err_cannot_open_file);
90
91 Value = std::make_pair(20, 1);
92 ASSERT_FALSE(!Value);
93 EXPECT_EQ(*Value, std::make_pair(20, 1));
94 EXPECT_EQ(Value->first, 20);
95}
Alexander Kornienkoab9db512015-06-22 23:07:51 +000096}