[flang-commits] [flang] [flang] Fix issue when macro is followed by OpenMP pragma (PR #123035)
via flang-commits
flang-commits at lists.llvm.org
Tue Jan 21 04:32:13 PST 2025
https://github.com/shivaramaarao updated https://github.com/llvm/llvm-project/pull/123035
>From 35bbc60b45832339cb13af5fea20eedd1f99551c Mon Sep 17 00:00:00 2001
From: Shivarama Rao <shivarama.rao at amd.com>
Date: Wed, 15 Jan 2025 09:44:04 +0000
Subject: [PATCH] When calling IsCompilerDirectiveSentinel,the prefix comment
character need to be skipped.
Fixes #117693
---
flang/lib/Parser/prescan.cpp | 17 ++++++++++-------
flang/lib/Parser/token-sequence.cpp | 3 ++-
flang/test/Preprocessing/bug117693.f90 | 14 ++++++++++++++
flang/test/Preprocessing/bug117693_2.f90 | 15 +++++++++++++++
flang/test/Preprocessing/bug117693_3.f90 | 7 +++++++
5 files changed, 48 insertions(+), 8 deletions(-)
create mode 100644 flang/test/Preprocessing/bug117693.f90
create mode 100644 flang/test/Preprocessing/bug117693_2.f90
create mode 100644 flang/test/Preprocessing/bug117693_3.f90
diff --git a/flang/lib/Parser/prescan.cpp b/flang/lib/Parser/prescan.cpp
index c5939a1e0b6c2c..f85c9fab2b4d92 100644
--- a/flang/lib/Parser/prescan.cpp
+++ b/flang/lib/Parser/prescan.cpp
@@ -175,7 +175,7 @@ void Prescanner::Statement() {
EmitChar(tokens, '!');
++at_, ++column_;
for (const char *sp{directiveSentinel_}; *sp != '\0';
- ++sp, ++at_, ++column_) {
+ ++sp, ++at_, ++column_) {
EmitChar(tokens, *sp);
}
if (IsSpaceOrTab(at_)) {
@@ -306,8 +306,9 @@ void Prescanner::Statement() {
preprocessed->RemoveRedundantBlanks();
}
}
- CheckAndEmitLine(
- preprocessed->ToLowerCase().ClipComment(*this), newlineProvenance);
+ CheckAndEmitLine(preprocessed->ToLowerCase().ClipComment(
+ *this, true) /* skip first ! */,
+ newlineProvenance);
break;
}
} else { // no macro replacement
@@ -317,13 +318,15 @@ void Prescanner::Statement() {
}
tokens.ToLowerCase();
SourceFormChange(tokens.ToString());
+ CheckAndEmitLine(tokens.ClipComment(*this, true) /* skip first ! */,
+ newlineProvenance);
} else { // Kind::Source
tokens.ToLowerCase();
if (inFixedForm_) {
EnforceStupidEndStatementRules(tokens);
}
+ CheckAndEmitLine(tokens.ClipComment(*this, false), newlineProvenance);
}
- CheckAndEmitLine(tokens, newlineProvenance);
}
directiveSentinel_ = nullptr;
}
@@ -511,7 +514,7 @@ bool Prescanner::MustSkipToEndOfLine() const {
if (inFixedForm_ && column_ > fixedFormColumnLimit_ && !tabInCurrentLine_) {
return true; // skip over ignored columns in right margin (73:80)
} else if (*at_ == '!' && !inCharLiteral_) {
- return !IsCompilerDirectiveSentinel(at_);
+ return !IsCompilerDirectiveSentinel(at_ + 1);
} else {
return false;
}
@@ -1073,7 +1076,7 @@ std::optional<std::size_t> Prescanner::IsIncludeLine(const char *start) const {
}
if (IsDecimalDigit(*p)) { // accept & ignore a numeric kind prefix
for (p = SkipWhiteSpace(p + 1); IsDecimalDigit(*p);
- p = SkipWhiteSpace(p + 1)) {
+ p = SkipWhiteSpace(p + 1)) {
}
if (*p != '_') {
return std::nullopt;
@@ -1121,7 +1124,7 @@ void Prescanner::FortranInclude(const char *firstQuote) {
llvm::raw_string_ostream error{buf};
Provenance provenance{GetProvenance(nextLine_)};
std::optional<std::string> prependPath;
- if (const SourceFile * currentFile{allSources_.GetSourceFile(provenance)}) {
+ if (const SourceFile *currentFile{allSources_.GetSourceFile(provenance)}) {
prependPath = DirectoryName(currentFile->path());
}
const SourceFile *included{
diff --git a/flang/lib/Parser/token-sequence.cpp b/flang/lib/Parser/token-sequence.cpp
index cdbe89b1eb441c..2c0f3820f2c2ff 100644
--- a/flang/lib/Parser/token-sequence.cpp
+++ b/flang/lib/Parser/token-sequence.cpp
@@ -288,7 +288,8 @@ TokenSequence &TokenSequence::ClipComment(
isSentinel = prescanner.IsCompilerDirectiveSentinel(&tok[blanks + 1])
.has_value();
}
- if (isSentinel) {
+ if (isSentinel && skipFirst) {
+ skipFirst = false;
} else if (skipFirst) {
skipFirst = false;
} else {
diff --git a/flang/test/Preprocessing/bug117693.f90 b/flang/test/Preprocessing/bug117693.f90
new file mode 100644
index 00000000000000..ced7927606e623
--- /dev/null
+++ b/flang/test/Preprocessing/bug117693.f90
@@ -0,0 +1,14 @@
+! RUN: %flang -fopenmp -E %s 2>&1 | FileCheck %s
+!CHECK: !$OMP DO SCHEDULE(STATIC)
+program main
+IMPLICIT NONE
+INTEGER:: I
+#define OMPSUPPORT
+!$ INTEGER :: omp_id
+!$OMP PARALLEL DO
+OMPSUPPORT !$OMP DO SCHEDULE(STATIC)
+DO I=1,100
+print *, omp_id
+ENDDO
+!$OMP END PARALLEL DO
+end program
diff --git a/flang/test/Preprocessing/bug117693_2.f90 b/flang/test/Preprocessing/bug117693_2.f90
new file mode 100644
index 00000000000000..e739753ab97206
--- /dev/null
+++ b/flang/test/Preprocessing/bug117693_2.f90
@@ -0,0 +1,15 @@
+! RUN: %flang -fopenmp -E %s 2>&1 | FileCheck %s
+!CHECK: !$OMP DO SCHEDULE(STATIC)
+!CHECK-NOT: !$OMP DEFAULT(NONE)
+program main
+IMPLICIT NONE
+INTEGER:: I
+#define OMPSUPPORT
+!$ INTEGER :: omp_id
+!$OMP PARALLEL DO
+OMPSUPPORT !$OMP DO SCHEDULE(STATIC) !$OMP DEFAULT(NONE)
+DO I=1,100
+print *, omp_id
+ENDDO
+!$OMP END PARALLEL DO
+end program
diff --git a/flang/test/Preprocessing/bug117693_3.f90 b/flang/test/Preprocessing/bug117693_3.f90
new file mode 100644
index 00000000000000..1efcf8264ce80a
--- /dev/null
+++ b/flang/test/Preprocessing/bug117693_3.f90
@@ -0,0 +1,7 @@
+! RUN: %flang -fopenmp -E %s 2>&1 | FileCheck %s
+!CHECK-NOT: DO I=1,100 !$OMP
+program main
+INTEGER::n
+DO I=1,100 !$OMP
+ENDDO
+END PROGRAM
More information about the flang-commits
mailing list