1 | //===- unittests/Frontend/OutputStreamTest.cpp --- FrontendAction 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/LangStandard.h" |
10 | #include "clang/CodeGen/BackendUtil.h" |
11 | #include "clang/Frontend/CompilerInstance.h" |
12 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
13 | #include "clang/FrontendTool/Utils.h" |
14 | #include "clang/Lex/PreprocessorOptions.h" |
15 | #include "llvm/Support/TargetSelect.h" |
16 | #include "llvm/Support/VirtualFileSystem.h" |
17 | #include "gtest/gtest.h" |
18 | |
19 | using namespace llvm; |
20 | using namespace clang; |
21 | using namespace clang::frontend; |
22 | |
23 | namespace { |
24 | |
25 | TEST(FrontendOutputTests, TestOutputStream) { |
26 | llvm::InitializeAllTargetMCs(); |
27 | auto Invocation = std::make_shared<CompilerInvocation>(); |
28 | Invocation->getPreprocessorOpts().addRemappedFile( |
29 | From: "test.cc" , To: MemoryBuffer::getMemBuffer(InputData: "" ).release()); |
30 | Invocation->getFrontendOpts().Inputs.push_back( |
31 | Elt: FrontendInputFile("test.cc" , Language::CXX)); |
32 | Invocation->getFrontendOpts().ProgramAction = EmitBC; |
33 | Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu" ; |
34 | CompilerInstance Compiler(std::move(Invocation)); |
35 | |
36 | SmallVector<char, 256> IRBuffer; |
37 | std::unique_ptr<raw_pwrite_stream> IRStream( |
38 | new raw_svector_ostream(IRBuffer)); |
39 | |
40 | Compiler.setOutputStream(std::move(IRStream)); |
41 | Compiler.createDiagnostics(VFS&: *llvm::vfs::getRealFileSystem()); |
42 | |
43 | bool Success = ExecuteCompilerInvocation(Clang: &Compiler); |
44 | EXPECT_TRUE(Success); |
45 | EXPECT_TRUE(!IRBuffer.empty()); |
46 | EXPECT_TRUE(StringRef(IRBuffer.data()).starts_with("BC" )); |
47 | } |
48 | |
49 | TEST(FrontendOutputTests, TestVerboseOutputStreamShared) { |
50 | llvm::InitializeAllTargetMCs(); |
51 | auto Invocation = std::make_shared<CompilerInvocation>(); |
52 | Invocation->getPreprocessorOpts().addRemappedFile( |
53 | From: "test.cc" , To: MemoryBuffer::getMemBuffer(InputData: "invalid" ).release()); |
54 | Invocation->getFrontendOpts().Inputs.push_back( |
55 | Elt: FrontendInputFile("test.cc" , Language::CXX)); |
56 | Invocation->getFrontendOpts().ProgramAction = EmitBC; |
57 | Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu" ; |
58 | CompilerInstance Compiler(std::move(Invocation)); |
59 | |
60 | std::string VerboseBuffer; |
61 | raw_string_ostream VerboseStream(VerboseBuffer); |
62 | |
63 | Compiler.setOutputStream(std::make_unique<raw_null_ostream>()); |
64 | DiagnosticOptions DiagOpts; |
65 | Compiler.createDiagnostics(VFS&: *llvm::vfs::getRealFileSystem(), |
66 | Client: new TextDiagnosticPrinter(llvm::nulls(), DiagOpts), |
67 | ShouldOwnClient: true); |
68 | Compiler.setVerboseOutputStream(VerboseStream); |
69 | |
70 | bool Success = ExecuteCompilerInvocation(Clang: &Compiler); |
71 | EXPECT_FALSE(Success); |
72 | EXPECT_TRUE(!VerboseBuffer.empty()); |
73 | EXPECT_TRUE(StringRef(VerboseBuffer.data()).contains("errors generated" )); |
74 | } |
75 | |
76 | TEST(FrontendOutputTests, TestVerboseOutputStreamOwned) { |
77 | std::string VerboseBuffer; |
78 | bool Success; |
79 | { |
80 | llvm::InitializeAllTargetMCs(); |
81 | auto Invocation = std::make_shared<CompilerInvocation>(); |
82 | Invocation->getPreprocessorOpts().addRemappedFile( |
83 | From: "test.cc" , To: MemoryBuffer::getMemBuffer(InputData: "invalid" ).release()); |
84 | Invocation->getFrontendOpts().Inputs.push_back( |
85 | Elt: FrontendInputFile("test.cc" , Language::CXX)); |
86 | Invocation->getFrontendOpts().ProgramAction = EmitBC; |
87 | Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu" ; |
88 | CompilerInstance Compiler(std::move(Invocation)); |
89 | |
90 | std::unique_ptr<raw_ostream> VerboseStream = |
91 | std::make_unique<raw_string_ostream>(args&: VerboseBuffer); |
92 | |
93 | Compiler.setOutputStream(std::make_unique<raw_null_ostream>()); |
94 | DiagnosticOptions DiagOpts; |
95 | Compiler.createDiagnostics( |
96 | VFS&: *llvm::vfs::getRealFileSystem(), |
97 | Client: new TextDiagnosticPrinter(llvm::nulls(), DiagOpts), ShouldOwnClient: true); |
98 | Compiler.setVerboseOutputStream(std::move(VerboseStream)); |
99 | |
100 | Success = ExecuteCompilerInvocation(Clang: &Compiler); |
101 | } |
102 | EXPECT_FALSE(Success); |
103 | EXPECT_TRUE(!VerboseBuffer.empty()); |
104 | EXPECT_TRUE(StringRef(VerboseBuffer.data()).contains("errors generated" )); |
105 | } |
106 | |
107 | TEST(FrontendOutputTests, TestVerboseOutputStreamOwnedNotLeaked) { |
108 | CompilerInstance Compiler; |
109 | Compiler.setVerboseOutputStream(std::make_unique<raw_null_ostream>()); |
110 | |
111 | // Trust leak sanitizer bots to catch a leak here. |
112 | Compiler.setVerboseOutputStream(llvm::nulls()); |
113 | } |
114 | |
115 | } // anonymous namespace |
116 | |