blob: 3068e1c34050d4653ef79e7d0c910b78dc3cba43 [file] [log] [blame]
Richard Smitha2686712014-12-05 21:52:58 +00001//===- 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"
Alex Lorenzd0e27262017-08-25 15:48:00 +000011#include "clang/Basic/DiagnosticError.h"
Richard Smitha2686712014-12-05 21:52:58 +000012#include "clang/Basic/DiagnosticIDs.h"
13#include "gtest/gtest.h"
14
15using namespace llvm;
16using namespace clang;
17
18namespace {
19
20// Check that DiagnosticErrorTrap works with SuppressAllDiagnostics.
21TEST(DiagnosticTest, suppressAndTrap) {
22 DiagnosticsEngine Diags(new DiagnosticIDs(),
23 new DiagnosticOptions,
24 new IgnoringDiagConsumer());
25 Diags.setSuppressAllDiagnostics(true);
26
27 {
28 DiagnosticErrorTrap trap(Diags);
29
30 // Diag that would set UncompilableErrorOccurred and ErrorOccurred.
31 Diags.Report(diag::err_target_unknown_triple) << "unknown";
32
33 // Diag that would set UnrecoverableErrorOccurred and ErrorOccurred.
34 Diags.Report(diag::err_cannot_open_file) << "file" << "error";
35
36 // Diag that would set FatalErrorOccurred
37 // (via non-note following a fatal error).
38 Diags.Report(diag::warn_mt_message) << "warning";
39
40 EXPECT_TRUE(trap.hasErrorOccurred());
41 EXPECT_TRUE(trap.hasUnrecoverableErrorOccurred());
42 }
43
44 EXPECT_FALSE(Diags.hasErrorOccurred());
45 EXPECT_FALSE(Diags.hasFatalErrorOccurred());
46 EXPECT_FALSE(Diags.hasUncompilableErrorOccurred());
47 EXPECT_FALSE(Diags.hasUnrecoverableErrorOccurred());
48}
49
Richard Smithe37391c2017-05-03 00:28:49 +000050// Check that SuppressAfterFatalError works as intended
51TEST(DiagnosticTest, suppressAfterFatalError) {
52 for (unsigned Suppress = 0; Suppress != 2; ++Suppress) {
53 DiagnosticsEngine Diags(new DiagnosticIDs(),
54 new DiagnosticOptions,
55 new IgnoringDiagConsumer());
56 Diags.setSuppressAfterFatalError(Suppress);
Manuel Klimek016c0242016-03-01 10:56:19 +000057
Richard Smithe37391c2017-05-03 00:28:49 +000058 // Diag that would set UnrecoverableErrorOccurred and ErrorOccurred.
59 Diags.Report(diag::err_cannot_open_file) << "file" << "error";
Manuel Klimek016c0242016-03-01 10:56:19 +000060
Richard Smithe37391c2017-05-03 00:28:49 +000061 // Diag that would set FatalErrorOccurred
62 // (via non-note following a fatal error).
63 Diags.Report(diag::warn_mt_message) << "warning";
Manuel Klimek016c0242016-03-01 10:56:19 +000064
Richard Smithe37391c2017-05-03 00:28:49 +000065 EXPECT_TRUE(Diags.hasErrorOccurred());
66 EXPECT_TRUE(Diags.hasFatalErrorOccurred());
67 EXPECT_TRUE(Diags.hasUncompilableErrorOccurred());
68 EXPECT_TRUE(Diags.hasUnrecoverableErrorOccurred());
Manuel Klimek016c0242016-03-01 10:56:19 +000069
Richard Smithe37391c2017-05-03 00:28:49 +000070 // The warning should be emitted and counted only if we're not suppressing
71 // after fatal errors.
72 EXPECT_EQ(Diags.getNumWarnings(), Suppress ? 0u : 1u);
73 }
Manuel Klimek016c0242016-03-01 10:56:19 +000074}
75
Alex Lorenzd0e27262017-08-25 15:48:00 +000076TEST(DiagnosticTest, diagnosticError) {
77 DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions,
78 new IgnoringDiagConsumer());
79 PartialDiagnostic::StorageAllocator Alloc;
80 llvm::Expected<std::pair<int, int>> Value = DiagnosticError::create(
81 SourceLocation(), PartialDiagnostic(diag::err_cannot_open_file, Alloc)
82 << "file"
83 << "error");
84 ASSERT_TRUE(!Value);
85 llvm::Error Err = Value.takeError();
86 Optional<PartialDiagnosticAt> ErrDiag = DiagnosticError::take(Err);
87 llvm::cantFail(std::move(Err));
88 ASSERT_FALSE(!ErrDiag);
89 EXPECT_EQ(ErrDiag->first, SourceLocation());
90 EXPECT_EQ(ErrDiag->second.getDiagID(), diag::err_cannot_open_file);
91
92 Value = std::make_pair(20, 1);
93 ASSERT_FALSE(!Value);
94 EXPECT_EQ(*Value, std::make_pair(20, 1));
95 EXPECT_EQ(Value->first, 20);
96}
Alexander Kornienkoab9db512015-06-22 23:07:51 +000097}