Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[flang] Add __COUNTER__ preprocessor macro #136827

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

N00byKing
Copy link

This commit adds support for the __COUNTER__ preprocessor macro, which works the same as the one found in clang.
It is useful to generate unique names at compile-time.

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 Apr 23, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 23, 2025

@llvm/pr-subscribers-flang-parser

Author: Yussur Mustafa Oraji (N00byKing)

Changes

This commit adds support for the __COUNTER__ preprocessor macro, which works the same as the one found in clang.
It is useful to generate unique names at compile-time.


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

3 Files Affected:

  • (modified) flang/include/flang/Parser/preprocessor.h (+2)
  • (modified) flang/lib/Parser/preprocessor.cpp (+4)
  • (added) flang/test/Preprocessing/counter.F90 (+9)
diff --git a/flang/include/flang/Parser/preprocessor.h b/flang/include/flang/Parser/preprocessor.h
index 86528a7e68def..34cf02ccb4596 100644
--- a/flang/include/flang/Parser/preprocessor.h
+++ b/flang/include/flang/Parser/preprocessor.h
@@ -121,6 +121,8 @@ class Preprocessor {
   std::list<std::string> names_;
   std::unordered_map<CharBlock, Definition> definitions_;
   std::stack<CanDeadElseAppear> ifStack_;
+
+  unsigned CounterValue = 0;
 };
 } // namespace Fortran::parser
 #endif // FORTRAN_PARSER_PREPROCESSOR_H_
diff --git a/flang/lib/Parser/preprocessor.cpp b/flang/lib/Parser/preprocessor.cpp
index a47f9c32ad27c..f4f716f7c1269 100644
--- a/flang/lib/Parser/preprocessor.cpp
+++ b/flang/lib/Parser/preprocessor.cpp
@@ -22,6 +22,7 @@
 #include <memory>
 #include <optional>
 #include <set>
+#include <string>
 #include <utility>
 #include <vector>
 
@@ -299,6 +300,7 @@ void Preprocessor::DefineStandardMacros() {
   Define("__FILE__"s, "__FILE__"s);
   Define("__LINE__"s, "__LINE__"s);
   Define("__TIMESTAMP__"s, "__TIMESTAMP__"s);
+  Define("__COUNTER__"s, "__COUNTER__"s);
 }
 
 void Preprocessor::Define(const std::string &macro, const std::string &value) {
@@ -421,6 +423,8 @@ std::optional<TokenSequence> Preprocessor::MacroReplacement(
               repl = "\""s + time + '"';
             }
           }
+        } else if (name == "__COUNTER__") {
+          repl = std::to_string(CounterValue++);
         }
         if (!repl.empty()) {
           ProvenanceRange insert{allSources_.AddCompilerInsertion(repl)};
diff --git a/flang/test/Preprocessing/counter.F90 b/flang/test/Preprocessing/counter.F90
new file mode 100644
index 0000000000000..9761c8fb7f355
--- /dev/null
+++ b/flang/test/Preprocessing/counter.F90
@@ -0,0 +1,9 @@
+! RUN: %flang -E %s | FileCheck %s
+! CHECK: print *, 0
+! CHECK: print *, 1
+! CHECK: print *, 2
+! Check incremental counter macro
+#define foo bar
+print *, __COUNTER__
+print *, __COUNTER__
+print *, __COUNTER__

@N00byKing
Copy link
Author

@akuhlens @eugeneepshteyn Ping for review. I dont have write access and can't specify reviewers.
I apologize in advance should this not be correct

@eugeneepshteyn
Copy link
Contributor

eugeneepshteyn commented May 8, 2025

@N00byKing , sorry it's the first time I see it, because you tagged me in the comment.

I guess, if you can't add the reviewers, you could post your patch to flang Discourse.

@eugeneepshteyn
Copy link
Contributor

Also adding @klausler , since this change implements GNU extension.

@N00byKing
Copy link
Author

Thank you for the quick response!
I've adjusted the variable style.
I will make sure to check discourse first next time :)

@eugeneepshteyn
Copy link
Contributor

Ok, I have no other comments, let's wait for Peter to see it.

Copy link
Contributor

@klausler klausler left a comment

Choose a reason for hiding this comment

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

There is no documentation for users.

@N00byKing
Copy link
Author

Wasn't quite sure where to put the documentation. I've added a section on builtins on the preprocessing page with a subsection on non-standard ones. Feel free to request changes though :)

@N00byKing
Copy link
Author

I've adjusted the extensions page with a link to the new preprocessing section

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.

LGTM

@eugeneepshteyn eugeneepshteyn requested a review from klausler May 13, 2025 10:48
Copy link
Contributor

@akuhlens akuhlens left a comment

Choose a reason for hiding this comment

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

LGTM - I would wait on the review from @klausler, but thanks for the addition.

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.

5 participants