[flang-commits] [flang] 4706880 - [flang] Allow Fortran comments after #include path

peter klausler via flang-commits flang-commits at lists.llvm.org
Mon Sep 14 16:58:42 PDT 2020


Author: peter klausler
Date: 2020-09-14T16:58:14-07:00
New Revision: 4706880f06fbaf5f95dab2b6fd4cd2a5cf1693e6

URL: https://github.com/llvm/llvm-project/commit/4706880f06fbaf5f95dab2b6fd4cd2a5cf1693e6
DIFF: https://github.com/llvm/llvm-project/commit/4706880f06fbaf5f95dab2b6fd4cd2a5cf1693e6.diff

LOG: [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

Added: 
    flang/test/Preprocessing/empty.h
    flang/test/Preprocessing/include-comment.F90

Modified: 
    flang/lib/Parser/preprocessor.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Parser/preprocessor.cpp b/flang/lib/Parser/preprocessor.cpp
index a1f07967d9b0..823adda8e95a 100644
--- a/flang/lib/Parser/preprocessor.cpp
+++ b/flang/lib/Parser/preprocessor.cpp
@@ -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)};

diff  --git a/flang/test/Preprocessing/empty.h b/flang/test/Preprocessing/empty.h
new file mode 100644
index 000000000000..e69de29bb2d1

diff  --git a/flang/test/Preprocessing/include-comment.F90 b/flang/test/Preprocessing/include-comment.F90
new file mode 100644
index 000000000000..6ac475f76e46
--- /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 <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


        


More information about the flang-commits mailing list