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/CodeGen/BackendUtil.h"
10 #include "clang/CodeGen/CodeGenAction.h"
11 #include "clang/Frontend/CompilerInstance.h"
12 #include "clang/FrontendTool/Utils.h"
13 #include "clang/Lex/PreprocessorOptions.h"
14 #include "gtest/gtest.h"
15 
16 using namespace llvm;
17 using namespace clang;
18 using namespace clang::frontend;
19 
20 namespace {
21 
22 TEST(FrontendOutputTests, TestOutputStream) {
23   auto Invocation = std::make_shared<CompilerInvocation>();
24   Invocation->getPreprocessorOpts().addRemappedFile(
25       "test.cc", MemoryBuffer::getMemBuffer("").release());
26   Invocation->getFrontendOpts().Inputs.push_back(
27       FrontendInputFile("test.cc", InputKind::CXX));
28   Invocation->getFrontendOpts().ProgramAction = EmitBC;
29   Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
30   CompilerInstance Compiler;
31 
32   SmallVector<char, 256> IRBuffer;
33   std::unique_ptr<raw_pwrite_stream> IRStream(
34       new raw_svector_ostream(IRBuffer));
35 
36   Compiler.setOutputStream(std::move(IRStream));
37   Compiler.setInvocation(std::move(Invocation));
38   Compiler.createDiagnostics();
39 
40   bool Success = ExecuteCompilerInvocation(&Compiler);
41   EXPECT_TRUE(Success);
42   EXPECT_TRUE(!IRBuffer.empty());
43   EXPECT_TRUE(StringRef(IRBuffer.data()).startswith("BC"));
44 }
45 }
46