blob: e225e04f3fa0d89dc31259d22fbca6a3b3139d3b [file] [log] [blame]
// Part of the Crubit project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "lifetime_annotations/test/run_on_code.h"
#include <functional>
#include <string>
#include "devtools/cymbal/data_flow/testing_support.h"
#include "lifetime_annotations/lifetime_annotations.h"
namespace devtools_rust {
namespace {
class RunOnCodeASTConsumer : public clang::ASTConsumer {
public:
explicit RunOnCodeASTConsumer(
const std::function<void(clang::ASTContext&,
const LifetimeAnnotationContext&)>& operation,
std::shared_ptr<LifetimeAnnotationContext> lifetime_context)
: operation_(operation), lifetime_context_(lifetime_context) {}
void HandleTranslationUnit(clang::ASTContext& ast_context) override {
operation_(ast_context, *lifetime_context_);
}
private:
const std::function<void(clang::ASTContext&,
const LifetimeAnnotationContext&)>& operation_;
std::shared_ptr<LifetimeAnnotationContext> lifetime_context_;
};
class RunOnCodeAction : public clang::ASTFrontendAction {
public:
explicit RunOnCodeAction(
const std::function<void(clang::ASTContext&,
const LifetimeAnnotationContext&)>& operation,
std::shared_ptr<LifetimeAnnotationContext> lifetime_context)
: operation_(operation), lifetime_context_(lifetime_context) {}
std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(
clang::CompilerInstance& compiler, llvm::StringRef) override {
AddLifetimeAnnotationHandlers(compiler.getPreprocessor(),
lifetime_context_);
return std::make_unique<RunOnCodeASTConsumer>(operation_,
lifetime_context_);
}
private:
const std::function<void(clang::ASTContext&,
const LifetimeAnnotationContext&)>& operation_;
std::shared_ptr<LifetimeAnnotationContext> lifetime_context_;
};
} // namespace
bool runOnCodeWithLifetimeHandlers(
llvm::StringRef code,
const std::function<void(clang::ASTContext&,
const LifetimeAnnotationContext&)>& operation,
llvm::ArrayRef<std::string> args,
const clang::tooling::FileContentMappings& file_contents) {
auto context = std::make_shared<LifetimeAnnotationContext>();
return clang::tooling::runToolOnCodeWithArgs(
std::make_unique<RunOnCodeAction>(operation, context), code, args,
"input.cc", "run-on-code-with-lifetime-handlers",
std::make_shared<clang::PCHContainerOperations>(), file_contents);
}
} // namespace devtools_rust