OutputStreamTest.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. #include "clang/Basic/LangStandard.h"
  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. using namespace llvm;
  16. using namespace clang;
  17. using namespace clang::frontend;
  18. namespace {
  19. TEST(FrontendOutputTests, TestOutputStream) {
  20. auto Invocation = std::make_shared<CompilerInvocation>();
  21. Invocation->getPreprocessorOpts().addRemappedFile(
  22. "test.cc", MemoryBuffer::getMemBuffer("").release());
  23. Invocation->getFrontendOpts().Inputs.push_back(
  24. FrontendInputFile("test.cc", Language::CXX));
  25. Invocation->getFrontendOpts().ProgramAction = EmitBC;
  26. Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
  27. CompilerInstance Compiler;
  28. SmallVector<char, 256> IRBuffer;
  29. std::unique_ptr<raw_pwrite_stream> IRStream(
  30. new raw_svector_ostream(IRBuffer));
  31. Compiler.setOutputStream(std::move(IRStream));
  32. Compiler.setInvocation(std::move(Invocation));
  33. Compiler.createDiagnostics();
  34. bool Success = ExecuteCompilerInvocation(&Compiler);
  35. EXPECT_TRUE(Success);
  36. EXPECT_TRUE(!IRBuffer.empty());
  37. EXPECT_TRUE(StringRef(IRBuffer.data()).startswith("BC"));
  38. }
  39. }