mirror of
https://github.com/google/sandboxed-api.git
synced 2024-03-22 13:11:30 +08:00
Skip system headers in Clang generator
When not requesting any particular function, `sapi_library()` will try and make available _all_ functions it finds. In this case, system headers should be skipped to avoid inflating the API surface. Standard library functions can still be manually requested by adding them to the `functions` (Bazel)/ `FUNCTIONS` (CMake) argument. PiperOrigin-RevId: 472272506 Change-Id: I8f8d79796d3044e598eebb7f87ce4cf464b47ed7
This commit is contained in:
parent
75c7081622
commit
39a1bc9d7a
|
@ -52,7 +52,7 @@ TEST_F(EmitterTest, BasicFunctionality) {
|
|||
|
||||
EmitterForTesting emitter;
|
||||
RunFrontendAction(R"(extern "C" void ExposedFunction() {})",
|
||||
absl::make_unique<GeneratorAction>(emitter, options));
|
||||
std::make_unique<GeneratorAction>(emitter, options));
|
||||
|
||||
EXPECT_THAT(emitter.functions_, SizeIs(1));
|
||||
|
||||
|
@ -80,7 +80,7 @@ TEST_F(EmitterTest, RelatedTypes) {
|
|||
typedef struct { int member; } MyStruct;
|
||||
extern "C" void Structize(MyStruct* s);
|
||||
)",
|
||||
absl::make_unique<GeneratorAction>(emitter, GeneratorOptions()));
|
||||
std::make_unique<GeneratorAction>(emitter, GeneratorOptions()));
|
||||
|
||||
// Types from "std" should be skipped
|
||||
EXPECT_THAT(emitter.rendered_types_["std"], IsEmpty());
|
||||
|
@ -106,7 +106,7 @@ TEST_F(EmitterTest, NestedStruct) {
|
|||
};
|
||||
extern "C" void Structize(A* s);
|
||||
)",
|
||||
absl::make_unique<GeneratorAction>(emitter, GeneratorOptions()));
|
||||
std::make_unique<GeneratorAction>(emitter, GeneratorOptions()));
|
||||
|
||||
EXPECT_THAT(UglifyAll(emitter.rendered_types_[""]),
|
||||
ElementsAre("struct A {"
|
||||
|
@ -125,7 +125,7 @@ TEST_F(EmitterTest, NestedAnonymousStruct) {
|
|||
};
|
||||
extern "C" void Structize(A* s);
|
||||
)",
|
||||
absl::make_unique<GeneratorAction>(emitter, GeneratorOptions()));
|
||||
std::make_unique<GeneratorAction>(emitter, GeneratorOptions()));
|
||||
|
||||
EXPECT_THAT(UglifyAll(emitter.rendered_types_[""]),
|
||||
ElementsAre("struct A {"
|
||||
|
@ -144,7 +144,7 @@ TEST_F(EmitterTest, ParentNotCollected) {
|
|||
};
|
||||
extern "C" void Structize(A::B* s);
|
||||
)",
|
||||
absl::make_unique<GeneratorAction>(emitter, GeneratorOptions()));
|
||||
std::make_unique<GeneratorAction>(emitter, GeneratorOptions()));
|
||||
|
||||
EXPECT_THAT(UglifyAll(emitter.rendered_types_[""]),
|
||||
ElementsAre("struct A {"
|
||||
|
@ -160,7 +160,7 @@ TEST_F(EmitterTest, RemoveQualifiers) {
|
|||
struct A { int data; };
|
||||
extern "C" void Structize(const A* in, A* out);
|
||||
)",
|
||||
absl::make_unique<GeneratorAction>(emitter, GeneratorOptions()));
|
||||
std::make_unique<GeneratorAction>(emitter, GeneratorOptions()));
|
||||
|
||||
EXPECT_THAT(UglifyAll(emitter.rendered_types_[""]),
|
||||
ElementsAre("struct A { int data; }"));
|
||||
|
|
|
@ -48,12 +48,25 @@ std::string GetOutputFilename(absl::string_view source_file) {
|
|||
}
|
||||
|
||||
bool GeneratorASTVisitor::VisitFunctionDecl(clang::FunctionDecl* decl) {
|
||||
if (!decl->isCXXClassMember() && // Skip classes
|
||||
decl->isExternC() && // Skip non external functions
|
||||
!decl->isTemplated() && // Skip function templates
|
||||
// Process either all function or just the requested ones
|
||||
(options_.function_names.empty() ||
|
||||
options_.function_names.count(ToStringView(decl->getName())) > 0)) {
|
||||
if (decl->isCXXClassMember() || // Skip classes
|
||||
!decl->isExternC() || // Skip non external functions
|
||||
decl->isTemplated() // Skip function templates
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Process either all function or just the requested ones
|
||||
if (bool all_functions = options_.function_names.empty();
|
||||
all_functions ||
|
||||
options_.function_names.count(ToStringView(decl->getName())) > 0) {
|
||||
// Skip functions from system headers when all functions are requested.
|
||||
// This allows to still specify standard library functions explicitly.
|
||||
if (all_functions &&
|
||||
decl->getASTContext().getSourceManager().isInSystemHeader(
|
||||
decl->getBeginLoc())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
functions_.push_back(decl);
|
||||
|
||||
collector_.CollectRelatedTypes(decl->getDeclaredReturnType());
|
||||
|
|
|
@ -97,8 +97,8 @@ class GeneratorAction : public clang::ASTFrontendAction {
|
|||
private:
|
||||
std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(
|
||||
clang::CompilerInstance&, llvm::StringRef in_file) override {
|
||||
return absl::make_unique<GeneratorASTConsumer>(std::string(in_file),
|
||||
emitter_, options_);
|
||||
return std::make_unique<GeneratorASTConsumer>(std::string(in_file),
|
||||
emitter_, options_);
|
||||
}
|
||||
|
||||
bool hasCodeCompletionSupport() const override { return false; }
|
||||
|
@ -116,7 +116,7 @@ class GeneratorFactory : public clang::tooling::FrontendActionFactory {
|
|||
private:
|
||||
#if LLVM_VERSION_MAJOR >= 10
|
||||
std::unique_ptr<clang::FrontendAction> create() override {
|
||||
return absl::make_unique<GeneratorAction>(emitter_, options_);
|
||||
return std::make_unique<GeneratorAction>(emitter_, options_);
|
||||
}
|
||||
#else
|
||||
clang::FrontendAction* create() override {
|
||||
|
|
|
@ -136,7 +136,7 @@ absl::Status GeneratorMain(int argc, char* argv[]) {
|
|||
}
|
||||
|
||||
if (int result = tool.run(
|
||||
absl::make_unique<sapi::GeneratorFactory>(emitter, options).get());
|
||||
std::make_unique<sapi::GeneratorFactory>(emitter, options).get());
|
||||
result != 0) {
|
||||
return absl::UnknownError("header generation failed");
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user