| 1 |
//===-- lib/Parser/prescan.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 "prescan.h" |
| 10 |
#include "flang/Common/idioms.h" |
| 11 |
#include "flang/Parser/characters.h" |
| 12 |
#include "flang/Parser/message.h" |
| 13 |
#include "flang/Parser/preprocessor.h" |
| 14 |
#include "flang/Parser/source.h" |
| 15 |
#include "flang/Parser/token-sequence.h" |
| 16 |
#include "llvm/Support/raw_ostream.h" |
| 17 |
#include <cstddef> |
| 18 |
#include <cstring> |
| 19 |
#include <utility> |
| 20 |
#include <vector> |
| 21 |
|
| 22 |
namespace Fortran::parser { |
| 23 |
|
| 24 |
using common::LanguageFeature; |
| 25 |
|
| 26 |
static constexpr int maxPrescannerNesting{100}; |
| 27 |
|
| 28 |
Prescanner::Prescanner(Messages &messages, CookedSource &cooked, |
| 29 |
Preprocessor &preprocessor, common::LanguageFeatureControl lfc) |
| 30 |
: messages_{messages}, cooked_{cooked}, preprocessor_{preprocessor}, |
| 31 |
allSources_{preprocessor_.allSources()}, features_{lfc}, |
| 32 |
backslashFreeFormContinuation_{preprocessor.AnyDefinitions()}, |
| 33 |
encoding_{allSources_.encoding()} {} |
| 34 |
|
| 35 |
Prescanner::Prescanner(const Prescanner &that, bool isNestedInIncludeDirective) |
| 36 |
: messages_{that.messages_}, cooked_{that.cooked_}, |
| 37 |
preprocessor_{that.preprocessor_}, allSources_{that.allSources_}, |
| 38 |
features_{that.features_}, |
| 39 |
isNestedInIncludeDirective_{isNestedInIncludeDirective}, |
| 40 |
backslashFreeFormContinuation_{that.backslashFreeFormContinuation_}, |
| 41 |
inFixedForm_{that.inFixedForm_}, |
| 42 |
fixedFormColumnLimit_{that.fixedFormColumnLimit_}, |
| 43 |
encoding_{that.encoding_}, |
| 44 |
prescannerNesting_{that.prescannerNesting_ + 1}, |
| 45 |
skipLeadingAmpersand_{that.skipLeadingAmpersand_}, |
| 46 |
compilerDirectiveBloomFilter_{that.compilerDirectiveBloomFilter_}, |
| 47 |
compilerDirectiveSentinels_{that.compilerDirectiveSentinels_} {} |
| 48 |
|
| 49 |
static inline constexpr bool IsFixedFormCommentChar(char ch) { |
| 50 |
return ch == '!' || ch == '*' || ch == 'C' || ch == 'c'; |
| 51 |
} |
| 52 |
|
| 53 |
static void NormalizeCompilerDirectiveCommentMarker(TokenSequence &dir) { |
| 54 |
char *p{dir.GetMutableCharData()}; |
| 55 |
char *limit{p + dir.SizeInChars()}; |
| 56 |
for (; p < limit; ++p) { |
| 57 |
if (*p != ' ') { |
| 58 |
CHECK(IsFixedFormCommentChar(*p)); |
| 59 |
*p = '!'; |
| 60 |
return; |
| 61 |
} |
| 62 |
} |
| 63 |
DIE("compiler directive all blank"); |
| 64 |
} |
| 65 |
|
| 66 |
void Prescanner::Prescan(ProvenanceRange range) { |
| 67 |
startProvenance_ = range.start(); |
| 68 |
start_ = allSources_.GetSource(range); |
| 69 |
CHECK(start_); |
| 70 |
limit_ = start_ + range.size(); |
| 71 |
nextLine_ = start_; |
| 72 |
const bool beganInFixedForm{inFixedForm_}; |
| 73 |
if (prescannerNesting_ > maxPrescannerNesting) { |
| 74 |
Say(GetProvenance(start_), |
| 75 |
"too many nested INCLUDE/#include files, possibly circular"_err_en_US); |
| 76 |
return; |
| 77 |
} |
| 78 |
while (!IsAtEnd()) { |
| 79 |
Statement(); |
| 80 |
} |
| 81 |
if (inFixedForm_ != beganInFixedForm) { |
| 82 |
std::string dir{"!dir$ "}; |
| 83 |
if (beganInFixedForm) { |
| 84 |
dir += "fixed"; |
| 85 |
} else { |
| 86 |
dir += "free"; |
| 87 |
} |
| 88 |
dir += '\n'; |
| 89 |
TokenSequence tokens{dir, allSources_.AddCompilerInsertion(dir).start()}; |
| 90 |
tokens.Emit(cooked_); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
void Prescanner::Statement() { |
| 95 |
TokenSequence tokens; |
| 96 |
const char *statementStart{nextLine_}; |
| 97 |
LineClassification line{ClassifyLine(statementStart)}; |
| 98 |
switch (line.kind) { |
| 99 |
case LineClassification::Kind::Comment: |
| 100 |
nextLine_ += line.payloadOffset; // advance to '!' or newline |
| 101 |
NextLine(); |
| 102 |
return; |
| 103 |
case LineClassification::Kind::IncludeLine: |
| 104 |
FortranInclude(nextLine_ + line.payloadOffset); |
| 105 |
NextLine(); |
| 106 |
return; |
| 107 |
case LineClassification::Kind::ConditionalCompilationDirective: |
| 108 |
case LineClassification::Kind::IncludeDirective: |
| 109 |
preprocessor_.Directive(TokenizePreprocessorDirective(), *this); |
| 110 |
afterPreprocessingDirective_ = true; |
| 111 |
skipLeadingAmpersand_ |= !inFixedForm_; |
| 112 |
return; |
| 113 |
case LineClassification::Kind::PreprocessorDirective: |
| 114 |
case LineClassification::Kind::DefinitionDirective: |
| 115 |
preprocessor_.Directive(TokenizePreprocessorDirective(), *this); |
| 116 |
// Don't set afterPreprocessingDirective_ |
| 117 |
return; |
| 118 |
case LineClassification::Kind::CompilerDirective: { |
| 119 |
directiveSentinel_ = line.sentinel; |
| 120 |
CHECK(InCompilerDirective()); |
| 121 |
BeginStatementAndAdvance(); |
| 122 |
if (inFixedForm_) { |
| 123 |
CHECK(IsFixedFormCommentChar(*at_)); |
| 124 |
} else { |
| 125 |
while (*at_ == ' ' || *at_ == '\t') { |
| 126 |
++at_, ++column_; |
| 127 |
} |
| 128 |
CHECK(*at_ == '!'); |
| 129 |
} |
| 130 |
std::optional<int> condOffset; |
| 131 |
if (directiveSentinel_[0] == '$' && directiveSentinel_[1] == '\0') { |
| 132 |
// OpenMP conditional compilation line. |
| 133 |
condOffset = 2; |
| 134 |
} else if (directiveSentinel_[0] == '@' && directiveSentinel_[1] == 'c' && |
| 135 |
directiveSentinel_[2] == 'u' && directiveSentinel_[3] == 'f' && |
| 136 |
directiveSentinel_[4] == '\0') { |
| 137 |
// CUDA conditional compilation line. |
| 138 |
condOffset = 5; |
| 139 |
} |
| 140 |
if (condOffset) { |
| 141 |
at_ += *condOffset, column_ += *condOffset; |
| 142 |
if (auto payload{IsIncludeLine(at_)}) { |
| 143 |
FortranInclude(at_ + *payload); |
| 144 |
return; |
| 145 |
} else if (inFixedForm_) { |
| 146 |
LabelField(tokens); |
| 147 |
} else { |
| 148 |
SkipSpaces(); |
| 149 |
} |
| 150 |
} else { |
| 151 |
// Compiler directive. Emit normalized sentinel, squash following spaces. |
| 152 |
EmitChar(tokens, '!'); |
| 153 |
++at_, ++column_; |
| 154 |
for (const char *sp{directiveSentinel_}; *sp != '\0'; |
| 155 |
++sp, ++at_, ++column_) { |
| 156 |
EmitChar(tokens, *sp); |
| 157 |
} |
| 158 |
if (*at_ == ' ' || *at_ == '\t') { |
| 159 |
EmitChar(tokens, ' '); |
| 160 |
while (*at_ == ' ' || *at_ == '\t') { |
| 161 |
++at_, ++column_; |
| 162 |
} |
| 163 |
} |
| 164 |
tokens.CloseToken(); |
| 165 |
} |
| 166 |
break; |
| 167 |
} |
| 168 |
case LineClassification::Kind::Source: |
| 169 |
BeginStatementAndAdvance(); |
| 170 |
if (inFixedForm_) { |
| 171 |
if (features_.IsEnabled(LanguageFeature::OldDebugLines) && |
| 172 |
(*at_ == 'D' || *at_ == 'd')) { |
| 173 |
NextChar(); |
| 174 |
} |
| 175 |
LabelField(tokens); |
| 176 |
} else { |
| 177 |
if (skipLeadingAmpersand_) { |
| 178 |
skipLeadingAmpersand_ = false; |
| 179 |
const char *p{SkipWhiteSpace(at_)}; |
| 180 |
if (p < limit_ && *p == '&') { |
| 181 |
column_ += ++p - at_; |
| 182 |
at_ = p; |
| 183 |
} |
| 184 |
} else { |
| 185 |
SkipSpaces(); |
| 186 |
} |
| 187 |
// Check for a leading identifier that might be a keyword macro |
| 188 |
// that will expand to anything indicating a non-source line, like |
| 189 |
// a comment marker or directive sentinel. If so, disable line |
| 190 |
// continuation, so that NextToken() won't consume anything from |
| 191 |
// following lines. |
| 192 |
if (IsLegalIdentifierStart(*at_)) { |
| 193 |
// TODO: Only bother with these cases when any keyword macro has |
| 194 |
// been defined with replacement text that could begin a comment |
| 195 |
// or directive sentinel. |
| 196 |
const char *p{at_}; |
| 197 |
while (IsLegalInIdentifier(*++p)) { |
| 198 |
} |
| 199 |
CharBlock id{at_, static_cast<std::size_t>(p - at_)}; |
| 200 |
if (preprocessor_.IsNameDefined(id) && |
| 201 |
!preprocessor_.IsFunctionLikeDefinition(id)) { |
| 202 |
TokenSequence toks; |
| 203 |
toks.Put(id, GetProvenance(at_)); |
| 204 |
if (auto replaced{preprocessor_.MacroReplacement(toks, *this)}) { |
| 205 |
auto newLineClass{ClassifyLine(*replaced, GetCurrentProvenance())}; |
| 206 |
disableSourceContinuation_ = |
| 207 |
newLineClass.kind != LineClassification::Kind::Source; |
| 208 |
if (newLineClass.kind == |
| 209 |
LineClassification::Kind::CompilerDirective) { |
| 210 |
directiveSentinel_ = newLineClass.sentinel; |
| 211 |
} |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
} |
| 216 |
break; |
| 217 |
} |
| 218 |
|
| 219 |
while (NextToken(tokens)) { |
| 220 |
} |
| 221 |
if (continuationLines_ > 255) { |
| 222 |
if (features_.ShouldWarn(common::LanguageFeature::MiscSourceExtensions)) { |
| 223 |
Say(GetProvenance(statementStart), |
| 224 |
"%d continuation lines is more than the Fortran standard allows"_port_en_US, |
| 225 |
continuationLines_); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
Provenance newlineProvenance{GetCurrentProvenance()}; |
| 230 |
if (std::optional<TokenSequence> preprocessed{ |
| 231 |
preprocessor_.MacroReplacement(tokens, *this)}) { |
| 232 |
// Reprocess the preprocessed line. |
| 233 |
LineClassification ppl{ClassifyLine(*preprocessed, newlineProvenance)}; |
| 234 |
switch (ppl.kind) { |
| 235 |
case LineClassification::Kind::Comment: |
| 236 |
break; |
| 237 |
case LineClassification::Kind::IncludeLine: |
| 238 |
FortranInclude(preprocessed->TokenAt(0).begin() + ppl.payloadOffset); |
| 239 |
break; |
| 240 |
case LineClassification::Kind::ConditionalCompilationDirective: |
| 241 |
case LineClassification::Kind::IncludeDirective: |
| 242 |
case LineClassification::Kind::DefinitionDirective: |
| 243 |
case LineClassification::Kind::PreprocessorDirective: |
| 244 |
if (features_.ShouldWarn(common::UsageWarning::Preprocessing)) { |
| 245 |
Say(preprocessed->GetProvenanceRange(), |
| 246 |
"Preprocessed line resembles a preprocessor directive"_warn_en_US); |
| 247 |
} |
| 248 |
CheckAndEmitLine(preprocessed->ToLowerCase(), newlineProvenance); |
| 249 |
break; |
| 250 |
case LineClassification::Kind::CompilerDirective: |
| 251 |
if (preprocessed->HasRedundantBlanks()) { |
| 252 |
preprocessed->RemoveRedundantBlanks(); |
| 253 |
} |
| 254 |
while (CompilerDirectiveContinuation(*preprocessed, ppl.sentinel)) { |
| 255 |
newlineProvenance = GetCurrentProvenance(); |
| 256 |
} |
| 257 |
NormalizeCompilerDirectiveCommentMarker(*preprocessed); |
| 258 |
preprocessed->ToLowerCase(); |
| 259 |
SourceFormChange(preprocessed->ToString()); |
| 260 |
CheckAndEmitLine(preprocessed->ToLowerCase().ClipComment( |
| 261 |
*this, true /* skip first ! */), |
| 262 |
newlineProvenance); |
| 263 |
break; |
| 264 |
case LineClassification::Kind::Source: |
| 265 |
if (inFixedForm_) { |
| 266 |
if (preprocessed->HasBlanks(/*after column*/ 6)) { |
| 267 |
preprocessed->RemoveBlanks(/*after column*/ 6); |
| 268 |
} |
| 269 |
} else { |
| 270 |
while (SourceLineContinuation(*preprocessed)) { |
| 271 |
newlineProvenance = GetCurrentProvenance(); |
| 272 |
} |
| 273 |
if (preprocessed->HasRedundantBlanks()) { |
| 274 |
preprocessed->RemoveRedundantBlanks(); |
| 275 |
} |
| 276 |
} |
| 277 |
CheckAndEmitLine( |
| 278 |
preprocessed->ToLowerCase().ClipComment(*this), newlineProvenance); |
| 279 |
break; |
| 280 |
} |
| 281 |
} else { // no macro replacement |
| 282 |
if (line.kind == LineClassification::Kind::CompilerDirective) { |
| 283 |
while (CompilerDirectiveContinuation(tokens, line.sentinel)) { |
| 284 |
newlineProvenance = GetCurrentProvenance(); |
| 285 |
} |
| 286 |
tokens.ToLowerCase(); |
| 287 |
SourceFormChange(tokens.ToString()); |
| 288 |
} else { // Kind::Source |
| 289 |
tokens.ToLowerCase(); |
| 290 |
if (inFixedForm_) { |
| 291 |
EnforceStupidEndStatementRules(tokens); |
| 292 |
} |
| 293 |
} |
| 294 |
CheckAndEmitLine(tokens, newlineProvenance); |
| 295 |
} |
| 296 |
directiveSentinel_ = nullptr; |
| 297 |
} |
| 298 |
|
| 299 |
void Prescanner::CheckAndEmitLine( |
| 300 |
TokenSequence &tokens, Provenance newlineProvenance) { |
| 301 |
tokens.CheckBadFortranCharacters( |
| 302 |
messages_, *this, disableSourceContinuation_); |
| 303 |
// Parenthesis nesting check does not apply while any #include is |
| 304 |
// active, nor on the lines before and after a top-level #include, |
| 305 |
// nor before or after conditional source. |
| 306 |
// Applications play shenanigans with line continuation before and |
| 307 |
// after #include'd subprogram argument lists and conditional source. |
| 308 |
if (!isNestedInIncludeDirective_ && !omitNewline_ && |
| 309 |
!afterPreprocessingDirective_ && tokens.BadlyNestedParentheses() && |
| 310 |
!preprocessor_.InConditional()) { |
| 311 |
if (nextLine_ < limit_ && IsPreprocessorDirectiveLine(nextLine_)) { |
| 312 |
// don't complain |
| 313 |
} else { |
| 314 |
tokens.CheckBadParentheses(messages_); |
| 315 |
} |
| 316 |
} |
| 317 |
tokens.Emit(cooked_); |
| 318 |
if (omitNewline_) { |
| 319 |
omitNewline_ = false; |
| 320 |
} else { |
| 321 |
cooked_.Put('\n', newlineProvenance); |
| 322 |
afterPreprocessingDirective_ = false; |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
TokenSequence Prescanner::TokenizePreprocessorDirective() { |
| 327 |
CHECK(!IsAtEnd() && !inPreprocessorDirective_); |
| 328 |
inPreprocessorDirective_ = true; |
| 329 |
BeginStatementAndAdvance(); |
| 330 |
TokenSequence tokens; |
| 331 |
while (NextToken(tokens)) { |
| 332 |
} |
| 333 |
inPreprocessorDirective_ = false; |
| 334 |
return tokens; |
| 335 |
} |
| 336 |
|
| 337 |
void Prescanner::NextLine() { |
| 338 |
void *vstart{static_cast<void *>(const_cast<char *>(nextLine_))}; |
| 339 |
void *v{std::memchr(vstart, '\n', limit_ - nextLine_)}; |
| 340 |
if (!v) { |
| 341 |
nextLine_ = limit_; |
| 342 |
} else { |
| 343 |
const char *nl{const_cast<const char *>(static_cast<char *>(v))}; |
| 344 |
nextLine_ = nl + 1; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
void Prescanner::LabelField(TokenSequence &token) { |
| 349 |
int outCol{1}; |
| 350 |
const char *start{at_}; |
| 351 |
std::optional<int> badColumn; |
| 352 |
for (; *at_ != '\n' && column_ <= 6; ++at_) { |
| 353 |
if (*at_ == '\t') { |
| 354 |
++at_; |
| 355 |
column_ = 7; |
| 356 |
break; |
| 357 |
} |
| 358 |
if (*at_ != ' ' && |
| 359 |
!(*at_ == '0' && column_ == 6)) { // '0' in column 6 becomes space |
| 360 |
EmitChar(token, *at_); |
| 361 |
++outCol; |
| 362 |
if (!badColumn && (column_ == 6 || !IsDecimalDigit(*at_))) { |
| 363 |
badColumn = column_; |
| 364 |
} |
| 365 |
} |
| 366 |
++column_; |
| 367 |
} |
| 368 |
if (badColumn && !preprocessor_.IsNameDefined(token.CurrentOpenToken())) { |
| 369 |
if ((prescannerNesting_ > 0 && *badColumn == 6 && |
| 370 |
cooked_.BufferedBytes() == firstCookedCharacterOffset_) || |
| 371 |
afterPreprocessingDirective_) { |
| 372 |
// This is the first source line in #include'd text or conditional |
| 373 |
// code under #if, or the first source line after such. |
| 374 |
// If it turns out that the preprocessed text begins with a |
| 375 |
// fixed form continuation line, the newline at the end |
| 376 |
// of the latest source line beforehand will be deleted in |
| 377 |
// CookedSource::Marshal(). |
| 378 |
cooked_.MarkPossibleFixedFormContinuation(); |
| 379 |
} else if (features_.ShouldWarn(common::UsageWarning::Scanning)) { |
| 380 |
Say(GetProvenance(start + *badColumn - 1), |
| 381 |
*badColumn == 6 |
| 382 |
? "Statement should not begin with a continuation line"_warn_en_US |
| 383 |
: "Character in fixed-form label field must be a digit"_warn_en_US); |
| 384 |
} |
| 385 |
token.clear(); |
| 386 |
if (*badColumn < 6) { |
| 387 |
at_ = start; |
| 388 |
column_ = 1; |
| 389 |
return; |
| 390 |
} |
| 391 |
outCol = 1; |
| 392 |
} |
| 393 |
if (outCol == 1) { // empty label field |
| 394 |
// Emit a space so that, if the line is rescanned after preprocessing, |
| 395 |
// a leading 'C' or 'D' won't be left-justified and then accidentally |
| 396 |
// misinterpreted as a comment card. |
| 397 |
EmitChar(token, ' '); |
| 398 |
++outCol; |
| 399 |
} |
| 400 |
token.CloseToken(); |
| 401 |
SkipToNextSignificantCharacter(); |
| 402 |
if (IsDecimalDigit(*at_)) { |
| 403 |
if (features_.ShouldWarn(common::LanguageFeature::MiscSourceExtensions)) { |
| 404 |
Say(GetCurrentProvenance(), |
| 405 |
"Label digit is not in fixed-form label field"_port_en_US); |
| 406 |
} |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
// 6.3.3.5: A program unit END statement, or any other statement whose |
| 411 |
// initial line resembles an END statement, shall not be continued in |
| 412 |
// fixed form source. |
| 413 |
void Prescanner::EnforceStupidEndStatementRules(const TokenSequence &tokens) { |
| 414 |
CharBlock cBlock{tokens.ToCharBlock()}; |
| 415 |
const char *str{cBlock.begin()}; |
| 416 |
std::size_t n{cBlock.size()}; |
| 417 |
if (n < 3) { |
| 418 |
return; |
| 419 |
} |
| 420 |
std::size_t j{0}; |
| 421 |
for (; j < n && (str[j] == ' ' || (str[j] >= '0' && str[j] <= '9')); ++j) { |
| 422 |
} |
| 423 |
if (j + 3 > n || std::memcmp(str + j, "end", 3) != 0) { |
| 424 |
return; |
| 425 |
} |
| 426 |
// It starts with END, possibly after a label. |
| 427 |
auto start{allSources_.GetSourcePosition(tokens.GetCharProvenance(j))}; |
| 428 |
auto end{allSources_.GetSourcePosition(tokens.GetCharProvenance(n - 1))}; |
| 429 |
if (!start || !end) { |
| 430 |
return; |
| 431 |
} |
| 432 |
if (&*start->sourceFile == &*end->sourceFile && start->line == end->line) { |
| 433 |
return; // no continuation |
| 434 |
} |
| 435 |
j += 3; |
| 436 |
static const char *const prefixes[]{"program", "subroutine", "function", |
| 437 |
"blockdata", "module", "submodule", nullptr}; |
| 438 |
bool isPrefix{j == n || !IsLegalInIdentifier(str[j])}; // prefix is END |
| 439 |
std::size_t endOfPrefix{j - 1}; |
| 440 |
for (const char *const *p{prefixes}; *p; ++p) { |
| 441 |
std::size_t pLen{std::strlen(*p)}; |
| 442 |
if (j + pLen <= n && std::memcmp(str + j, *p, pLen) == 0) { |
| 443 |
isPrefix = true; // END thing as prefix |
| 444 |
j += pLen; |
| 445 |
endOfPrefix = j - 1; |
| 446 |
for (; j < n && IsLegalInIdentifier(str[j]); ++j) { |
| 447 |
} |
| 448 |
break; |
| 449 |
} |
| 450 |
} |
| 451 |
if (isPrefix) { |
| 452 |
auto range{tokens.GetTokenProvenanceRange(1)}; |
| 453 |
if (j == n) { // END or END thing [name] |
| 454 |
Say(range, |
| 455 |
"Program unit END statement may not be continued in fixed form source"_err_en_US); |
| 456 |
} else { |
| 457 |
auto endOfPrefixPos{ |
| 458 |
allSources_.GetSourcePosition(tokens.GetCharProvenance(endOfPrefix))}; |
| 459 |
auto next{allSources_.GetSourcePosition(tokens.GetCharProvenance(j))}; |
| 460 |
if (endOfPrefixPos && next && |
| 461 |
&*endOfPrefixPos->sourceFile == &*start->sourceFile && |
| 462 |
endOfPrefixPos->line == start->line && |
| 463 |
(&*next->sourceFile != &*start->sourceFile || |
| 464 |
next->line != start->line)) { |
| 465 |
Say(range, |
| 466 |
"Initial line of continued statement must not appear to be a program unit END in fixed form source"_err_en_US); |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
void Prescanner::SkipToEndOfLine() { |
| 473 |
while (*at_ != '\n') { |
| 474 |
++at_, ++column_; |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
bool Prescanner::MustSkipToEndOfLine() const { |
| 479 |
if (inFixedForm_ && column_ > fixedFormColumnLimit_ && !tabInCurrentLine_) { |
| 480 |
return true; // skip over ignored columns in right margin (73:80) |
| 481 |
} else if (*at_ == '!' && !inCharLiteral_) { |
| 482 |
return !IsCompilerDirectiveSentinel(at_); |
| 483 |
} else { |
| 484 |
return false; |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
void Prescanner::NextChar() { |
| 489 |
CHECK(*at_ != '\n'); |
| 490 |
++at_, ++column_; |
| 491 |
while (at_[0] == '\xef' && at_[1] == '\xbb' && at_[2] == '\xbf') { |
| 492 |
// UTF-8 byte order mark - treat this file as UTF-8 |
| 493 |
at_ += 3; |
| 494 |
encoding_ = Encoding::UTF_8; |
| 495 |
} |
| 496 |
SkipToNextSignificantCharacter(); |
| 497 |
} |
| 498 |
|
| 499 |
// Skip everything that should be ignored until the next significant |
| 500 |
// character is reached; handles C-style comments in preprocessing |
| 501 |
// directives, Fortran ! comments, stuff after the right margin in |
| 502 |
// fixed form, and all forms of line continuation. |
| 503 |
bool Prescanner::SkipToNextSignificantCharacter() { |
| 504 |
auto anyContinuationLine{false}; |
| 505 |
if (inPreprocessorDirective_) { |
| 506 |
SkipCComments(); |
| 507 |
} else { |
| 508 |
bool mightNeedSpace{false}; |
| 509 |
if (MustSkipToEndOfLine()) { |
| 510 |
SkipToEndOfLine(); |
| 511 |
} else { |
| 512 |
mightNeedSpace = *at_ == '\n'; |
| 513 |
} |
| 514 |
for (; Continuation(mightNeedSpace); mightNeedSpace = false) { |
| 515 |
anyContinuationLine = true; |
| 516 |
++continuationLines_; |
| 517 |
if (MustSkipToEndOfLine()) { |
| 518 |
SkipToEndOfLine(); |
| 519 |
} |
| 520 |
} |
| 521 |
if (*at_ == '\t') { |
| 522 |
tabInCurrentLine_ = true; |
| 523 |
} |
| 524 |
} |
| 525 |
return anyContinuationLine; |
| 526 |
} |
| 527 |
|
| 528 |
void Prescanner::SkipCComments() { |
| 529 |
while (true) { |
| 530 |
if (IsCComment(at_)) { |
| 531 |
if (const char *after{SkipCComment(at_)}) { |
| 532 |
column_ += after - at_; |
| 533 |
// May have skipped over one or more newlines; relocate the start of |
| 534 |
// the next line. |
| 535 |
nextLine_ = at_ = after; |
| 536 |
NextLine(); |
| 537 |
} else { |
| 538 |
// Don't emit any messages about unclosed C-style comments, because |
| 539 |
// the sequence /* can appear legally in a FORMAT statement. There's |
| 540 |
// no ambiguity, since the sequence */ cannot appear legally. |
| 541 |
break; |
| 542 |
} |
| 543 |
} else if (inPreprocessorDirective_ && at_[0] == '\\' && at_ + 2 < limit_ && |
| 544 |
at_[1] == '\n' && !IsAtEnd()) { |
| 545 |
BeginSourceLineAndAdvance(); |
| 546 |
} else { |
| 547 |
break; |
| 548 |
} |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
void Prescanner::SkipSpaces() { |
| 553 |
while (*at_ == ' ' || *at_ == '\t') { |
| 554 |
NextChar(); |
| 555 |
} |
| 556 |
insertASpace_ = false; |
| 557 |
} |
| 558 |
|
| 559 |
const char *Prescanner::SkipWhiteSpace(const char *p) { |
| 560 |
while (*p == ' ' || *p == '\t') { |
| 561 |
++p; |
| 562 |
} |
| 563 |
return p; |
| 564 |
} |
| 565 |
|
| 566 |
const char *Prescanner::SkipWhiteSpaceAndCComments(const char *p) const { |
| 567 |
while (true) { |
| 568 |
if (*p == ' ' || *p == '\t') { |
| 569 |
++p; |
| 570 |
} else if (IsCComment(p)) { |
| 571 |
if (const char *after{SkipCComment(p)}) { |
| 572 |
p = after; |
| 573 |
} else { |
| 574 |
break; |
| 575 |
} |
| 576 |
} else { |
| 577 |
break; |
| 578 |
} |
| 579 |
} |
| 580 |
return p; |
| 581 |
} |
| 582 |
|
| 583 |
const char *Prescanner::SkipCComment(const char *p) const { |
| 584 |
char star{' '}, slash{' '}; |
| 585 |
p += 2; |
| 586 |
while (star != '*' || slash != '/') { |
| 587 |
if (p >= limit_) { |
| 588 |
return nullptr; // signifies an unterminated comment |
| 589 |
} |
| 590 |
star = slash; |
| 591 |
slash = *p++; |
| 592 |
} |
| 593 |
return p; |
| 594 |
} |
| 595 |
|
| 596 |
bool Prescanner::NextToken(TokenSequence &tokens) { |
| 597 |
CHECK(at_ >= start_ && at_ < limit_); |
| 598 |
if (InFixedFormSource()) { |
| 599 |
SkipSpaces(); |
| 600 |
} else { |
| 601 |
if (*at_ == '/' && IsCComment(at_)) { |
| 602 |
// Recognize and skip over classic C style /*comments*/ when |
| 603 |
// outside a character literal. |
| 604 |
if (features_.ShouldWarn(LanguageFeature::ClassicCComments)) { |
| 605 |
Say(GetCurrentProvenance(), |
| 606 |
"nonstandard usage: C-style comment"_port_en_US); |
| 607 |
} |
| 608 |
SkipCComments(); |
| 609 |
} |
| 610 |
if (*at_ == ' ' || *at_ == '\t') { |
| 611 |
// Compress free-form white space into a single space character. |
| 612 |
const auto theSpace{at_}; |
| 613 |
char previous{at_ <= start_ ? ' ' : at_[-1]}; |
| 614 |
NextChar(); |
| 615 |
SkipSpaces(); |
| 616 |
if (*at_ == '\n' && !omitNewline_) { |
| 617 |
// Discard white space at the end of a line. |
| 618 |
} else if (!inPreprocessorDirective_ && |
| 619 |
(previous == '(' || *at_ == '(' || *at_ == ')')) { |
| 620 |
// Discard white space before/after '(' and before ')', unless in a |
| 621 |
// preprocessor directive. This helps yield space-free contiguous |
| 622 |
// names for generic interfaces like OPERATOR( + ) and |
| 623 |
// READ ( UNFORMATTED ), without misinterpreting #define f (notAnArg). |
| 624 |
// This has the effect of silently ignoring the illegal spaces in |
| 625 |
// the array constructor ( /1,2/ ) but that seems benign; it's |
| 626 |
// hard to avoid that while still removing spaces from OPERATOR( / ) |
| 627 |
// and OPERATOR( // ). |
| 628 |
} else { |
| 629 |
// Preserve the squashed white space as a single space character. |
| 630 |
tokens.PutNextTokenChar(' ', GetProvenance(theSpace)); |
| 631 |
tokens.CloseToken(); |
| 632 |
return true; |
| 633 |
} |
| 634 |
} |
| 635 |
} |
| 636 |
if (insertASpace_) { |
| 637 |
tokens.PutNextTokenChar(' ', spaceProvenance_); |
| 638 |
insertASpace_ = false; |
| 639 |
} |
| 640 |
if (*at_ == '\n') { |
| 641 |
return false; |
| 642 |
} |
| 643 |
const char *start{at_}; |
| 644 |
if (*at_ == '\'' || *at_ == '"') { |
| 645 |
QuotedCharacterLiteral(tokens, start); |
| 646 |
preventHollerith_ = false; |
| 647 |
} else if (IsDecimalDigit(*at_)) { |
| 648 |
int n{0}, digits{0}; |
| 649 |
static constexpr int maxHollerith{256 /*lines*/ * (132 - 6 /*columns*/)}; |
| 650 |
do { |
| 651 |
if (n < maxHollerith) { |
| 652 |
n = 10 * n + DecimalDigitValue(*at_); |
| 653 |
} |
| 654 |
EmitCharAndAdvance(tokens, *at_); |
| 655 |
++digits; |
| 656 |
if (InFixedFormSource()) { |
| 657 |
SkipSpaces(); |
| 658 |
} |
| 659 |
} while (IsDecimalDigit(*at_)); |
| 660 |
if ((*at_ == 'h' || *at_ == 'H') && n > 0 && n < maxHollerith && |
| 661 |
!preventHollerith_) { |
| 662 |
Hollerith(tokens, n, start); |
| 663 |
} else if (*at_ == '.') { |
| 664 |
while (IsDecimalDigit(EmitCharAndAdvance(tokens, *at_))) { |
| 665 |
} |
| 666 |
ExponentAndKind(tokens); |
| 667 |
} else if (ExponentAndKind(tokens)) { |
| 668 |
} else if (digits == 1 && n == 0 && (*at_ == 'x' || *at_ == 'X') && |
| 669 |
inPreprocessorDirective_) { |
| 670 |
do { |
| 671 |
EmitCharAndAdvance(tokens, *at_); |
| 672 |
} while (IsHexadecimalDigit(*at_)); |
| 673 |
} else if (at_[0] == '_' && (at_[1] == '\'' || at_[1] == '"')) { // 4_"..." |
| 674 |
EmitCharAndAdvance(tokens, *at_); |
| 675 |
QuotedCharacterLiteral(tokens, start); |
| 676 |
} else if (IsLetter(*at_) && !preventHollerith_ && |
| 677 |
parenthesisNesting_ > 0) { |
| 678 |
// Handles FORMAT(3I9HHOLLERITH) by skipping over the first I so that |
| 679 |
// we don't misrecognize I9HOLLERITH as an identifier in the next case. |
| 680 |
EmitCharAndAdvance(tokens, *at_); |
| 681 |
} |
| 682 |
preventHollerith_ = false; |
| 683 |
} else if (*at_ == '.') { |
| 684 |
char nch{EmitCharAndAdvance(tokens, '.')}; |
| 685 |
if (!inPreprocessorDirective_ && IsDecimalDigit(nch)) { |
| 686 |
while (IsDecimalDigit(EmitCharAndAdvance(tokens, *at_))) { |
| 687 |
} |
| 688 |
ExponentAndKind(tokens); |
| 689 |
} else if (nch == '.' && EmitCharAndAdvance(tokens, '.') == '.') { |
| 690 |
EmitCharAndAdvance(tokens, '.'); // variadic macro definition ellipsis |
| 691 |
} |
| 692 |
preventHollerith_ = false; |
| 693 |
} else if (IsLegalInIdentifier(*at_)) { |
| 694 |
int parts{1}; |
| 695 |
const char *afterLast{nullptr}; |
| 696 |
do { |
| 697 |
EmitChar(tokens, *at_); |
| 698 |
++at_, ++column_; |
| 699 |
afterLast = at_; |
| 700 |
if (SkipToNextSignificantCharacter() && IsLegalIdentifierStart(*at_)) { |
| 701 |
tokens.CloseToken(); |
| 702 |
++parts; |
| 703 |
} |
| 704 |
} while (IsLegalInIdentifier(*at_)); |
| 705 |
if (parts >= 3) { |
| 706 |
// Subtlety: When an identifier is split across three or more continuation |
| 707 |
// lines (or two continuation lines, immediately preceded or followed |
| 708 |
// by '&' free form continuation line markers, its parts are kept as |
| 709 |
// distinct pp-tokens so that macro operates on them independently. |
| 710 |
// This trick accommodates the historic practice of using line |
| 711 |
// continuation for token pasting after replacement. |
| 712 |
} else if (parts == 2) { |
| 713 |
if ((start > start_ && start[-1] == '&') || |
| 714 |
(afterLast < limit_ && (*afterLast == '&' || *afterLast == '\n'))) { |
| 715 |
// call & call foo& call foo& |
| 716 |
// &MACRO& OR &MACRO& OR &MACRO |
| 717 |
// &foo(...) &(...) |
| 718 |
} else { |
| 719 |
tokens.ReopenLastToken(); |
| 720 |
} |
| 721 |
} |
| 722 |
if (InFixedFormSource()) { |
| 723 |
SkipSpaces(); |
| 724 |
} |
| 725 |
if ((*at_ == '\'' || *at_ == '"') && |
| 726 |
tokens.CharAt(tokens.SizeInChars() - 1) == '_') { // kind_"..." |
| 727 |
QuotedCharacterLiteral(tokens, start); |
| 728 |
preventHollerith_ = false; |
| 729 |
} else { |
| 730 |
preventHollerith_ = true; // DO 10 H = ... |
| 731 |
} |
| 732 |
} else if (*at_ == '*') { |
| 733 |
if (EmitCharAndAdvance(tokens, '*') == '*') { |
| 734 |
EmitCharAndAdvance(tokens, '*'); |
| 735 |
} else { |
| 736 |
// Subtle ambiguity: |
| 737 |
// CHARACTER*2H declares H because *2 is a kind specifier |
| 738 |
// DATAC/N*2H / is repeated Hollerith |
| 739 |
preventHollerith_ = !slashInCurrentStatement_; |
| 740 |
} |
| 741 |
} else { |
| 742 |
char ch{*at_}; |
| 743 |
if (ch == '(') { |
| 744 |
if (parenthesisNesting_++ == 0) { |
| 745 |
isPossibleMacroCall_ = tokens.SizeInTokens() > 0 && |
| 746 |
preprocessor_.IsFunctionLikeDefinition( |
| 747 |
tokens.TokenAt(tokens.SizeInTokens() - 1)); |
| 748 |
} |
| 749 |
} else if (ch == ')' && parenthesisNesting_ > 0) { |
| 750 |
--parenthesisNesting_; |
| 751 |
} |
| 752 |
char nch{EmitCharAndAdvance(tokens, ch)}; |
| 753 |
preventHollerith_ = false; |
| 754 |
if ((nch == '=' && |
| 755 |
(ch == '<' || ch == '>' || ch == '/' || ch == '=' || ch == '!')) || |
| 756 |
(ch == nch && |
| 757 |
(ch == '/' || ch == ':' || ch == '*' || ch == '#' || ch == '&' || |
| 758 |
ch == '|' || ch == '<' || ch == '>')) || |
| 759 |
(ch == '=' && nch == '>')) { |
| 760 |
// token comprises two characters |
| 761 |
EmitCharAndAdvance(tokens, nch); |
| 762 |
} else if (ch == '/') { |
| 763 |
slashInCurrentStatement_ = true; |
| 764 |
} else if (ch == ';' && InFixedFormSource()) { |
| 765 |
SkipSpaces(); |
| 766 |
if (IsDecimalDigit(*at_)) { |
| 767 |
if (features_.ShouldWarn( |
| 768 |
common::LanguageFeature::MiscSourceExtensions)) { |
| 769 |
Say(GetProvenanceRange(at_, at_ + 1), |
| 770 |
"Label should be in the label field"_port_en_US); |
| 771 |
} |
| 772 |
} |
| 773 |
} |
| 774 |
} |
| 775 |
tokens.CloseToken(); |
| 776 |
return true; |
| 777 |
} |
| 778 |
|
| 779 |
bool Prescanner::ExponentAndKind(TokenSequence &tokens) { |
| 780 |
char ed{ToLowerCaseLetter(*at_)}; |
| 781 |
if (ed != 'e' && ed != 'd') { |
| 782 |
return false; |
| 783 |
} |
| 784 |
EmitCharAndAdvance(tokens, ed); |
| 785 |
if (*at_ == '+' || *at_ == '-') { |
| 786 |
EmitCharAndAdvance(tokens, *at_); |
| 787 |
} |
| 788 |
while (IsDecimalDigit(*at_)) { |
| 789 |
EmitCharAndAdvance(tokens, *at_); |
| 790 |
} |
| 791 |
if (*at_ == '_') { |
| 792 |
while (IsLegalInIdentifier(EmitCharAndAdvance(tokens, *at_))) { |
| 793 |
} |
| 794 |
} |
| 795 |
return true; |
| 796 |
} |
| 797 |
|
| 798 |
void Prescanner::QuotedCharacterLiteral( |
| 799 |
TokenSequence &tokens, const char *start) { |
| 800 |
char quote{*at_}; |
| 801 |
const char *end{at_ + 1}; |
| 802 |
inCharLiteral_ = true; |
| 803 |
continuationInCharLiteral_ = true; |
| 804 |
const auto emit{[&](char ch) { EmitChar(tokens, ch); }}; |
| 805 |
const auto insert{[&](char ch) { EmitInsertedChar(tokens, ch); }}; |
| 806 |
bool isEscaped{false}; |
| 807 |
bool escapesEnabled{features_.IsEnabled(LanguageFeature::BackslashEscapes)}; |
| 808 |
while (true) { |
| 809 |
if (*at_ == '\\') { |
| 810 |
if (escapesEnabled) { |
| 811 |
isEscaped = !isEscaped; |
| 812 |
} else { |
| 813 |
// The parser always processes escape sequences, so don't confuse it |
| 814 |
// when escapes are disabled. |
| 815 |
insert('\\'); |
| 816 |
} |
| 817 |
} else { |
| 818 |
isEscaped = false; |
| 819 |
} |
| 820 |
EmitQuotedChar(static_cast<unsigned char>(*at_), emit, insert, false, |
| 821 |
Encoding::LATIN_1); |
| 822 |
while (PadOutCharacterLiteral(tokens)) { |
| 823 |
} |
| 824 |
if (*at_ == '\n') { |
| 825 |
if (!inPreprocessorDirective_) { |
| 826 |
Say(GetProvenanceRange(start, end), |
| 827 |
"Incomplete character literal"_err_en_US); |
| 828 |
} |
| 829 |
break; |
| 830 |
} |
| 831 |
// Here's a weird edge case. When there's a two or more following |
| 832 |
// continuation lines at this point, and the entire significant part of |
| 833 |
// the next continuation line is the name of a keyword macro, replace |
| 834 |
// it in the character literal with its definition. Example: |
| 835 |
// #define FOO foo |
| 836 |
// subroutine subr() bind(c, name="my_& |
| 837 |
// &FOO& |
| 838 |
// &_bar") ... |
| 839 |
// produces a binding name of "my_foo_bar". |
| 840 |
while (at_[1] == '&' && nextLine_ < limit_ && !InFixedFormSource()) { |
| 841 |
const char *idStart{nextLine_}; |
| 842 |
if (const char *amper{SkipWhiteSpace(nextLine_)}; *amper == '&') { |
| 843 |
idStart = amper + 1; |
| 844 |
} |
| 845 |
if (IsLegalIdentifierStart(*idStart)) { |
| 846 |
std::size_t idLen{1}; |
| 847 |
for (; IsLegalInIdentifier(idStart[idLen]); ++idLen) { |
| 848 |
} |
| 849 |
if (idStart[idLen] == '&') { |
| 850 |
CharBlock id{idStart, idLen}; |
| 851 |
if (preprocessor_.IsNameDefined(id)) { |
| 852 |
TokenSequence ppTokens; |
| 853 |
ppTokens.Put(id, GetProvenance(idStart)); |
| 854 |
if (auto replaced{ |
| 855 |
preprocessor_.MacroReplacement(ppTokens, *this)}) { |
| 856 |
tokens.Put(*replaced); |
| 857 |
at_ = &idStart[idLen - 1]; |
| 858 |
NextLine(); |
| 859 |
continue; // try again on the next line |
| 860 |
} |
| 861 |
} |
| 862 |
} |
| 863 |
} |
| 864 |
break; |
| 865 |
} |
| 866 |
end = at_ + 1; |
| 867 |
NextChar(); |
| 868 |
if (*at_ == quote && !isEscaped) { |
| 869 |
// A doubled unescaped quote mark becomes a single instance of that |
| 870 |
// quote character in the literal (later). There can be spaces between |
| 871 |
// the quotes in fixed form source. |
| 872 |
EmitChar(tokens, quote); |
| 873 |
inCharLiteral_ = false; // for cases like print *, '...'!comment |
| 874 |
NextChar(); |
| 875 |
if (InFixedFormSource()) { |
| 876 |
SkipSpaces(); |
| 877 |
} |
| 878 |
if (*at_ != quote) { |
| 879 |
break; |
| 880 |
} |
| 881 |
inCharLiteral_ = true; |
| 882 |
} |
| 883 |
} |
| 884 |
continuationInCharLiteral_ = false; |
| 885 |
inCharLiteral_ = false; |
| 886 |
} |
| 887 |
|
| 888 |
void Prescanner::Hollerith( |
| 889 |
TokenSequence &tokens, int count, const char *start) { |
| 890 |
inCharLiteral_ = true; |
| 891 |
CHECK(*at_ == 'h' || *at_ == 'H'); |
| 892 |
EmitChar(tokens, 'H'); |
| 893 |
while (count-- > 0) { |
| 894 |
if (PadOutCharacterLiteral(tokens)) { |
| 895 |
} else if (*at_ == '\n') { |
| 896 |
if (features_.ShouldWarn(common::UsageWarning::Scanning)) { |
| 897 |
Say(GetProvenanceRange(start, at_), |
| 898 |
"Possible truncated Hollerith literal"_warn_en_US); |
| 899 |
} |
| 900 |
break; |
| 901 |
} else { |
| 902 |
NextChar(); |
| 903 |
// Each multi-byte character encoding counts as a single character. |
| 904 |
// No escape sequences are recognized. |
| 905 |
// Hollerith is always emitted to the cooked character |
| 906 |
// stream in UTF-8. |
| 907 |
DecodedCharacter decoded{DecodeCharacter( |
| 908 |
encoding_, at_, static_cast<std::size_t>(limit_ - at_), false)}; |
| 909 |
if (decoded.bytes > 0) { |
| 910 |
EncodedCharacter utf8{ |
| 911 |
EncodeCharacter<Encoding::UTF_8>(decoded.codepoint)}; |
| 912 |
for (int j{0}; j < utf8.bytes; ++j) { |
| 913 |
EmitChar(tokens, utf8.buffer[j]); |
| 914 |
} |
| 915 |
at_ += decoded.bytes - 1; |
| 916 |
} else { |
| 917 |
Say(GetProvenanceRange(start, at_), |
| 918 |
"Bad character in Hollerith literal"_err_en_US); |
| 919 |
break; |
| 920 |
} |
| 921 |
} |
| 922 |
} |
| 923 |
if (*at_ != '\n') { |
| 924 |
NextChar(); |
| 925 |
} |
| 926 |
inCharLiteral_ = false; |
| 927 |
} |
| 928 |
|
| 929 |
// In fixed form, source card images must be processed as if they were at |
| 930 |
// least 72 columns wide, at least in character literal contexts. |
| 931 |
bool Prescanner::PadOutCharacterLiteral(TokenSequence &tokens) { |
| 932 |
while (inFixedForm_ && !tabInCurrentLine_ && at_[1] == '\n') { |
| 933 |
if (column_ < fixedFormColumnLimit_) { |
| 934 |
tokens.PutNextTokenChar(' ', spaceProvenance_); |
| 935 |
++column_; |
| 936 |
return true; |
| 937 |
} |
| 938 |
if (!FixedFormContinuation(false /*no need to insert space*/) || |
| 939 |
tabInCurrentLine_) { |
| 940 |
return false; |
| 941 |
} |
| 942 |
CHECK(column_ == 7); |
| 943 |
--at_; // point to column 6 of continuation line |
| 944 |
column_ = 6; |
| 945 |
} |
| 946 |
return false; |
| 947 |
} |
| 948 |
|
| 949 |
static bool IsAtProcess(const char *p) { |
| 950 |
static const char pAtProc[]{"process"}; |
| 951 |
for (std::size_t i{0}; i < sizeof pAtProc - 1; ++i) { |
| 952 |
if (ToLowerCaseLetter(*++p) != pAtProc[i]) |
| 953 |
return false; |
| 954 |
} |
| 955 |
return true; |
| 956 |
} |
| 957 |
|
| 958 |
bool Prescanner::IsFixedFormCommentLine(const char *start) const { |
| 959 |
const char *p{start}; |
| 960 |
|
| 961 |
// The @process directive must start in column 1. |
| 962 |
if (*p == '@' && IsAtProcess(p)) { |
| 963 |
return true; |
| 964 |
} |
| 965 |
|
| 966 |
if (IsFixedFormCommentChar(*p) || *p == '%' || // VAX %list, %eject, &c. |
| 967 |
((*p == 'D' || *p == 'd') && |
| 968 |
!features_.IsEnabled(LanguageFeature::OldDebugLines))) { |
| 969 |
return true; |
| 970 |
} |
| 971 |
bool anyTabs{false}; |
| 972 |
while (true) { |
| 973 |
if (*p == ' ') { |
| 974 |
++p; |
| 975 |
} else if (*p == '\t') { |
| 976 |
anyTabs = true; |
| 977 |
++p; |
| 978 |
} else if (*p == '0' && !anyTabs && p == start + 5) { |
| 979 |
++p; // 0 in column 6 must treated as a space |
| 980 |
} else { |
| 981 |
break; |
| 982 |
} |
| 983 |
} |
| 984 |
if (!anyTabs && p >= start + fixedFormColumnLimit_) { |
| 985 |
return true; |
| 986 |
} |
| 987 |
if (*p == '!' && !inCharLiteral_ && (anyTabs || p != start + 5)) { |
| 988 |
return true; |
| 989 |
} |
| 990 |
return *p == '\n'; |
| 991 |
} |
| 992 |
|
| 993 |
const char *Prescanner::IsFreeFormComment(const char *p) const { |
| 994 |
p = SkipWhiteSpaceAndCComments(p); |
| 995 |
if (*p == '!' || *p == '\n') { |
| 996 |
return p; |
| 997 |
} else if (*p == '@') { |
| 998 |
return IsAtProcess(p) ? p : nullptr; |
| 999 |
} else { |
| 1000 |
return nullptr; |
| 1001 |
} |
| 1002 |
} |
| 1003 |
|
| 1004 |
std::optional<std::size_t> Prescanner::IsIncludeLine(const char *start) const { |
| 1005 |
const char *p{SkipWhiteSpace(start)}; |
| 1006 |
if (*p == '0' && inFixedForm_ && p == start + 5) { |
| 1007 |
// Accept " 0INCLUDE" in fixed form. |
| 1008 |
p = SkipWhiteSpace(p + 1); |
| 1009 |
} |
| 1010 |
for (const char *q{"include"}; *q; ++q) { |
| 1011 |
if (ToLowerCaseLetter(*p) != *q) { |
| 1012 |
return std::nullopt; |
| 1013 |
} |
| 1014 |
p = SkipWhiteSpace(p + 1); |
| 1015 |
} |
| 1016 |
if (IsDecimalDigit(*p)) { // accept & ignore a numeric kind prefix |
| 1017 |
for (p = SkipWhiteSpace(p + 1); IsDecimalDigit(*p); |
| 1018 |
p = SkipWhiteSpace(p + 1)) { |
| 1019 |
} |
| 1020 |
if (*p != '_') { |
| 1021 |
return std::nullopt; |
| 1022 |
} |
| 1023 |
p = SkipWhiteSpace(p + 1); |
| 1024 |
} |
| 1025 |
if (*p == '"' || *p == '\'') { |
| 1026 |
return {p - start}; |
| 1027 |
} |
| 1028 |
return std::nullopt; |
| 1029 |
} |
| 1030 |
|
| 1031 |
void Prescanner::FortranInclude(const char *firstQuote) { |
| 1032 |
const char *p{firstQuote}; |
| 1033 |
while (*p != '"' && *p != '\'') { |
| 1034 |
++p; |
| 1035 |
} |
| 1036 |
char quote{*p}; |
| 1037 |
std::string path; |
| 1038 |
for (++p; *p != '\n'; ++p) { |
| 1039 |
if (*p == quote) { |
| 1040 |
if (p[1] != quote) { |
| 1041 |
break; |
| 1042 |
} |
| 1043 |
++p; |
| 1044 |
} |
| 1045 |
path += *p; |
| 1046 |
} |
| 1047 |
if (*p != quote) { |
| 1048 |
Say(GetProvenanceRange(firstQuote, p), |
| 1049 |
"malformed path name string"_err_en_US); |
| 1050 |
return; |
| 1051 |
} |
| 1052 |
p = SkipWhiteSpace(p + 1); |
| 1053 |
if (*p != '\n' && *p != '!') { |
| 1054 |
const char *garbage{p}; |
| 1055 |
for (; *p != '\n' && *p != '!'; ++p) { |
| 1056 |
} |
| 1057 |
if (features_.ShouldWarn(common::UsageWarning::Scanning)) { |
| 1058 |
Say(GetProvenanceRange(garbage, p), |
| 1059 |
"excess characters after path name"_warn_en_US); |
| 1060 |
} |
| 1061 |
} |
| 1062 |
std::string buf; |
| 1063 |
llvm::raw_string_ostream error{buf}; |
| 1064 |
Provenance provenance{GetProvenance(nextLine_)}; |
| 1065 |
std::optional<std::string> prependPath; |
| 1066 |
if (const SourceFile * currentFile{allSources_.GetSourceFile(provenance)}) { |
| 1067 |
prependPath = DirectoryName(currentFile->path()); |
| 1068 |
} |
| 1069 |
const SourceFile *included{ |
| 1070 |
allSources_.Open(path, error, std::move(prependPath))}; |
| 1071 |
if (!included) { |
| 1072 |
Say(provenance, "INCLUDE: %s"_err_en_US, error.str()); |
| 1073 |
} else if (included->bytes() > 0) { |
| 1074 |
ProvenanceRange includeLineRange{ |
| 1075 |
provenance, static_cast<std::size_t>(p - nextLine_)}; |
| 1076 |
ProvenanceRange fileRange{ |
| 1077 |
allSources_.AddIncludedFile(*included, includeLineRange)}; |
| 1078 |
Prescanner{*this, /*isNestedInIncludeDirective=*/false} |
| 1079 |
.set_encoding(included->encoding()) |
| 1080 |
.Prescan(fileRange); |
| 1081 |
} |
| 1082 |
} |
| 1083 |
|
| 1084 |
const char *Prescanner::IsPreprocessorDirectiveLine(const char *start) const { |
| 1085 |
const char *p{start}; |
| 1086 |
for (; *p == ' '; ++p) { |
| 1087 |
} |
| 1088 |
if (*p == '#') { |
| 1089 |
if (inFixedForm_ && p == start + 5) { |
| 1090 |
return nullptr; |
| 1091 |
} |
| 1092 |
} else { |
| 1093 |
p = SkipWhiteSpace(p); |
| 1094 |
if (*p != '#') { |
| 1095 |
return nullptr; |
| 1096 |
} |
| 1097 |
} |
| 1098 |
return SkipWhiteSpace(p + 1); |
| 1099 |
} |
| 1100 |
|
| 1101 |
bool Prescanner::IsNextLinePreprocessorDirective() const { |
| 1102 |
return IsPreprocessorDirectiveLine(nextLine_) != nullptr; |
| 1103 |
} |
| 1104 |
|
| 1105 |
bool Prescanner::SkipCommentLine(bool afterAmpersand) { |
| 1106 |
if (IsAtEnd()) { |
| 1107 |
if (afterAmpersand && prescannerNesting_ > 0) { |
| 1108 |
// A continuation marker at the end of the last line in an |
| 1109 |
// include file inhibits the newline for that line. |
| 1110 |
SkipToEndOfLine(); |
| 1111 |
omitNewline_ = true; |
| 1112 |
} |
| 1113 |
return false; |
| 1114 |
} |
| 1115 |
auto lineClass{ClassifyLine(nextLine_)}; |
| 1116 |
if (lineClass.kind == LineClassification::Kind::Comment) { |
| 1117 |
NextLine(); |
| 1118 |
return true; |
| 1119 |
} else if (inPreprocessorDirective_) { |
| 1120 |
return false; |
| 1121 |
} else if (afterAmpersand && |
| 1122 |
(lineClass.kind == |
| 1123 |
LineClassification::Kind::ConditionalCompilationDirective || |
| 1124 |
lineClass.kind == LineClassification::Kind::DefinitionDirective || |
| 1125 |
lineClass.kind == LineClassification::Kind::PreprocessorDirective || |
| 1126 |
lineClass.kind == LineClassification::Kind::IncludeDirective || |
| 1127 |
lineClass.kind == LineClassification::Kind::IncludeLine)) { |
| 1128 |
SkipToEndOfLine(); |
| 1129 |
omitNewline_ = true; |
| 1130 |
skipLeadingAmpersand_ = true; |
| 1131 |
return false; |
| 1132 |
} else if (lineClass.kind == |
| 1133 |
LineClassification::Kind::ConditionalCompilationDirective || |
| 1134 |
lineClass.kind == LineClassification::Kind::PreprocessorDirective) { |
| 1135 |
// Allow conditional compilation directives (e.g., #ifdef) to affect |
| 1136 |
// continuation lines. |
| 1137 |
// Allow other preprocessor directives, too, except #include |
| 1138 |
// (when it does not follow '&'), #define, and #undef (because |
| 1139 |
// they cannot be allowed to affect preceding text on a |
| 1140 |
// continued line). |
| 1141 |
preprocessor_.Directive(TokenizePreprocessorDirective(), *this); |
| 1142 |
return true; |
| 1143 |
} else { |
| 1144 |
return false; |
| 1145 |
} |
| 1146 |
} |
| 1147 |
|
| 1148 |
const char *Prescanner::FixedFormContinuationLine(bool mightNeedSpace) { |
| 1149 |
if (IsAtEnd()) { |
| 1150 |
return nullptr; |
| 1151 |
} |
| 1152 |
tabInCurrentLine_ = false; |
| 1153 |
char col1{*nextLine_}; |
| 1154 |
if (IsFixedFormCommentChar(col1)) { |
| 1155 |
int j{1}; |
| 1156 |
if (InCompilerDirective()) { |
| 1157 |
// Must be a continued compiler directive. |
| 1158 |
for (; j < 5; ++j) { |
| 1159 |
char ch{directiveSentinel_[j - 1]}; |
| 1160 |
if (ch == '\0') { |
| 1161 |
break; |
| 1162 |
} |
| 1163 |
if (ch != ToLowerCaseLetter(nextLine_[j])) { |
| 1164 |
return nullptr; |
| 1165 |
} |
| 1166 |
} |
| 1167 |
} else if (features_.IsEnabled(LanguageFeature::OpenMP)) { |
| 1168 |
// Fixed Source Form Conditional Compilation Sentinels. |
| 1169 |
if (nextLine_[1] != '$') { |
| 1170 |
return nullptr; |
| 1171 |
} |
| 1172 |
j++; |
| 1173 |
} else { |
| 1174 |
return nullptr; |
| 1175 |
} |
| 1176 |
for (; j < 5; ++j) { |
| 1177 |
if (nextLine_[j] != ' ') { |
| 1178 |
return nullptr; |
| 1179 |
} |
| 1180 |
} |
| 1181 |
char col6{nextLine_[5]}; |
| 1182 |
if (col6 != '\n' && col6 != '\t' && col6 != ' ' && col6 != '0') { |
| 1183 |
if (nextLine_[6] != ' ' && mightNeedSpace) { |
| 1184 |
insertASpace_ = true; |
| 1185 |
} |
| 1186 |
return nextLine_ + 6; |
| 1187 |
} |
| 1188 |
return nullptr; |
| 1189 |
} else { |
| 1190 |
// Normal case: not in a compiler directive. |
| 1191 |
if (col1 == '&' && |
| 1192 |
features_.IsEnabled( |
| 1193 |
LanguageFeature::FixedFormContinuationWithColumn1Ampersand)) { |
| 1194 |
// Extension: '&' as continuation marker |
| 1195 |
if (features_.ShouldWarn( |
| 1196 |
LanguageFeature::FixedFormContinuationWithColumn1Ampersand)) { |
| 1197 |
Say(GetProvenance(nextLine_), "nonstandard usage"_port_en_US); |
| 1198 |
} |
| 1199 |
return nextLine_ + 1; |
| 1200 |
} |
| 1201 |
if (col1 == '\t' && nextLine_[1] >= '1' && nextLine_[1] <= '9') { |
| 1202 |
tabInCurrentLine_ = true; |
| 1203 |
return nextLine_ + 2; // VAX extension |
| 1204 |
} |
| 1205 |
if ((col1 == ' ' || |
| 1206 |
((col1 == 'D' || col1 == 'd') && |
| 1207 |
features_.IsEnabled(LanguageFeature::OldDebugLines))) && |
| 1208 |
nextLine_[1] == ' ' && nextLine_[2] == ' ' && nextLine_[3] == ' ' && |
| 1209 |
nextLine_[4] == ' ') { |
| 1210 |
char col6{nextLine_[5]}; |
| 1211 |
if (col6 != '\n' && col6 != '\t' && col6 != ' ' && col6 != '0') { |
| 1212 |
if ((col6 == 'i' || col6 == 'I') && IsIncludeLine(nextLine_)) { |
| 1213 |
// It's An INCLUDE line, not a continuation |
| 1214 |
} else { |
| 1215 |
return nextLine_ + 6; |
| 1216 |
} |
| 1217 |
} |
| 1218 |
} |
| 1219 |
if (IsImplicitContinuation()) { |
| 1220 |
return nextLine_; |
| 1221 |
} |
| 1222 |
} |
| 1223 |
return nullptr; // not a continuation line |
| 1224 |
} |
| 1225 |
|
| 1226 |
const char *Prescanner::FreeFormContinuationLine(bool ampersand) { |
| 1227 |
const char *p{nextLine_}; |
| 1228 |
if (p >= limit_) { |
| 1229 |
return nullptr; |
| 1230 |
} |
| 1231 |
p = SkipWhiteSpace(p); |
| 1232 |
if (InCompilerDirective()) { |
| 1233 |
if (*p++ != '!') { |
| 1234 |
return nullptr; |
| 1235 |
} |
| 1236 |
for (const char *s{directiveSentinel_}; *s != '\0'; ++p, ++s) { |
| 1237 |
if (*s != ToLowerCaseLetter(*p)) { |
| 1238 |
return nullptr; |
| 1239 |
} |
| 1240 |
} |
| 1241 |
p = SkipWhiteSpace(p); |
| 1242 |
if (*p == '&') { |
| 1243 |
if (!ampersand) { |
| 1244 |
insertASpace_ = true; |
| 1245 |
} |
| 1246 |
return p + 1; |
| 1247 |
} else if (ampersand) { |
| 1248 |
return p; |
| 1249 |
} else { |
| 1250 |
return nullptr; |
| 1251 |
} |
| 1252 |
} else { |
| 1253 |
if (*p == '&') { |
| 1254 |
return p + 1; |
| 1255 |
} else if (*p == '!' || *p == '\n' || *p == '#') { |
| 1256 |
return nullptr; |
| 1257 |
} else if (ampersand || IsImplicitContinuation()) { |
| 1258 |
if (continuationInCharLiteral_) { |
| 1259 |
// 'a'& -> 'a''b' == "a'b" |
| 1260 |
// 'b' |
| 1261 |
if (features_.ShouldWarn( |
| 1262 |
common::LanguageFeature::MiscSourceExtensions)) { |
| 1263 |
Say(GetProvenanceRange(p, p + 1), |
| 1264 |
"Character literal continuation line should have been preceded by '&'"_port_en_US); |
| 1265 |
} |
| 1266 |
} else if (p > nextLine_) { |
| 1267 |
--p; |
| 1268 |
} else { |
| 1269 |
insertASpace_ = true; |
| 1270 |
} |
| 1271 |
return p; |
| 1272 |
} else { |
| 1273 |
return nullptr; |
| 1274 |
} |
| 1275 |
} |
| 1276 |
} |
| 1277 |
|
| 1278 |
bool Prescanner::FixedFormContinuation(bool mightNeedSpace) { |
| 1279 |
// N.B. We accept '&' as a continuation indicator in fixed form, too, |
| 1280 |
// but not in a character literal. |
| 1281 |
if (*at_ == '&' && inCharLiteral_) { |
| 1282 |
return false; |
| 1283 |
} |
| 1284 |
do { |
| 1285 |
if (const char *cont{FixedFormContinuationLine(mightNeedSpace)}) { |
| 1286 |
BeginSourceLine(cont); |
| 1287 |
column_ = 7; |
| 1288 |
NextLine(); |
| 1289 |
return true; |
| 1290 |
} |
| 1291 |
} while (SkipCommentLine(false /* not after ampersand */)); |
| 1292 |
return false; |
| 1293 |
} |
| 1294 |
|
| 1295 |
bool Prescanner::FreeFormContinuation() { |
| 1296 |
const char *p{at_}; |
| 1297 |
bool ampersand{*p == '&'}; |
| 1298 |
if (ampersand) { |
| 1299 |
p = SkipWhiteSpace(p + 1); |
| 1300 |
} |
| 1301 |
if (*p != '\n') { |
| 1302 |
if (inCharLiteral_) { |
| 1303 |
return false; |
| 1304 |
} else if (*p == '!') { // & ! comment - ok |
| 1305 |
} else if (ampersand && isPossibleMacroCall_ && (*p == ',' || *p == ')')) { |
| 1306 |
return false; // allow & at end of a macro argument |
| 1307 |
} else if (features_.ShouldWarn(LanguageFeature::CruftAfterAmpersand)) { |
| 1308 |
Say(GetProvenance(p), "missing ! before comment after &"_warn_en_US); |
| 1309 |
} |
| 1310 |
} |
| 1311 |
do { |
| 1312 |
if (const char *cont{FreeFormContinuationLine(ampersand)}) { |
| 1313 |
BeginSourceLine(cont); |
| 1314 |
NextLine(); |
| 1315 |
return true; |
| 1316 |
} |
| 1317 |
} while (SkipCommentLine(ampersand)); |
| 1318 |
return false; |
| 1319 |
} |
| 1320 |
|
| 1321 |
// Implicit line continuation allows a preprocessor macro call with |
| 1322 |
// arguments to span multiple lines. |
| 1323 |
bool Prescanner::IsImplicitContinuation() const { |
| 1324 |
return !inPreprocessorDirective_ && !inCharLiteral_ && isPossibleMacroCall_ && |
| 1325 |
parenthesisNesting_ > 0 && !IsAtEnd() && |
| 1326 |
ClassifyLine(nextLine_).kind == LineClassification::Kind::Source; |
| 1327 |
} |
| 1328 |
|
| 1329 |
bool Prescanner::Continuation(bool mightNeedFixedFormSpace) { |
| 1330 |
if (disableSourceContinuation_) { |
| 1331 |
return false; |
| 1332 |
} else if (*at_ == '\n' || *at_ == '&') { |
| 1333 |
if (inFixedForm_) { |
| 1334 |
return FixedFormContinuation(mightNeedFixedFormSpace); |
| 1335 |
} else { |
| 1336 |
return FreeFormContinuation(); |
| 1337 |
} |
| 1338 |
} else if (*at_ == '\\' && at_ + 2 == nextLine_ && |
| 1339 |
backslashFreeFormContinuation_ && !inFixedForm_ && nextLine_ < limit_) { |
| 1340 |
// cpp-like handling of \ at end of a free form source line |
| 1341 |
BeginSourceLine(nextLine_); |
| 1342 |
NextLine(); |
| 1343 |
return true; |
| 1344 |
} else { |
| 1345 |
return false; |
| 1346 |
} |
| 1347 |
} |
| 1348 |
|
| 1349 |
std::optional<Prescanner::LineClassification> |
| 1350 |
Prescanner::IsFixedFormCompilerDirectiveLine(const char *start) const { |
| 1351 |
const char *p{start}; |
| 1352 |
char col1{*p++}; |
| 1353 |
if (!IsFixedFormCommentChar(col1)) { |
| 1354 |
return std::nullopt; |
| 1355 |
} |
| 1356 |
char sentinel[5], *sp{sentinel}; |
| 1357 |
int column{2}; |
| 1358 |
for (; column < 6; ++column, ++p) { |
| 1359 |
if (*p == ' ' || *p == '\n' || *p == '\t') { |
| 1360 |
break; |
| 1361 |
} |
| 1362 |
if (sp == sentinel + 1 && sentinel[0] == '$' && IsDecimalDigit(*p)) { |
| 1363 |
// OpenMP conditional compilation line: leave the label alone |
| 1364 |
break; |
| 1365 |
} |
| 1366 |
*sp++ = ToLowerCaseLetter(*p); |
| 1367 |
} |
| 1368 |
if (column == 6) { |
| 1369 |
if (*p == ' ' || *p == '\t' || *p == '0') { |
| 1370 |
++p; |
| 1371 |
} else { |
| 1372 |
// This is a Continuation line, not an initial directive line. |
| 1373 |
return std::nullopt; |
| 1374 |
} |
| 1375 |
} |
| 1376 |
if (sp == sentinel) { |
| 1377 |
return std::nullopt; |
| 1378 |
} |
| 1379 |
*sp = '\0'; |
| 1380 |
if (const char *ss{IsCompilerDirectiveSentinel( |
| 1381 |
sentinel, static_cast<std::size_t>(sp - sentinel))}) { |
| 1382 |
std::size_t payloadOffset = p - start; |
| 1383 |
return {LineClassification{ |
| 1384 |
LineClassification::Kind::CompilerDirective, payloadOffset, ss}}; |
| 1385 |
} |
| 1386 |
return std::nullopt; |
| 1387 |
} |
| 1388 |
|
| 1389 |
std::optional<Prescanner::LineClassification> |
| 1390 |
Prescanner::IsFreeFormCompilerDirectiveLine(const char *start) const { |
| 1391 |
if (const char *p{SkipWhiteSpace(start)}; p && *p++ == '!') { |
| 1392 |
if (auto maybePair{IsCompilerDirectiveSentinel(p)}) { |
| 1393 |
auto offset{static_cast<std::size_t>(maybePair->second - start)}; |
| 1394 |
return {LineClassification{LineClassification::Kind::CompilerDirective, |
| 1395 |
offset, maybePair->first}}; |
| 1396 |
} |
| 1397 |
} |
| 1398 |
return std::nullopt; |
| 1399 |
} |
| 1400 |
|
| 1401 |
Prescanner &Prescanner::AddCompilerDirectiveSentinel(const std::string &dir) { |
| 1402 |
std::uint64_t packed{0}; |
| 1403 |
for (char ch : dir) { |
| 1404 |
packed = (packed << 8) | (ToLowerCaseLetter(ch) & 0xff); |
| 1405 |
} |
| 1406 |
compilerDirectiveBloomFilter_.set(packed % prime1); |
| 1407 |
compilerDirectiveBloomFilter_.set(packed % prime2); |
| 1408 |
compilerDirectiveSentinels_.insert(dir); |
| 1409 |
return *this; |
| 1410 |
} |
| 1411 |
|
| 1412 |
const char *Prescanner::IsCompilerDirectiveSentinel( |
| 1413 |
const char *sentinel, std::size_t len) const { |
| 1414 |
std::uint64_t packed{0}; |
| 1415 |
for (std::size_t j{0}; j < len; ++j) { |
| 1416 |
packed = (packed << 8) | (sentinel[j] & 0xff); |
| 1417 |
} |
| 1418 |
if (len == 0 || !compilerDirectiveBloomFilter_.test(packed % prime1) || |
| 1419 |
!compilerDirectiveBloomFilter_.test(packed % prime2)) { |
| 1420 |
return nullptr; |
| 1421 |
} |
| 1422 |
const auto iter{compilerDirectiveSentinels_.find(std::string(sentinel, len))}; |
| 1423 |
return iter == compilerDirectiveSentinels_.end() ? nullptr : iter->c_str(); |
| 1424 |
} |
| 1425 |
|
| 1426 |
const char *Prescanner::IsCompilerDirectiveSentinel(CharBlock token) const { |
| 1427 |
const char *p{token.begin()}; |
| 1428 |
const char *end{p + token.size()}; |
| 1429 |
while (p < end && (*p == ' ' || *p == '\n')) { |
| 1430 |
++p; |
| 1431 |
} |
| 1432 |
if (p < end && *p == '!') { |
| 1433 |
++p; |
| 1434 |
} |
| 1435 |
while (end > p && (end[-1] == ' ' || end[-1] == '\t')) { |
| 1436 |
--end; |
| 1437 |
} |
| 1438 |
return end > p && IsCompilerDirectiveSentinel(p, end - p) ? p : nullptr; |
| 1439 |
} |
| 1440 |
|
| 1441 |
std::optional<std::pair<const char *, const char *>> |
| 1442 |
Prescanner::IsCompilerDirectiveSentinel(const char *p) const { |
| 1443 |
char sentinel[8]; |
| 1444 |
for (std::size_t j{0}; j + 1 < sizeof sentinel && *p != '\n'; ++p, ++j) { |
| 1445 |
if (*p == ' ' || *p == '\t' || *p == '&') { |
| 1446 |
if (j > 0) { |
| 1447 |
sentinel[j] = '\0'; |
| 1448 |
p = SkipWhiteSpace(p + 1); |
| 1449 |
if (*p != '!') { |
| 1450 |
if (const char *sp{IsCompilerDirectiveSentinel(sentinel, j)}) { |
| 1451 |
return std::make_pair(sp, p); |
| 1452 |
} |
| 1453 |
} |
| 1454 |
} |
| 1455 |
break; |
| 1456 |
} else { |
| 1457 |
sentinel[j] = ToLowerCaseLetter(*p); |
| 1458 |
} |
| 1459 |
} |
| 1460 |
return std::nullopt; |
| 1461 |
} |
| 1462 |
|
| 1463 |
constexpr bool IsDirective(const char *match, const char *dir) { |
| 1464 |
for (; *match; ++match) { |
| 1465 |
if (*match != ToLowerCaseLetter(*dir++)) { |
| 1466 |
return false; |
| 1467 |
} |
| 1468 |
} |
| 1469 |
return true; |
| 1470 |
} |
| 1471 |
|
| 1472 |
Prescanner::LineClassification Prescanner::ClassifyLine( |
| 1473 |
const char *start) const { |
| 1474 |
if (inFixedForm_) { |
| 1475 |
if (std::optional<LineClassification> lc{ |
| 1476 |
IsFixedFormCompilerDirectiveLine(start)}) { |
| 1477 |
return std::move(*lc); |
| 1478 |
} |
| 1479 |
if (IsFixedFormCommentLine(start)) { |
| 1480 |
return {LineClassification::Kind::Comment}; |
| 1481 |
} |
| 1482 |
} else { |
| 1483 |
if (std::optional<LineClassification> lc{ |
| 1484 |
IsFreeFormCompilerDirectiveLine(start)}) { |
| 1485 |
return std::move(*lc); |
| 1486 |
} |
| 1487 |
if (const char *bang{IsFreeFormComment(start)}) { |
| 1488 |
return {LineClassification::Kind::Comment, |
| 1489 |
static_cast<std::size_t>(bang - start)}; |
| 1490 |
} |
| 1491 |
} |
| 1492 |
if (std::optional<std::size_t> quoteOffset{IsIncludeLine(start)}) { |
| 1493 |
return {LineClassification::Kind::IncludeLine, *quoteOffset}; |
| 1494 |
} |
| 1495 |
if (const char *dir{IsPreprocessorDirectiveLine(start)}) { |
| 1496 |
if (IsDirective("if", dir) || IsDirective("elif", dir) || |
| 1497 |
IsDirective("else", dir) || IsDirective("endif", dir)) { |
| 1498 |
return {LineClassification::Kind::ConditionalCompilationDirective}; |
| 1499 |
} else if (IsDirective("include", dir)) { |
| 1500 |
return {LineClassification::Kind::IncludeDirective}; |
| 1501 |
} else if (IsDirective("define", dir) || IsDirective("undef", dir)) { |
| 1502 |
return {LineClassification::Kind::DefinitionDirective}; |
| 1503 |
} else { |
| 1504 |
return {LineClassification::Kind::PreprocessorDirective}; |
| 1505 |
} |
| 1506 |
} |
| 1507 |
return {LineClassification::Kind::Source}; |
| 1508 |
} |
| 1509 |
|
| 1510 |
Prescanner::LineClassification Prescanner::ClassifyLine( |
| 1511 |
TokenSequence &tokens, Provenance newlineProvenance) const { |
| 1512 |
// Append a newline temporarily. |
| 1513 |
tokens.PutNextTokenChar('\n', newlineProvenance); |
| 1514 |
tokens.CloseToken(); |
| 1515 |
const char *ppd{tokens.ToCharBlock().begin()}; |
| 1516 |
LineClassification classification{ClassifyLine(ppd)}; |
| 1517 |
tokens.pop_back(); // remove the newline |
| 1518 |
return classification; |
| 1519 |
} |
| 1520 |
|
| 1521 |
void Prescanner::SourceFormChange(std::string &&dir) { |
| 1522 |
if (dir == "!dir$ free") { |
| 1523 |
inFixedForm_ = false; |
| 1524 |
} else if (dir == "!dir$ fixed") { |
| 1525 |
inFixedForm_ = true; |
| 1526 |
} |
| 1527 |
} |
| 1528 |
|
| 1529 |
// Acquire and append compiler directive continuation lines to |
| 1530 |
// the tokens that constitute a compiler directive, even when those |
| 1531 |
// directive continuation lines are the result of macro expansion. |
| 1532 |
// (Not used when neither the original compiler directive line nor |
| 1533 |
// the directive continuation line result from preprocessing; regular |
| 1534 |
// line continuation during tokenization handles that normal case.) |
| 1535 |
bool Prescanner::CompilerDirectiveContinuation( |
| 1536 |
TokenSequence &tokens, const char *origSentinel) { |
| 1537 |
if (inFixedForm_ || tokens.empty() || |
| 1538 |
tokens.TokenAt(tokens.SizeInTokens() - 1) != "&") { |
| 1539 |
return false; |
| 1540 |
} |
| 1541 |
LineClassification followingLine{ClassifyLine(nextLine_)}; |
| 1542 |
if (followingLine.kind == LineClassification::Kind::Comment) { |
| 1543 |
nextLine_ += followingLine.payloadOffset; // advance to '!' or newline |
| 1544 |
NextLine(); |
| 1545 |
return true; |
| 1546 |
} |
| 1547 |
CHECK(origSentinel != nullptr); |
| 1548 |
directiveSentinel_ = origSentinel; // so InCompilerDirective() is true |
| 1549 |
const char *nextContinuation{ |
| 1550 |
followingLine.kind == LineClassification::Kind::CompilerDirective |
| 1551 |
? FreeFormContinuationLine(true) |
| 1552 |
: nullptr}; |
| 1553 |
if (!nextContinuation && |
| 1554 |
followingLine.kind != LineClassification::Kind::Source) { |
| 1555 |
return false; |
| 1556 |
} |
| 1557 |
auto origNextLine{nextLine_}; |
| 1558 |
BeginSourceLine(nextLine_); |
| 1559 |
NextLine(); |
| 1560 |
if (nextContinuation) { |
| 1561 |
// What follows is !DIR$ & xxx; skip over the & so that it |
| 1562 |
// doesn't cause a spurious continuation. |
| 1563 |
at_ = nextContinuation; |
| 1564 |
} else { |
| 1565 |
// What follows looks like a source line before macro expansion, |
| 1566 |
// but might become a directive continuation afterwards. |
| 1567 |
SkipSpaces(); |
| 1568 |
} |
| 1569 |
TokenSequence followingTokens; |
| 1570 |
while (NextToken(followingTokens)) { |
| 1571 |
} |
| 1572 |
if (auto followingPrepro{ |
| 1573 |
preprocessor_.MacroReplacement(followingTokens, *this)}) { |
| 1574 |
followingTokens = std::move(*followingPrepro); |
| 1575 |
} |
| 1576 |
followingTokens.RemoveRedundantBlanks(); |
| 1577 |
std::size_t startAt{0}; |
| 1578 |
std::size_t following{followingTokens.SizeInTokens()}; |
| 1579 |
bool ok{false}; |
| 1580 |
if (nextContinuation) { |
| 1581 |
ok = true; |
| 1582 |
} else { |
| 1583 |
startAt = 2; |
| 1584 |
if (startAt < following && followingTokens.TokenAt(0) == "!") { |
| 1585 |
CharBlock sentinel{followingTokens.TokenAt(1)}; |
| 1586 |
if (!sentinel.empty() && |
| 1587 |
std::memcmp(sentinel.begin(), origSentinel, sentinel.size()) == 0) { |
| 1588 |
ok = true; |
| 1589 |
while ( |
| 1590 |
startAt < following && followingTokens.TokenAt(startAt).IsBlank()) { |
| 1591 |
++startAt; |
| 1592 |
} |
| 1593 |
if (startAt < following && followingTokens.TokenAt(startAt) == "&") { |
| 1594 |
++startAt; |
| 1595 |
} |
| 1596 |
} |
| 1597 |
} |
| 1598 |
} |
| 1599 |
if (ok) { |
| 1600 |
tokens.pop_back(); // delete original '&' |
| 1601 |
tokens.Put(followingTokens, startAt, following - startAt); |
| 1602 |
tokens.RemoveRedundantBlanks(); |
| 1603 |
} else { |
| 1604 |
nextLine_ = origNextLine; |
| 1605 |
} |
| 1606 |
return ok; |
| 1607 |
} |
| 1608 |
|
| 1609 |
// Similar, but for source line continuation after macro replacement. |
| 1610 |
bool Prescanner::SourceLineContinuation(TokenSequence &tokens) { |
| 1611 |
if (!inFixedForm_ && !tokens.empty() && |
| 1612 |
tokens.TokenAt(tokens.SizeInTokens() - 1) == "&") { |
| 1613 |
LineClassification followingLine{ClassifyLine(nextLine_)}; |
| 1614 |
if (followingLine.kind == LineClassification::Kind::Comment) { |
| 1615 |
nextLine_ += followingLine.payloadOffset; // advance to '!' or newline |
| 1616 |
NextLine(); |
| 1617 |
return true; |
| 1618 |
} else if (const char *nextContinuation{FreeFormContinuationLine(true)}) { |
| 1619 |
BeginSourceLine(nextLine_); |
| 1620 |
NextLine(); |
| 1621 |
TokenSequence followingTokens; |
| 1622 |
at_ = nextContinuation; |
| 1623 |
while (NextToken(followingTokens)) { |
| 1624 |
} |
| 1625 |
if (auto followingPrepro{ |
| 1626 |
preprocessor_.MacroReplacement(followingTokens, *this)}) { |
| 1627 |
followingTokens = std::move(*followingPrepro); |
| 1628 |
} |
| 1629 |
followingTokens.RemoveRedundantBlanks(); |
| 1630 |
tokens.pop_back(); // delete original '&' |
| 1631 |
tokens.Put(followingTokens); |
| 1632 |
return true; |
| 1633 |
} |
| 1634 |
} |
| 1635 |
return false; |
| 1636 |
} |
| 1637 |
} // namespace Fortran::parser |