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;
|
EmitterForTesting emitter;
|
||||||
RunFrontendAction(R"(extern "C" void ExposedFunction() {})",
|
RunFrontendAction(R"(extern "C" void ExposedFunction() {})",
|
||||||
absl::make_unique<GeneratorAction>(emitter, options));
|
std::make_unique<GeneratorAction>(emitter, options));
|
||||||
|
|
||||||
EXPECT_THAT(emitter.functions_, SizeIs(1));
|
EXPECT_THAT(emitter.functions_, SizeIs(1));
|
||||||
|
|
||||||
|
@ -80,7 +80,7 @@ TEST_F(EmitterTest, RelatedTypes) {
|
||||||
typedef struct { int member; } MyStruct;
|
typedef struct { int member; } MyStruct;
|
||||||
extern "C" void Structize(MyStruct* s);
|
extern "C" void Structize(MyStruct* s);
|
||||||
)",
|
)",
|
||||||
absl::make_unique<GeneratorAction>(emitter, GeneratorOptions()));
|
std::make_unique<GeneratorAction>(emitter, GeneratorOptions()));
|
||||||
|
|
||||||
// Types from "std" should be skipped
|
// Types from "std" should be skipped
|
||||||
EXPECT_THAT(emitter.rendered_types_["std"], IsEmpty());
|
EXPECT_THAT(emitter.rendered_types_["std"], IsEmpty());
|
||||||
|
@ -106,7 +106,7 @@ TEST_F(EmitterTest, NestedStruct) {
|
||||||
};
|
};
|
||||||
extern "C" void Structize(A* s);
|
extern "C" void Structize(A* s);
|
||||||
)",
|
)",
|
||||||
absl::make_unique<GeneratorAction>(emitter, GeneratorOptions()));
|
std::make_unique<GeneratorAction>(emitter, GeneratorOptions()));
|
||||||
|
|
||||||
EXPECT_THAT(UglifyAll(emitter.rendered_types_[""]),
|
EXPECT_THAT(UglifyAll(emitter.rendered_types_[""]),
|
||||||
ElementsAre("struct A {"
|
ElementsAre("struct A {"
|
||||||
|
@ -125,7 +125,7 @@ TEST_F(EmitterTest, NestedAnonymousStruct) {
|
||||||
};
|
};
|
||||||
extern "C" void Structize(A* s);
|
extern "C" void Structize(A* s);
|
||||||
)",
|
)",
|
||||||
absl::make_unique<GeneratorAction>(emitter, GeneratorOptions()));
|
std::make_unique<GeneratorAction>(emitter, GeneratorOptions()));
|
||||||
|
|
||||||
EXPECT_THAT(UglifyAll(emitter.rendered_types_[""]),
|
EXPECT_THAT(UglifyAll(emitter.rendered_types_[""]),
|
||||||
ElementsAre("struct A {"
|
ElementsAre("struct A {"
|
||||||
|
@ -144,7 +144,7 @@ TEST_F(EmitterTest, ParentNotCollected) {
|
||||||
};
|
};
|
||||||
extern "C" void Structize(A::B* s);
|
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_[""]),
|
EXPECT_THAT(UglifyAll(emitter.rendered_types_[""]),
|
||||||
ElementsAre("struct A {"
|
ElementsAre("struct A {"
|
||||||
|
@ -160,7 +160,7 @@ TEST_F(EmitterTest, RemoveQualifiers) {
|
||||||
struct A { int data; };
|
struct A { int data; };
|
||||||
extern "C" void Structize(const A* in, A* out);
|
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_[""]),
|
EXPECT_THAT(UglifyAll(emitter.rendered_types_[""]),
|
||||||
ElementsAre("struct A { int data; }"));
|
ElementsAre("struct A { int data; }"));
|
||||||
|
|
|
@ -48,12 +48,25 @@ std::string GetOutputFilename(absl::string_view source_file) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GeneratorASTVisitor::VisitFunctionDecl(clang::FunctionDecl* decl) {
|
bool GeneratorASTVisitor::VisitFunctionDecl(clang::FunctionDecl* decl) {
|
||||||
if (!decl->isCXXClassMember() && // Skip classes
|
if (decl->isCXXClassMember() || // Skip classes
|
||||||
decl->isExternC() && // Skip non external functions
|
!decl->isExternC() || // Skip non external functions
|
||||||
!decl->isTemplated() && // Skip function templates
|
decl->isTemplated() // Skip function templates
|
||||||
// Process either all function or just the requested ones
|
) {
|
||||||
(options_.function_names.empty() ||
|
return true;
|
||||||
options_.function_names.count(ToStringView(decl->getName())) > 0)) {
|
}
|
||||||
|
|
||||||
|
// 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);
|
functions_.push_back(decl);
|
||||||
|
|
||||||
collector_.CollectRelatedTypes(decl->getDeclaredReturnType());
|
collector_.CollectRelatedTypes(decl->getDeclaredReturnType());
|
||||||
|
|
|
@ -97,8 +97,8 @@ class GeneratorAction : public clang::ASTFrontendAction {
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(
|
std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(
|
||||||
clang::CompilerInstance&, llvm::StringRef in_file) override {
|
clang::CompilerInstance&, llvm::StringRef in_file) override {
|
||||||
return absl::make_unique<GeneratorASTConsumer>(std::string(in_file),
|
return std::make_unique<GeneratorASTConsumer>(std::string(in_file),
|
||||||
emitter_, options_);
|
emitter_, options_);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool hasCodeCompletionSupport() const override { return false; }
|
bool hasCodeCompletionSupport() const override { return false; }
|
||||||
|
@ -116,7 +116,7 @@ class GeneratorFactory : public clang::tooling::FrontendActionFactory {
|
||||||
private:
|
private:
|
||||||
#if LLVM_VERSION_MAJOR >= 10
|
#if LLVM_VERSION_MAJOR >= 10
|
||||||
std::unique_ptr<clang::FrontendAction> create() override {
|
std::unique_ptr<clang::FrontendAction> create() override {
|
||||||
return absl::make_unique<GeneratorAction>(emitter_, options_);
|
return std::make_unique<GeneratorAction>(emitter_, options_);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
clang::FrontendAction* create() override {
|
clang::FrontendAction* create() override {
|
||||||
|
|
|
@ -136,7 +136,7 @@ absl::Status GeneratorMain(int argc, char* argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (int result = tool.run(
|
if (int result = tool.run(
|
||||||
absl::make_unique<sapi::GeneratorFactory>(emitter, options).get());
|
std::make_unique<sapi::GeneratorFactory>(emitter, options).get());
|
||||||
result != 0) {
|
result != 0) {
|
||||||
return absl::UnknownError("header generation failed");
|
return absl::UnknownError("header generation failed");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user