1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
//===- unittests/Frontend/FrontendActionTest.cpp FrontendAction tests-----===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "flang/Frontend/CompilerInstance.h"
#include "flang/Frontend/CompilerInvocation.h"
#include "flang/Frontend/FrontendOptions.h"
#include "flang/FrontendTool/Utils.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TargetParser/Host.h"
#include "llvm/TargetParser/Triple.h"
#include "gtest/gtest.h"
using namespace Fortran::frontend;
namespace {
class FrontendActionTest : public ::testing::Test {
protected:
// AllSources (which is used to manage files inside every compiler
// instance), works with paths. So we need a filename and a path for the
// input file.
// TODO: We could use `-` for inputFilePath, but then we'd need a way to
// write to stdin that's then read by AllSources. Ideally, AllSources should
// be capable of reading from any stream.
std::string inputFileName;
std::string inputFilePath;
// The output stream for the input file. Use this to populate the input.
std::unique_ptr<llvm::raw_fd_ostream> inputFileOs;
std::error_code ec;
CompilerInstance compInst;
std::shared_ptr<CompilerInvocation> invoc;
void SetUp() override {
// Generate a unique test file name.
const testing::TestInfo *const testInfo =
testing::UnitTest::GetInstance()->current_test_info();
inputFileName = std::string(testInfo->name()) + "_test-file.f90";
// Create the input file stream. Note that this stream is populated
// separately in every test (i.e. the input is test specific).
inputFileOs = std::make_unique<llvm::raw_fd_ostream>(
inputFileName, ec, llvm::sys::fs::OF_None);
if (ec)
FAIL() << "Failed to create the input file";
// Get the path of the input file.
llvm::SmallString<256> cwd;
if (std::error_code ec = llvm::sys::fs::current_path(cwd))
FAIL() << "Failed to obtain the current working directory";
inputFilePath = cwd.c_str();
inputFilePath += "/" + inputFileName;
// Prepare the compiler (CompilerInvocation + CompilerInstance)
compInst.createDiagnostics();
invoc = std::make_shared<CompilerInvocation>();
compInst.setInvocation(std::move(invoc));
compInst.getFrontendOpts().inputs.push_back(
FrontendInputFile(inputFilePath, Language::Fortran));
}
void TearDown() override {
// Clear the input file.
llvm::sys::fs::remove(inputFileName);
// Clear the output files.
// Note that these tests use an output buffer (as opposed to an output
// file), hence there are no physical output files to delete and
// `EraseFiles` is set to `false`. Also, some actions (e.g.
// `ParseSyntaxOnly`) don't generated output. In such cases there's no
// output to clear and `ClearOutputFile` returns immediately.
compInst.clearOutputFiles(/*EraseFiles=*/false);
}
};
TEST_F(FrontendActionTest, TestInputOutput) {
// Populate the input file with the pre-defined input and flush it.
*(inputFileOs) << "End Program arithmetic";
inputFileOs.reset();
// Set-up the action kind.
compInst.getInvocation().getFrontendOpts().programAction = InputOutputTest;
// Set-up the output stream. Using output buffer wrapped as an output
// stream, as opposed to an actual file (or a file descriptor).
llvm::SmallVector<char, 256> outputFileBuffer;
std::unique_ptr<llvm::raw_pwrite_stream> outputFileStream(
new llvm::raw_svector_ostream(outputFileBuffer));
compInst.setOutputStream(std::move(outputFileStream));
// Execute the action.
bool success = executeCompilerInvocation(&compInst);
// Validate the expected output.
EXPECT_TRUE(success);
EXPECT_TRUE(!outputFileBuffer.empty());
EXPECT_TRUE(llvm::StringRef(outputFileBuffer.data())
.startswith("End Program arithmetic"));
}
TEST_F(FrontendActionTest, PrintPreprocessedInput) {
// Populate the input file with the pre-defined input and flush it.
*(inputFileOs) << "#ifdef NEW\n"
<< " Program A \n"
<< "#else\n"
<< " Program B\n"
<< "#endif";
inputFileOs.reset();
// Set-up the action kind.
compInst.getInvocation().getFrontendOpts().programAction =
PrintPreprocessedInput;
compInst.getInvocation().getPreprocessorOpts().noReformat = true;
// Set-up the output stream. We are using output buffer wrapped as an output
// stream, as opposed to an actual file (or a file descriptor).
llvm::SmallVector<char, 256> outputFileBuffer;
std::unique_ptr<llvm::raw_pwrite_stream> outputFileStream(
new llvm::raw_svector_ostream(outputFileBuffer));
compInst.setOutputStream(std::move(outputFileStream));
// Execute the action.
bool success = executeCompilerInvocation(&compInst);
// Validate the expected output.
EXPECT_TRUE(success);
EXPECT_TRUE(!outputFileBuffer.empty());
EXPECT_TRUE(
llvm::StringRef(outputFileBuffer.data()).startswith("program b\n"));
}
TEST_F(FrontendActionTest, ParseSyntaxOnly) {
// Populate the input file with the pre-defined input and flush it.
*(inputFileOs) << "IF (A > 0.0) IF (B < 0.0) A = LOG (A)\n"
<< "END";
inputFileOs.reset();
// Set-up the action kind.
compInst.getInvocation().getFrontendOpts().programAction = ParseSyntaxOnly;
// Set-up the output stream for the semantic diagnostics.
llvm::SmallVector<char, 256> outputDiagBuffer;
std::unique_ptr<llvm::raw_pwrite_stream> outputStream(
new llvm::raw_svector_ostream(outputDiagBuffer));
compInst.setSemaOutputStream(std::move(outputStream));
// Execute the action.
bool success = executeCompilerInvocation(&compInst);
// Validate the expected output.
EXPECT_FALSE(success);
EXPECT_TRUE(!outputDiagBuffer.empty());
EXPECT_TRUE(
llvm::StringRef(outputDiagBuffer.data())
.contains(
":1:14: error: IF statement is not allowed in IF statement\n"));
}
TEST_F(FrontendActionTest, EmitLLVM) {
// Populate the input file with the pre-defined input and flush it.
*(inputFileOs) << "end program";
inputFileOs.reset();
// Set-up the action kind.
compInst.getInvocation().getFrontendOpts().programAction = EmitLLVM;
// Set-up default target triple.
compInst.getInvocation().getTargetOpts().triple =
llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple());
// Initialise LLVM backend
llvm::InitializeAllTargets();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllAsmPrinters();
// Set-up the output stream. We are using output buffer wrapped as an output
// stream, as opposed to an actual file (or a file descriptor).
llvm::SmallVector<char> outputFileBuffer;
std::unique_ptr<llvm::raw_pwrite_stream> outputFileStream(
new llvm::raw_svector_ostream(outputFileBuffer));
compInst.setOutputStream(std::move(outputFileStream));
// Execute the action.
bool success = executeCompilerInvocation(&compInst);
// Validate the expected output.
EXPECT_TRUE(success);
EXPECT_TRUE(!outputFileBuffer.empty());
EXPECT_TRUE(llvm::StringRef(outputFileBuffer.data())
.contains("define void @_QQmain()"));
}
TEST_F(FrontendActionTest, EmitAsm) {
// Populate the input file with the pre-defined input and flush it.
*(inputFileOs) << "end program";
inputFileOs.reset();
// Set-up the action kind.
compInst.getInvocation().getFrontendOpts().programAction = EmitAssembly;
// Set-up default target triple.
compInst.getInvocation().getTargetOpts().triple =
llvm::Triple::normalize(llvm::sys::getDefaultTargetTriple());
// Initialise LLVM backend
llvm::InitializeAllTargets();
llvm::InitializeAllTargetMCs();
llvm::InitializeAllAsmPrinters();
// Set-up the output stream. We are using output buffer wrapped as an output
// stream, as opposed to an actual file (or a file descriptor).
llvm::SmallVector<char, 256> outputFileBuffer;
std::unique_ptr<llvm::raw_pwrite_stream> outputFileStream(
new llvm::raw_svector_ostream(outputFileBuffer));
compInst.setOutputStream(std::move(outputFileStream));
// Execute the action.
bool success = executeCompilerInvocation(&compInst);
// Validate the expected output.
EXPECT_TRUE(success);
EXPECT_TRUE(!outputFileBuffer.empty());
EXPECT_TRUE(llvm::StringRef(outputFileBuffer.data()).contains("_QQmain"));
}
} // namespace
|