[PATCH] D155361: [Tooling] Avoid boilerplate in common cases
Sam McCall via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sat Jul 15 02:36:27 PDT 2023
sammccall created this revision.
Herald added a project: All.
sammccall requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
The tooling APIs have a lot of extension points for customization:
e.g. Executor, FrontendActionFactory, FrontendAction, ASTConsumer.
This customization is often not needed, and makes the APIs unergonomic.
This change introduces some shortcuts to bypass them, while attempting
not to add more concepts/complexity to the APIs.
1. main() entrypoint
Today tools are expected to create an executor (which can fail), then
use it to execute their action (which can report errors).
Errors are reported with llvm::Error, which is not what main() needs
(exit codes) but must be handled.
This patch adds executeFromCommandLineArgs(), which wraps this in a
single function which returns an exit code.
(It also tweaks ToolExecutor's execute() API to avoid replicating several
overloads here).
2. Skipping over FrontendAction to ASTConsumer
Tools that override ASTConsumer must provide two factory indirections:
FrontendActionFactory -> FrontendAction -> ASTConsumer.
These are sometimes meaningful, but often not.
newFrontendActionFactory<ActionT>() already lets you skip the first.
Now newFrontendActionFactory<ConsumerT>() lets you skip the second, too.
3. Implementing simple actions as lambdas.
A large fraction of tools simply want to consume the final AST and do
something with it. Even ASTConsumer is too heavyweight an abstraction.
This patch adds consumeASTs(), which is a FrontendActionFactory that
calls a function once for each parsed AST:
Exec->execute(consumeASTs([&](ASTContext &Ctx) {
... process AST ...
}));
Bonus: through lambda captures, state can be easily shared across TUs.
(This otherwise tends to require plumbing, or use of global variables)
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D155361
Files:
clang/include/clang/Tooling/AllTUsExecution.h
clang/include/clang/Tooling/Execution.h
clang/include/clang/Tooling/StandaloneExecution.h
clang/include/clang/Tooling/Tooling.h
clang/lib/Tooling/AllTUsExecution.cpp
clang/lib/Tooling/Execution.cpp
clang/lib/Tooling/StandaloneExecution.cpp
clang/lib/Tooling/Tooling.cpp
clang/unittests/Tooling/ExecutionTest.cpp
clang/unittests/Tooling/ToolingTest.cpp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155361.540655.patch
Type: text/x-patch
Size: 15880 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230715/ff69019a/attachment.bin>
More information about the cfe-commits
mailing list