1 //===--- FrontendAction.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 
9 #include "flang/Frontend/FrontendAction.h"
10 #include "flang/Frontend/CompilerInstance.h"
11 #include "flang/Frontend/FrontendActions.h"
12 #include "llvm/Support/Errc.h"
13 
14 using namespace Fortran::frontend;
15 
16 void FrontendAction::SetCurrentInput(const FrontendInputFile &currentInput) {
17   this->currentInput_ = currentInput;
18 }
19 
20 // Call this method if BeginSourceFile fails.
21 // Deallocate compiler instance, input and output descriptors
22 static void BeginSourceFileCleanUp(FrontendAction &fa, CompilerInstance &ci) {
23   ci.ClearOutputFiles(/*EraseFiles=*/true);
24   fa.SetCurrentInput(FrontendInputFile());
25   fa.SetCompilerInstance(nullptr);
26 }
27 
28 bool FrontendAction::BeginSourceFile(
29     CompilerInstance &ci, const FrontendInputFile &realInput) {
30 
31   FrontendInputFile input(realInput);
32   assert(!instance_ && "Already processing a source file!");
33   assert(!realInput.IsEmpty() && "Unexpected empty filename!");
34   SetCurrentInput(realInput);
35   SetCompilerInstance(&ci);
36   if (!ci.HasAllSources()) {
37     BeginSourceFileCleanUp(*this, ci);
38     return false;
39   }
40   return true;
41 }
42 
43 bool FrontendAction::ShouldEraseOutputFiles() {
44   return GetCompilerInstance().getDiagnostics().hasErrorOccurred();
45 }
46 
47 llvm::Error FrontendAction::Execute() {
48   ExecuteAction();
49   return llvm::Error::success();
50 }
51 
52 void FrontendAction::EndSourceFile() {
53   CompilerInstance &ci = GetCompilerInstance();
54 
55   // Cleanup the output streams, and erase the output files if instructed by the
56   // FrontendAction.
57   ci.ClearOutputFiles(/*EraseFiles=*/ShouldEraseOutputFiles());
58 
59   SetCompilerInstance(nullptr);
60   SetCurrentInput(FrontendInputFile());
61 }
62