Skip to content

[flang] Fixes in preprocessing conditional compilation code when preceded by macros #129015

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

shivaramaarao
Copy link
Contributor

The compiler directives and conditional compilation codes are getting ignored in the presence of preprocessor macros. The necessary changes are added to identify the compiler directives and to emit proper preprocessor outputs.

Fixes issue #126459

The compiler directive after preprocessor macro is getting ignored. The necessary changes are
added to identify the compiler directives and emit proper preprocessor outputwq!
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:parser labels Feb 27, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 27, 2025

@llvm/pr-subscribers-flang-parser

Author: None (shivaramaarao)

Changes

The compiler directives and conditional compilation codes are getting ignored in the presence of preprocessor macros. The necessary changes are added to identify the compiler directives and to emit proper preprocessor outputs.

Fixes issue #126459


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

5 Files Affected:

  • (modified) flang/include/flang/Parser/parsing.h (+3)
  • (modified) flang/lib/Parser/parsing.cpp (+9-1)
  • (modified) flang/lib/Parser/prescan.cpp (+1-1)
  • (modified) flang/lib/Parser/prescan.h (+3)
  • (added) flang/test/Preprocessing/bug126459.f90 (+8)
diff --git a/flang/include/flang/Parser/parsing.h b/flang/include/flang/Parser/parsing.h
index 0c774decb16d3..029b5115707b5 100644
--- a/flang/include/flang/Parser/parsing.h
+++ b/flang/include/flang/Parser/parsing.h
@@ -19,6 +19,7 @@
 #include "llvm/Support/raw_ostream.h"
 #include <optional>
 #include <string>
+#include <unordered_set>
 #include <utility>
 #include <vector>
 
@@ -66,6 +67,7 @@ class Parsing {
   void DumpProvenance(llvm::raw_ostream &) const;
   void DumpParsingLog(llvm::raw_ostream &) const;
   void Parse(llvm::raw_ostream &debugOutput);
+  const char *IsCompilerDirectiveSentinel(const char *, std::size_t) const;
   void ClearLog();
 
   void EmitMessage(llvm::raw_ostream &o, const char *at,
@@ -87,6 +89,7 @@ class Parsing {
   std::optional<Program> parseTree_;
   ParsingLog log_;
   Preprocessor preprocessor_{allCooked_.allSources()};
+  std::unordered_set<std::string> compilerDirectiveSentinels_;
 };
 } // namespace Fortran::parser
 #endif // FORTRAN_PARSER_PARSING_H_
diff --git a/flang/lib/Parser/parsing.cpp b/flang/lib/Parser/parsing.cpp
index 8fcac7b3cacb1..d161809bf7401 100644
--- a/flang/lib/Parser/parsing.cpp
+++ b/flang/lib/Parser/parsing.cpp
@@ -106,6 +106,7 @@ const SourceFile *Parsing::Prescan(const std::string &path, Options options) {
   if (options.showColors) {
     allSources.setShowColors(/*showColors=*/true);
   }
+  compilerDirectiveSentinels_ = prescanner.getCompilerDirectiveSentinels();
   return sourceFile;
 }
 
@@ -153,7 +154,8 @@ void Parsing::EmitPreprocessedSource(
         return ch;
       }};
 
-      if (ch == '!' && lineWasBlankBefore) {
+      if (ch == '!' && lineWasBlankBefore &&
+          IsCompilerDirectiveSentinel((const char *)&atChar + 1, 4)) {
         // Other comment markers (C, *, D) in original fixed form source
         // input card column 1 will have been deleted or normalized to !,
         // which signifies a comment (directive) in both source forms.
@@ -258,6 +260,12 @@ void Parsing::Parse(llvm::raw_ostream &out) {
   finalRestingPlace_ = parseState.GetLocation();
 }
 
+const char *Parsing::IsCompilerDirectiveSentinel(
+    const char *sentinel, std::size_t len) const {
+  const auto iter{compilerDirectiveSentinels_.find(std::string(sentinel, len))};
+  return iter == compilerDirectiveSentinels_.end() ? nullptr : iter->c_str();
+}
+
 void Parsing::ClearLog() { log_.clear(); }
 
 } // namespace Fortran::parser
diff --git a/flang/lib/Parser/prescan.cpp b/flang/lib/Parser/prescan.cpp
index c5939a1e0b6c2..b928ed03a2493 100644
--- a/flang/lib/Parser/prescan.cpp
+++ b/flang/lib/Parser/prescan.cpp
@@ -511,7 +511,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;
   }
diff --git a/flang/lib/Parser/prescan.h b/flang/lib/Parser/prescan.h
index 08041f93b14b6..9099085d079cc 100644
--- a/flang/lib/Parser/prescan.h
+++ b/flang/lib/Parser/prescan.h
@@ -90,6 +90,9 @@ class Prescanner {
   template <typename... A> Message &Say(A &&...a) {
     return messages_.Say(std::forward<A>(a)...);
   }
+  std::unordered_set<std::string> getCompilerDirectiveSentinels() {
+    return compilerDirectiveSentinels_;
+  }
 
 private:
   struct LineClassification {
diff --git a/flang/test/Preprocessing/bug126459.f90 b/flang/test/Preprocessing/bug126459.f90
new file mode 100644
index 0000000000000..95f3e792560e4
--- /dev/null
+++ b/flang/test/Preprocessing/bug126459.f90
@@ -0,0 +1,8 @@
+! RUN: %flang -fopenmp -E %s 2>&1 | FileCheck %s
+!CHECK: !$ NDIR=0
+program main
+integer NDIR
+#define BLANKMACRO
+
+BLANKMACRO !$ NDIR=0
+end program main

@shivaramaarao shivaramaarao changed the title Fixes in preprocessing conditional compilation code when preceded by macros [flang] Fixes in preprocessing conditional compilation code when preceded by macros Feb 27, 2025
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.

2 participants