aboutsummaryrefslogtreecommitdiff
path: root/flang/unittests/Frontend/CompilerInstanceTest.cpp
diff options
context:
space:
mode:
authorCaroline Concatto <caroline.concatto@arm.com>2020-10-24 12:33:19 +0100
committerAndrzej Warzynski <andrzej.warzynski@arm.com>2020-10-24 14:58:32 +0100
commit4c5906cffd04202387d2f6b50a47d39c0e4f2c0e (patch)
treed03c6ef3813c405e602a735f479723ee208c6586 /flang/unittests/Frontend/CompilerInstanceTest.cpp
parent65a36bbc3d79f4f23843ec632e86363bc87b02d9 (diff)
downloadllvm-4c5906cffd04202387d2f6b50a47d39c0e4f2c0e.zip
llvm-4c5906cffd04202387d2f6b50a47d39c0e4f2c0e.tar.gz
llvm-4c5906cffd04202387d2f6b50a47d39c0e4f2c0e.tar.bz2
[Flang][Driver] Add infrastructure for basic frontend actions and file I/O
This patch introduces the dependencies required to read and manage input files provided by the command line option. It also adds the infrastructure to create and write to output files. The output is sent to either stdout or a file (specified with the `-o` flag). Separately, in order to be able to test the code for file I/O, it adds infrastructure to create frontend actions. As a basic testable example, it adds the `InputOutputTest` FrontendAction. The sole purpose of this action is to read a file from the command line and print it either to stdout or the output file. This action is run by using the `-test-io` flag also introduced in this patch (available for `flang-new` and `flang-new -fc1`). With this patch: ``` flang-new -test-io input-file.f90 ``` will read input-file.f90 and print it in the output file. The `InputOutputTest` frontend action has been introduced primarily to facilitate testing. It is hidden from users (i.e. it's only displayed with `--help-hidden`). Currently Clang doesn’t have an equivalent action. `-test-io` is used to trigger the InputOutputTest action in the Flang frontend driver. This patch makes sure that “flang-new” forwards it to “flang-new -fc1" by creating a preprocessor job. However, in Flang.cpp, `-test-io` is passed to “flang-new -fc1” without `-E`. This way we make sure that the preprocessor is _not_ run in the frontend driver. This is the desired behaviour: `-test-io` should only read the input file and print it to the output stream. co-authored-by: Andrzej Warzynski <andrzej.warzynski@arm.com> Differential Revision: https://reviews.llvm.org/D87989
Diffstat (limited to 'flang/unittests/Frontend/CompilerInstanceTest.cpp')
-rw-r--r--flang/unittests/Frontend/CompilerInstanceTest.cpp46
1 files changed, 46 insertions, 0 deletions
diff --git a/flang/unittests/Frontend/CompilerInstanceTest.cpp b/flang/unittests/Frontend/CompilerInstanceTest.cpp
index 04d581c..932cacd 100644
--- a/flang/unittests/Frontend/CompilerInstanceTest.cpp
+++ b/flang/unittests/Frontend/CompilerInstanceTest.cpp
@@ -9,6 +9,7 @@
#include "flang/Frontend/CompilerInstance.h"
#include "flang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Basic/DiagnosticOptions.h"
+#include "llvm/Support//FileSystem.h"
#include "gtest/gtest.h"
@@ -17,6 +18,51 @@ using namespace Fortran::frontend;
namespace {
+TEST(CompilerInstance, SanityCheckForFileManager) {
+ const char *inputSource = "InputSourceFile";
+ std::string inputFile = "buffer-file-test.f";
+ std::error_code ec;
+
+ // 1. Create the input file for the file manager
+ // AllSources (which is used to manage files inside every compiler instance),
+ // works with paths. This means that it requires a physical file. Create one.
+ std::unique_ptr<llvm::raw_fd_ostream> os{
+ new llvm::raw_fd_ostream(inputFile, ec, llvm::sys::fs::OF_None)};
+ if (ec)
+ FAIL() << "Failed to create the input file";
+
+ // Populate the input file with the pre-defined input and flush it.
+ *(os) << inputSource;
+ os.reset();
+
+ // Get the path of the input file
+ llvm::SmallString<64> cwd;
+ if (std::error_code ec = llvm::sys::fs::current_path(cwd))
+ FAIL() << "Failed to obtain the current working directory";
+ std::string testFilePath(cwd.c_str());
+ testFilePath += "/" + inputFile;
+
+ // 2. Set up CompilerInstance (i.e. specify the input file)
+ std::string buf;
+ llvm::raw_string_ostream error_stream{buf};
+ CompilerInstance compInst;
+ const Fortran::parser::SourceFile *sf =
+ compInst.GetAllSources().Open(testFilePath, error_stream);
+
+ // 3. Verify the content of the input file
+ // This is just a sanity check to make sure that CompilerInstance is capable
+ // of reading input files.
+ llvm::ArrayRef<char> fileContent = sf->content();
+ EXPECT_FALSE(fileContent.size() == 0);
+ EXPECT_TRUE(
+ llvm::StringRef(fileContent.data()).startswith("InputSourceFile"));
+
+ // 4. Delete the test file
+ ec = llvm::sys::fs::remove(inputFile);
+ if (ec)
+ FAIL() << "Failed to delete the test file";
+}
+
TEST(CompilerInstance, AllowDiagnosticLogWithUnownedDiagnosticConsumer) {
// 1. Set-up a basic DiagnosticConsumer
std::string diagnosticOutput;