diff --git a/flang/lib/Parser/preprocessor.cpp b/flang/lib/Parser/preprocessor.cpp --- a/flang/lib/Parser/preprocessor.cpp +++ b/flang/lib/Parser/preprocessor.cpp @@ -540,7 +540,7 @@ return; } std::string include; - if (dir.TokenAt(j).ToString() == "<") { + if (dir.TokenAt(j).ToString() == "<") { // #include std::size_t k{j + 1}; if (k >= tokens) { prescanner->Say(dir.GetIntervalProvenanceRange(j, tokens - j), @@ -553,15 +553,12 @@ 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 @@ "#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)}; diff --git a/flang/test/Preprocessing/empty.h b/flang/test/Preprocessing/empty.h new file mode 100644 diff --git a/flang/test/Preprocessing/include-comment.F90 b/flang/test/Preprocessing/include-comment.F90 new file mode 100644 --- /dev/null +++ b/flang/test/Preprocessing/include-comment.F90 @@ -0,0 +1,18 @@ +! RUN: %f18 -I%S -E %s 2>&1 | FileCheck %s +! CHECK-NOT: :3: +#include ! comment +! CHECK-NOT: :5: +#include /* comment */ +! CHECK-NOT: :7: +#include !comment +! CHECK: :9:20: #include: extra stuff ignored after file name +#include 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