Skip to content

Commit 413e71b

Browse files
[flang] Main program symbol no longer conflicts with the other symbols (#149169)
The following code is now accepted: ``` module m end program m use m end ``` The PROGRAM name doesn't really have an effect on the compilation result, so it shouldn't result in symbol name conflicts. This change makes the main program symbol name all uppercase in the cooked character stream. This makes it distinct from all other symbol names that are all lowercase in cooked character stream. Modified the tests that were checking for lower case main program name.
1 parent afff28e commit 413e71b

File tree

108 files changed

+537
-513
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+537
-513
lines changed

flang/docs/Extensions.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
<!--===- docs/Extensions.md
2-
1+
<!--===- docs/Extensions.md
2+
33
Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
See https://llvm.org/LICENSE.txt for license information.
55
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6-
6+
77
-->
88

99
# Fortran Extensions supported by Flang
@@ -170,6 +170,18 @@ end
170170
In the case of `DEFERRED` bindings in an `ABSTRACT` derived type,
171171
however, overrides are necessary, so they are permitted for inaccessible
172172
bindings with an optional warning.
173+
* Main program name is allowed to be the same as the other symbols used
174+
in the main program, for example:
175+
```
176+
module m
177+
end
178+
program m
179+
use m
180+
end
181+
```
182+
Note that internally the main program symbol name is all uppercase, unlike
183+
the names of all other symbols, which are usually all lowercase. This
184+
may make a difference in testing/debugging.
173185

174186
## Extensions, deletions, and legacy features supported by default
175187

flang/lib/Semantics/check-omp-structure.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,8 +1156,7 @@ void OmpStructureChecker::CheckThreadprivateOrDeclareTargetVar(
11561156
(sym->has<MainProgramDetails>() ||
11571157
sym->has<ModuleDetails>())) {
11581158
context_.Say(name->source,
1159-
"The module name or main program name cannot be in a "
1160-
"%s "
1159+
"The module name cannot be in a %s "
11611160
"directive"_err_en_US,
11621161
ContextDirectiveAsFortran());
11631162
} else if (!IsSaved(*name->symbol) &&

flang/lib/Semantics/resolve-labels.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -489,15 +489,30 @@ class ParseTreeAnalyzer {
489489

490490
// C1401
491491
void Post(const parser::MainProgram &mainProgram) {
492+
// Uppercase the name of the main program, so that its symbol name
493+
// would be unique from similarly named non-main-program symbols.
494+
auto upperCaseCharBlock = [](const parser::CharBlock &cb) {
495+
char *ch{const_cast<char *>(cb.begin())};
496+
char *endCh{ch + cb.size()};
497+
while (ch != endCh) {
498+
*ch++ = parser::ToUpperCaseLetter(*ch);
499+
}
500+
};
501+
const parser::CharBlock *progName{nullptr};
502+
if (const auto &program{
503+
std::get<std::optional<parser::Statement<parser::ProgramStmt>>>(
504+
mainProgram.t)}) {
505+
progName = &program->statement.v.source;
506+
upperCaseCharBlock(*progName);
507+
}
492508
if (const parser::CharBlock *
493509
endName{GetStmtName(std::get<parser::Statement<parser::EndProgramStmt>>(
494510
mainProgram.t))}) {
495-
if (const auto &program{
496-
std::get<std::optional<parser::Statement<parser::ProgramStmt>>>(
497-
mainProgram.t)}) {
498-
if (*endName != program->statement.v.source) {
511+
upperCaseCharBlock(*endName);
512+
if (progName) {
513+
if (*endName != *progName) {
499514
context_.Say(*endName, "END PROGRAM name mismatch"_err_en_US)
500-
.Attach(program->statement.v.source, "should be"_en_US);
515+
.Attach(*progName, "should be"_en_US);
501516
}
502517
} else {
503518
context_.Say(*endName,

flang/test/Driver/cuda-option.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ program main
88
integer, device :: dvar
99
end program
1010

11-
! CHECK-LABEL: PROGRAM main
11+
! CHECK-LABEL: PROGRAM MAIN
1212
! CHECK: INTEGER :: var = 1
1313
! CHECK: INTEGER, DEVICE :: dvar
1414

flang/test/Driver/unparse-use-analyzed.f95

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
! RUN: %flang_fc1 -fdebug-unparse %s | FileCheck %s --check-prefix=DEFAULT
77
! RUN: %flang_fc1 -fdebug-unparse -fno-analyzed-objects-for-unparse %s | FileCheck %s --check-prefix=DISABLED
88

9-
! DEFAULT: PROGRAM test
9+
! DEFAULT: PROGRAM TEST
1010
! DEFAULT-NEXT: REAL, PARAMETER :: val = 3.43e2_4
1111
! DEFAULT-NEXT: PRINT *, 3.47e2_4
1212
! DEFAULT-NEXT: END PROGRAM
1313

14-
! DISABLED: PROGRAM test
14+
! DISABLED: PROGRAM TEST
1515
! DISABLED-NEXT: REAL, PARAMETER :: val = 343.0
1616
! DISABLED-NEXT: PRINT *, val+4
1717
! DISABLED-NEXT: END PROGRAM

flang/test/Driver/unparse-with-modules.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ program test
2525
!CHECK: implicit none
2626
!CHECK: real(kind=real32) x
2727
!CHECK: end module
28-
!CHECK: program test
28+
!CHECK: program TEST
2929
!CHECK: use :: m1
3030
!CHECK: use :: basictestmoduletwo
3131
!CHECK: implicit none

flang/test/Integration/debug-common-block-1.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ program test
8989
! CHECK-DAG: ![[CBF3]] = !DICommonBlock(scope: ![[F3]], declaration: null, name: "__BLNK__"{{.*}})
9090
! CHECK-DAG: ![[CBAF3]] = !DICommonBlock(scope: ![[F3]], declaration: null, name: "a"{{.*}})
9191

92-
! CHECK-DAG: ![[MAIN:[0-9]+]] = {{.*}}!DISubprogram(name: "test"{{.*}})
92+
! CHECK-DAG: ![[MAIN:[0-9]+]] = {{.*}}!DISubprogram(name: "TEST"{{.*}})
9393
! CHECK-DAG: ![[CBM]] = !DICommonBlock(scope: ![[MAIN]], declaration: null, name: "__BLNK__"{{.*}})
9494
! CHECK-DAG: ![[CBAM]] = !DICommonBlock(scope: ![[MAIN]], declaration: null, name: "a"{{.*}})
9595

flang/test/Integration/debug-local-var-2.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
! BOTH-LABEL: }
3838

3939
program mn
40-
! BOTH-DAG: ![[MAIN:.*]] = distinct !DISubprogram(name: "mn", {{.*}})
40+
! BOTH-DAG: ![[MAIN:.*]] = distinct !DISubprogram(name: "MN", {{.*}})
4141

4242
! BOTH-DAG: ![[TYI32:.*]] = !DIBasicType(name: "integer", size: 32, encoding: DW_ATE_signed)
4343
! BOTH-DAG: ![[TYI64:.*]] = !DIBasicType(name: "integer", size: 64, encoding: DW_ATE_signed)

flang/test/Lower/CUDA/cuda-derived.cuf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ program main
2525
type(t2) :: b
2626
end
2727

28-
! CHECK-LABEL: func.func @_QQmain() attributes {fir.bindc_name = "main"}
28+
! CHECK-LABEL: func.func @_QQmain() attributes {fir.bindc_name = "MAIN"}
2929
! CHECK: %{{.*}} = cuf.alloc !fir.type<_QMm1Tty_device{x:!fir.box<!fir.heap<!fir.array<?xi32>>>}> {bindc_name = "a", data_attr = #cuf.cuda<managed>, uniq_name = "_QFEa"}
3030
! CHECK: %{{.*}} = cuf.alloc !fir.type<_QMm1Tt2{b:!fir.type<_QMm1Tt1{a:!fir.box<!fir.heap<!fir.array<?xf32>>>}>}> {bindc_name = "b", data_attr = #cuf.cuda<managed>, uniq_name = "_QFEb"}

flang/test/Lower/CUDA/cuda-return01.cuf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ program main
2828
return
2929
end
3030

31-
! CHECK-LABEL: func.func @_QQmain() attributes {fir.bindc_name = "main"}
31+
! CHECK-LABEL: func.func @_QQmain() attributes {fir.bindc_name = "MAIN"}
3232
! CHECK: cuf.alloc !fir.box<!fir.heap<!fir.array<?xi32>>> {bindc_name = "a", data_attr = #cuf.cuda<device>, uniq_name = "_QFEa"} -> !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
3333
! CHECK-NOT: cuf.free

0 commit comments

Comments
 (0)