xref: /llvm-project/flang/lib/Parser/parsing.cpp (revision 1f8790050b0e99e7b46cc69518aa84f46f50738e)
1 //===-- lib/Parser/parsing.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/Parser/parsing.h"
10 #include "preprocessor.h"
11 #include "prescan.h"
12 #include "type-parsers.h"
13 #include "flang/Parser/message.h"
14 #include "flang/Parser/provenance.h"
15 #include "flang/Parser/source.h"
16 #include "llvm/Support/raw_ostream.h"
17 
18 namespace Fortran::parser {
19 
20 Parsing::Parsing(AllSources &s) : cooked_{s} {}
21 Parsing::~Parsing() {}
22 
23 const SourceFile *Parsing::Prescan(const std::string &path, Options options) {
24   options_ = options;
25   AllSources &allSources{cooked_.allSources()};
26   if (options.isModuleFile) {
27     for (const auto &path : options.searchDirectories) {
28       allSources.PushSearchPathDirectory(path);
29     }
30   }
31 
32   std::string buf;
33   llvm::raw_string_ostream fileError{buf};
34   const SourceFile *sourceFile;
35   if (path == "-") {
36     sourceFile = allSources.ReadStandardInput(fileError);
37   } else {
38     sourceFile = allSources.Open(path, fileError);
39   }
40   if (!fileError.str().empty()) {
41     ProvenanceRange range{allSources.AddCompilerInsertion(path)};
42     messages_.Say(range, "%s"_err_en_US, fileError.str());
43     return sourceFile;
44   }
45   CHECK(sourceFile);
46 
47   if (!options.isModuleFile) {
48     // For .mod files we always want to look in the search directories.
49     // For normal source files we don't push them until after the primary
50     // source file has been opened.  If foo.f is missing from the current
51     // working directory, we don't want to accidentally read another foo.f
52     // from another directory that's on the search path.
53     for (const auto &path : options.searchDirectories) {
54       allSources.PushSearchPathDirectory(path);
55     }
56   }
57 
58   Preprocessor preprocessor{allSources};
59   for (const auto &predef : options.predefinitions) {
60     if (predef.second) {
61       preprocessor.Define(predef.first, *predef.second);
62     } else {
63       preprocessor.Undefine(predef.first);
64     }
65   }
66   Prescanner prescanner{messages_, cooked_, preprocessor, options.features};
67   prescanner.set_fixedForm(options.isFixedForm)
68       .set_fixedFormColumnLimit(options.fixedFormColumns)
69       .AddCompilerDirectiveSentinel("dir$");
70   if (options.features.IsEnabled(LanguageFeature::OpenMP)) {
71     prescanner.AddCompilerDirectiveSentinel("$omp");
72     prescanner.AddCompilerDirectiveSentinel("$"); // OMP conditional line
73   }
74   ProvenanceRange range{allSources.AddIncludedFile(
75       *sourceFile, ProvenanceRange{}, options.isModuleFile)};
76   prescanner.Prescan(range);
77   if (cooked_.BufferedBytes() == 0 && !options.isModuleFile) {
78     // Input is empty.  Append a newline so that any warning
79     // message about nonstandard usage will have provenance.
80     cooked_.Put('\n', range.start());
81   }
82   cooked_.Marshal();
83   if (options.needProvenanceRangeToCharBlockMappings) {
84     cooked_.CompileProvenanceRangeToOffsetMappings();
85   }
86   return sourceFile;
87 }
88 
89 void Parsing::DumpCookedChars(llvm::raw_ostream &out) const {
90   UserState userState{cooked_, common::LanguageFeatureControl{}};
91   ParseState parseState{cooked_};
92   parseState.set_inFixedForm(options_.isFixedForm).set_userState(&userState);
93   while (std::optional<const char *> p{parseState.GetNextChar()}) {
94     out << **p;
95   }
96 }
97 
98 void Parsing::DumpProvenance(llvm::raw_ostream &out) const {
99   cooked_.Dump(out);
100 }
101 
102 void Parsing::DumpParsingLog(llvm::raw_ostream &out) const {
103   log_.Dump(out, cooked_);
104 }
105 
106 void Parsing::Parse(llvm::raw_ostream &out) {
107   UserState userState{cooked_, options_.features};
108   userState.set_debugOutput(out)
109       .set_instrumentedParse(options_.instrumentedParse)
110       .set_log(&log_);
111   ParseState parseState{cooked_};
112   parseState.set_inFixedForm(options_.isFixedForm).set_userState(&userState);
113   parseTree_ = program.Parse(parseState);
114   CHECK(
115       !parseState.anyErrorRecovery() || parseState.messages().AnyFatalError());
116   consumedWholeFile_ = parseState.IsAtEnd();
117   messages_.Annex(std::move(parseState.messages()));
118   finalRestingPlace_ = parseState.GetLocation();
119 }
120 
121 void Parsing::ClearLog() { log_.clear(); }
122 
123 bool Parsing::ForTesting(std::string path, llvm::raw_ostream &err) {
124   llvm::raw_null_ostream NullStream;
125   Prescan(path, Options{});
126   if (messages_.AnyFatalError()) {
127     messages_.Emit(err, cooked_);
128     err << "could not scan " << path << '\n';
129     return false;
130   }
131   Parse(NullStream);
132   messages_.Emit(err, cooked_);
133   if (!consumedWholeFile_) {
134     EmitMessage(err, finalRestingPlace_, "parser FAIL; final position");
135     return false;
136   }
137   if (messages_.AnyFatalError() || !parseTree_.has_value()) {
138     err << "could not parse " << path << '\n';
139     return false;
140   }
141   return true;
142 }
143 } // namespace Fortran::parser
144