[flang] Allow Fortran comments after #include path

C-style /*comments*/ are removed during preprocessing directive
tokenization, but Fortran !comments need to be specifically
allowed.

Fixes LLVM bugzilla 47466.

Differential Revision: https://reviews.llvm.org/D87638
This commit is contained in:
peter klausler 2020-09-14 14:09:01 -07:00
parent e6bc7037d3
commit 4706880f06
3 changed files with 27 additions and 7 deletions

View File

@ -540,7 +540,7 @@ void Preprocessor::Directive(const TokenSequence &dir, Prescanner *prescanner) {
return;
}
std::string include;
if (dir.TokenAt(j).ToString() == "<") {
if (dir.TokenAt(j).ToString() == "<") { // #include <foo>
std::size_t k{j + 1};
if (k >= tokens) {
prescanner->Say(dir.GetIntervalProvenanceRange(j, tokens - j),
@ -553,15 +553,12 @@ void Preprocessor::Directive(const TokenSequence &dir, Prescanner *prescanner) {
if (k >= tokens) {
prescanner->Say(dir.GetIntervalProvenanceRange(j, tokens - j),
"#include: expected '>' at end of included file"_en_US);
} else if (k + 1 < tokens) {
prescanner->Say(dir.GetIntervalProvenanceRange(k + 1, tokens - k - 1),
"#include: extra stuff ignored after '>'"_en_US);
}
TokenSequence braced{dir, j + 1, k - j - 1};
include = ReplaceMacros(braced, *prescanner).ToString();
} else if (j + 1 == tokens &&
(include = dir.TokenAt(j).ToString()).substr(0, 1) == "\"" &&
include.substr(include.size() - 1, 1) == "\"") {
j = k;
} else if ((include = dir.TokenAt(j).ToString()).substr(0, 1) == "\"" &&
include.substr(include.size() - 1, 1) == "\"") { // #include "foo"
include = include.substr(1, include.size() - 2);
} else {
prescanner->Say(dir.GetTokenProvenanceRange(j < tokens ? j : tokens - 1),
@ -573,6 +570,11 @@ void Preprocessor::Directive(const TokenSequence &dir, Prescanner *prescanner) {
"#include: empty include file name"_err_en_US);
return;
}
j = dir.SkipBlanks(j + 1);
if (j < tokens && dir.TokenAt(j).ToString() != "!") {
prescanner->Say(dir.GetIntervalProvenanceRange(j, tokens - j),
"#include: extra stuff ignored after file name"_en_US);
}
std::string buf;
llvm::raw_string_ostream error{buf};
const SourceFile *included{allSources_.Open(include, error)};

View File

View File

@ -0,0 +1,18 @@
! RUN: %f18 -I%S -E %s 2>&1 | FileCheck %s
! CHECK-NOT: :3:
#include <empty.h> ! comment
! CHECK-NOT: :5:
#include <empty.h> /* comment */
! CHECK-NOT: :7:
#include <empty.h> !comment
! CHECK: :9:20: #include: extra stuff ignored after file name
#include <empty.h> comment
! CHECK-NOT: :11:
#include "empty.h" ! comment
! CHECK-NOT: :13:
#include "empty.h" /* comment */
! CHECK-NOT: :15:
#include "empty.h" !comment
! CHECK: :17:20: #include: extra stuff ignored after file name
#include "empty.h" comment
end