[flang-commits] [flang] c7f8bc0 - [flang][parser] Diagnose an invalid space

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Fri Jan 27 15:01:42 PST 2023


Author: Peter Klausler
Date: 2023-01-27T14:54:53-08:00
New Revision: c7f8bc0ee8d391e47286adcf6998a217bcfebd32

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

LOG: [flang][parser] Diagnose an invalid space

In free form, don't silently skip over an invalid space between
an underscore and a kind parameter.

Differential Revision: https://reviews.llvm.org/D142760

Added: 
    flang/test/Parser/bad-space.f90

Modified: 
    flang/lib/Parser/Fortran-parsers.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Parser/Fortran-parsers.cpp b/flang/lib/Parser/Fortran-parsers.cpp
index a6bec93517ce7..1e7d7deed0a4f 100644
--- a/flang/lib/Parser/Fortran-parsers.cpp
+++ b/flang/lib/Parser/Fortran-parsers.cpp
@@ -230,19 +230,24 @@ TYPE_PARSER(construct<KindSelector>(
         construct<KindSelector>(construct<KindSelector::StarSize>(
             "*" >> digitString64 / spaceCheck))))
 
+constexpr auto noSpace{
+    recovery(withMessage("invalid space"_err_en_US, !" "_ch), space)};
+
 // R707 signed-int-literal-constant -> [sign] int-literal-constant
-TYPE_PARSER(sourced(construct<SignedIntLiteralConstant>(
-    SignedIntLiteralConstantWithoutKind{}, maybe(underscore >> kindParam))))
+TYPE_PARSER(sourced(
+    construct<SignedIntLiteralConstant>(SignedIntLiteralConstantWithoutKind{},
+        maybe(noSpace >> underscore >> noSpace >> kindParam))))
 
 // R708 int-literal-constant -> digit-string [_ kind-param]
 // The negated look-ahead for a trailing underscore prevents misrecognition
 // when the digit string is a numeric kind parameter of a character literal.
-TYPE_PARSER(construct<IntLiteralConstant>(
-    space >> digitString, maybe(underscore >> kindParam) / !underscore))
+TYPE_PARSER(construct<IntLiteralConstant>(space >> digitString,
+    maybe(underscore >> noSpace >> kindParam) / !underscore))
 
 // R709 kind-param -> digit-string | scalar-int-constant-name
 TYPE_PARSER(construct<KindParam>(digitString64) ||
-    construct<KindParam>(scalar(integer(constant(name)))))
+    construct<KindParam>(
+        scalar(integer(constant(sourced(rawName >> construct<Name>()))))))
 
 // R712 sign -> + | -
 // N.B. A sign constitutes a whole token, so a space is allowed in free form
@@ -278,7 +283,7 @@ TYPE_CONTEXT_PARSER("REAL literal constant"_en_US,
                         "."_ch >> digitString >> maybe(exponentPart) >> ok ||
                         digitString >> exponentPart >> ok) >>
                 construct<RealLiteralConstant::Real>()),
-            maybe(underscore >> kindParam)))
+            maybe(noSpace >> underscore >> noSpace >> kindParam)))
 
 // R718 complex-literal-constant -> ( real-part , imag-part )
 TYPE_CONTEXT_PARSER("COMPLEX literal constant"_en_US,
@@ -343,10 +348,10 @@ TYPE_CONTEXT_PARSER(
 // R725 logical-literal-constant ->
 //        .TRUE. [_ kind-param] | .FALSE. [_ kind-param]
 // Also accept .T. and .F. as extensions.
-TYPE_PARSER(construct<LogicalLiteralConstant>(
-                logicalTRUE, maybe(underscore >> kindParam)) ||
+TYPE_PARSER(construct<LogicalLiteralConstant>(logicalTRUE,
+                maybe(noSpace >> underscore >> noSpace >> kindParam)) ||
     construct<LogicalLiteralConstant>(
-        logicalFALSE, maybe(underscore >> kindParam)))
+        logicalFALSE, maybe(noSpace >> underscore >> noSpace >> kindParam)))
 
 // R726 derived-type-def ->
 //        derived-type-stmt [type-param-def-stmt]...

diff  --git a/flang/test/Parser/bad-space.f90 b/flang/test/Parser/bad-space.f90
new file mode 100644
index 0000000000000..3b086b070db67
--- /dev/null
+++ b/flang/test/Parser/bad-space.f90
@@ -0,0 +1,4 @@
+! RUN: not %flang_fc1 -fsyntax-only %s 2>&1 | FileCheck %s
+! CHECK: 3:8: error: invalid space
+x = 1._ 4
+end


        


More information about the flang-commits mailing list