OutputStreamTest.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 = ParseSyntaxOnly;
  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 = ParseSyntaxOnly;
  47. Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
  48. CompilerInstance Compiler;
  49. std::string VerboseBuffer;
  50. raw_string_ostream VerboseStream(VerboseBuffer);
  51. Compiler.setInvocation(std::move(Invocation));
  52. IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
  53. Compiler.createDiagnostics(
  54. new TextDiagnosticPrinter(llvm::nulls(), &*DiagOpts), true);
  55. Compiler.setVerboseOutputStream(VerboseStream);
  56. bool Success = ExecuteCompilerInvocation(&Compiler);
  57. EXPECT_FALSE(Success);
  58. EXPECT_TRUE(!VerboseStream.str().empty());
  59. EXPECT_TRUE(StringRef(VerboseBuffer.data()).contains("errors generated"));
  60. }
  61. TEST(FrontendOutputTests, TestVerboseOutputStreamOwned) {
  62. std::string VerboseBuffer;
  63. bool Success;
  64. {
  65. auto Invocation = std::make_shared<CompilerInvocation>();
  66. Invocation->getPreprocessorOpts().addRemappedFile(
  67. "test.cc", MemoryBuffer::getMemBuffer("invalid").release());
  68. Invocation->getFrontendOpts().Inputs.push_back(
  69. FrontendInputFile("test.cc", Language::CXX));
  70. Invocation->getFrontendOpts().ProgramAction = EmitBC;
  71. Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
  72. CompilerInstance Compiler;
  73. std::unique_ptr<raw_ostream> VerboseStream =
  74. std::make_unique<raw_string_ostream>(VerboseBuffer);
  75. Compiler.setInvocation(std::move(Invocation));
  76. IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
  77. Compiler.createDiagnostics(
  78. new TextDiagnosticPrinter(llvm::nulls(), &*DiagOpts), true);
  79. Compiler.setVerboseOutputStream(std::move(VerboseStream));
  80. Success = ExecuteCompilerInvocation(&Compiler);
  81. }
  82. EXPECT_FALSE(Success);
  83. EXPECT_TRUE(!VerboseBuffer.empty());
  84. EXPECT_TRUE(StringRef(VerboseBuffer.data()).contains("errors generated"));
  85. }
  86. }