17a545536SSerge Pavlov //===- unittests/Frontend/CodeGenActionTest.cpp --- FrontendAction tests --===//
27a545536SSerge Pavlov //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67a545536SSerge Pavlov //
77a545536SSerge Pavlov //===----------------------------------------------------------------------===//
87a545536SSerge Pavlov //
97a545536SSerge Pavlov // Unit tests for CodeGenAction.
107a545536SSerge Pavlov //
117a545536SSerge Pavlov //===----------------------------------------------------------------------===//
127a545536SSerge Pavlov 
139670f847SMehdi Amini #include "clang/CodeGen/CodeGenAction.h"
1409d890d7SRainer Orth #include "clang/Basic/LangStandard.h"
1509d890d7SRainer Orth #include "clang/CodeGen/BackendUtil.h"
169670f847SMehdi Amini #include "clang/Frontend/CompilerInstance.h"
179670f847SMehdi Amini #include "clang/Lex/PreprocessorOptions.h"
18*8dfaecc4SArgyrios Kyrtzidis #include "llvm/Support/FormatVariadic.h"
197a545536SSerge Pavlov #include "gtest/gtest.h"
207a545536SSerge Pavlov 
217a545536SSerge Pavlov using namespace llvm;
227a545536SSerge Pavlov using namespace clang;
237a545536SSerge Pavlov using namespace clang::frontend;
247a545536SSerge Pavlov 
257a545536SSerge Pavlov namespace {
267a545536SSerge Pavlov 
277a545536SSerge Pavlov 
287a545536SSerge Pavlov class NullCodeGenAction : public CodeGenAction {
297a545536SSerge Pavlov public:
NullCodeGenAction(llvm::LLVMContext * _VMContext=nullptr)307a545536SSerge Pavlov   NullCodeGenAction(llvm::LLVMContext *_VMContext = nullptr)
31cd58bd62SDavid L. Jones     : CodeGenAction(Backend_EmitMCNull, _VMContext) {}
327a545536SSerge Pavlov 
337a545536SSerge Pavlov   // The action does not call methods of ATContext.
ExecuteAction()347a545536SSerge Pavlov   void ExecuteAction() override {
357a545536SSerge Pavlov     CompilerInstance &CI = getCompilerInstance();
367a545536SSerge Pavlov     if (!CI.hasPreprocessor())
377a545536SSerge Pavlov       return;
387a545536SSerge Pavlov     if (!CI.hasSema())
397a545536SSerge Pavlov       CI.createSema(getTranslationUnitKind(), nullptr);
407a545536SSerge Pavlov   }
417a545536SSerge Pavlov };
427a545536SSerge Pavlov 
437a545536SSerge Pavlov 
TEST(CodeGenTest,TestNullCodeGen)447a545536SSerge Pavlov TEST(CodeGenTest, TestNullCodeGen) {
45ea4395ebSDavid Blaikie   auto Invocation = std::make_shared<CompilerInvocation>();
467a545536SSerge Pavlov   Invocation->getPreprocessorOpts().addRemappedFile(
477a545536SSerge Pavlov       "test.cc",
487a545536SSerge Pavlov       MemoryBuffer::getMemBuffer("").release());
497a545536SSerge Pavlov   Invocation->getFrontendOpts().Inputs.push_back(
5009d890d7SRainer Orth       FrontendInputFile("test.cc", Language::CXX));
517a545536SSerge Pavlov   Invocation->getFrontendOpts().ProgramAction = EmitLLVM;
527a545536SSerge Pavlov   Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
537a545536SSerge Pavlov   CompilerInstance Compiler;
54ea4395ebSDavid Blaikie   Compiler.setInvocation(std::move(Invocation));
557a545536SSerge Pavlov   Compiler.createDiagnostics();
567a545536SSerge Pavlov   EXPECT_TRUE(Compiler.hasDiagnostics());
577a545536SSerge Pavlov 
587a545536SSerge Pavlov   std::unique_ptr<FrontendAction> Act(new NullCodeGenAction);
597a545536SSerge Pavlov   bool Success = Compiler.ExecuteAction(*Act);
607a545536SSerge Pavlov   EXPECT_TRUE(Success);
617a545536SSerge Pavlov }
627a545536SSerge Pavlov 
TEST(CodeGenTest,CodeGenFromIRMemBuffer)63b3dae59bSRyan Senanayake TEST(CodeGenTest, CodeGenFromIRMemBuffer) {
64b3dae59bSRyan Senanayake   auto Invocation = std::make_shared<CompilerInvocation>();
65b3dae59bSRyan Senanayake   std::unique_ptr<MemoryBuffer> MemBuffer =
66b3dae59bSRyan Senanayake       MemoryBuffer::getMemBuffer("", "test.ll");
67b3dae59bSRyan Senanayake   Invocation->getFrontendOpts().Inputs.push_back(
68b3dae59bSRyan Senanayake       FrontendInputFile(*MemBuffer, Language::LLVM_IR));
69b3dae59bSRyan Senanayake   Invocation->getFrontendOpts().ProgramAction = frontend::EmitLLVMOnly;
70b3dae59bSRyan Senanayake   Invocation->getTargetOpts().Triple = "i386-unknown-linux-gnu";
71b3dae59bSRyan Senanayake   CompilerInstance Compiler;
72b3dae59bSRyan Senanayake   Compiler.setInvocation(std::move(Invocation));
73b3dae59bSRyan Senanayake   Compiler.createDiagnostics();
74b3dae59bSRyan Senanayake   EXPECT_TRUE(Compiler.hasDiagnostics());
75b3dae59bSRyan Senanayake 
76b3dae59bSRyan Senanayake   EmitLLVMOnlyAction Action;
77b3dae59bSRyan Senanayake   bool Success = Compiler.ExecuteAction(Action);
78b3dae59bSRyan Senanayake   EXPECT_TRUE(Success);
79b3dae59bSRyan Senanayake }
80*8dfaecc4SArgyrios Kyrtzidis 
TEST(CodeGenTest,DebugInfoCWDCodeGen)81*8dfaecc4SArgyrios Kyrtzidis TEST(CodeGenTest, DebugInfoCWDCodeGen) {
82*8dfaecc4SArgyrios Kyrtzidis   // Check that debug info is accessing the current working directory from the
83*8dfaecc4SArgyrios Kyrtzidis   // VFS instead of calling \p llvm::sys::fs::current_path() directly.
84*8dfaecc4SArgyrios Kyrtzidis 
85*8dfaecc4SArgyrios Kyrtzidis   auto VFS = std::make_unique<llvm::vfs::InMemoryFileSystem>();
86*8dfaecc4SArgyrios Kyrtzidis   VFS->setCurrentWorkingDirectory("/in-memory-fs-cwd");
87*8dfaecc4SArgyrios Kyrtzidis   auto Sept = llvm::sys::path::get_separator();
88*8dfaecc4SArgyrios Kyrtzidis   std::string TestPath =
89*8dfaecc4SArgyrios Kyrtzidis       std::string(llvm::formatv("{0}in-memory-fs-cwd{0}test.cpp", Sept));
90*8dfaecc4SArgyrios Kyrtzidis   VFS->addFile(TestPath, 0, llvm::MemoryBuffer::getMemBuffer("int x;\n"));
91*8dfaecc4SArgyrios Kyrtzidis 
92*8dfaecc4SArgyrios Kyrtzidis   auto Invocation = std::make_shared<CompilerInvocation>();
93*8dfaecc4SArgyrios Kyrtzidis   Invocation->getFrontendOpts().Inputs.push_back(
94*8dfaecc4SArgyrios Kyrtzidis       FrontendInputFile("test.cpp", Language::CXX));
95*8dfaecc4SArgyrios Kyrtzidis   Invocation->getFrontendOpts().ProgramAction = EmitLLVM;
96*8dfaecc4SArgyrios Kyrtzidis   Invocation->getTargetOpts().Triple = "x86_64-unknown-linux-gnu";
97*8dfaecc4SArgyrios Kyrtzidis   Invocation->getCodeGenOpts().setDebugInfo(codegenoptions::FullDebugInfo);
98*8dfaecc4SArgyrios Kyrtzidis   CompilerInstance Compiler;
99*8dfaecc4SArgyrios Kyrtzidis 
100*8dfaecc4SArgyrios Kyrtzidis   SmallString<256> IRBuffer;
101*8dfaecc4SArgyrios Kyrtzidis   Compiler.setOutputStream(std::make_unique<raw_svector_ostream>(IRBuffer));
102*8dfaecc4SArgyrios Kyrtzidis   Compiler.setInvocation(std::move(Invocation));
103*8dfaecc4SArgyrios Kyrtzidis   Compiler.createDiagnostics();
104*8dfaecc4SArgyrios Kyrtzidis   Compiler.createFileManager(std::move(VFS));
105*8dfaecc4SArgyrios Kyrtzidis 
106*8dfaecc4SArgyrios Kyrtzidis   EmitLLVMAction Action;
107*8dfaecc4SArgyrios Kyrtzidis   bool Success = Compiler.ExecuteAction(Action);
108*8dfaecc4SArgyrios Kyrtzidis   EXPECT_TRUE(Success);
109*8dfaecc4SArgyrios Kyrtzidis 
110*8dfaecc4SArgyrios Kyrtzidis   SmallString<128> RealCWD;
111*8dfaecc4SArgyrios Kyrtzidis   llvm::sys::fs::current_path(RealCWD);
112*8dfaecc4SArgyrios Kyrtzidis   EXPECT_TRUE(!RealCWD.empty());
113*8dfaecc4SArgyrios Kyrtzidis   EXPECT_FALSE(IRBuffer.str().contains(RealCWD));
114*8dfaecc4SArgyrios Kyrtzidis   EXPECT_TRUE(IRBuffer.str().contains("in-memory-fs-cwd"));
115*8dfaecc4SArgyrios Kyrtzidis }
1167a545536SSerge Pavlov }
117