xref: /llvm-project/clang/unittests/Frontend/OutputStreamTest.cpp (revision 09d890d728e6c51854b0452fe0e467381b3c51d4)
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/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", Language::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