CodeGenActionTest.cpp 1.9 KB

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