1 //===--- FrontendActions.cpp ----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #include "flang/Frontend/FrontendActions.h"
9 #include "flang/Common/Fortran-features.h"
10 #include "flang/Common/default-kinds.h"
11 #include "flang/Frontend/CompilerInstance.h"
12 #include "flang/Parser/source.h"
13 #include "clang/Serialization/PCHContainerOperations.h"
14 
15 using namespace Fortran::frontend;
16 
17 void InputOutputTestAction::ExecuteAction() {
18 
19   // Get the name of the file from FrontendInputFile current.
20   std::string path{GetCurrentFileOrBufferName()};
21   std::string buf;
22   llvm::raw_string_ostream error_stream{buf};
23   bool binaryMode = true;
24 
25   // Set/store input file info into CompilerInstance.
26   CompilerInstance &ci = GetCompilerInstance();
27   Fortran::parser::AllSources &allSources{ci.GetAllSources()};
28   const Fortran::parser::SourceFile *sf;
29   sf = allSources.Open(path, error_stream);
30   llvm::ArrayRef<char> fileContent = sf->content();
31 
32   // Output file descriptor to receive the content of input file.
33   std::unique_ptr<llvm::raw_ostream> os;
34 
35   // Do not write on the output file if using outputStream_.
36   if (ci.IsOutputStreamNull()) {
37     os = ci.CreateDefaultOutputFile(
38         binaryMode, GetCurrentFileOrBufferName(), "txt");
39     if (!os)
40       return;
41     (*os) << fileContent.data();
42   } else {
43     ci.WriteOutputStream(fileContent.data());
44   }
45 }
46