Skip to content

Commit 27d7ded

Browse files
N00byKingsivan-shani
authored andcommitted
[flang] Add __COUNTER__ preprocessor macro (llvm#136827)
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.
1 parent ede8d25 commit 27d7ded

File tree

5 files changed

+29
-0
lines changed

5 files changed

+29
-0
lines changed

flang/docs/Extensions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,8 @@ end
521521
* We respect Fortran comments in macro actual arguments (like GNU, Intel, NAG;
522522
unlike PGI and XLF) on the principle that macro calls should be treated
523523
like function references. Fortran's line continuation methods also work.
524+
* We implement the `__COUNTER__` preprocessing extension,
525+
see [Non-standard Extensions](Preprocessing.md#non-standard-extensions)
524526

525527
## Standard features not silently accepted
526528

flang/docs/Preprocessing.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,18 @@ text.
138138
OpenMP-style directives that look like comments are not addressed by
139139
this scheme but are obvious extensions.
140140

141+
## Currently implemented built-ins
142+
143+
* `__DATE__`: Date, given as e.g. "Jun 16 1904"
144+
* `__TIME__`: Time in 24-hour format including seconds, e.g. "09:24:13"
145+
* `__TIMESTAMP__`: Date, time and year of last modification, given as e.g. "Fri May 9 09:16:17 2025"
146+
* `__FILE__`: Current file
147+
* `__LINE__`: Current line
148+
149+
### Non-standard Extensions
150+
151+
* `__COUNTER__`: Replaced by sequential integers on each expansion, starting from 0.
152+
141153
## Appendix
142154
`N` in the table below means "not supported"; this doesn't
143155
mean a bug, it just means that a particular behavior was

flang/include/flang/Parser/preprocessor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ class Preprocessor {
122122
std::list<std::string> names_;
123123
std::unordered_map<CharBlock, Definition> definitions_;
124124
std::stack<CanDeadElseAppear> ifStack_;
125+
126+
unsigned int counterVal_{0};
125127
};
126128
} // namespace Fortran::parser
127129
#endif // FORTRAN_PARSER_PREPROCESSOR_H_

flang/lib/Parser/preprocessor.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <memory>
2323
#include <optional>
2424
#include <set>
25+
#include <string>
2526
#include <utility>
2627
#include <vector>
2728

@@ -299,6 +300,7 @@ void Preprocessor::DefineStandardMacros() {
299300
Define("__FILE__"s, "__FILE__"s);
300301
Define("__LINE__"s, "__LINE__"s);
301302
Define("__TIMESTAMP__"s, "__TIMESTAMP__"s);
303+
Define("__COUNTER__"s, "__COUNTER__"s);
302304
}
303305

304306
static const std::string idChars{
@@ -495,6 +497,8 @@ std::optional<TokenSequence> Preprocessor::MacroReplacement(
495497
repl = "\""s + time + '"';
496498
}
497499
}
500+
} else if (name == "__COUNTER__") {
501+
repl = std::to_string(counterVal_++);
498502
}
499503
if (!repl.empty()) {
500504
ProvenanceRange insert{allSources_.AddCompilerInsertion(repl)};

flang/test/Preprocessing/counter.F90

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
! RUN: %flang -E %s | FileCheck %s
2+
! CHECK: print *, 0
3+
! CHECK: print *, 1
4+
! CHECK: print *, 2
5+
! Check incremental counter macro
6+
#define foo bar
7+
print *, __COUNTER__
8+
print *, __COUNTER__
9+
print *, __COUNTER__

0 commit comments

Comments
 (0)