2020-02-25 23:11:52 +08:00
|
|
|
//===-- lib/Parser/prescan.cpp --------------------------------------------===//
|
2018-05-02 03:50:34 +08:00
|
|
|
//
|
2019-12-21 04:52:07 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-05-02 03:50:34 +08:00
|
|
|
//
|
2020-01-11 04:12:03 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2018-05-02 03:50:34 +08:00
|
|
|
|
2018-02-08 08:27:36 +08:00
|
|
|
#include "prescan.h"
|
2018-02-14 04:50:47 +08:00
|
|
|
#include "preprocessor.h"
|
|
|
|
#include "token-sequence.h"
|
2020-02-25 23:11:52 +08:00
|
|
|
#include "flang/Common/idioms.h"
|
|
|
|
#include "flang/Parser/characters.h"
|
|
|
|
#include "flang/Parser/message.h"
|
|
|
|
#include "flang/Parser/source.h"
|
2020-02-28 23:11:03 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-03-21 01:59:07 +08:00
|
|
|
#include <cstddef>
|
2018-01-31 03:54:47 +08:00
|
|
|
#include <cstring>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2018-05-03 04:48:12 +08:00
|
|
|
namespace Fortran::parser {
|
2018-01-31 03:54:47 +08:00
|
|
|
|
2019-11-07 03:15:03 +08:00
|
|
|
using common::LanguageFeature;
|
|
|
|
|
2018-04-07 05:20:29 +08:00
|
|
|
static constexpr int maxPrescannerNesting{100};
|
|
|
|
|
2018-07-19 02:19:21 +08:00
|
|
|
Prescanner::Prescanner(Messages &messages, CookedSource &cooked,
|
2019-11-07 03:15:03 +08:00
|
|
|
Preprocessor &preprocessor, common::LanguageFeatureControl lfc)
|
2020-03-29 12:00:16 +08:00
|
|
|
: messages_{messages}, cooked_{cooked}, preprocessor_{preprocessor},
|
|
|
|
features_{lfc}, encoding_{cooked.allSources().encoding()} {}
|
2018-02-13 06:43:16 +08:00
|
|
|
|
2018-02-14 06:22:08 +08:00
|
|
|
Prescanner::Prescanner(const Prescanner &that)
|
2020-03-29 12:00:16 +08:00
|
|
|
: messages_{that.messages_}, cooked_{that.cooked_},
|
|
|
|
preprocessor_{that.preprocessor_}, features_{that.features_},
|
|
|
|
inFixedForm_{that.inFixedForm_},
|
|
|
|
fixedFormColumnLimit_{that.fixedFormColumnLimit_},
|
|
|
|
encoding_{that.encoding_}, prescannerNesting_{that.prescannerNesting_ +
|
|
|
|
1},
|
|
|
|
skipLeadingAmpersand_{that.skipLeadingAmpersand_},
|
|
|
|
compilerDirectiveBloomFilter_{that.compilerDirectiveBloomFilter_},
|
|
|
|
compilerDirectiveSentinels_{that.compilerDirectiveSentinels_} {}
|
2018-02-14 06:22:08 +08:00
|
|
|
|
2018-05-11 04:48:55 +08:00
|
|
|
static inline constexpr bool IsFixedFormCommentChar(char ch) {
|
|
|
|
return ch == '!' || ch == '*' || ch == 'C' || ch == 'c';
|
|
|
|
}
|
|
|
|
|
|
|
|
static void NormalizeCompilerDirectiveCommentMarker(TokenSequence &dir) {
|
|
|
|
char *p{dir.GetMutableCharData()};
|
|
|
|
char *limit{p + dir.SizeInChars()};
|
2018-03-24 06:14:52 +08:00
|
|
|
for (; p < limit; ++p) {
|
|
|
|
if (*p != ' ') {
|
2018-05-11 04:48:55 +08:00
|
|
|
CHECK(IsFixedFormCommentChar(*p));
|
2018-03-24 06:14:52 +08:00
|
|
|
*p = '!';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-10-03 06:48:20 +08:00
|
|
|
DIE("compiler directive all blank");
|
2018-03-24 06:14:52 +08:00
|
|
|
}
|
|
|
|
|
2018-04-03 06:51:04 +08:00
|
|
|
void Prescanner::Prescan(ProvenanceRange range) {
|
2018-03-24 06:14:52 +08:00
|
|
|
AllSources &allSources{cooked_.allSources()};
|
2018-02-28 06:02:10 +08:00
|
|
|
startProvenance_ = range.start();
|
2018-03-21 01:59:07 +08:00
|
|
|
std::size_t offset{0};
|
2018-03-24 06:14:52 +08:00
|
|
|
const SourceFile *source{allSources.GetSourceFile(startProvenance_, &offset)};
|
2019-11-10 01:29:31 +08:00
|
|
|
CHECK(source);
|
2020-02-27 21:42:56 +08:00
|
|
|
start_ = source->content().data() + offset;
|
2018-02-16 02:42:36 +08:00
|
|
|
limit_ = start_ + range.size();
|
2019-06-12 03:06:18 +08:00
|
|
|
nextLine_ = start_;
|
2018-03-24 06:14:52 +08:00
|
|
|
const bool beganInFixedForm{inFixedForm_};
|
2018-04-07 05:20:29 +08:00
|
|
|
if (prescannerNesting_ > maxPrescannerNesting) {
|
2018-08-09 02:29:05 +08:00
|
|
|
Say(GetProvenance(start_),
|
|
|
|
"too many nested INCLUDE/#include files, possibly circular"_err_en_US);
|
2018-04-07 05:20:29 +08:00
|
|
|
return;
|
|
|
|
}
|
2019-06-12 03:06:18 +08:00
|
|
|
while (nextLine_ < limit_) {
|
2018-03-24 06:14:52 +08:00
|
|
|
Statement();
|
|
|
|
}
|
|
|
|
if (inFixedForm_ != beganInFixedForm) {
|
|
|
|
std::string dir{"!dir$ "};
|
|
|
|
if (beganInFixedForm) {
|
|
|
|
dir += "fixed";
|
|
|
|
} else {
|
|
|
|
dir += "free";
|
2018-01-31 07:22:26 +08:00
|
|
|
}
|
2018-03-24 06:14:52 +08:00
|
|
|
dir += '\n';
|
|
|
|
TokenSequence tokens{dir, allSources.AddCompilerInsertion(dir).start()};
|
2018-05-12 02:15:20 +08:00
|
|
|
tokens.Emit(cooked_);
|
2018-03-24 06:14:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Prescanner::Statement() {
|
|
|
|
TokenSequence tokens;
|
2019-06-12 03:06:18 +08:00
|
|
|
LineClassification line{ClassifyLine(nextLine_)};
|
2018-03-24 06:14:52 +08:00
|
|
|
switch (line.kind) {
|
2019-03-16 04:57:41 +08:00
|
|
|
case LineClassification::Kind::Comment:
|
2020-03-29 12:00:16 +08:00
|
|
|
nextLine_ += line.payloadOffset; // advance to '!' or newline
|
2019-03-16 04:57:41 +08:00
|
|
|
NextLine();
|
|
|
|
return;
|
2019-03-15 03:19:26 +08:00
|
|
|
case LineClassification::Kind::IncludeLine:
|
2019-06-12 03:06:18 +08:00
|
|
|
FortranInclude(nextLine_ + line.payloadOffset);
|
2018-03-24 06:14:52 +08:00
|
|
|
NextLine();
|
|
|
|
return;
|
2018-05-03 07:09:12 +08:00
|
|
|
case LineClassification::Kind::ConditionalCompilationDirective:
|
2019-03-15 03:19:26 +08:00
|
|
|
case LineClassification::Kind::IncludeDirective:
|
2019-06-19 04:46:54 +08:00
|
|
|
case LineClassification::Kind::DefinitionDirective:
|
2018-03-24 06:14:52 +08:00
|
|
|
case LineClassification::Kind::PreprocessorDirective:
|
2018-04-10 02:47:00 +08:00
|
|
|
preprocessor_.Directive(TokenizePreprocessorDirective(), this);
|
2018-03-24 06:14:52 +08:00
|
|
|
return;
|
|
|
|
case LineClassification::Kind::CompilerDirective:
|
|
|
|
directiveSentinel_ = line.sentinel;
|
2018-07-26 02:44:20 +08:00
|
|
|
CHECK(InCompilerDirective());
|
2018-03-24 06:14:52 +08:00
|
|
|
BeginSourceLineAndAdvance();
|
|
|
|
if (inFixedForm_) {
|
2018-05-11 04:48:55 +08:00
|
|
|
CHECK(IsFixedFormCommentChar(*at_));
|
2018-03-24 06:14:52 +08:00
|
|
|
} else {
|
|
|
|
while (*at_ == ' ' || *at_ == '\t') {
|
2018-05-10 06:32:22 +08:00
|
|
|
++at_, ++column_;
|
2018-03-24 06:14:52 +08:00
|
|
|
}
|
|
|
|
CHECK(*at_ == '!');
|
|
|
|
}
|
2018-07-26 02:44:20 +08:00
|
|
|
if (directiveSentinel_[0] == '$' && directiveSentinel_[1] == '\0') {
|
|
|
|
// OpenMP conditional compilation line. Remove the sentinel and then
|
|
|
|
// treat the line as if it were normal source.
|
|
|
|
at_ += 2, column_ += 2;
|
|
|
|
if (inFixedForm_) {
|
|
|
|
LabelField(tokens);
|
|
|
|
} else {
|
|
|
|
SkipSpaces();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Compiler directive. Emit normalized sentinel.
|
|
|
|
EmitChar(tokens, '!');
|
2018-05-10 06:32:22 +08:00
|
|
|
++at_, ++column_;
|
2018-07-26 02:44:20 +08:00
|
|
|
for (const char *sp{directiveSentinel_}; *sp != '\0';
|
|
|
|
++sp, ++at_, ++column_) {
|
|
|
|
EmitChar(tokens, *sp);
|
|
|
|
}
|
|
|
|
if (*at_ == ' ') {
|
|
|
|
EmitChar(tokens, ' ');
|
|
|
|
++at_, ++column_;
|
|
|
|
}
|
|
|
|
tokens.CloseToken();
|
2018-05-10 06:32:22 +08:00
|
|
|
}
|
2018-03-24 06:14:52 +08:00
|
|
|
break;
|
|
|
|
case LineClassification::Kind::Source:
|
2018-01-31 03:54:47 +08:00
|
|
|
BeginSourceLineAndAdvance();
|
|
|
|
if (inFixedForm_) {
|
2018-05-11 04:48:55 +08:00
|
|
|
LabelField(tokens);
|
2019-03-15 04:53:35 +08:00
|
|
|
} else if (skipLeadingAmpersand_) {
|
|
|
|
skipLeadingAmpersand_ = false;
|
|
|
|
const char *p{SkipWhiteSpace(at_)};
|
|
|
|
if (p < limit_ && *p == '&') {
|
|
|
|
column_ += ++p - at_;
|
|
|
|
at_ = p;
|
|
|
|
}
|
2018-01-31 03:54:47 +08:00
|
|
|
} else {
|
|
|
|
SkipSpaces();
|
|
|
|
}
|
2018-03-24 06:14:52 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-05-11 04:48:55 +08:00
|
|
|
while (NextToken(tokens)) {
|
2018-03-24 06:14:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Provenance newlineProvenance{GetCurrentProvenance()};
|
|
|
|
if (std::optional<TokenSequence> preprocessed{
|
|
|
|
preprocessor_.MacroReplacement(tokens, *this)}) {
|
2019-06-15 06:44:32 +08:00
|
|
|
// Reprocess the preprocessed line. Append a newline temporarily.
|
2018-03-24 06:14:52 +08:00
|
|
|
preprocessed->PutNextTokenChar('\n', newlineProvenance);
|
|
|
|
preprocessed->CloseToken();
|
|
|
|
const char *ppd{preprocessed->ToCharBlock().begin()};
|
|
|
|
LineClassification ppl{ClassifyLine(ppd)};
|
2020-03-29 12:00:16 +08:00
|
|
|
preprocessed->RemoveLastToken(); // remove the newline
|
2018-03-24 06:14:52 +08:00
|
|
|
switch (ppl.kind) {
|
2020-03-29 12:00:16 +08:00
|
|
|
case LineClassification::Kind::Comment:
|
|
|
|
break;
|
2019-03-15 03:19:26 +08:00
|
|
|
case LineClassification::Kind::IncludeLine:
|
2018-03-24 06:14:52 +08:00
|
|
|
FortranInclude(ppd + ppl.payloadOffset);
|
|
|
|
break;
|
2018-05-03 07:09:12 +08:00
|
|
|
case LineClassification::Kind::ConditionalCompilationDirective:
|
2019-03-15 03:19:26 +08:00
|
|
|
case LineClassification::Kind::IncludeDirective:
|
2019-06-19 04:46:54 +08:00
|
|
|
case LineClassification::Kind::DefinitionDirective:
|
2018-03-24 06:14:52 +08:00
|
|
|
case LineClassification::Kind::PreprocessorDirective:
|
2018-08-09 02:29:05 +08:00
|
|
|
Say(preprocessed->GetProvenanceRange(),
|
2019-06-15 06:44:32 +08:00
|
|
|
"Preprocessed line resembles a preprocessor directive"_en_US);
|
2018-05-12 02:15:20 +08:00
|
|
|
preprocessed->ToLowerCase().Emit(cooked_);
|
2018-03-24 06:14:52 +08:00
|
|
|
break;
|
|
|
|
case LineClassification::Kind::CompilerDirective:
|
2018-05-12 03:20:00 +08:00
|
|
|
if (preprocessed->HasRedundantBlanks()) {
|
|
|
|
preprocessed->RemoveRedundantBlanks();
|
|
|
|
}
|
2018-05-11 04:48:55 +08:00
|
|
|
NormalizeCompilerDirectiveCommentMarker(*preprocessed);
|
2018-03-24 06:14:52 +08:00
|
|
|
preprocessed->ToLowerCase();
|
|
|
|
SourceFormChange(preprocessed->ToString());
|
2019-05-16 03:35:36 +08:00
|
|
|
preprocessed->ClipComment(true /* skip first ! */).Emit(cooked_);
|
2018-03-24 06:14:52 +08:00
|
|
|
break;
|
|
|
|
case LineClassification::Kind::Source:
|
2018-05-12 03:20:00 +08:00
|
|
|
if (inFixedForm_) {
|
2018-05-12 03:44:56 +08:00
|
|
|
if (preprocessed->HasBlanks(/*after column*/ 6)) {
|
|
|
|
preprocessed->RemoveBlanks(/*after column*/ 6);
|
2018-05-12 03:20:00 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (preprocessed->HasRedundantBlanks()) {
|
|
|
|
preprocessed->RemoveRedundantBlanks();
|
|
|
|
}
|
|
|
|
}
|
2019-05-11 07:04:10 +08:00
|
|
|
preprocessed->ToLowerCase().ClipComment().Emit(cooked_);
|
2018-03-24 06:14:52 +08:00
|
|
|
break;
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
2018-03-24 06:14:52 +08:00
|
|
|
} else {
|
|
|
|
tokens.ToLowerCase();
|
|
|
|
if (line.kind == LineClassification::Kind::CompilerDirective) {
|
|
|
|
SourceFormChange(tokens.ToString());
|
|
|
|
}
|
2018-05-12 02:15:20 +08:00
|
|
|
tokens.Emit(cooked_);
|
2019-03-15 03:19:26 +08:00
|
|
|
}
|
|
|
|
if (omitNewline_) {
|
|
|
|
omitNewline_ = false;
|
|
|
|
} else {
|
2018-03-24 06:14:52 +08:00
|
|
|
cooked_.Put('\n', newlineProvenance);
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
2018-03-24 06:14:52 +08:00
|
|
|
directiveSentinel_ = nullptr;
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
|
|
|
|
2018-03-17 07:58:35 +08:00
|
|
|
TokenSequence Prescanner::TokenizePreprocessorDirective() {
|
2019-06-12 03:06:18 +08:00
|
|
|
CHECK(nextLine_ < limit_ && !inPreprocessorDirective_);
|
2018-03-17 07:58:35 +08:00
|
|
|
inPreprocessorDirective_ = true;
|
2018-01-31 03:54:47 +08:00
|
|
|
BeginSourceLineAndAdvance();
|
|
|
|
TokenSequence tokens;
|
2018-05-11 04:48:55 +08:00
|
|
|
while (NextToken(tokens)) {
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
2018-03-17 07:13:49 +08:00
|
|
|
inPreprocessorDirective_ = false;
|
2018-04-10 03:52:52 +08:00
|
|
|
return tokens;
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Prescanner::NextLine() {
|
2019-06-12 03:06:18 +08:00
|
|
|
void *vstart{static_cast<void *>(const_cast<char *>(nextLine_))};
|
|
|
|
void *v{std::memchr(vstart, '\n', limit_ - nextLine_)};
|
2019-11-10 01:29:31 +08:00
|
|
|
if (!v) {
|
2019-06-12 03:06:18 +08:00
|
|
|
nextLine_ = limit_;
|
2018-01-31 03:54:47 +08:00
|
|
|
} else {
|
|
|
|
const char *nl{const_cast<const char *>(static_cast<char *>(v))};
|
2019-06-12 03:06:18 +08:00
|
|
|
nextLine_ = nl + 1;
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-26 02:44:20 +08:00
|
|
|
void Prescanner::LabelField(TokenSequence &token, int outCol) {
|
2020-07-22 08:25:46 +08:00
|
|
|
bool badLabel{false};
|
2018-02-10 06:04:11 +08:00
|
|
|
for (; *at_ != '\n' && column_ <= 6; ++at_) {
|
2018-01-31 03:54:47 +08:00
|
|
|
if (*at_ == '\t') {
|
2018-01-31 07:22:26 +08:00
|
|
|
++at_;
|
2018-02-10 06:04:11 +08:00
|
|
|
column_ = 7;
|
2018-01-31 03:54:47 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*at_ != ' ' &&
|
2020-03-29 12:00:16 +08:00
|
|
|
!(*at_ == '0' && column_ == 6)) { // '0' in column 6 becomes space
|
2018-02-10 06:04:11 +08:00
|
|
|
EmitChar(token, *at_);
|
2020-07-22 08:25:46 +08:00
|
|
|
if (!IsDecimalDigit(*at_) && !badLabel) {
|
|
|
|
Say(GetProvenance(at_),
|
|
|
|
"Character in fixed-form label field must be a digit"_en_US);
|
|
|
|
badLabel = true;
|
|
|
|
}
|
2018-01-31 03:54:47 +08:00
|
|
|
++outCol;
|
|
|
|
}
|
2018-02-10 06:04:11 +08:00
|
|
|
++column_;
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
2018-01-31 07:22:26 +08:00
|
|
|
if (outCol > 1) {
|
2018-05-11 04:48:55 +08:00
|
|
|
token.CloseToken();
|
2018-01-31 07:22:26 +08:00
|
|
|
}
|
2020-06-27 03:58:05 +08:00
|
|
|
SkipToNextSignificantCharacter();
|
2020-07-22 08:25:46 +08:00
|
|
|
if (IsDecimalDigit(*at_)) {
|
|
|
|
Say(GetProvenance(at_),
|
|
|
|
"Label digit is not in fixed-form label field"_en_US);
|
|
|
|
}
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
|
|
|
|
2018-05-04 02:09:04 +08:00
|
|
|
void Prescanner::SkipToEndOfLine() {
|
|
|
|
while (*at_ != '\n') {
|
|
|
|
++at_, ++column_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-12 03:06:18 +08:00
|
|
|
bool Prescanner::MustSkipToEndOfLine() const {
|
|
|
|
if (inFixedForm_ && column_ > fixedFormColumnLimit_ && !tabInCurrentLine_) {
|
2020-03-29 12:00:16 +08:00
|
|
|
return true; // skip over ignored columns in right margin (73:80)
|
2019-06-12 03:06:18 +08:00
|
|
|
} else if (*at_ == '!' && !inCharLiteral_) {
|
2020-03-29 12:00:16 +08:00
|
|
|
return true; // inline comment goes to end of source line
|
2019-06-12 03:06:18 +08:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 03:54:47 +08:00
|
|
|
void Prescanner::NextChar() {
|
2018-02-14 04:24:54 +08:00
|
|
|
CHECK(*at_ != '\n');
|
2018-05-04 02:09:04 +08:00
|
|
|
++at_, ++column_;
|
2019-06-13 06:26:37 +08:00
|
|
|
while (at_[0] == '\xef' && at_[1] == '\xbb' && at_[2] == '\xbf') {
|
|
|
|
// UTF-8 byte order mark - treat this file as UTF-8
|
|
|
|
at_ += 3;
|
|
|
|
encoding_ = Encoding::UTF_8;
|
|
|
|
}
|
2020-06-27 03:58:05 +08:00
|
|
|
SkipToNextSignificantCharacter();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip everything that should be ignored until the next significant
|
|
|
|
// character is reached; handles C-style comments in preprocessing
|
|
|
|
// directives, Fortran ! comments, stuff after the right margin in
|
|
|
|
// fixed form, and all forms of line continuation.
|
|
|
|
void Prescanner::SkipToNextSignificantCharacter() {
|
2018-01-31 03:54:47 +08:00
|
|
|
if (inPreprocessorDirective_) {
|
2019-03-16 04:57:41 +08:00
|
|
|
SkipCComments();
|
2018-01-31 03:54:47 +08:00
|
|
|
} else {
|
2019-06-12 03:06:18 +08:00
|
|
|
bool mightNeedSpace{false};
|
|
|
|
if (MustSkipToEndOfLine()) {
|
2018-05-04 02:09:04 +08:00
|
|
|
SkipToEndOfLine();
|
2019-06-12 03:06:18 +08:00
|
|
|
} else {
|
|
|
|
mightNeedSpace = *at_ == '\n';
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
2019-06-12 03:50:27 +08:00
|
|
|
for (; Continuation(mightNeedSpace); mightNeedSpace = false) {
|
|
|
|
if (MustSkipToEndOfLine()) {
|
|
|
|
SkipToEndOfLine();
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*at_ == '\t') {
|
|
|
|
tabInCurrentLine_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-16 04:57:41 +08:00
|
|
|
void Prescanner::SkipCComments() {
|
|
|
|
while (true) {
|
|
|
|
if (IsCComment(at_)) {
|
|
|
|
if (const char *after{SkipCComment(at_)}) {
|
|
|
|
column_ += after - at_;
|
|
|
|
// May have skipped over one or more newlines; relocate the start of
|
|
|
|
// the next line.
|
2019-06-12 03:06:18 +08:00
|
|
|
nextLine_ = at_ = after;
|
2019-03-16 04:57:41 +08:00
|
|
|
NextLine();
|
|
|
|
} else {
|
2019-06-18 03:29:20 +08:00
|
|
|
// Don't emit any messages about unclosed C-style comments, because
|
|
|
|
// the sequence /* can appear legally in a FORMAT statement. There's
|
|
|
|
// no ambiguity, since the sequence */ cannot appear legally.
|
2019-03-16 04:57:41 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else if (inPreprocessorDirective_ && at_[0] == '\\' && at_ + 2 < limit_ &&
|
2019-06-12 03:06:18 +08:00
|
|
|
at_[1] == '\n' && nextLine_ < limit_) {
|
2019-03-16 04:57:41 +08:00
|
|
|
BeginSourceLineAndAdvance();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 03:54:47 +08:00
|
|
|
void Prescanner::SkipSpaces() {
|
|
|
|
while (*at_ == ' ' || *at_ == '\t') {
|
|
|
|
NextChar();
|
|
|
|
}
|
2018-05-11 04:48:55 +08:00
|
|
|
insertASpace_ = false;
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
|
|
|
|
2018-07-31 03:56:58 +08:00
|
|
|
const char *Prescanner::SkipWhiteSpace(const char *p) {
|
|
|
|
while (*p == ' ' || *p == '\t') {
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2019-03-16 04:57:41 +08:00
|
|
|
const char *Prescanner::SkipWhiteSpaceAndCComments(const char *p) const {
|
|
|
|
while (true) {
|
|
|
|
if (*p == ' ' || *p == '\t') {
|
|
|
|
++p;
|
|
|
|
} else if (IsCComment(p)) {
|
|
|
|
if (const char *after{SkipCComment(p)}) {
|
|
|
|
p = after;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *Prescanner::SkipCComment(const char *p) const {
|
|
|
|
char star{' '}, slash{' '};
|
|
|
|
p += 2;
|
|
|
|
while (star != '*' || slash != '/') {
|
|
|
|
if (p >= limit_) {
|
2020-03-29 12:00:16 +08:00
|
|
|
return nullptr; // signifies an unterminated comment
|
2019-03-16 04:57:41 +08:00
|
|
|
}
|
|
|
|
star = slash;
|
|
|
|
slash = *p++;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2018-05-11 04:48:55 +08:00
|
|
|
bool Prescanner::NextToken(TokenSequence &tokens) {
|
2018-02-14 04:24:54 +08:00
|
|
|
CHECK(at_ >= start_ && at_ < limit_);
|
2018-05-11 04:48:55 +08:00
|
|
|
if (InFixedFormSource()) {
|
2018-01-31 03:54:47 +08:00
|
|
|
SkipSpaces();
|
2019-03-16 04:57:41 +08:00
|
|
|
} else {
|
|
|
|
if (*at_ == '/' && IsCComment(at_)) {
|
|
|
|
// Recognize and skip over classic C style /*comments*/ when
|
|
|
|
// outside a character literal.
|
|
|
|
if (features_.ShouldWarn(LanguageFeature::ClassicCComments)) {
|
|
|
|
Say(GetProvenance(at_), "nonstandard usage: C-style comment"_en_US);
|
|
|
|
}
|
|
|
|
SkipCComments();
|
|
|
|
}
|
|
|
|
if (*at_ == ' ' || *at_ == '\t') {
|
|
|
|
// Compress free-form white space into a single space character.
|
|
|
|
const auto theSpace{at_};
|
2019-03-19 03:19:35 +08:00
|
|
|
char previous{at_ <= start_ ? ' ' : at_[-1]};
|
2019-03-16 04:57:41 +08:00
|
|
|
NextChar();
|
|
|
|
SkipSpaces();
|
2019-03-19 03:19:35 +08:00
|
|
|
if (*at_ == '\n') {
|
|
|
|
// Discard white space at the end of a line.
|
|
|
|
} else if (!inPreprocessorDirective_ &&
|
2019-03-19 04:05:21 +08:00
|
|
|
(previous == '(' || *at_ == '(' || *at_ == ')')) {
|
2019-03-19 04:43:03 +08:00
|
|
|
// Discard white space before/after '(' and before ')', unless in a
|
2019-03-19 03:19:35 +08:00
|
|
|
// preprocessor directive. This helps yield space-free contiguous
|
|
|
|
// names for generic interfaces like OPERATOR( + ) and
|
|
|
|
// READ ( UNFORMATTED ), without misinterpreting #define f (notAnArg).
|
|
|
|
// This has the effect of silently ignoring the illegal spaces in
|
|
|
|
// the array constructor ( /1,2/ ) but that seems benign; it's
|
|
|
|
// hard to avoid that while still removing spaces from OPERATOR( / )
|
|
|
|
// and OPERATOR( // ).
|
|
|
|
} else {
|
|
|
|
// Preserve the squashed white space as a single space character.
|
2019-03-16 04:57:41 +08:00
|
|
|
tokens.PutNextTokenChar(' ', GetProvenance(theSpace));
|
|
|
|
tokens.CloseToken();
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
|
|
|
}
|
2018-05-11 04:48:55 +08:00
|
|
|
if (insertASpace_) {
|
|
|
|
tokens.PutNextTokenChar(' ', spaceProvenance_);
|
|
|
|
insertASpace_ = false;
|
|
|
|
}
|
2018-01-31 03:54:47 +08:00
|
|
|
if (*at_ == '\n') {
|
|
|
|
return false;
|
|
|
|
}
|
2018-08-04 05:00:36 +08:00
|
|
|
const char *start{at_};
|
2018-01-31 03:54:47 +08:00
|
|
|
if (*at_ == '\'' || *at_ == '"') {
|
2019-03-22 04:42:35 +08:00
|
|
|
QuotedCharacterLiteral(tokens, start);
|
2018-01-31 03:54:47 +08:00
|
|
|
preventHollerith_ = false;
|
2018-02-28 06:02:10 +08:00
|
|
|
} else if (IsDecimalDigit(*at_)) {
|
2018-03-20 02:48:49 +08:00
|
|
|
int n{0}, digits{0};
|
2018-03-01 08:56:10 +08:00
|
|
|
static constexpr int maxHollerith{256 /*lines*/ * (132 - 6 /*columns*/)};
|
2018-01-31 03:54:47 +08:00
|
|
|
do {
|
|
|
|
if (n < maxHollerith) {
|
2018-02-28 06:02:10 +08:00
|
|
|
n = 10 * n + DecimalDigitValue(*at_);
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
|
|
|
EmitCharAndAdvance(tokens, *at_);
|
2018-03-20 02:48:49 +08:00
|
|
|
++digits;
|
2018-05-11 04:48:55 +08:00
|
|
|
if (InFixedFormSource()) {
|
2018-01-31 03:54:47 +08:00
|
|
|
SkipSpaces();
|
|
|
|
}
|
2018-02-28 06:02:10 +08:00
|
|
|
} while (IsDecimalDigit(*at_));
|
2018-02-06 06:29:26 +08:00
|
|
|
if ((*at_ == 'h' || *at_ == 'H') && n > 0 && n < maxHollerith &&
|
2018-01-31 03:54:47 +08:00
|
|
|
!preventHollerith_) {
|
2018-08-04 05:00:36 +08:00
|
|
|
Hollerith(tokens, n, start);
|
2018-01-31 03:54:47 +08:00
|
|
|
} else if (*at_ == '.') {
|
2018-02-28 06:02:10 +08:00
|
|
|
while (IsDecimalDigit(EmitCharAndAdvance(tokens, *at_))) {
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
|
|
|
ExponentAndKind(tokens);
|
|
|
|
} else if (ExponentAndKind(tokens)) {
|
2018-03-20 02:48:49 +08:00
|
|
|
} else if (digits == 1 && n == 0 && (*at_ == 'x' || *at_ == 'X') &&
|
|
|
|
inPreprocessorDirective_) {
|
|
|
|
do {
|
|
|
|
EmitCharAndAdvance(tokens, *at_);
|
|
|
|
} while (IsHexadecimalDigit(*at_));
|
2018-02-28 06:02:10 +08:00
|
|
|
} else if (IsLetter(*at_)) {
|
2018-01-31 03:54:47 +08:00
|
|
|
// Handles FORMAT(3I9HHOLLERITH) by skipping over the first I so that
|
|
|
|
// we don't misrecognize I9HOLLERITH as an identifier in the next case.
|
2018-03-17 07:13:49 +08:00
|
|
|
EmitCharAndAdvance(tokens, *at_);
|
2019-03-22 01:36:04 +08:00
|
|
|
} else if (at_[0] == '_' && (at_[1] == '\'' || at_[1] == '"')) {
|
|
|
|
EmitCharAndAdvance(tokens, *at_);
|
2019-03-22 04:42:35 +08:00
|
|
|
QuotedCharacterLiteral(tokens, start);
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
|
|
|
preventHollerith_ = false;
|
|
|
|
} else if (*at_ == '.') {
|
2018-02-06 04:48:09 +08:00
|
|
|
char nch{EmitCharAndAdvance(tokens, '.')};
|
2018-05-03 07:38:31 +08:00
|
|
|
if (!inPreprocessorDirective_ && IsDecimalDigit(nch)) {
|
2018-02-28 06:02:10 +08:00
|
|
|
while (IsDecimalDigit(EmitCharAndAdvance(tokens, *at_))) {
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
|
|
|
ExponentAndKind(tokens);
|
2018-02-06 04:48:09 +08:00
|
|
|
} else if (nch == '.' && EmitCharAndAdvance(tokens, '.') == '.') {
|
2020-03-29 12:00:16 +08:00
|
|
|
EmitCharAndAdvance(tokens, '.'); // variadic macro definition ellipsis
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
|
|
|
preventHollerith_ = false;
|
2018-02-28 06:02:10 +08:00
|
|
|
} else if (IsLegalInIdentifier(*at_)) {
|
2019-06-13 06:26:37 +08:00
|
|
|
do {
|
2019-06-15 06:44:32 +08:00
|
|
|
} while (IsLegalInIdentifier(EmitCharAndAdvance(tokens, *at_)));
|
2018-01-31 03:54:47 +08:00
|
|
|
if (*at_ == '\'' || *at_ == '"') {
|
2019-06-19 04:46:54 +08:00
|
|
|
QuotedCharacterLiteral(tokens, start);
|
2019-03-22 01:36:04 +08:00
|
|
|
preventHollerith_ = false;
|
|
|
|
} else {
|
|
|
|
// Subtle: Don't misrecognize labeled DO statement label as Hollerith
|
|
|
|
// when the loop control variable starts with 'H'.
|
|
|
|
preventHollerith_ = true;
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
|
|
|
} else if (*at_ == '*') {
|
|
|
|
if (EmitCharAndAdvance(tokens, '*') == '*') {
|
|
|
|
EmitCharAndAdvance(tokens, '*');
|
|
|
|
} else {
|
2018-04-10 02:47:00 +08:00
|
|
|
// Subtle ambiguity:
|
|
|
|
// CHARACTER*2H declares H because *2 is a kind specifier
|
|
|
|
// DATAC/N*2H / is repeated Hollerith
|
|
|
|
preventHollerith_ = !slashInCurrentLine_;
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
|
|
|
} else {
|
2018-01-31 07:22:26 +08:00
|
|
|
char ch{*at_};
|
|
|
|
if (ch == '(' || ch == '[') {
|
|
|
|
++delimiterNesting_;
|
|
|
|
} else if ((ch == ')' || ch == ']') && delimiterNesting_ > 0) {
|
|
|
|
--delimiterNesting_;
|
|
|
|
}
|
|
|
|
char nch{EmitCharAndAdvance(tokens, ch)};
|
2018-01-31 03:54:47 +08:00
|
|
|
preventHollerith_ = false;
|
2018-02-02 04:08:02 +08:00
|
|
|
if ((nch == '=' &&
|
2018-02-06 06:29:26 +08:00
|
|
|
(ch == '<' || ch == '>' || ch == '/' || ch == '=' || ch == '!')) ||
|
2018-02-02 04:08:02 +08:00
|
|
|
(ch == nch &&
|
2018-02-06 06:29:26 +08:00
|
|
|
(ch == '/' || ch == ':' || ch == '*' || ch == '#' || ch == '&' ||
|
|
|
|
ch == '|' || ch == '<' || ch == '>')) ||
|
2018-01-31 03:54:47 +08:00
|
|
|
(ch == '=' && nch == '>')) {
|
2018-02-02 04:08:02 +08:00
|
|
|
// token comprises two characters
|
2018-01-31 03:54:47 +08:00
|
|
|
EmitCharAndAdvance(tokens, nch);
|
2018-04-10 02:47:00 +08:00
|
|
|
} else if (ch == '/') {
|
|
|
|
slashInCurrentLine_ = true;
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
|
|
|
}
|
2018-05-11 04:48:55 +08:00
|
|
|
tokens.CloseToken();
|
2018-01-31 03:54:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-11 04:48:55 +08:00
|
|
|
bool Prescanner::ExponentAndKind(TokenSequence &tokens) {
|
2019-06-13 06:26:37 +08:00
|
|
|
char ed{ToLowerCaseLetter(*at_)};
|
2018-01-31 03:54:47 +08:00
|
|
|
if (ed != 'e' && ed != 'd') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
EmitCharAndAdvance(tokens, ed);
|
|
|
|
if (*at_ == '+' || *at_ == '-') {
|
|
|
|
EmitCharAndAdvance(tokens, *at_);
|
|
|
|
}
|
2018-02-28 06:02:10 +08:00
|
|
|
while (IsDecimalDigit(*at_)) {
|
2018-01-31 03:54:47 +08:00
|
|
|
EmitCharAndAdvance(tokens, *at_);
|
|
|
|
}
|
|
|
|
if (*at_ == '_') {
|
2018-03-17 07:13:49 +08:00
|
|
|
while (IsLegalInIdentifier(EmitCharAndAdvance(tokens, *at_))) {
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-22 04:42:35 +08:00
|
|
|
void Prescanner::QuotedCharacterLiteral(
|
2019-06-19 04:46:54 +08:00
|
|
|
TokenSequence &tokens, const char *start) {
|
2019-03-22 01:36:04 +08:00
|
|
|
char quote{*at_};
|
|
|
|
const char *end{at_ + 1};
|
2018-01-31 03:54:47 +08:00
|
|
|
inCharLiteral_ = true;
|
2018-07-11 08:09:07 +08:00
|
|
|
const auto emit{[&](char ch) { EmitChar(tokens, ch); }};
|
|
|
|
const auto insert{[&](char ch) { EmitInsertedChar(tokens, ch); }};
|
2019-06-18 07:12:28 +08:00
|
|
|
bool isEscaped{false};
|
2018-07-19 02:19:21 +08:00
|
|
|
bool escapesEnabled{features_.IsEnabled(LanguageFeature::BackslashEscapes)};
|
2018-03-01 08:56:10 +08:00
|
|
|
while (true) {
|
2019-06-19 03:34:23 +08:00
|
|
|
if (*at_ == '\\') {
|
|
|
|
if (escapesEnabled) {
|
|
|
|
isEscaped = !isEscaped;
|
|
|
|
} else {
|
|
|
|
// The parser always processes escape sequences, so don't confuse it
|
|
|
|
// when escapes are disabled.
|
|
|
|
insert('\\');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
isEscaped = false;
|
|
|
|
}
|
2019-06-18 07:12:28 +08:00
|
|
|
EmitQuotedChar(static_cast<unsigned char>(*at_), emit, insert, false,
|
|
|
|
Encoding::LATIN_1);
|
2018-02-17 03:14:11 +08:00
|
|
|
while (PadOutCharacterLiteral(tokens)) {
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
2018-03-01 08:56:10 +08:00
|
|
|
if (*at_ == '\n') {
|
2018-03-17 07:58:35 +08:00
|
|
|
if (!inPreprocessorDirective_) {
|
2018-08-09 02:29:05 +08:00
|
|
|
Say(GetProvenanceRange(start, end),
|
2019-06-14 05:16:04 +08:00
|
|
|
"Incomplete character literal"_err_en_US);
|
2018-03-17 07:58:35 +08:00
|
|
|
}
|
2018-03-01 08:56:10 +08:00
|
|
|
break;
|
|
|
|
}
|
2018-05-04 02:09:04 +08:00
|
|
|
end = at_ + 1;
|
2018-03-01 08:56:10 +08:00
|
|
|
NextChar();
|
2019-06-19 03:34:23 +08:00
|
|
|
if (*at_ == quote && !isEscaped) {
|
2019-06-18 07:12:28 +08:00
|
|
|
// A doubled unescaped quote mark becomes a single instance of that
|
|
|
|
// quote character in the literal (later). There can be spaces between
|
|
|
|
// the quotes in fixed form source.
|
2018-05-03 04:25:03 +08:00
|
|
|
EmitChar(tokens, quote);
|
2020-03-29 12:00:16 +08:00
|
|
|
inCharLiteral_ = false; // for cases like print *, '...'!comment
|
2018-05-03 04:25:03 +08:00
|
|
|
NextChar();
|
2018-05-11 04:48:55 +08:00
|
|
|
if (InFixedFormSource()) {
|
2018-01-31 03:54:47 +08:00
|
|
|
SkipSpaces();
|
|
|
|
}
|
|
|
|
if (*at_ != quote) {
|
|
|
|
break;
|
|
|
|
}
|
2018-05-03 04:25:03 +08:00
|
|
|
inCharLiteral_ = true;
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
2018-03-01 08:56:10 +08:00
|
|
|
}
|
2018-01-31 03:54:47 +08:00
|
|
|
inCharLiteral_ = false;
|
|
|
|
}
|
|
|
|
|
2018-08-04 05:00:36 +08:00
|
|
|
void Prescanner::Hollerith(
|
|
|
|
TokenSequence &tokens, int count, const char *start) {
|
2018-03-01 08:56:10 +08:00
|
|
|
inCharLiteral_ = true;
|
2018-05-04 02:09:04 +08:00
|
|
|
CHECK(*at_ == 'h' || *at_ == 'H');
|
2018-03-01 08:56:10 +08:00
|
|
|
EmitChar(tokens, 'H');
|
|
|
|
while (count-- > 0) {
|
|
|
|
if (PadOutCharacterLiteral(tokens)) {
|
2018-05-04 02:09:04 +08:00
|
|
|
} else if (*at_ == '\n') {
|
2018-08-09 02:29:05 +08:00
|
|
|
Say(GetProvenanceRange(start, at_),
|
2019-06-12 01:34:58 +08:00
|
|
|
"Possible truncated Hollerith literal"_en_US);
|
2018-05-04 02:09:04 +08:00
|
|
|
break;
|
|
|
|
} else {
|
2018-03-01 08:56:10 +08:00
|
|
|
NextChar();
|
2019-06-19 03:34:23 +08:00
|
|
|
// Each multi-byte character encoding counts as a single character.
|
|
|
|
// No escape sequences are recognized.
|
|
|
|
// Hollerith is always emitted to the cooked character
|
|
|
|
// stream in UTF-8.
|
2019-06-12 01:34:58 +08:00
|
|
|
DecodedCharacter decoded{DecodeCharacter(
|
2019-06-19 03:34:23 +08:00
|
|
|
encoding_, at_, static_cast<std::size_t>(limit_ - at_), false)};
|
2019-06-12 01:34:58 +08:00
|
|
|
if (decoded.bytes > 0) {
|
2019-06-19 03:34:23 +08:00
|
|
|
EncodedCharacter utf8{
|
|
|
|
EncodeCharacter<Encoding::UTF_8>(decoded.codepoint)};
|
2019-06-12 01:34:58 +08:00
|
|
|
for (int j{0}; j < utf8.bytes; ++j) {
|
|
|
|
EmitChar(tokens, utf8.buffer[j]);
|
2018-03-01 08:56:10 +08:00
|
|
|
}
|
2019-06-13 06:26:37 +08:00
|
|
|
at_ += decoded.bytes - 1;
|
2019-06-12 01:34:58 +08:00
|
|
|
} else {
|
|
|
|
Say(GetProvenanceRange(start, at_),
|
|
|
|
"Bad character in Hollerith literal"_err_en_US);
|
|
|
|
break;
|
2018-03-01 08:56:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-04 02:09:04 +08:00
|
|
|
if (*at_ != '\n') {
|
2018-03-01 08:56:10 +08:00
|
|
|
NextChar();
|
|
|
|
}
|
|
|
|
inCharLiteral_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In fixed form, source card images must be processed as if they were at
|
|
|
|
// least 72 columns wide, at least in character literal contexts.
|
2018-05-11 04:48:55 +08:00
|
|
|
bool Prescanner::PadOutCharacterLiteral(TokenSequence &tokens) {
|
2018-03-01 08:56:10 +08:00
|
|
|
while (inFixedForm_ && !tabInCurrentLine_ && at_[1] == '\n') {
|
|
|
|
if (column_ < fixedFormColumnLimit_) {
|
2018-05-11 04:48:55 +08:00
|
|
|
tokens.PutNextTokenChar(' ', spaceProvenance_);
|
2018-03-01 08:56:10 +08:00
|
|
|
++column_;
|
|
|
|
return true;
|
|
|
|
}
|
2018-05-11 04:48:55 +08:00
|
|
|
if (!FixedFormContinuation(false /*no need to insert space*/) ||
|
|
|
|
tabInCurrentLine_) {
|
2018-03-01 08:56:10 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
CHECK(column_ == 7);
|
2020-03-29 12:00:16 +08:00
|
|
|
--at_; // point to column 6 of continuation line
|
2018-03-01 08:56:10 +08:00
|
|
|
column_ = 6;
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-21 01:59:07 +08:00
|
|
|
bool Prescanner::IsFixedFormCommentLine(const char *start) const {
|
2018-01-31 03:54:47 +08:00
|
|
|
const char *p{start};
|
2020-03-29 12:00:16 +08:00
|
|
|
if (IsFixedFormCommentChar(*p) || *p == '%' || // VAX %list, %eject, &c.
|
2019-06-12 03:06:18 +08:00
|
|
|
((*p == 'D' || *p == 'd') &&
|
2018-07-19 02:19:21 +08:00
|
|
|
!features_.IsEnabled(LanguageFeature::OldDebugLines))) {
|
2018-01-31 03:54:47 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool anyTabs{false};
|
|
|
|
while (true) {
|
2019-06-12 03:06:18 +08:00
|
|
|
if (*p == ' ') {
|
2018-01-31 03:54:47 +08:00
|
|
|
++p;
|
2019-06-12 03:06:18 +08:00
|
|
|
} else if (*p == '\t') {
|
2018-01-31 03:54:47 +08:00
|
|
|
anyTabs = true;
|
|
|
|
++p;
|
2019-06-12 03:06:18 +08:00
|
|
|
} else if (*p == '0' && !anyTabs && p == start + 5) {
|
2020-03-29 12:00:16 +08:00
|
|
|
++p; // 0 in column 6 must treated as a space
|
2018-01-31 03:54:47 +08:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!anyTabs && p >= start + fixedFormColumnLimit_) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (*p == '!' && !inCharLiteral_ && (anyTabs || p != start + 5)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return *p == '\n';
|
|
|
|
}
|
|
|
|
|
2019-03-16 04:57:41 +08:00
|
|
|
const char *Prescanner::IsFreeFormComment(const char *p) const {
|
|
|
|
p = SkipWhiteSpaceAndCComments(p);
|
|
|
|
if (*p == '!' || *p == '\n') {
|
|
|
|
return p;
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
|
|
|
|
2018-03-24 06:14:52 +08:00
|
|
|
std::optional<std::size_t> Prescanner::IsIncludeLine(const char *start) const {
|
2018-07-31 03:56:58 +08:00
|
|
|
const char *p{SkipWhiteSpace(start)};
|
2018-02-14 06:22:08 +08:00
|
|
|
for (char ch : "include"s) {
|
2018-03-01 08:56:10 +08:00
|
|
|
if (ToLowerCaseLetter(*p++) != ch) {
|
2018-07-26 06:13:40 +08:00
|
|
|
return std::nullopt;
|
2018-02-14 06:22:08 +08:00
|
|
|
}
|
|
|
|
}
|
2018-07-31 03:56:58 +08:00
|
|
|
p = SkipWhiteSpace(p);
|
2018-03-24 06:14:52 +08:00
|
|
|
if (*p == '"' || *p == '\'') {
|
|
|
|
return {p - start};
|
|
|
|
}
|
2018-07-26 06:13:40 +08:00
|
|
|
return std::nullopt;
|
2018-03-24 06:14:52 +08:00
|
|
|
}
|
|
|
|
|
2018-04-04 23:06:15 +08:00
|
|
|
void Prescanner::FortranInclude(const char *firstQuote) {
|
2018-03-24 06:14:52 +08:00
|
|
|
const char *p{firstQuote};
|
|
|
|
while (*p != '"' && *p != '\'') {
|
|
|
|
++p;
|
2018-02-14 06:22:08 +08:00
|
|
|
}
|
|
|
|
char quote{*p};
|
|
|
|
std::string path;
|
|
|
|
for (++p; *p != '\n'; ++p) {
|
|
|
|
if (*p == quote) {
|
|
|
|
if (p[1] != quote) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
path += *p;
|
|
|
|
}
|
|
|
|
if (*p != quote) {
|
2018-08-09 02:29:05 +08:00
|
|
|
Say(GetProvenanceRange(firstQuote, p),
|
|
|
|
"malformed path name string"_err_en_US);
|
2018-04-04 23:06:15 +08:00
|
|
|
return;
|
2018-02-14 06:22:08 +08:00
|
|
|
}
|
2018-07-31 03:56:58 +08:00
|
|
|
p = SkipWhiteSpace(p + 1);
|
2018-02-14 06:22:08 +08:00
|
|
|
if (*p != '\n' && *p != '!') {
|
2018-04-24 03:17:11 +08:00
|
|
|
const char *garbage{p};
|
|
|
|
for (; *p != '\n' && *p != '!'; ++p) {
|
|
|
|
}
|
2018-08-09 02:29:05 +08:00
|
|
|
Say(GetProvenanceRange(garbage, p),
|
|
|
|
"excess characters after path name"_en_US);
|
2018-02-14 06:22:08 +08:00
|
|
|
}
|
2020-02-28 23:11:03 +08:00
|
|
|
std::string buf;
|
|
|
|
llvm::raw_string_ostream error{buf};
|
2019-06-12 03:06:18 +08:00
|
|
|
Provenance provenance{GetProvenance(nextLine_)};
|
2018-03-24 06:14:52 +08:00
|
|
|
AllSources &allSources{cooked_.allSources()};
|
|
|
|
const SourceFile *currentFile{allSources.GetSourceFile(provenance)};
|
2019-11-10 01:29:31 +08:00
|
|
|
if (currentFile) {
|
2018-03-24 06:14:52 +08:00
|
|
|
allSources.PushSearchPathDirectory(DirectoryName(currentFile->path()));
|
2018-02-14 07:24:43 +08:00
|
|
|
}
|
2020-02-28 23:11:03 +08:00
|
|
|
const SourceFile *included{allSources.Open(path, error)};
|
2019-11-10 01:29:31 +08:00
|
|
|
if (currentFile) {
|
2018-03-24 06:14:52 +08:00
|
|
|
allSources.PopSearchPathDirectory();
|
2018-02-14 07:24:43 +08:00
|
|
|
}
|
2019-11-10 01:29:31 +08:00
|
|
|
if (!included) {
|
2019-05-07 00:33:45 +08:00
|
|
|
Say(provenance, "INCLUDE: %s"_err_en_US, error.str());
|
2018-04-04 23:06:15 +08:00
|
|
|
} else if (included->bytes() > 0) {
|
|
|
|
ProvenanceRange includeLineRange{
|
2019-06-12 03:06:18 +08:00
|
|
|
provenance, static_cast<std::size_t>(p - nextLine_)};
|
2018-04-04 23:06:15 +08:00
|
|
|
ProvenanceRange fileRange{
|
|
|
|
allSources.AddIncludedFile(*included, includeLineRange)};
|
2019-06-13 06:26:37 +08:00
|
|
|
Prescanner{*this}.set_encoding(included->encoding()).Prescan(fileRange);
|
2018-03-21 01:59:07 +08:00
|
|
|
}
|
2018-02-14 06:22:08 +08:00
|
|
|
}
|
|
|
|
|
2018-05-03 07:09:12 +08:00
|
|
|
const char *Prescanner::IsPreprocessorDirectiveLine(const char *start) const {
|
2018-01-31 03:54:47 +08:00
|
|
|
const char *p{start};
|
|
|
|
for (; *p == ' '; ++p) {
|
|
|
|
}
|
|
|
|
if (*p == '#') {
|
2018-05-03 07:09:12 +08:00
|
|
|
if (inFixedForm_ && p == start + 5) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
2018-07-31 03:56:58 +08:00
|
|
|
p = SkipWhiteSpace(p);
|
2018-05-03 07:09:12 +08:00
|
|
|
if (*p != '#') {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
2018-07-31 03:56:58 +08:00
|
|
|
return SkipWhiteSpace(p + 1);
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
|
|
|
|
2018-03-17 07:58:35 +08:00
|
|
|
bool Prescanner::IsNextLinePreprocessorDirective() const {
|
2019-06-12 03:06:18 +08:00
|
|
|
return IsPreprocessorDirectiveLine(nextLine_) != nullptr;
|
2018-03-17 07:58:35 +08:00
|
|
|
}
|
|
|
|
|
2019-03-15 03:19:26 +08:00
|
|
|
bool Prescanner::SkipCommentLine(bool afterAmpersand) {
|
2019-06-12 03:06:18 +08:00
|
|
|
if (nextLine_ >= limit_) {
|
2019-03-15 03:19:26 +08:00
|
|
|
if (afterAmpersand && prescannerNesting_ > 0) {
|
|
|
|
// A continuation marker at the end of the last line in an
|
2019-03-15 04:53:35 +08:00
|
|
|
// include file inhibits the newline for that line.
|
2019-03-15 03:19:26 +08:00
|
|
|
SkipToEndOfLine();
|
|
|
|
omitNewline_ = true;
|
|
|
|
}
|
2018-05-10 06:32:22 +08:00
|
|
|
return false;
|
|
|
|
}
|
2019-06-12 03:06:18 +08:00
|
|
|
auto lineClass{ClassifyLine(nextLine_)};
|
2018-05-10 06:32:22 +08:00
|
|
|
if (lineClass.kind == LineClassification::Kind::Comment) {
|
|
|
|
NextLine();
|
|
|
|
return true;
|
2019-03-15 03:19:26 +08:00
|
|
|
} else if (inPreprocessorDirective_) {
|
|
|
|
return false;
|
|
|
|
} else if (lineClass.kind ==
|
2019-06-18 07:12:28 +08:00
|
|
|
LineClassification::Kind::ConditionalCompilationDirective ||
|
|
|
|
lineClass.kind == LineClassification::Kind::PreprocessorDirective) {
|
2018-05-10 06:32:22 +08:00
|
|
|
// Allow conditional compilation directives (e.g., #ifdef) to affect
|
|
|
|
// continuation lines.
|
2019-09-17 07:58:13 +08:00
|
|
|
// Allow other preprocessor directives, too, except #include
|
|
|
|
// (when it does not follow '&'), #define, and #undef (because
|
|
|
|
// they cannot be allowed to affect preceding text on a
|
|
|
|
// continued line).
|
2018-05-10 06:32:22 +08:00
|
|
|
preprocessor_.Directive(TokenizePreprocessorDirective(), this);
|
|
|
|
return true;
|
2019-03-15 03:19:26 +08:00
|
|
|
} else if (afterAmpersand &&
|
|
|
|
(lineClass.kind == LineClassification::Kind::IncludeDirective ||
|
|
|
|
lineClass.kind == LineClassification::Kind::IncludeLine)) {
|
|
|
|
SkipToEndOfLine();
|
|
|
|
omitNewline_ = true;
|
2019-03-15 04:53:35 +08:00
|
|
|
skipLeadingAmpersand_ = true;
|
2019-03-15 03:19:26 +08:00
|
|
|
return false;
|
2018-05-10 06:32:22 +08:00
|
|
|
} else {
|
|
|
|
return false;
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-11 04:48:55 +08:00
|
|
|
const char *Prescanner::FixedFormContinuationLine(bool mightNeedSpace) {
|
2019-06-12 03:06:18 +08:00
|
|
|
if (nextLine_ >= limit_) {
|
2018-01-31 03:54:47 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
tabInCurrentLine_ = false;
|
2019-06-12 03:06:18 +08:00
|
|
|
char col1{*nextLine_};
|
2018-07-26 02:44:20 +08:00
|
|
|
if (InCompilerDirective()) {
|
2018-03-24 06:14:52 +08:00
|
|
|
// Must be a continued compiler directive.
|
2018-05-11 04:48:55 +08:00
|
|
|
if (!IsFixedFormCommentChar(col1)) {
|
2018-03-24 06:14:52 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
int j{1};
|
|
|
|
for (; j < 5; ++j) {
|
|
|
|
char ch{directiveSentinel_[j - 1]};
|
|
|
|
if (ch == '\0') {
|
|
|
|
break;
|
|
|
|
}
|
2019-06-12 03:06:18 +08:00
|
|
|
if (ch != ToLowerCaseLetter(nextLine_[j])) {
|
2018-03-24 06:14:52 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (; j < 5; ++j) {
|
2019-06-12 03:06:18 +08:00
|
|
|
if (nextLine_[j] != ' ') {
|
2018-03-24 06:14:52 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
2019-06-12 03:06:18 +08:00
|
|
|
char col6{nextLine_[5]};
|
2018-03-24 06:14:52 +08:00
|
|
|
if (col6 != '\n' && col6 != '\t' && col6 != ' ' && col6 != '0') {
|
2019-06-12 03:06:18 +08:00
|
|
|
if (nextLine_[6] != ' ' && mightNeedSpace) {
|
2018-05-11 04:48:55 +08:00
|
|
|
insertASpace_ = true;
|
|
|
|
}
|
2019-06-12 03:06:18 +08:00
|
|
|
return nextLine_ + 6;
|
2018-03-24 06:14:52 +08:00
|
|
|
}
|
|
|
|
return nullptr;
|
2018-05-10 07:16:10 +08:00
|
|
|
} else {
|
|
|
|
// Normal case: not in a compiler directive.
|
2018-07-19 02:19:21 +08:00
|
|
|
if (col1 == '&' &&
|
|
|
|
features_.IsEnabled(
|
|
|
|
LanguageFeature::FixedFormContinuationWithColumn1Ampersand)) {
|
2018-05-10 07:16:10 +08:00
|
|
|
// Extension: '&' as continuation marker
|
2018-07-19 02:19:21 +08:00
|
|
|
if (features_.ShouldWarn(
|
|
|
|
LanguageFeature::FixedFormContinuationWithColumn1Ampersand)) {
|
2019-06-12 03:06:18 +08:00
|
|
|
Say(GetProvenance(nextLine_), "nonstandard usage"_en_US);
|
2018-05-10 07:16:10 +08:00
|
|
|
}
|
2019-06-12 03:06:18 +08:00
|
|
|
return nextLine_ + 1;
|
2018-03-31 07:21:12 +08:00
|
|
|
}
|
2019-06-12 03:06:18 +08:00
|
|
|
if (col1 == '\t' && nextLine_[1] >= '1' && nextLine_[1] <= '9') {
|
2018-05-10 07:16:10 +08:00
|
|
|
tabInCurrentLine_ = true;
|
2020-03-29 12:00:16 +08:00
|
|
|
return nextLine_ + 2; // VAX extension
|
2018-05-10 07:16:10 +08:00
|
|
|
}
|
2019-06-12 03:06:18 +08:00
|
|
|
if (col1 == ' ' && nextLine_[1] == ' ' && nextLine_[2] == ' ' &&
|
|
|
|
nextLine_[3] == ' ' && nextLine_[4] == ' ') {
|
|
|
|
char col6{nextLine_[5]};
|
2018-05-10 07:16:10 +08:00
|
|
|
if (col6 != '\n' && col6 != '\t' && col6 != ' ' && col6 != '0') {
|
2019-06-12 03:06:18 +08:00
|
|
|
return nextLine_ + 6;
|
2018-05-10 07:16:10 +08:00
|
|
|
}
|
|
|
|
}
|
2020-07-22 08:57:06 +08:00
|
|
|
if (IsImplicitContinuation()) {
|
|
|
|
return nextLine_;
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
2018-01-31 07:22:26 +08:00
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
return nullptr; // not a continuation line
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
|
|
|
|
2018-05-10 06:32:22 +08:00
|
|
|
const char *Prescanner::FreeFormContinuationLine(bool ampersand) {
|
2019-06-12 03:06:18 +08:00
|
|
|
const char *p{nextLine_};
|
2018-01-31 03:54:47 +08:00
|
|
|
if (p >= limit_) {
|
2018-05-10 06:32:22 +08:00
|
|
|
return nullptr;
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
2018-07-31 03:56:58 +08:00
|
|
|
p = SkipWhiteSpace(p);
|
2018-07-26 02:44:20 +08:00
|
|
|
if (InCompilerDirective()) {
|
2018-03-24 06:14:52 +08:00
|
|
|
if (*p++ != '!') {
|
2018-05-10 06:32:22 +08:00
|
|
|
return nullptr;
|
2018-03-24 06:14:52 +08:00
|
|
|
}
|
|
|
|
for (const char *s{directiveSentinel_}; *s != '\0'; ++p, ++s) {
|
|
|
|
if (*s != ToLowerCaseLetter(*p)) {
|
2018-05-10 06:32:22 +08:00
|
|
|
return nullptr;
|
2018-03-24 06:14:52 +08:00
|
|
|
}
|
|
|
|
}
|
2018-07-31 03:56:58 +08:00
|
|
|
p = SkipWhiteSpace(p);
|
2018-03-24 06:14:52 +08:00
|
|
|
if (*p == '&') {
|
2018-05-11 04:48:55 +08:00
|
|
|
if (!ampersand) {
|
|
|
|
insertASpace_ = true;
|
|
|
|
}
|
2018-05-10 07:06:57 +08:00
|
|
|
return p + 1;
|
|
|
|
} else if (ampersand) {
|
|
|
|
return p;
|
|
|
|
} else {
|
2018-05-10 06:32:22 +08:00
|
|
|
return nullptr;
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
|
|
|
} else {
|
2018-03-24 06:14:52 +08:00
|
|
|
if (*p == '&') {
|
2018-05-10 07:06:57 +08:00
|
|
|
return p + 1;
|
2019-03-15 03:19:26 +08:00
|
|
|
} else if (*p == '!' || *p == '\n' || *p == '#') {
|
2018-05-10 07:06:57 +08:00
|
|
|
return nullptr;
|
2020-07-22 08:57:06 +08:00
|
|
|
} else if (ampersand || IsImplicitContinuation()) {
|
2019-06-12 03:06:18 +08:00
|
|
|
if (p > nextLine_) {
|
2018-03-24 06:14:52 +08:00
|
|
|
--p;
|
2018-05-11 04:48:55 +08:00
|
|
|
} else {
|
|
|
|
insertASpace_ = true;
|
2018-03-24 06:14:52 +08:00
|
|
|
}
|
2018-05-10 07:06:57 +08:00
|
|
|
return p;
|
2018-03-24 06:14:52 +08:00
|
|
|
} else {
|
2018-05-10 06:32:22 +08:00
|
|
|
return nullptr;
|
2018-03-24 06:14:52 +08:00
|
|
|
}
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
2018-05-10 06:32:22 +08:00
|
|
|
}
|
|
|
|
|
2018-05-11 04:48:55 +08:00
|
|
|
bool Prescanner::FixedFormContinuation(bool mightNeedSpace) {
|
|
|
|
// N.B. We accept '&' as a continuation indicator in fixed form, too,
|
|
|
|
// but not in a character literal.
|
2018-05-10 06:32:22 +08:00
|
|
|
if (*at_ == '&' && inCharLiteral_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
do {
|
2018-05-11 04:48:55 +08:00
|
|
|
if (const char *cont{FixedFormContinuationLine(mightNeedSpace)}) {
|
2018-05-10 06:32:22 +08:00
|
|
|
BeginSourceLine(cont);
|
|
|
|
column_ = 7;
|
|
|
|
NextLine();
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-18 07:12:28 +08:00
|
|
|
} while (SkipCommentLine(false /* not after ampersand */));
|
2018-05-10 06:32:22 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Prescanner::FreeFormContinuation() {
|
|
|
|
const char *p{at_};
|
|
|
|
bool ampersand{*p == '&'};
|
|
|
|
if (ampersand) {
|
2018-07-31 03:56:58 +08:00
|
|
|
p = SkipWhiteSpace(p + 1);
|
2018-05-10 06:32:22 +08:00
|
|
|
}
|
2019-03-15 04:53:35 +08:00
|
|
|
if (*p != '\n') {
|
|
|
|
if (inCharLiteral_) {
|
|
|
|
return false;
|
2019-03-15 05:51:40 +08:00
|
|
|
} else if (*p != '!' &&
|
|
|
|
features_.ShouldWarn(LanguageFeature::CruftAfterAmpersand)) {
|
|
|
|
Say(GetProvenance(p), "missing ! before comment after &"_en_US);
|
2019-03-15 04:53:35 +08:00
|
|
|
}
|
2018-05-10 06:32:22 +08:00
|
|
|
}
|
|
|
|
do {
|
|
|
|
if (const char *cont{FreeFormContinuationLine(ampersand)}) {
|
2018-05-11 04:48:55 +08:00
|
|
|
BeginSourceLine(cont);
|
2018-05-10 06:32:22 +08:00
|
|
|
NextLine();
|
|
|
|
return true;
|
|
|
|
}
|
2019-03-15 03:19:26 +08:00
|
|
|
} while (SkipCommentLine(ampersand));
|
2018-05-10 06:32:22 +08:00
|
|
|
return false;
|
2018-01-31 03:54:47 +08:00
|
|
|
}
|
2018-03-21 01:59:07 +08:00
|
|
|
|
2020-07-22 08:57:06 +08:00
|
|
|
// Implicit line continuation allows a preprocessor macro call with
|
|
|
|
// arguments to span multiple lines.
|
|
|
|
bool Prescanner::IsImplicitContinuation() const {
|
|
|
|
return !inPreprocessorDirective_ && !inCharLiteral_ &&
|
|
|
|
delimiterNesting_ > 0 && nextLine_ < limit_ &&
|
|
|
|
ClassifyLine(nextLine_).kind == LineClassification::Kind::Source;
|
|
|
|
}
|
|
|
|
|
2019-06-12 03:50:27 +08:00
|
|
|
bool Prescanner::Continuation(bool mightNeedFixedFormSpace) {
|
|
|
|
if (*at_ == '\n' || *at_ == '&') {
|
|
|
|
if (inFixedForm_) {
|
|
|
|
return FixedFormContinuation(mightNeedFixedFormSpace);
|
|
|
|
} else {
|
|
|
|
return FreeFormContinuation();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-24 06:14:52 +08:00
|
|
|
std::optional<Prescanner::LineClassification>
|
|
|
|
Prescanner::IsFixedFormCompilerDirectiveLine(const char *start) const {
|
2018-03-21 01:59:07 +08:00
|
|
|
const char *p{start};
|
2018-05-10 06:32:22 +08:00
|
|
|
char col1{*p++};
|
2018-05-11 04:48:55 +08:00
|
|
|
if (!IsFixedFormCommentChar(col1)) {
|
2018-07-26 06:13:40 +08:00
|
|
|
return std::nullopt;
|
2018-03-21 01:59:07 +08:00
|
|
|
}
|
2018-03-24 06:14:52 +08:00
|
|
|
char sentinel[5], *sp{sentinel};
|
2018-07-26 02:44:20 +08:00
|
|
|
int column{2};
|
|
|
|
for (; column < 6; ++column, ++p) {
|
|
|
|
if (*p != ' ') {
|
|
|
|
if (*p == '\n' || *p == '\t') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (sp == sentinel + 1 && sentinel[0] == '$' && IsDecimalDigit(*p)) {
|
|
|
|
// OpenMP conditional compilation line: leave the label alone
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*sp++ = ToLowerCaseLetter(*p);
|
2018-06-15 00:35:38 +08:00
|
|
|
}
|
2018-07-26 02:44:20 +08:00
|
|
|
}
|
|
|
|
if (column == 6) {
|
|
|
|
if (*p == ' ' || *p == '\t' || *p == '0') {
|
|
|
|
++p;
|
|
|
|
} else {
|
|
|
|
// This is a Continuation line, not an initial directive line.
|
2018-07-26 06:13:40 +08:00
|
|
|
return std::nullopt;
|
2018-03-21 01:59:07 +08:00
|
|
|
}
|
|
|
|
}
|
2018-07-26 02:44:20 +08:00
|
|
|
if (sp == sentinel) {
|
2018-07-26 06:13:40 +08:00
|
|
|
return std::nullopt;
|
2018-05-08 04:26:40 +08:00
|
|
|
}
|
2018-03-21 01:59:07 +08:00
|
|
|
*sp = '\0';
|
2018-07-26 02:44:20 +08:00
|
|
|
if (const char *ss{IsCompilerDirectiveSentinel(sentinel)}) {
|
|
|
|
std::size_t payloadOffset = p - start;
|
|
|
|
return {LineClassification{
|
|
|
|
LineClassification::Kind::CompilerDirective, payloadOffset, ss}};
|
2018-03-24 06:14:52 +08:00
|
|
|
}
|
2018-07-26 06:13:40 +08:00
|
|
|
return std::nullopt;
|
2018-03-21 01:59:07 +08:00
|
|
|
}
|
|
|
|
|
2018-03-24 06:14:52 +08:00
|
|
|
std::optional<Prescanner::LineClassification>
|
|
|
|
Prescanner::IsFreeFormCompilerDirectiveLine(const char *start) const {
|
|
|
|
char sentinel[8];
|
2018-07-31 03:56:58 +08:00
|
|
|
const char *p{SkipWhiteSpace(start)};
|
2018-03-21 01:59:07 +08:00
|
|
|
if (*p++ != '!') {
|
2018-07-26 06:13:40 +08:00
|
|
|
return std::nullopt;
|
2018-03-21 01:59:07 +08:00
|
|
|
}
|
2018-03-24 06:14:52 +08:00
|
|
|
for (std::size_t j{0}; j + 1 < sizeof sentinel; ++p, ++j) {
|
|
|
|
if (*p == '\n') {
|
2018-03-21 01:59:07 +08:00
|
|
|
break;
|
|
|
|
}
|
2018-03-24 06:14:52 +08:00
|
|
|
if (*p == ' ' || *p == '\t' || *p == '&') {
|
2018-03-21 01:59:07 +08:00
|
|
|
if (j == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sentinel[j] = '\0';
|
2018-07-31 03:56:58 +08:00
|
|
|
p = SkipWhiteSpace(p + 1);
|
2018-03-24 06:14:52 +08:00
|
|
|
if (*p == '!') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (const char *sp{IsCompilerDirectiveSentinel(sentinel)}) {
|
|
|
|
std::size_t offset = p - start;
|
|
|
|
return {LineClassification{
|
|
|
|
LineClassification::Kind::CompilerDirective, offset, sp}};
|
|
|
|
}
|
|
|
|
break;
|
2018-03-21 01:59:07 +08:00
|
|
|
}
|
|
|
|
sentinel[j] = ToLowerCaseLetter(*p);
|
|
|
|
}
|
2018-07-26 06:13:40 +08:00
|
|
|
return std::nullopt;
|
2018-03-21 01:59:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Prescanner &Prescanner::AddCompilerDirectiveSentinel(const std::string &dir) {
|
|
|
|
std::uint64_t packed{0};
|
|
|
|
for (char ch : dir) {
|
|
|
|
packed = (packed << 8) | (ToLowerCaseLetter(ch) & 0xff);
|
|
|
|
}
|
|
|
|
compilerDirectiveBloomFilter_.set(packed % prime1);
|
|
|
|
compilerDirectiveBloomFilter_.set(packed % prime2);
|
|
|
|
compilerDirectiveSentinels_.insert(dir);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-05-10 06:32:22 +08:00
|
|
|
const char *Prescanner::IsCompilerDirectiveSentinel(
|
|
|
|
const char *sentinel) const {
|
2018-03-21 01:59:07 +08:00
|
|
|
std::uint64_t packed{0};
|
|
|
|
std::size_t n{0};
|
2018-05-10 06:32:22 +08:00
|
|
|
for (; sentinel[n] != '\0'; ++n) {
|
|
|
|
packed = (packed << 8) | (sentinel[n] & 0xff);
|
2018-03-21 01:59:07 +08:00
|
|
|
}
|
2018-03-24 06:14:52 +08:00
|
|
|
if (n == 0 || !compilerDirectiveBloomFilter_.test(packed % prime1) ||
|
|
|
|
!compilerDirectiveBloomFilter_.test(packed % prime2)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-07-11 08:09:07 +08:00
|
|
|
const auto iter{compilerDirectiveSentinels_.find(std::string(sentinel, n))};
|
2019-11-01 04:25:28 +08:00
|
|
|
return iter == compilerDirectiveSentinels_.end() ? nullptr : iter->c_str();
|
2018-03-24 06:14:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Prescanner::LineClassification Prescanner::ClassifyLine(
|
|
|
|
const char *start) const {
|
|
|
|
if (inFixedForm_) {
|
|
|
|
if (std::optional<LineClassification> lc{
|
|
|
|
IsFixedFormCompilerDirectiveLine(start)}) {
|
|
|
|
return std::move(*lc);
|
|
|
|
}
|
|
|
|
if (IsFixedFormCommentLine(start)) {
|
|
|
|
return {LineClassification::Kind::Comment};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (std::optional<LineClassification> lc{
|
|
|
|
IsFreeFormCompilerDirectiveLine(start)}) {
|
|
|
|
return std::move(*lc);
|
|
|
|
}
|
2019-03-16 04:57:41 +08:00
|
|
|
if (const char *bang{IsFreeFormComment(start)}) {
|
|
|
|
return {LineClassification::Kind::Comment,
|
|
|
|
static_cast<std::size_t>(bang - start)};
|
2018-03-24 06:14:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (std::optional<std::size_t> quoteOffset{IsIncludeLine(start)}) {
|
2019-03-15 03:19:26 +08:00
|
|
|
return {LineClassification::Kind::IncludeLine, *quoteOffset};
|
2018-03-24 06:14:52 +08:00
|
|
|
}
|
2018-05-03 07:09:12 +08:00
|
|
|
if (const char *dir{IsPreprocessorDirectiveLine(start)}) {
|
|
|
|
if (std::memcmp(dir, "if", 2) == 0 || std::memcmp(dir, "elif", 4) == 0 ||
|
|
|
|
std::memcmp(dir, "else", 4) == 0 || std::memcmp(dir, "endif", 5) == 0) {
|
|
|
|
return {LineClassification::Kind::ConditionalCompilationDirective};
|
2019-03-15 03:19:26 +08:00
|
|
|
} else if (std::memcmp(dir, "include", 7) == 0) {
|
|
|
|
return {LineClassification::Kind::IncludeDirective};
|
2019-06-19 04:46:54 +08:00
|
|
|
} else if (std::memcmp(dir, "define", 6) == 0 ||
|
|
|
|
std::memcmp(dir, "undef", 5) == 0) {
|
|
|
|
return {LineClassification::Kind::DefinitionDirective};
|
2018-05-03 07:09:12 +08:00
|
|
|
} else {
|
|
|
|
return {LineClassification::Kind::PreprocessorDirective};
|
|
|
|
}
|
2018-03-24 06:14:52 +08:00
|
|
|
}
|
|
|
|
return {LineClassification::Kind::Source};
|
|
|
|
}
|
|
|
|
|
|
|
|
void Prescanner::SourceFormChange(std::string &&dir) {
|
|
|
|
if (dir == "!dir$ free") {
|
|
|
|
inFixedForm_ = false;
|
|
|
|
} else if (dir == "!dir$ fixed") {
|
|
|
|
inFixedForm_ = true;
|
|
|
|
}
|
2018-03-21 01:59:07 +08:00
|
|
|
}
|
2020-03-29 12:00:16 +08:00
|
|
|
} // namespace Fortran::parser
|