Skip to content

Commit fc1c481

Browse files
authored
[flang][preprocessor] Change handling of macros in text from Fortran … (llvm#108113)
…INCLUDE lines The compiler current treats an INCLUDE line as essentially a synonym for a preprocessing #include directive. The causes macros that have been defined at the point where the INCLUDE line is processed to be replaced within the text of the included file. This behavior is surprising to users who expect an INCLUDE line to be expanded into its contents *after* preprocessing has been applied to the original source file, with no further macro expansion. Change INCLUDE line processing to use a fresh instance of Preprocessor containing no macro definitions except _CUDA in CUDA Fortran compilations and, if the original file was being preprocessed, the standard definitions of __FILE__, __LINE__, and so forth.
1 parent 1ad84d7 commit fc1c481

File tree

10 files changed

+27
-12
lines changed

10 files changed

+27
-12
lines changed

flang/lib/Parser/preprocessor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ void Preprocessor::Directive(const TokenSequence &dir, Prescanner &prescanner) {
769769
if (included->bytes() > 0) {
770770
ProvenanceRange fileRange{
771771
allSources_.AddIncludedFile(*included, dir.GetProvenanceRange())};
772-
Prescanner{prescanner, /*isNestedInIncludeDirective=*/true}
772+
Prescanner{prescanner, *this, /*isNestedInIncludeDirective=*/true}
773773
.set_encoding(included->encoding())
774774
.Prescan(fileRange);
775775
}

flang/lib/Parser/prescan.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ Prescanner::Prescanner(Messages &messages, CookedSource &cooked,
3232
backslashFreeFormContinuation_{preprocessor.AnyDefinitions()},
3333
encoding_{allSources_.encoding()} {}
3434

35-
Prescanner::Prescanner(const Prescanner &that, bool isNestedInIncludeDirective)
36-
: messages_{that.messages_}, cooked_{that.cooked_},
37-
preprocessor_{that.preprocessor_}, allSources_{that.allSources_},
38-
features_{that.features_},
35+
Prescanner::Prescanner(const Prescanner &that, Preprocessor &prepro,
36+
bool isNestedInIncludeDirective)
37+
: messages_{that.messages_}, cooked_{that.cooked_}, preprocessor_{prepro},
38+
allSources_{that.allSources_}, features_{that.features_},
3939
isNestedInIncludeDirective_{isNestedInIncludeDirective},
4040
backslashFreeFormContinuation_{that.backslashFreeFormContinuation_},
4141
inFixedForm_{that.inFixedForm_},
@@ -1104,7 +1104,14 @@ void Prescanner::FortranInclude(const char *firstQuote) {
11041104
provenance, static_cast<std::size_t>(p - nextLine_)};
11051105
ProvenanceRange fileRange{
11061106
allSources_.AddIncludedFile(*included, includeLineRange)};
1107-
Prescanner{*this, /*isNestedInIncludeDirective=*/false}
1107+
Preprocessor cleanPrepro{allSources_};
1108+
if (preprocessor_.IsNameDefined("__FILE__"s)) {
1109+
cleanPrepro.DefineStandardMacros(); // __FILE__, __LINE__, &c.
1110+
}
1111+
if (preprocessor_.IsNameDefined("_CUDA"s)) {
1112+
cleanPrepro.Define("_CUDA"s, "1");
1113+
}
1114+
Prescanner{*this, cleanPrepro, /*isNestedInIncludeDirective=*/false}
11081115
.set_encoding(included->encoding())
11091116
.Prescan(fileRange);
11101117
}

flang/lib/Parser/prescan.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ class Prescanner {
3535
public:
3636
Prescanner(Messages &, CookedSource &, Preprocessor &,
3737
common::LanguageFeatureControl);
38-
Prescanner(const Prescanner &, bool isNestedInIncludeDirective);
38+
Prescanner(
39+
const Prescanner &, Preprocessor &, bool isNestedInIncludeDirective);
3940
Prescanner(const Prescanner &) = delete;
4041
Prescanner(Prescanner &&) = delete;
4142

flang/module/__fortran_builtins.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
!
77
!===------------------------------------------------------------------------===!
88

9-
include '../include/flang/Runtime/magic-numbers.h'
9+
#include '../include/flang/Runtime/magic-numbers.h'
1010

1111
! These naming shenanigans prevent names from Fortran intrinsic modules
1212
! from being usable on INTRINSIC statements, and force the program

flang/module/__fortran_ieee_exceptions.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
! here under another name so that IEEE_ARITHMETIC can USE it and export its
1212
! declarations without clashing with a non-intrinsic module in a program.
1313

14-
include '../include/flang/Runtime/magic-numbers.h'
14+
#include '../include/flang/Runtime/magic-numbers.h'
1515

1616
module __fortran_ieee_exceptions
1717
use __fortran_builtins, only: &

flang/module/ieee_arithmetic.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
! Fortran 2018 Clause 17
1010

11-
include '../include/flang/Runtime/magic-numbers.h'
11+
#include '../include/flang/Runtime/magic-numbers.h'
1212

1313
module ieee_arithmetic
1414
! F18 Clause 17.1p1:

flang/module/iso_fortran_env.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
! See Fortran 2023, subclause 16.10.2
1010

11-
include '../include/flang/Runtime/magic-numbers.h'
11+
#include '../include/flang/Runtime/magic-numbers.h'
1212

1313
module iso_fortran_env
1414

flang/test/Driver/include-header.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ program B
5151
end
5252

5353
! include-test-two.f90
54-
INCLUDE "basic-header-two.h"
54+
#include "basic-header-two.h"
5555
#ifdef Y
5656
program Y
5757
#else
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print *, sin(0.), j
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
! RUN: %flang_fc1 -fdebug-unparse %s -Dj=1 2>&1 | FileCheck %s
2+
! Ensure that macro definitions don't affect INCLUDE lines (unlike #include)
3+
#define sin cos
4+
!CHECK: PRINT *, 0._4, j
5+
include "include-file.h"
6+
end

0 commit comments

Comments
 (0)