clang_generator: Implement limit_scan_depth feature

PiperOrigin-RevId: 490517864
Change-Id: I93c5df370501f9af745b53791ae8ac2a18220ff9
This commit is contained in:
Christian Blichmann 2022-11-23 09:09:09 -08:00 committed by Copybara-Service
parent e223ef4790
commit d31e691705
5 changed files with 42 additions and 9 deletions

View File

@ -33,6 +33,7 @@ cc_library(
copts = sapi_platform_copts(),
deps = [
"//sandboxed_api/util:status",
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/container:node_hash_set",
"@com_google_absl//absl/random",
@ -89,6 +90,7 @@ cc_binary(
visibility = ["//visibility:public"],
deps = [
":generator",
"//sandboxed_api/util:file_base",
"//sandboxed_api/util:file_helpers",
"//sandboxed_api/util:fileops",
"//sandboxed_api/util:status",

View File

@ -41,6 +41,7 @@ llvm_map_components_to_libnames(_sapi_generator_llvm_libs
)
target_link_libraries(sapi_generator PUBLIC
sapi::base
absl::algorithm_container
absl::btree
absl::flat_hash_set
absl::node_hash_set
@ -62,6 +63,7 @@ add_executable(sapi_generator_tool
)
target_link_libraries(sapi_generator_tool PRIVATE
sapi::base
sapi::file_base
sapi::file_helpers
sapi::fileops
sapi::generator
@ -76,11 +78,11 @@ if(BUILD_TESTING AND SAPI_BUILD_TESTING)
target_link_libraries(sapi_generator_test PRIVATE
absl::flat_hash_map
absl::memory
absl::statusor
benchmark
sapi::sapi
sapi::generator
sapi::status
absl::statusor
sapi::status_matchers
sapi::test_main
)

View File

@ -17,8 +17,12 @@
#include <fstream>
#include <iostream>
#include "absl/algorithm/container.h"
#include "absl/status/status.h"
#include "absl/strings/match.h"
#include "absl/strings/str_format.h"
#include "absl/strings/strip.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/Type.h"
#include "clang/Basic/SourceLocation.h"
@ -84,7 +88,14 @@ bool GeneratorASTVisitor::VisitFunctionDecl(clang::FunctionDecl* decl) {
return true;
}
// TODO(cblichmann): Skip functions to implement limit_scan_depth feature.
if (all_functions) {
const std::string filename(absl::StripPrefix(
ToStringView(source_manager.getFilename(decl_start)), "./"));
if (options_.limit_scan_depth && !options_.in_files.contains(filename)) {
return true;
}
}
functions_.push_back(decl);
collector_.CollectRelatedTypes(decl->getDeclaredReturnType());

View File

@ -40,9 +40,23 @@ struct GeneratorOptions {
return *this;
}
template <typename ContainerT>
GeneratorOptions& set_in_files(const ContainerT& value) {
in_files.clear();
in_files.insert(std::begin(value), std::end(value));
return *this;
}
GeneratorOptions& set_limit_scan_depth(bool value) {
limit_scan_depth = value;
return *this;
}
bool has_namespace() const { return !namespace_name.empty(); }
absl::flat_hash_set<std::string> function_names;
absl::flat_hash_set<std::string> in_files;
bool limit_scan_depth = false;
// Output options
std::string work_dir;

View File

@ -20,10 +20,6 @@
#include "absl/status/status.h"
#include "absl/strings/match.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_split.h"
#include "clang/AST/ASTContext.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/ArgumentsAdjusters.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/CompilationDatabase.h"
#include "llvm/Support/CommandLine.h"
@ -31,6 +27,7 @@
#include "sandboxed_api/tools/clang_generator/generator.h"
#include "sandboxed_api/util/file_helpers.h"
#include "sandboxed_api/util/fileops.h"
#include "sandboxed_api/util/path.h"
#include "sandboxed_api/util/status_macros.h"
namespace sapi {
@ -90,9 +87,16 @@ static auto* g_sapi_out = new llvm::cl::opt<std::string>(
GeneratorOptions GeneratorOptionsFromFlags(
const std::vector<std::string>& sources) {
GeneratorOptions options;
options.function_names.insert(g_sapi_functions->begin(),
g_sapi_functions->end());
options.work_dir = sapi::file_util::fileops::GetCWD();
options.set_function_names(*g_sapi_functions);
for (const auto& input : sources) {
// Keep absolute paths as is, turn
options.in_files.insert(
absl::StartsWith(input, "/")
? input
: sapi::file::JoinPath(options.work_dir, input));
}
options.set_limit_scan_depth(*g_sapi_limit_scan_depth);
options.name = *g_sapi_name;
options.namespace_name = *g_sapi_ns;
options.out_file =
@ -131,7 +135,7 @@ absl::Status GeneratorMain(int argc, char* argv[]) {
if (!g_sapi_isystem->empty()) {
absl::FPrintF(
stderr,
"Note: Ignoring deprecated command-line option: sapi_isystem\n");
"note: ignoring deprecated command-line option: sapi_isystem\n");
}
if (int result = tool.run(