Serge Pavlov | 7a54553 | 2016-02-18 16:43:24 +0000 | [diff] [blame] | 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 | |
Serge Pavlov | 7a54553 | 2016-02-18 16:43:24 +0000 | [diff] [blame] | 14 | #include "clang/CodeGen/BackendUtil.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 15 | #include "clang/CodeGen/CodeGenAction.h" |
| 16 | #include "clang/Frontend/CompilerInstance.h" |
| 17 | #include "clang/Lex/PreprocessorOptions.h" |
Serge Pavlov | 7a54553 | 2016-02-18 16:43:24 +0000 | [diff] [blame] | 18 | #include "gtest/gtest.h" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | using namespace clang; |
| 22 | using namespace clang::frontend; |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | |
| 27 | class NullCodeGenAction : public CodeGenAction { |
| 28 | public: |
| 29 | NullCodeGenAction(llvm::LLVMContext *_VMContext = nullptr) |
David L. Jones | cd58bd6 | 2016-02-18 20:27:16 +0000 | [diff] [blame] | 30 | : CodeGenAction(Backend_EmitMCNull, _VMContext) {} |
Serge Pavlov | 7a54553 | 2016-02-18 16:43:24 +0000 | [diff] [blame] | 31 | |
| 32 | // The action does not call methods of ATContext. |
| 33 | void ExecuteAction() override { |
| 34 | CompilerInstance &CI = getCompilerInstance(); |
| 35 | if (!CI.hasPreprocessor()) |
| 36 | return; |
| 37 | if (!CI.hasSema()) |
| 38 | CI.createSema(getTranslationUnitKind(), nullptr); |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | |
| 43 | TEST(CodeGenTest, TestNullCodeGen) { |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 44 | auto Invocation = std::make_shared<CompilerInvocation>(); |
Serge Pavlov | 7a54553 | 2016-02-18 16:43:24 +0000 | [diff] [blame] | 45 | Invocation->getPreprocessorOpts().addRemappedFile( |
| 46 | "test.cc", |
| 47 | MemoryBuffer::getMemBuffer("").release()); |
| 48 | Invocation->getFrontendOpts().Inputs.push_back( |
Richard Smith | 40c0efa | 2017-04-26 18:57:40 +0000 | [diff] [blame] | 49 | FrontendInputFile("test.cc", InputKind::CXX)); |
Serge Pavlov | 7a54553 | 2016-02-18 16:43:24 +0000 | [diff] [blame] | 50 | Invocation->getFrontendOpts().ProgramAction = EmitLLVM; |
| 51 | Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu"; |
| 52 | CompilerInstance Compiler; |
David Blaikie | ea4395e | 2017-01-06 19:49:01 +0000 | [diff] [blame] | 53 | Compiler.setInvocation(std::move(Invocation)); |
Serge Pavlov | 7a54553 | 2016-02-18 16:43:24 +0000 | [diff] [blame] | 54 | Compiler.createDiagnostics(); |
| 55 | EXPECT_TRUE(Compiler.hasDiagnostics()); |
| 56 | |
| 57 | std::unique_ptr<FrontendAction> Act(new NullCodeGenAction); |
| 58 | bool Success = Compiler.ExecuteAction(*Act); |
| 59 | EXPECT_TRUE(Success); |
| 60 | } |
| 61 | |
| 62 | } |