[flang-commits] [flang] [flang] Add __COUNTER__ preprocessor macro (PR #136827)
Yussur Mustafa Oraji via flang-commits
flang-commits at lists.llvm.org
Thu May 8 06:13:08 PDT 2025
https://github.com/N00byKing updated https://github.com/llvm/llvm-project/pull/136827
>From 0b007f31fbc6f54571cae051f00d071edbe4962f Mon Sep 17 00:00:00 2001
From: Yussur Mustafa Oraji <yussur.oraji at tu-darmstadt.de>
Date: Wed, 23 Apr 2025 10:33:04 +0200
Subject: [PATCH] [flang] Add __COUNTER__ preprocessor macro
---
flang/include/flang/Parser/preprocessor.h | 2 ++
flang/lib/Parser/preprocessor.cpp | 4 ++++
flang/test/Preprocessing/counter.F90 | 9 +++++++++
3 files changed, 15 insertions(+)
create mode 100644 flang/test/Preprocessing/counter.F90
diff --git a/flang/include/flang/Parser/preprocessor.h b/flang/include/flang/Parser/preprocessor.h
index 86528a7e68def..6209f993f8ed0 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 int counter_val_{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__
More information about the flang-commits
mailing list