CodeGenActionTest.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //===- unittests/Frontend/CodeGenActionTest.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. // Unit tests for CodeGenAction.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Frontend/CompilerInstance.h"
  14. #include "clang/CodeGen/CodeGenAction.h"
  15. #include "clang/CodeGen/BackendUtil.h"
  16. #include "gtest/gtest.h"
  17. using namespace llvm;
  18. using namespace clang;
  19. using namespace clang::frontend;
  20. namespace {
  21. class NullCodeGenAction : public CodeGenAction {
  22. public:
  23. NullCodeGenAction(llvm::LLVMContext *_VMContext = nullptr)
  24. : CodeGenAction(Backend_EmitMCNull, _VMContext) {}
  25. // The action does not call methods of ATContext.
  26. void ExecuteAction() override {
  27. CompilerInstance &CI = getCompilerInstance();
  28. if (!CI.hasPreprocessor())
  29. return;
  30. if (!CI.hasSema())
  31. CI.createSema(getTranslationUnitKind(), nullptr);
  32. }
  33. };
  34. TEST(CodeGenTest, TestNullCodeGen) {
  35. CompilerInvocation *Invocation = new CompilerInvocation;
  36. Invocation->getPreprocessorOpts().addRemappedFile(
  37. "test.cc",
  38. MemoryBuffer::getMemBuffer("").release());
  39. Invocation->getFrontendOpts().Inputs.push_back(
  40. FrontendInputFile("test.cc", IK_CXX));
  41. Invocation->getFrontendOpts().ProgramAction = EmitLLVM;
  42. Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
  43. CompilerInstance Compiler;
  44. Compiler.setInvocation(Invocation);
  45. Compiler.createDiagnostics();
  46. EXPECT_TRUE(Compiler.hasDiagnostics());
  47. std::unique_ptr<FrontendAction> Act(new NullCodeGenAction);
  48. bool Success = Compiler.ExecuteAction(*Act);
  49. EXPECT_TRUE(Success);
  50. }
  51. }