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