Skip to content

[flang] Handle substring in data statement constant #120130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 17, 2024

Conversation

klausler
Copy link
Contributor

The case of a constant substring wasn't handled in the parser for data statement constants.

Fixes #119005.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:parser labels Dec 16, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 16, 2024

@llvm/pr-subscribers-flang-parser

Author: Peter Klausler (klausler)

Changes

The case of a constant substring wasn't handled in the parser for data statement constants.

Fixes #119005.


Full diff: https://github.com/llvm/llvm-project/pull/120130.diff

5 Files Affected:

  • (modified) flang/include/flang/Parser/parse-tree.h (+4-3)
  • (modified) flang/lib/Parser/Fortran-parsers.cpp (+3-1)
  • (modified) flang/lib/Parser/expr-parsers.cpp (+1-1)
  • (modified) flang/lib/Parser/type-parsers.h (+1)
  • (added) flang/test/Parser/lit-substr-data.f90 (+7)
diff --git a/flang/include/flang/Parser/parse-tree.h b/flang/include/flang/Parser/parse-tree.h
index 1d97126d17dbc4..f87a1cfceb37ba 100644
--- a/flang/include/flang/Parser/parse-tree.h
+++ b/flang/include/flang/Parser/parse-tree.h
@@ -1481,9 +1481,10 @@ struct DataStmtConstant {
   UNION_CLASS_BOILERPLATE(DataStmtConstant);
   CharBlock source;
   mutable TypedExpr typedExpr;
-  std::variant<LiteralConstant, SignedIntLiteralConstant,
-      SignedRealLiteralConstant, SignedComplexLiteralConstant, NullInit,
-      common::Indirection<Designator>, StructureConstructor>
+  std::variant<common::Indirection<CharLiteralConstantSubstring>,
+      LiteralConstant, SignedIntLiteralConstant, SignedRealLiteralConstant,
+      SignedComplexLiteralConstant, NullInit, common::Indirection<Designator>,
+      StructureConstructor>
       u;
 };
 
diff --git a/flang/lib/Parser/Fortran-parsers.cpp b/flang/lib/Parser/Fortran-parsers.cpp
index a3d2c363108073..426d1d01793612 100644
--- a/flang/lib/Parser/Fortran-parsers.cpp
+++ b/flang/lib/Parser/Fortran-parsers.cpp
@@ -930,7 +930,9 @@ TYPE_PARSER(construct<DataStmtRepeat>(intLiteralConstant) ||
 // So we parse literal constants, designator, null-init, and
 // structure-constructor, so that semantics can figure things out later
 // with the symbol table.
-TYPE_PARSER(sourced(first(construct<DataStmtConstant>(literalConstant),
+TYPE_PARSER(sourced(first(
+    construct<DataStmtConstant>(indirect(charLiteralConstantSubstring)),
+    construct<DataStmtConstant>(literalConstant),
     construct<DataStmtConstant>(signedRealLiteralConstant),
     construct<DataStmtConstant>(signedIntLiteralConstant),
     extension<LanguageFeature::SignedComplexLiteral>(
diff --git a/flang/lib/Parser/expr-parsers.cpp b/flang/lib/Parser/expr-parsers.cpp
index 77a13de7fd02d8..0b6e21e1ba2ff2 100644
--- a/flang/lib/Parser/expr-parsers.cpp
+++ b/flang/lib/Parser/expr-parsers.cpp
@@ -68,7 +68,7 @@ TYPE_PARSER(construct<AcImpliedDoControl>(
 // type-param-inquiry is parsed as a structure component, except for
 // substring%KIND/LEN
 constexpr auto primary{instrumented("primary"_en_US,
-    first(construct<Expr>(indirect(Parser<CharLiteralConstantSubstring>{})),
+    first(construct<Expr>(indirect(charLiteralConstantSubstring)),
         construct<Expr>(literalConstant),
         construct<Expr>(construct<Expr::Parentheses>("(" >>
             expr / !","_tok / recovery(")"_tok, SkipPastNested<'(', ')'>{}))),
diff --git a/flang/lib/Parser/type-parsers.h b/flang/lib/Parser/type-parsers.h
index d7e0cd06c3f444..623437f9d2e1d8 100644
--- a/flang/lib/Parser/type-parsers.h
+++ b/flang/lib/Parser/type-parsers.h
@@ -63,6 +63,7 @@ constexpr Parser<KindParam> kindParam; // R709
 constexpr Parser<RealLiteralConstant> realLiteralConstant; // R714
 constexpr Parser<CharLength> charLength; // R723
 constexpr Parser<CharLiteralConstant> charLiteralConstant; // R724
+constexpr Parser<CharLiteralConstantSubstring> charLiteralConstantSubstring;
 constexpr Parser<Initialization> initialization; // R743 & R805
 constexpr Parser<DerivedTypeSpec> derivedTypeSpec; // R754
 constexpr Parser<TypeDeclarationStmt> typeDeclarationStmt; // R801
diff --git a/flang/test/Parser/lit-substr-data.f90 b/flang/test/Parser/lit-substr-data.f90
new file mode 100644
index 00000000000000..7eed616a1ee2ec
--- /dev/null
+++ b/flang/test/Parser/lit-substr-data.f90
@@ -0,0 +1,7 @@
+!RUN: %flang_fc1 -fdebug-unparse %s 2>&1 | FileCheck %s
+!Regression test for bug #119005
+character*2 :: ary4
+!CHECK: DATA ary4/"cd"/
+data ary4/"abcdef"(3:4)/
+end
+

@@ -930,7 +930,9 @@ TYPE_PARSER(construct<DataStmtRepeat>(intLiteralConstant) ||
// So we parse literal constants, designator, null-init, and
// structure-constructor, so that semantics can figure things out later
// with the symbol table.
TYPE_PARSER(sourced(first(construct<DataStmtConstant>(literalConstant),
TYPE_PARSER(sourced(first(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the comment above (starting on line 921) could be updated to include literal constant substrings?

Copy link
Contributor

@psteinfeld psteinfeld left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All builds and tests correctly and looks good.

@@ -930,7 +930,9 @@ TYPE_PARSER(construct<DataStmtRepeat>(intLiteralConstant) ||
// So we parse literal constants, designator, null-init, and
// structure-constructor, so that semantics can figure things out later
// with the symbol table.
TYPE_PARSER(sourced(first(construct<DataStmtConstant>(literalConstant),
TYPE_PARSER(sourced(first(
construct<DataStmtConstant>(indirect(charLiteralConstantSubstring)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: indirect here is just to ensure that charLiteralConstantSubstring is not null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it allows the type CharLiteralConstantSubstring to be forward-referenced before it can be defined.

The case of a constant substring wasn't handled in the parser
for data statement constants.

Fixes llvm#119005.
Copy link
Contributor

@eugeneepshteyn eugeneepshteyn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment change request, but otherwise LGTM

@klausler klausler merged commit a957ced into llvm:main Dec 17, 2024
8 checks passed
@klausler klausler deleted the bug119005 branch December 17, 2024 20:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:parser flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[flang] Allow constant expressions in DATA statements
4 participants