-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[flang] Support -D for function-like macros #139812
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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Handle a command-line function-like macro definition like "-Dfoo(a)=...". TODO: error reporting for badly formed argument lists.
@llvm/pr-subscribers-flang-parser Author: Peter Klausler (klausler) ChangesHandle a command-line function-like macro definition like "-Dfoo(a)=...". TODO: error reporting for badly formed argument lists. Full diff: https://github.com/llvm/llvm-project/pull/139812.diff 3 Files Affected:
diff --git a/flang/include/flang/Parser/preprocessor.h b/flang/include/flang/Parser/preprocessor.h
index 86528a7e68def..15810a34ee6a5 100644
--- a/flang/include/flang/Parser/preprocessor.h
+++ b/flang/include/flang/Parser/preprocessor.h
@@ -116,6 +116,7 @@ class Preprocessor {
bool IsIfPredicateTrue(const TokenSequence &expr, std::size_t first,
std::size_t exprTokens, Prescanner &);
void LineDirective(const TokenSequence &, std::size_t, Prescanner &);
+ TokenSequence TokenizeMacroBody(const std::string &);
AllSources &allSources_;
std::list<std::string> names_;
diff --git a/flang/lib/Parser/preprocessor.cpp b/flang/lib/Parser/preprocessor.cpp
index 6e8e3aee19b09..a5de14d864762 100644
--- a/flang/lib/Parser/preprocessor.cpp
+++ b/flang/lib/Parser/preprocessor.cpp
@@ -301,8 +301,82 @@ void Preprocessor::DefineStandardMacros() {
Define("__TIMESTAMP__"s, "__TIMESTAMP__"s);
}
+static const std::string idChars{
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789"s};
+
+static std::optional<std::vector<std::string>> TokenizeMacroNameAndArgs(
+ const std::string &str) {
+ // TODO: variadic macros on the command line (?)
+ std::vector<std::string> names;
+ for (std::string::size_type at{0};;) {
+ auto nameStart{str.find_first_not_of(" "s, at)};
+ if (nameStart == str.npos) {
+ return std::nullopt;
+ }
+ auto nameEnd{str.find_first_not_of(idChars, nameStart)};
+ if (nameEnd == str.npos) {
+ return std::nullopt;
+ }
+ auto punc{str.find_first_not_of(" "s, nameEnd)};
+ if (punc == str.npos) {
+ return std::nullopt;
+ }
+ if ((at == 0 && str[punc] != '(') ||
+ (at > 0 && str[punc] != ',' && str[punc] != ')')) {
+ return std::nullopt;
+ }
+ names.push_back(str.substr(nameStart, nameEnd - nameStart));
+ at = punc + 1;
+ if (str[punc] == ')') {
+ if (str.find_first_not_of(" "s, at) != str.npos) {
+ return std::nullopt;
+ } else {
+ return names;
+ }
+ }
+ }
+}
+
+TokenSequence Preprocessor::TokenizeMacroBody(const std::string &str) {
+ TokenSequence tokens;
+ Provenance provenance{allSources_.AddCompilerInsertion(str).start()};
+ auto end{str.size()};
+ for (std::string::size_type at{0}; at < end;) {
+ // Alternate between tokens that are identifiers (and therefore subject
+ // to argument replacement) and those that are not.
+ auto start{str.find_first_of(idChars, at)};
+ if (start == str.npos) {
+ tokens.Put(str.substr(at), provenance + at);
+ break;
+ } else if (start > at) {
+ tokens.Put(str.substr(at, start - at), provenance + at);
+ }
+ at = str.find_first_not_of(idChars, start + 1);
+ if (at == str.npos) {
+ tokens.Put(str.substr(start), provenance + start);
+ break;
+ } else {
+ tokens.Put(str.substr(start, at - start), provenance + start);
+ }
+ }
+ return tokens;
+}
+
void Preprocessor::Define(const std::string ¯o, const std::string &value) {
- definitions_.emplace(SaveTokenAsName(macro), Definition{value, allSources_});
+ if (auto lhs{TokenizeMacroNameAndArgs(macro)}) {
+ // function-like macro
+ CharBlock macroName{SaveTokenAsName(lhs->front())};
+ auto iter{lhs->begin()};
+ ++iter;
+ std::vector<std::string> argNames{iter, lhs->end()};
+ auto rhs{TokenizeMacroBody(value)};
+ definitions_.emplace(std::make_pair(macroName,
+ Definition{
+ argNames, rhs, 0, rhs.SizeInTokens(), /*isVariadic=*/false}));
+ } else { // keyword macro
+ definitions_.emplace(
+ SaveTokenAsName(macro), Definition{value, allSources_});
+ }
}
void Preprocessor::Undefine(std::string macro) { definitions_.erase(macro); }
diff --git a/flang/test/Preprocessing/func-on-command-line.F90 b/flang/test/Preprocessing/func-on-command-line.F90
new file mode 100644
index 0000000000000..cf844e021b371
--- /dev/null
+++ b/flang/test/Preprocessing/func-on-command-line.F90
@@ -0,0 +1,4 @@
+! RUN: %flang_fc1 -fdebug-unparse "-Dfoo(a,b)=bar(a+b)" %s | FileCheck %s
+! CHECK: CALL bar(3_4)
+call foo(1,2)
+end
|
eugeneepshteyn
approved these changes
May 14, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Handle a command-line function-like macro definition like "-Dfoo(a)=...".
TODO: error reporting for badly formed argument lists.