blob: d90c2bce2f6e55cf662b366e3961322dc758058c [file] [log] [blame]
Serge Pavlov7a545532016-02-18 16:43:24 +00001//===- 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 Pavlov7a545532016-02-18 16:43:24 +000014#include "clang/CodeGen/BackendUtil.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000015#include "clang/CodeGen/CodeGenAction.h"
16#include "clang/Frontend/CompilerInstance.h"
17#include "clang/Lex/PreprocessorOptions.h"
Serge Pavlov7a545532016-02-18 16:43:24 +000018#include "gtest/gtest.h"
19
20using namespace llvm;
21using namespace clang;
22using namespace clang::frontend;
23
24namespace {
25
26
27class NullCodeGenAction : public CodeGenAction {
28public:
29 NullCodeGenAction(llvm::LLVMContext *_VMContext = nullptr)
David L. Jonescd58bd62016-02-18 20:27:16 +000030 : CodeGenAction(Backend_EmitMCNull, _VMContext) {}
Serge Pavlov7a545532016-02-18 16:43:24 +000031
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
43TEST(CodeGenTest, TestNullCodeGen) {
David Blaikieea4395e2017-01-06 19:49:01 +000044 auto Invocation = std::make_shared<CompilerInvocation>();
Serge Pavlov7a545532016-02-18 16:43:24 +000045 Invocation->getPreprocessorOpts().addRemappedFile(
46 "test.cc",
47 MemoryBuffer::getMemBuffer("").release());
48 Invocation->getFrontendOpts().Inputs.push_back(
Richard Smith40c0efa2017-04-26 18:57:40 +000049 FrontendInputFile("test.cc", InputKind::CXX));
Serge Pavlov7a545532016-02-18 16:43:24 +000050 Invocation->getFrontendOpts().ProgramAction = EmitLLVM;
51 Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
52 CompilerInstance Compiler;
David Blaikieea4395e2017-01-06 19:49:01 +000053 Compiler.setInvocation(std::move(Invocation));
Serge Pavlov7a545532016-02-18 16:43:24 +000054 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}