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(AllCookedSources &allCooked) : allCooked_{allCooked} {} 21 Parsing::~Parsing() {} 22 23 const SourceFile *Parsing::Prescan(const std::string &path, Options options) { 24 options_ = options; 25 AllSources &allSources{allCooked_.allSources()}; 26 allSources.ClearSearchPath(); 27 if (options.isModuleFile) { 28 for (const auto &path : options.searchDirectories) { 29 allSources.AppendSearchPathDirectory(path); 30 } 31 } 32 33 std::string buf; 34 llvm::raw_string_ostream fileError{buf}; 35 const SourceFile *sourceFile; 36 if (path == "-") { 37 sourceFile = allSources.ReadStandardInput(fileError); 38 } else if (options.isModuleFile) { 39 // Don't mess with intrinsic module search path 40 sourceFile = allSources.Open(path, fileError); 41 } else { 42 sourceFile = 43 allSources.Open(path, fileError, "."s /*prepend to search path*/); 44 } 45 if (!fileError.str().empty()) { 46 ProvenanceRange range{allSources.AddCompilerInsertion(path)}; 47 messages_.Say(range, "%s"_err_en_US, fileError.str()); 48 return sourceFile; 49 } 50 CHECK(sourceFile); 51 52 if (!options.isModuleFile) { 53 // For .mod files we always want to look in the search directories. 54 // For normal source files we don't add them until after the primary 55 // source file has been opened. If foo.f is missing from the current 56 // working directory, we don't want to accidentally read another foo.f 57 // from another directory that's on the search path. 58 for (const auto &path : options.searchDirectories) { 59 allSources.AppendSearchPathDirectory(path); 60 } 61 } 62 63 Preprocessor preprocessor{allSources}; 64 if (!options.predefinitions.empty()) { 65 preprocessor.DefineStandardMacros(); 66 for (const auto &predef : options.predefinitions) { 67 if (predef.second) { 68 preprocessor.Define(predef.first, *predef.second); 69 } else { 70 preprocessor.Undefine(predef.first); 71 } 72 } 73 } 74 currentCooked_ = &allCooked_.NewCookedSource(); 75 Prescanner prescanner{ 76 messages_, *currentCooked_, preprocessor, options.features}; 77 prescanner.set_fixedForm(options.isFixedForm) 78 .set_fixedFormColumnLimit(options.fixedFormColumns) 79 .AddCompilerDirectiveSentinel("dir$"); 80 if (options.features.IsEnabled(LanguageFeature::OpenACC)) { 81 prescanner.AddCompilerDirectiveSentinel("$acc"); 82 } 83 if (options.features.IsEnabled(LanguageFeature::OpenMP)) { 84 prescanner.AddCompilerDirectiveSentinel("$omp"); 85 prescanner.AddCompilerDirectiveSentinel("$"); // OMP conditional line 86 } 87 ProvenanceRange range{allSources.AddIncludedFile( 88 *sourceFile, ProvenanceRange{}, options.isModuleFile)}; 89 prescanner.Prescan(range); 90 if (currentCooked_->BufferedBytes() == 0 && !options.isModuleFile) { 91 // Input is empty. Append a newline so that any warning 92 // message about nonstandard usage will have provenance. 93 currentCooked_->Put('\n', range.start()); 94 } 95 currentCooked_->Marshal(allCooked_); 96 if (options.needProvenanceRangeToCharBlockMappings) { 97 currentCooked_->CompileProvenanceRangeToOffsetMappings(allSources); 98 } 99 if (options.showColors) { 100 allSources.setShowColors(/*showColors=*/true); 101 } 102 return sourceFile; 103 } 104 105 void Parsing::EmitPreprocessedSource( 106 llvm::raw_ostream &out, bool lineDirectives) const { 107 const SourceFile *sourceFile{nullptr}; 108 int sourceLine{0}; 109 int column{1}; 110 bool inDirective{false}; 111 bool inContinuation{false}; 112 bool lineWasBlankBefore{true}; 113 const AllSources &allSources{allCooked().allSources()}; 114 // All directives that flang support are known to have a length of 3 chars 115 constexpr int directiveNameLength{3}; 116 // We need to know the current directive in order to provide correct 117 // continuation for the directive 118 std::string directive; 119 for (const char &atChar : cooked().AsCharBlock()) { 120 char ch{atChar}; 121 if (ch == '\n') { 122 out << '\n'; // TODO: DOS CR-LF line ending if necessary 123 column = 1; 124 inDirective = false; 125 inContinuation = false; 126 lineWasBlankBefore = true; 127 ++sourceLine; 128 directive.clear(); 129 } else { 130 auto provenance{cooked().GetProvenanceRange(CharBlock{&atChar, 1})}; 131 132 // Preserves original case of the character 133 const auto getOriginalChar{[&](char ch) { 134 if (IsLetter(ch) && provenance && provenance->size() == 1) { 135 if (const char *orig{allSources.GetSource(*provenance)}) { 136 const char upper{ToUpperCaseLetter(ch)}; 137 if (*orig == upper) { 138 return upper; 139 } 140 } 141 } 142 return ch; 143 }}; 144 145 if (ch == '!' && lineWasBlankBefore) { 146 // Other comment markers (C, *, D) in original fixed form source 147 // input card column 1 will have been deleted or normalized to !, 148 // which signifies a comment (directive) in both source forms. 149 inDirective = true; 150 } 151 if (inDirective && directive.size() < directiveNameLength && 152 IsLetter(ch)) { 153 directive += getOriginalChar(ch); 154 } 155 156 std::optional<SourcePosition> position{provenance 157 ? allSources.GetSourcePosition(provenance->start()) 158 : std::nullopt}; 159 if (lineDirectives && column == 1 && position) { 160 if (&position->file != sourceFile) { 161 out << "#line \"" << position->file.path() << "\" " << position->line 162 << '\n'; 163 } else if (position->line != sourceLine) { 164 if (sourceLine < position->line && 165 sourceLine + 10 >= position->line) { 166 // Emit a few newlines to catch up when they'll likely 167 // require fewer bytes than a #line directive would have 168 // occupied. 169 while (sourceLine++ < position->line) { 170 out << '\n'; 171 } 172 } else { 173 out << "#line " << position->line << '\n'; 174 } 175 } 176 sourceFile = &position->file; 177 sourceLine = position->line; 178 } 179 if (column > 72) { 180 // Wrap long lines in a portable fashion that works in both 181 // of the Fortran source forms. The first free-form continuation 182 // marker ("&") lands in column 73, which begins the card commentary 183 // field of fixed form, and the second one is put in column 6, 184 // where it signifies fixed form line continuation. 185 // The standard Fortran fixed form column limit (72) is used 186 // for output, even if the input was parsed with a nonstandard 187 // column limit override option. 188 // OpenMP and OpenACC directives' continuations should have the 189 // corresponding sentinel at the next line. 190 const auto continuation{ 191 inDirective ? "&\n!$" + directive + "&" : "&\n &"s}; 192 out << continuation; 193 column = 7; // start of fixed form source field 194 ++sourceLine; 195 inContinuation = true; 196 } else if (!inDirective && ch != ' ' && (ch < '0' || ch > '9')) { 197 // Put anything other than a label or directive into the 198 // Fortran fixed form source field (columns [7:72]). 199 for (; column < 7; ++column) { 200 out << ' '; 201 } 202 } 203 if (!inContinuation && position && position->column <= 72 && ch != ' ') { 204 // Preserve original indentation 205 for (; column < position->column; ++column) { 206 out << ' '; 207 } 208 } 209 out << getOriginalChar(ch); 210 lineWasBlankBefore = ch == ' ' && lineWasBlankBefore; 211 ++column; 212 } 213 } 214 } 215 216 void Parsing::DumpCookedChars(llvm::raw_ostream &out) const { 217 UserState userState{allCooked_, common::LanguageFeatureControl{}}; 218 ParseState parseState{cooked()}; 219 parseState.set_inFixedForm(options_.isFixedForm).set_userState(&userState); 220 while (std::optional<const char *> p{parseState.GetNextChar()}) { 221 out << **p; 222 } 223 } 224 225 void Parsing::DumpProvenance(llvm::raw_ostream &out) const { 226 allCooked_.Dump(out); 227 } 228 229 void Parsing::DumpParsingLog(llvm::raw_ostream &out) const { 230 log_.Dump(out, allCooked_); 231 } 232 233 void Parsing::Parse(llvm::raw_ostream &out) { 234 UserState userState{allCooked_, options_.features}; 235 userState.set_debugOutput(out) 236 .set_instrumentedParse(options_.instrumentedParse) 237 .set_log(&log_); 238 ParseState parseState{cooked()}; 239 parseState.set_inFixedForm(options_.isFixedForm).set_userState(&userState); 240 parseTree_ = program.Parse(parseState); 241 CHECK( 242 !parseState.anyErrorRecovery() || parseState.messages().AnyFatalError()); 243 consumedWholeFile_ = parseState.IsAtEnd(); 244 messages_.Annex(std::move(parseState.messages())); 245 finalRestingPlace_ = parseState.GetLocation(); 246 } 247 248 void Parsing::ClearLog() { log_.clear(); } 249 250 } // namespace Fortran::parser 251