-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[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
base: main
Are you sure you want to change the base?
Conversation
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 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. |
@llvm/pr-subscribers-flang-parser Author: Yussur Mustafa Oraji (N00byKing) ChangesThis commit adds support for the Full diff: https://github.com/llvm/llvm-project/pull/136827.diff 3 Files Affected:
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 ¯o, 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__
|
@akuhlens @eugeneepshteyn Ping for review. I dont have write access and can't specify reviewers. |
@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. |
Also adding @klausler , since this change implements GNU extension. |
Thank you for the quick response! |
Ok, I have no other comments, let's wait for Peter to see it. |
There was a problem hiding this 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.
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 :) |
I've adjusted the extensions page with a link to the new preprocessing section |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this 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.
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.