#include "flang/Frontend/CompilerInstance.h"
#include "flang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "llvm/Support/FileSystem.h"
#include "gtest/gtest.h"
using namespace llvm;
using namespace Fortran::frontend;
namespace {
TEST(CompilerInstance, SanityCheckForFileManager) {
const char *inputSource = "InputSourceFile";
std::string inputFile = "buffer-file-test.f";
std::error_code ec;
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";
*(os) << inputSource;
os.reset();
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;
std::string buf;
llvm::raw_string_ostream errorStream{buf};
CompilerInstance compInst;
const Fortran::parser::SourceFile *sf =
compInst.getAllSources().Open(testFilePath, errorStream);
llvm::ArrayRef<char> fileContent = sf->content();
EXPECT_FALSE(fileContent.size() == 0);
EXPECT_TRUE(
llvm::StringRef(fileContent.data()).starts_with("InputSourceFile"));
ec = llvm::sys::fs::remove(inputFile);
if (ec)
FAIL() << "Failed to delete the test file";
}
TEST(CompilerInstance, AllowDiagnosticLogWithUnownedDiagnosticConsumer) {
std::string diagnosticOutput;
llvm::raw_string_ostream diagnosticsOS(diagnosticOutput);
auto diagPrinter = std::make_unique<Fortran::frontend::TextDiagnosticPrinter>(
diagnosticsOS, new clang::DiagnosticOptions());
CompilerInstance compInst;
auto diagOpts = new clang::DiagnosticOptions();
diagOpts->DiagnosticLogFile = "-";
IntrusiveRefCntPtr<clang::DiagnosticsEngine> diags =
compInst.createDiagnostics(diagOpts, diagPrinter.get(),
false);
diags->Report(clang::diag::err_expected) << "no crash";
ASSERT_EQ(diagnosticsOS.str(), "error: expected no crash\n");
}
}