This is an archive of the discontinued LLVM Phabricator instance.

[Experimental] String-injection consteval metaprogramming
DraftPublic

Authored by davrec on Feb 2 2022, 6:36 AM.
This is a draft revision that has not yet been submitted for review.

Details

Reviewers
None
Summary

This is an experimental patch which enables this sort of consteval metaprogramming:

consteval void inject_foo() {
  __inj("static const int foo = 42");
}
consteval {
  inject_foo();
  const char *barname = "bar";
  __injf("static const int {} = {};", barname, "foo");
}
static_assert(foo==bar);

The required AST nodes are the MetaprogramDecl [ consteval {...} ] and the StringInjectionStmt [ __injf(...)/__inj(...) ].

Metaprograms may be placed in namespace contexts as well as dependent and nondependent class and function contexts. Other contexts (parameter/argument/base specifier lists etc.) are not supported, but could be.

String injection statements must be placed in metaprograms or in consteval functions called only from within metaprograms; their content is injected into the enclosing context. See clang/test/CXX/meta/stringinj (at the bottom) for sample usages.

Setting syntax/language support aside, the underlying AST nodes could also be useful as implicitly generated nodes intended e.g. to represent preprocessor actions within the AST.

Enable the syntax via -fstring-injection.

Diff Detail

Event Timeline

davrec created this revision.Feb 2 2022, 6:36 AM
davrec abandoned this revision.Feb 2 2022, 6:38 AM
davrec edited the summary of this revision. (Show Details)
davrec reclaimed this revision.Feb 2 2022, 7:20 AM
This comment was removed by davrec.
davrec abandoned this revision.Feb 2 2022, 7:25 AM
davrec changed the visibility from "Public (No Login Required)" to "No One".
davrec updated this revision to Diff 405269.EditedFeb 2 2022, 7:31 AM
davrec retitled this revision from [libc] use llvm_update_compile_flags to populate rtti/exception compilation flags to [Experimental] Adds string injection consteval metaprogramming feature.Feb 2 2022, 7:32 AM
davrec edited the summary of this revision. (Show Details)
davrec removed a project: Restricted Project.
davrec removed subscribers: mgorny, tschuett, ecnelises, libc-commits.
davrec retitled this revision from [Experimental] Adds string injection consteval metaprogramming feature to [Experimental] String-injection consteval metaprogramming.Feb 2 2022, 7:44 AM
davrec edited the summary of this revision. (Show Details)
davrec edited the summary of this revision. (Show Details)
davrec edited the summary of this revision. (Show Details)Feb 2 2022, 7:47 AM
davrec changed the visibility from "No One" to "Public (No Login Required)".
davrec edited the summary of this revision. (Show Details)Feb 2 2022, 7:48 AM