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

Merged
merged 2 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions flang/docs/Extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,8 @@ end
* We respect Fortran comments in macro actual arguments (like GNU, Intel, NAG;
unlike PGI and XLF) on the principle that macro calls should be treated
like function references. Fortran's line continuation methods also work.
* We implement the `__COUNTER__` preprocessing extension,
see [Non-standard Extensions](Preprocessing.md#non-standard-extensions)

## Standard features not silently accepted

Expand Down
12 changes: 12 additions & 0 deletions flang/docs/Preprocessing.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ text.
OpenMP-style directives that look like comments are not addressed by
this scheme but are obvious extensions.

## Currently implemented built-ins

* `__DATE__`: Date, given as e.g. "Jun 16 1904"
* `__TIME__`: Time in 24-hour format including seconds, e.g. "09:24:13"
* `__TIMESTAMP__`: Date, time and year of last modification, given as e.g. "Fri May 9 09:16:17 2025"
* `__FILE__`: Current file
* `__LINE__`: Current line

### Non-standard Extensions

* `__COUNTER__`: Replaced by sequential integers on each expansion, starting from 0.

## Appendix
`N` in the table below means "not supported"; this doesn't
mean a bug, it just means that a particular behavior was
Expand Down
2 changes: 2 additions & 0 deletions flang/include/flang/Parser/preprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ class Preprocessor {
std::list<std::string> names_;
std::unordered_map<CharBlock, Definition> definitions_;
std::stack<CanDeadElseAppear> ifStack_;

unsigned int counterVal_{0};
};
} // namespace Fortran::parser
#endif // FORTRAN_PARSER_PREPROCESSOR_H_
4 changes: 4 additions & 0 deletions flang/lib/Parser/preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -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);
}

static const std::string idChars{
Expand Down Expand Up @@ -495,6 +497,8 @@ std::optional<TokenSequence> Preprocessor::MacroReplacement(
repl = "\""s + time + '"';
}
}
} else if (name == "__COUNTER__") {
repl = std::to_string(counterVal_++);
}
if (!repl.empty()) {
ProvenanceRange insert{allSources_.AddCompilerInsertion(repl)};
Expand Down
9 changes: 9 additions & 0 deletions flang/test/Preprocessing/counter.F90
Original file line number Diff line number Diff line change
@@ -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__