OutputStreamTest.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/Frontend/TextDiagnosticPrinter.h"
  13. #include "clang/FrontendTool/Utils.h"
  14. #include "clang/Lex/PreprocessorOptions.h"
  15. #include "gtest/gtest.h"
  16. using namespace llvm;
  17. using namespace clang;
  18. using namespace clang::frontend;
  19. namespace {
  20. TEST(FrontendOutputTests, TestOutputStream) {
  21. auto Invocation = std::make_shared<CompilerInvocation>();
  22. Invocation->getPreprocessorOpts().addRemappedFile(
  23. "test.cc", MemoryBuffer::getMemBuffer("").release());
  24. Invocation->getFrontendOpts().Inputs.push_back(
  25. FrontendInputFile("test.cc", Language::CXX));
  26. Invocation->getFrontendOpts().ProgramAction = EmitBC;
  27. Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
  28. CompilerInstance Compiler;
  29. SmallVector<char, 256> IRBuffer;
  30. std::unique_ptr<raw_pwrite_stream> IRStream(
  31. new raw_svector_ostream(IRBuffer));
  32. Compiler.setOutputStream(std::move(IRStream));
  33. Compiler.setInvocation(std::move(Invocation));
  34. Compiler.createDiagnostics();
  35. bool Success = ExecuteCompilerInvocation(&Compiler);
  36. EXPECT_TRUE(Success);
  37. EXPECT_TRUE(!IRBuffer.empty());
  38. EXPECT_TRUE(StringRef(IRBuffer.data()).startswith("BC"));
  39. }
  40. TEST(FrontendOutputTests, TestVerboseOutputStreamShared) {
  41. auto Invocation = std::make_shared<CompilerInvocation>();
  42. Invocation->getPreprocessorOpts().addRemappedFile(
  43. "test.cc", MemoryBuffer::getMemBuffer("invalid").release());
  44. Invocation->getFrontendOpts().Inputs.push_back(
  45. FrontendInputFile("test.cc", Language::CXX));
  46. Invocation->getFrontendOpts().ProgramAction = EmitBC;
  47. Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
  48. CompilerInstance Compiler;
  49. std::string VerboseBuffer;
  50. raw_string_ostream VerboseStream(VerboseBuffer);
  51. Compiler.setOutputStream(std::make_unique<raw_null_ostream>());
  52. Compiler.setInvocation(std::move(Invocation));
  53. IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
  54. Compiler.createDiagnostics(
  55. new TextDiagnosticPrinter(llvm::nulls(), &*DiagOpts), true);
  56. Compiler.setVerboseOutputStream(VerboseStream);
  57. bool Success = ExecuteCompilerInvocation(&Compiler);
  58. EXPECT_FALSE(Success);
  59. EXPECT_TRUE(!VerboseStream.str().empty());
  60. EXPECT_TRUE(StringRef(VerboseBuffer.data()).contains("errors generated"));
  61. }
  62. TEST(FrontendOutputTests, TestVerboseOutputStreamOwned) {
  63. std::string VerboseBuffer;
  64. bool Success;
  65. {
  66. auto Invocation = std::make_shared<CompilerInvocation>();
  67. Invocation->getPreprocessorOpts().addRemappedFile(
  68. "test.cc", MemoryBuffer::getMemBuffer("invalid").release());
  69. Invocation->getFrontendOpts().Inputs.push_back(
  70. FrontendInputFile("test.cc", Language::CXX));
  71. Invocation->getFrontendOpts().ProgramAction = EmitBC;
  72. Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
  73. CompilerInstance Compiler;
  74. std::unique_ptr<raw_ostream> VerboseStream =
  75. std::make_unique<raw_string_ostream>(VerboseBuffer);
  76. Compiler.setOutputStream(std::make_unique<raw_null_ostream>());
  77. Compiler.setInvocation(std::move(Invocation));
  78. IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
  79. Compiler.createDiagnostics(
  80. new TextDiagnosticPrinter(llvm::nulls(), &*DiagOpts), true);
  81. Compiler.setVerboseOutputStream(std::move(VerboseStream));
  82. Success = ExecuteCompilerInvocation(&Compiler);
  83. }
  84. EXPECT_FALSE(Success);
  85. EXPECT_TRUE(!VerboseBuffer.empty());
  86. EXPECT_TRUE(StringRef(VerboseBuffer.data()).contains("errors generated"));
  87. }
  88. }