2019-06-25 20:48:56 +08:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2019-06-25 20:48:56 +08:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "sandboxed_api/tools/clang_generator/emitter.h"
|
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "absl/container/flat_hash_set.h"
|
|
|
|
#include "absl/container/node_hash_set.h"
|
2019-06-25 20:48:56 +08:00
|
|
|
#include "absl/random/random.h"
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "absl/status/status.h"
|
2020-09-02 23:46:48 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2019-06-25 20:48:56 +08:00
|
|
|
#include "absl/strings/ascii.h"
|
2020-05-15 21:44:44 +08:00
|
|
|
#include "absl/strings/match.h"
|
2019-06-25 20:48:56 +08:00
|
|
|
#include "absl/strings/str_cat.h"
|
2020-06-15 15:58:58 +08:00
|
|
|
#include "absl/strings/str_format.h"
|
2019-06-25 20:48:56 +08:00
|
|
|
#include "absl/strings/str_join.h"
|
|
|
|
#include "absl/strings/str_replace.h"
|
2022-03-17 18:52:38 +08:00
|
|
|
#include "absl/strings/str_split.h"
|
2020-09-25 16:13:50 +08:00
|
|
|
#include "absl/strings/string_view.h"
|
2020-05-15 21:44:44 +08:00
|
|
|
#include "absl/strings/strip.h"
|
2022-11-23 23:54:13 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2022-11-15 00:18:42 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2019-06-25 20:48:56 +08:00
|
|
|
#include "clang/AST/DeclCXX.h"
|
2020-10-24 01:21:55 +08:00
|
|
|
#include "clang/AST/DeclTemplate.h"
|
2022-11-23 23:54:13 +08:00
|
|
|
#include "clang/AST/QualTypeNames.h"
|
2020-09-25 16:13:50 +08:00
|
|
|
#include "clang/AST/Type.h"
|
|
|
|
#include "clang/Format/Format.h"
|
2019-06-25 20:48:56 +08:00
|
|
|
#include "sandboxed_api/tools/clang_generator/diagnostics.h"
|
2020-09-25 16:13:50 +08:00
|
|
|
#include "sandboxed_api/tools/clang_generator/generator.h"
|
2022-11-02 15:42:51 +08:00
|
|
|
#include "sandboxed_api/tools/clang_generator/types.h"
|
2019-06-25 20:48:56 +08:00
|
|
|
#include "sandboxed_api/util/status_macros.h"
|
|
|
|
|
|
|
|
namespace sapi {
|
|
|
|
|
2020-05-28 22:01:09 +08:00
|
|
|
// Common file prolog with auto-generation notice.
|
|
|
|
// Note: The includes will be adjusted by Copybara when converting to/from
|
|
|
|
// internal code. This is intentional.
|
|
|
|
// Text template arguments:
|
|
|
|
// 1. Header guard
|
|
|
|
constexpr absl::string_view kHeaderProlog =
|
|
|
|
R"(// AUTO-GENERATED by the Sandboxed API generator.
|
|
|
|
// Edits will be discarded when regenerating this file.
|
|
|
|
|
|
|
|
#ifndef %1$s
|
|
|
|
#define %1$s
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
#include "absl/base/macros.h"
|
|
|
|
#include "absl/status/status.h"
|
2020-09-02 23:46:48 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2020-05-28 22:01:09 +08:00
|
|
|
#include "sandboxed_api/sandbox.h"
|
|
|
|
#include "sandboxed_api/util/status_macros.h"
|
2021-01-22 22:01:05 +08:00
|
|
|
#include "sandboxed_api/vars.h"
|
2020-05-28 22:01:09 +08:00
|
|
|
|
|
|
|
)";
|
|
|
|
constexpr absl::string_view kHeaderEpilog =
|
|
|
|
R"(
|
|
|
|
#endif // %1$s)";
|
|
|
|
|
|
|
|
// Text template arguments:
|
|
|
|
// 1. Include for embedded sandboxee objects
|
|
|
|
constexpr absl::string_view kEmbedInclude = R"(#include "%1$s_embed.h"
|
|
|
|
|
|
|
|
)";
|
|
|
|
|
|
|
|
// Text template arguments:
|
|
|
|
// 1. Namespace name
|
|
|
|
constexpr absl::string_view kNamespaceBeginTemplate =
|
|
|
|
R"(
|
|
|
|
namespace %1$s {
|
|
|
|
|
|
|
|
)";
|
|
|
|
constexpr absl::string_view kNamespaceEndTemplate =
|
|
|
|
R"(
|
|
|
|
} // namespace %1$s
|
|
|
|
)";
|
|
|
|
|
|
|
|
// Text template arguments:
|
|
|
|
// 1. Class name
|
|
|
|
// 2. Embedded object identifier
|
|
|
|
constexpr absl::string_view kEmbedClassTemplate = R"(
|
|
|
|
// Sandbox with embedded sandboxee and default policy
|
|
|
|
class %1$s : public ::sapi::Sandbox {
|
|
|
|
public:
|
|
|
|
%1$s() : ::sapi::Sandbox(%2$s_embed_create()) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
)";
|
|
|
|
|
|
|
|
// Text template arguments:
|
|
|
|
// 1. Class name
|
|
|
|
constexpr absl::string_view kClassHeaderTemplate = R"(
|
|
|
|
// Sandboxed API
|
|
|
|
class %1$s {
|
|
|
|
public:
|
|
|
|
explicit %1$s(::sapi::Sandbox* sandbox) : sandbox_(sandbox) {}
|
|
|
|
|
|
|
|
ABSL_DEPRECATED("Call sandbox() instead")
|
|
|
|
::sapi::Sandbox* GetSandbox() const { return sandbox(); }
|
|
|
|
::sapi::Sandbox* sandbox() const { return sandbox_; }
|
|
|
|
)";
|
|
|
|
|
|
|
|
constexpr absl::string_view kClassFooterTemplate = R"(
|
|
|
|
private:
|
|
|
|
::sapi::Sandbox* sandbox_;
|
|
|
|
};
|
|
|
|
)";
|
|
|
|
|
2020-09-25 16:13:50 +08:00
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
absl::StatusOr<std::string> ReformatGoogleStyle(const std::string& filename,
|
2022-03-17 18:52:38 +08:00
|
|
|
const std::string& code,
|
|
|
|
int column_limit) {
|
2020-09-25 16:13:50 +08:00
|
|
|
// Configure code style based on Google style, but enforce pointer alignment
|
|
|
|
clang::format::FormatStyle style =
|
|
|
|
clang::format::getGoogleStyle(clang::format::FormatStyle::LK_Cpp);
|
|
|
|
style.DerivePointerAlignment = false;
|
|
|
|
style.PointerAlignment = clang::format::FormatStyle::PAS_Left;
|
2022-03-17 18:52:38 +08:00
|
|
|
if (column_limit >= 0) {
|
|
|
|
style.ColumnLimit = column_limit;
|
|
|
|
}
|
2020-09-25 16:13:50 +08:00
|
|
|
|
|
|
|
clang::tooling::Replacements replacements = clang::format::reformat(
|
2023-01-11 07:13:31 +08:00
|
|
|
style, code, llvm::ArrayRef(clang::tooling::Range(0, code.size())),
|
2020-09-25 16:13:50 +08:00
|
|
|
filename);
|
|
|
|
|
|
|
|
llvm::Expected<std::string> formatted_header =
|
|
|
|
clang::tooling::applyAllReplacements(code, replacements);
|
|
|
|
if (!formatted_header) {
|
|
|
|
return absl::InternalError(llvm::toString(formatted_header.takeError()));
|
|
|
|
}
|
|
|
|
return *formatted_header;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
2019-06-25 20:48:56 +08:00
|
|
|
std::string GetIncludeGuard(absl::string_view filename) {
|
|
|
|
if (filename.empty()) {
|
|
|
|
static auto* bit_gen = new absl::BitGen();
|
|
|
|
return absl::StrCat(
|
2020-05-28 22:01:09 +08:00
|
|
|
// Copybara will transform the string. This is intentional.
|
2019-06-25 20:48:56 +08:00
|
|
|
"SANDBOXED_API_GENERATED_HEADER_",
|
2020-06-09 18:24:30 +08:00
|
|
|
absl::AsciiStrToUpper(absl::StrCat(
|
|
|
|
absl::Hex(absl::Uniform<uint64_t>(*bit_gen), absl::kZeroPad16))),
|
|
|
|
"_");
|
2019-06-25 20:48:56 +08:00
|
|
|
}
|
|
|
|
|
2020-06-09 18:24:30 +08:00
|
|
|
constexpr absl::string_view kUnderscorePrefix = "SAPI_";
|
2019-06-25 20:48:56 +08:00
|
|
|
std::string guard;
|
2020-05-28 22:01:09 +08:00
|
|
|
guard.reserve(filename.size() + kUnderscorePrefix.size() + 1);
|
2019-06-25 20:48:56 +08:00
|
|
|
for (auto c : filename) {
|
2020-05-28 22:01:09 +08:00
|
|
|
if (absl::ascii_isalpha(c)) {
|
2020-06-09 18:24:30 +08:00
|
|
|
guard += absl::ascii_toupper(c);
|
2020-05-28 22:01:09 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (guard.empty()) {
|
|
|
|
guard = kUnderscorePrefix;
|
|
|
|
}
|
|
|
|
if (absl::ascii_isdigit(c)) {
|
|
|
|
guard += c;
|
|
|
|
} else if (guard.back() != '_') {
|
2019-06-25 20:48:56 +08:00
|
|
|
guard += '_';
|
|
|
|
}
|
|
|
|
}
|
2020-05-28 22:01:09 +08:00
|
|
|
if (!absl::EndsWith(guard, "_")) {
|
|
|
|
guard += '_';
|
|
|
|
}
|
2019-06-25 20:48:56 +08:00
|
|
|
return guard;
|
|
|
|
}
|
|
|
|
|
2020-05-15 21:44:44 +08:00
|
|
|
// Returns the namespace components of a declaration's qualified name.
|
|
|
|
std::vector<std::string> GetNamespacePath(const clang::TypeDecl* decl) {
|
2019-06-25 20:48:56 +08:00
|
|
|
std::vector<std::string> comps;
|
|
|
|
for (const auto* ctx = decl->getDeclContext(); ctx; ctx = ctx->getParent()) {
|
2020-05-15 21:44:44 +08:00
|
|
|
if (const auto* nd = llvm::dyn_cast<clang::NamespaceDecl>(ctx)) {
|
2022-03-17 17:19:23 +08:00
|
|
|
comps.push_back(nd->getName().str());
|
2019-06-25 20:48:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
std::reverse(comps.begin(), comps.end());
|
|
|
|
return comps;
|
|
|
|
}
|
|
|
|
|
2020-10-24 01:21:55 +08:00
|
|
|
std::string PrintRecordTemplateArguments(const clang::CXXRecordDecl* record) {
|
|
|
|
const auto* template_inst_decl = record->getTemplateInstantiationPattern();
|
|
|
|
if (!template_inst_decl) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
const auto* template_decl = template_inst_decl->getDescribedClassTemplate();
|
|
|
|
if (!template_decl) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
const auto* template_params = template_decl->getTemplateParameters();
|
|
|
|
if (!template_params) {
|
|
|
|
return "";
|
|
|
|
}
|
2022-11-23 23:54:13 +08:00
|
|
|
clang::ASTContext& context = record->getASTContext();
|
2020-10-24 01:21:55 +08:00
|
|
|
std::vector<std::string> params;
|
|
|
|
params.reserve(template_params->size());
|
|
|
|
for (const auto& template_param : *template_params) {
|
|
|
|
if (const auto* p =
|
|
|
|
llvm::dyn_cast<clang::NonTypeTemplateParmDecl>(template_param)) {
|
2022-11-23 23:54:13 +08:00
|
|
|
// TODO(cblichmann): Should be included by CollectRelatedTypes().
|
|
|
|
params.push_back(clang::TypeName::getFullyQualifiedName(
|
|
|
|
p->getType().getDesugaredType(context), context,
|
|
|
|
context.getPrintingPolicy()));
|
2020-10-24 01:21:55 +08:00
|
|
|
} else { // Also covers template template parameters
|
|
|
|
params.push_back("typename");
|
|
|
|
}
|
|
|
|
absl::StrAppend(¶ms.back(), " /*",
|
|
|
|
std::string(template_param->getName()), "*/");
|
|
|
|
}
|
|
|
|
return absl::StrCat("template <", absl::StrJoin(params, ", "), ">");
|
|
|
|
}
|
|
|
|
|
2019-06-25 20:48:56 +08:00
|
|
|
// Serializes the given Clang AST declaration back into compilable source code.
|
2022-03-08 23:16:17 +08:00
|
|
|
std::string PrintDecl(const clang::Decl* decl) {
|
|
|
|
std::string pretty;
|
|
|
|
llvm::raw_string_ostream os(pretty);
|
|
|
|
decl->print(os);
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the spelling for a given declaration will be emitted to the final
|
|
|
|
// header. This may rewrite declarations (like converting typedefs to using,
|
2022-11-07 16:37:13 +08:00
|
|
|
// etc.). Note that the resulting spelling will need to be wrapped inside a
|
|
|
|
// namespace if the original declaration was inside one.
|
2022-03-08 23:16:17 +08:00
|
|
|
std::string GetSpelling(const clang::Decl* decl) {
|
2020-05-15 21:44:44 +08:00
|
|
|
// TODO(cblichmann): Make types nicer
|
|
|
|
// - Rewrite typedef to using
|
|
|
|
// - Rewrite function pointers using std::add_pointer_t<>;
|
|
|
|
|
2022-03-08 23:16:17 +08:00
|
|
|
if (const auto* typedef_decl = llvm::dyn_cast<clang::TypedefNameDecl>(decl)) {
|
|
|
|
// Special case: anonymous enum/struct
|
|
|
|
if (auto* tag_decl = typedef_decl->getAnonDeclWithTypedefName()) {
|
|
|
|
return absl::StrCat("typedef ", PrintDecl(tag_decl), " ",
|
|
|
|
ToStringView(typedef_decl->getName()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const auto* record_decl = llvm::dyn_cast<clang::CXXRecordDecl>(decl)) {
|
2023-08-14 21:13:51 +08:00
|
|
|
if (record_decl->hasDefinition() &&
|
|
|
|
// Aggregates capture all C-like structs, but also structs with
|
|
|
|
// non-static members that have default initializers.
|
|
|
|
record_decl->isAggregate() &&
|
|
|
|
// Make sure to skip types with user-defined methods (including
|
|
|
|
// constructors).
|
|
|
|
record_decl->methods().empty()) {
|
|
|
|
return PrintDecl(decl);
|
2022-03-17 18:12:46 +08:00
|
|
|
}
|
2023-08-14 21:13:51 +08:00
|
|
|
// For unsupported types or types with no definition, only emit a forward
|
|
|
|
// declaration.
|
|
|
|
return absl::StrCat(PrintRecordTemplateArguments(record_decl),
|
|
|
|
record_decl->isClass() ? "class " : "struct ",
|
|
|
|
ToStringView(record_decl->getName()));
|
2020-05-15 21:44:44 +08:00
|
|
|
}
|
2022-03-08 23:16:17 +08:00
|
|
|
return PrintDecl(decl);
|
2019-06-25 20:48:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetParamName(const clang::ParmVarDecl* decl, int index) {
|
2020-06-15 15:58:58 +08:00
|
|
|
if (std::string name = decl->getName().str(); !name.empty()) {
|
2019-06-25 20:48:56 +08:00
|
|
|
return absl::StrCat(name, "_"); // Suffix to avoid collisions
|
|
|
|
}
|
|
|
|
return absl::StrCat("unnamed", index, "_");
|
|
|
|
}
|
|
|
|
|
2022-03-17 18:52:38 +08:00
|
|
|
absl::StatusOr<std::string> PrintFunctionPrototypeComment(
|
|
|
|
const clang::FunctionDecl* decl) {
|
2022-11-07 16:37:13 +08:00
|
|
|
const clang::ASTContext& context = decl->getASTContext();
|
|
|
|
|
|
|
|
std::string out = absl::StrCat(
|
|
|
|
MapQualTypeParameterForCxx(context, decl->getDeclaredReturnType()), " ",
|
|
|
|
decl->getQualifiedNameAsString(), "(");
|
2019-06-25 20:48:56 +08:00
|
|
|
|
|
|
|
std::string print_separator;
|
|
|
|
for (int i = 0; i < decl->getNumParams(); ++i) {
|
|
|
|
const clang::ParmVarDecl* param = decl->getParamDecl(i);
|
|
|
|
|
|
|
|
absl::StrAppend(&out, print_separator);
|
|
|
|
print_separator = ", ";
|
2022-11-07 16:37:13 +08:00
|
|
|
absl::StrAppend(&out,
|
|
|
|
MapQualTypeParameterForCxx(context, param->getType()));
|
2020-06-15 15:58:58 +08:00
|
|
|
if (std::string name = param->getName().str(); !name.empty()) {
|
2019-06-25 20:48:56 +08:00
|
|
|
absl::StrAppend(&out, " ", name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
absl::StrAppend(&out, ")");
|
2022-03-17 18:52:38 +08:00
|
|
|
|
|
|
|
SAPI_ASSIGN_OR_RETURN(
|
|
|
|
std::string formatted,
|
|
|
|
internal::ReformatGoogleStyle(/*filename=*/"input", out, 75));
|
|
|
|
out.clear();
|
|
|
|
for (const auto& line : absl::StrSplit(formatted, '\n')) {
|
|
|
|
absl::StrAppend(&out, "// ", line, "\n");
|
|
|
|
}
|
2019-06-25 20:48:56 +08:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2020-09-02 23:46:48 +08:00
|
|
|
absl::StatusOr<std::string> EmitFunction(const clang::FunctionDecl* decl) {
|
2022-11-23 23:54:13 +08:00
|
|
|
const clang::QualType return_type = decl->getDeclaredReturnType();
|
|
|
|
if (return_type->isRecordType()) {
|
|
|
|
return MakeStatusWithDiagnostic(
|
|
|
|
decl->getBeginLoc(), absl::StatusCode::kCancelled,
|
|
|
|
"returning record by value, skipping function");
|
|
|
|
}
|
2019-06-25 20:48:56 +08:00
|
|
|
std::string out;
|
2022-03-17 18:52:38 +08:00
|
|
|
|
|
|
|
SAPI_ASSIGN_OR_RETURN(std::string prototype,
|
|
|
|
PrintFunctionPrototypeComment(decl));
|
|
|
|
absl::StrAppend(&out, "\n", prototype);
|
|
|
|
|
2022-03-17 17:19:23 +08:00
|
|
|
auto function_name = ToStringView(decl->getName());
|
2019-06-25 20:48:56 +08:00
|
|
|
const bool returns_void = return_type->isVoidType();
|
|
|
|
|
2020-05-15 21:44:44 +08:00
|
|
|
const clang::ASTContext& context = decl->getASTContext();
|
|
|
|
|
2019-06-25 20:48:56 +08:00
|
|
|
// "Status<OptionalReturn> FunctionName("
|
2020-05-15 21:44:44 +08:00
|
|
|
absl::StrAppend(&out, MapQualTypeReturn(context, return_type), " ",
|
|
|
|
function_name, "(");
|
2019-06-25 20:48:56 +08:00
|
|
|
|
|
|
|
struct ParameterInfo {
|
|
|
|
clang::QualType qual;
|
|
|
|
std::string name;
|
|
|
|
};
|
|
|
|
std::vector<ParameterInfo> params;
|
|
|
|
|
|
|
|
std::string print_separator;
|
|
|
|
for (int i = 0; i < decl->getNumParams(); ++i) {
|
|
|
|
const clang::ParmVarDecl* param = decl->getParamDecl(i);
|
2022-11-02 15:42:51 +08:00
|
|
|
if (param->getType()->isRecordType()) {
|
|
|
|
return MakeStatusWithDiagnostic(
|
2022-11-03 21:01:50 +08:00
|
|
|
param->getBeginLoc(), absl::StatusCode::kCancelled,
|
2022-11-02 15:42:51 +08:00
|
|
|
absl::StrCat("passing record parameter '",
|
2022-11-03 21:01:50 +08:00
|
|
|
ToStringView(param->getName()),
|
|
|
|
"' by value, skipping function"));
|
2022-11-02 15:42:51 +08:00
|
|
|
}
|
2019-06-25 20:48:56 +08:00
|
|
|
|
2020-05-15 21:44:44 +08:00
|
|
|
ParameterInfo& param_info = params.emplace_back();
|
|
|
|
param_info.qual = param->getType();
|
|
|
|
param_info.name = GetParamName(param, i);
|
2019-06-25 20:48:56 +08:00
|
|
|
|
|
|
|
absl::StrAppend(&out, print_separator);
|
|
|
|
print_separator = ", ";
|
2020-05-15 21:44:44 +08:00
|
|
|
absl::StrAppend(&out, MapQualTypeParameter(context, param_info.qual), " ",
|
|
|
|
param_info.name);
|
2019-06-25 20:48:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
absl::StrAppend(&out, ") {\n");
|
2020-05-15 21:44:44 +08:00
|
|
|
absl::StrAppend(&out, MapQualType(context, return_type), " v_ret_;\n");
|
2019-06-25 20:48:56 +08:00
|
|
|
for (const auto& [qual, name] : params) {
|
|
|
|
if (!IsPointerOrReference(qual)) {
|
2020-05-15 21:44:44 +08:00
|
|
|
absl::StrAppend(&out, MapQualType(context, qual), " v_", name, "(", name,
|
|
|
|
");\n");
|
2019-06-25 20:48:56 +08:00
|
|
|
}
|
|
|
|
}
|
2021-01-22 22:01:05 +08:00
|
|
|
absl::StrAppend(&out, "\nSAPI_RETURN_IF_ERROR(sandbox_->Call(\"",
|
|
|
|
function_name, "\", &v_ret_");
|
2019-06-25 20:48:56 +08:00
|
|
|
for (const auto& [qual, name] : params) {
|
|
|
|
absl::StrAppend(&out, ", ", IsPointerOrReference(qual) ? "" : "&v_", name);
|
|
|
|
}
|
|
|
|
absl::StrAppend(&out, "));\nreturn ",
|
2022-03-02 21:17:10 +08:00
|
|
|
(returns_void ? "::absl::OkStatus()" : "v_ret_.GetValue()"),
|
2019-06-25 20:48:56 +08:00
|
|
|
";\n}\n");
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2020-09-02 23:46:48 +08:00
|
|
|
absl::StatusOr<std::string> EmitHeader(
|
2022-11-07 16:37:13 +08:00
|
|
|
const std::vector<std::string>& function_definitions,
|
|
|
|
const std::vector<const RenderedType*>& rendered_types,
|
2019-06-25 20:48:56 +08:00
|
|
|
const GeneratorOptions& options) {
|
|
|
|
std::string out;
|
|
|
|
const std::string include_guard = GetIncludeGuard(options.out_file);
|
|
|
|
absl::StrAppendFormat(&out, kHeaderProlog, include_guard);
|
2020-05-15 21:44:44 +08:00
|
|
|
// When embedding the sandboxee, add embed header include
|
|
|
|
if (!options.embed_name.empty()) {
|
|
|
|
// Not using JoinPath() because even on Windows include paths use plain
|
|
|
|
// slashes.
|
|
|
|
std::string include_file(absl::StripSuffix(
|
|
|
|
absl::StrReplaceAll(options.embed_dir, {{"\\", "/"}}), "/"));
|
|
|
|
if (!include_file.empty()) {
|
|
|
|
absl::StrAppend(&include_file, "/");
|
|
|
|
}
|
|
|
|
absl::StrAppend(&include_file, options.embed_name);
|
|
|
|
absl::StrAppendFormat(&out, kEmbedInclude, include_file);
|
|
|
|
}
|
|
|
|
|
2022-03-04 06:14:02 +08:00
|
|
|
// If specified, wrap the generated API in a namespace
|
|
|
|
if (options.has_namespace()) {
|
|
|
|
absl::StrAppendFormat(&out, kNamespaceBeginTemplate,
|
|
|
|
options.namespace_name);
|
|
|
|
}
|
|
|
|
|
2020-05-15 21:44:44 +08:00
|
|
|
// Emit type dependencies
|
2020-09-25 16:13:50 +08:00
|
|
|
if (!rendered_types.empty()) {
|
|
|
|
absl::StrAppend(&out, "// Types this API depends on\n");
|
2023-08-16 16:17:11 +08:00
|
|
|
std::string last_ns_name = options.namespace_name;
|
2022-11-07 16:37:13 +08:00
|
|
|
for (const RenderedType* rt : rendered_types) {
|
|
|
|
const auto& [ns_name, spelling] = *rt;
|
|
|
|
if (last_ns_name != ns_name) {
|
2023-08-16 16:17:11 +08:00
|
|
|
if (!last_ns_name.empty() && last_ns_name != options.namespace_name) {
|
2022-11-07 16:37:13 +08:00
|
|
|
absl::StrAppend(&out, "} // namespace ", last_ns_name, "\n\n");
|
|
|
|
}
|
2023-08-16 16:17:11 +08:00
|
|
|
|
|
|
|
if (!ns_name.empty() && ns_name != options.namespace_name) {
|
2022-11-07 16:37:13 +08:00
|
|
|
absl::StrAppend(&out, "namespace ", ns_name, " {\n");
|
|
|
|
}
|
|
|
|
last_ns_name = ns_name;
|
2019-06-25 20:48:56 +08:00
|
|
|
}
|
2022-11-07 16:37:13 +08:00
|
|
|
|
|
|
|
absl::StrAppend(&out, spelling, ";\n");
|
|
|
|
}
|
2023-08-16 16:17:11 +08:00
|
|
|
if (!last_ns_name.empty() && last_ns_name != options.namespace_name) {
|
2022-11-07 16:37:13 +08:00
|
|
|
absl::StrAppend(&out, "} // namespace ", last_ns_name, "\n\n");
|
2019-06-25 20:48:56 +08:00
|
|
|
}
|
2020-05-15 21:44:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Optionally emit a default sandbox that instantiates an embedded sandboxee
|
|
|
|
if (!options.embed_name.empty()) {
|
|
|
|
// TODO(cblichmann): Make the "Sandbox" suffix configurable.
|
|
|
|
absl::StrAppendFormat(
|
|
|
|
&out, kEmbedClassTemplate, absl::StrCat(options.name, "Sandbox"),
|
|
|
|
absl::StrReplaceAll(options.embed_name, {{"-", "_"}}));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the actual Sandboxed API
|
2019-06-25 20:48:56 +08:00
|
|
|
// TODO(cblichmann): Make the "Api" suffix configurable or at least optional.
|
|
|
|
absl::StrAppendFormat(&out, kClassHeaderTemplate,
|
|
|
|
absl::StrCat(options.name, "Api"));
|
2022-11-07 16:37:13 +08:00
|
|
|
absl::StrAppend(&out, absl::StrJoin(function_definitions, "\n"));
|
2019-06-25 20:48:56 +08:00
|
|
|
absl::StrAppend(&out, kClassFooterTemplate);
|
|
|
|
|
2020-05-15 21:44:44 +08:00
|
|
|
// Close out the header: close namespace (if needed) and end include guard
|
2019-06-25 20:48:56 +08:00
|
|
|
if (options.has_namespace()) {
|
|
|
|
absl::StrAppendFormat(&out, kNamespaceEndTemplate, options.namespace_name);
|
|
|
|
}
|
|
|
|
absl::StrAppendFormat(&out, kHeaderEpilog, include_guard);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2022-11-15 00:18:42 +08:00
|
|
|
void Emitter::EmitType(clang::TypeDecl* type_decl) {
|
|
|
|
if (!type_decl) {
|
2020-09-25 16:13:50 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-07 16:37:13 +08:00
|
|
|
// Skip types defined in system headers.
|
|
|
|
// TODO(cblichmann): Instead of this and the hard-coded entities below, we
|
2022-11-23 23:54:13 +08:00
|
|
|
// should map types and add the correct (system) headers to
|
|
|
|
// the generated output.
|
2022-11-15 00:18:42 +08:00
|
|
|
if (type_decl->getASTContext().getSourceManager().isInSystemHeader(
|
|
|
|
type_decl->getBeginLoc())) {
|
2022-11-07 16:37:13 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-15 00:18:42 +08:00
|
|
|
const std::vector<std::string> ns_path = GetNamespacePath(type_decl);
|
2020-09-25 16:13:50 +08:00
|
|
|
std::string ns_name;
|
|
|
|
if (!ns_path.empty()) {
|
2022-03-02 21:17:10 +08:00
|
|
|
const auto& ns_root = ns_path.front();
|
2022-11-15 00:18:42 +08:00
|
|
|
// Filter out declarations from the C++ standard library, from SAPI itself
|
|
|
|
// and from other well-known namespaces.
|
2022-03-02 21:17:10 +08:00
|
|
|
if (ns_root == "std" || ns_root == "__gnu_cxx" || ns_root == "sapi") {
|
2020-09-25 16:13:50 +08:00
|
|
|
return;
|
|
|
|
}
|
2022-03-02 21:17:10 +08:00
|
|
|
if (ns_root == "absl") {
|
2022-11-07 16:37:13 +08:00
|
|
|
// Skip Abseil internal namespaces
|
|
|
|
if (ns_path.size() > 1 && absl::EndsWith(ns_path[1], "_internal")) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Skip types from Abseil that will already be included in the generated
|
2022-03-02 21:17:10 +08:00
|
|
|
// header.
|
2022-11-15 00:18:42 +08:00
|
|
|
if (auto name = ToStringView(type_decl->getName());
|
2022-11-24 00:46:59 +08:00
|
|
|
name == "CordMemoryAccounting" || name == "Duration" ||
|
|
|
|
name == "LogEntry" || name == "LogSeverity" || name == "Span" ||
|
2022-03-02 21:17:10 +08:00
|
|
|
name == "StatusCode" || name == "StatusToStringMode" ||
|
2022-11-24 00:46:59 +08:00
|
|
|
name == "SynchLocksHeld" || name == "SynchWaitParams" ||
|
|
|
|
name == "Time" || name == "string_view" || name == "tid_t") {
|
2022-03-02 21:17:10 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-08-17 16:57:58 +08:00
|
|
|
// Skip Protocol Buffers namespaces
|
|
|
|
if (ns_root == "google" && ns_path.size() > 1 && ns_path[1] == "protobuf") {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-17 18:12:46 +08:00
|
|
|
ns_name = absl::StrJoin(ns_path, "::");
|
2020-09-25 16:13:50 +08:00
|
|
|
}
|
|
|
|
|
2022-11-15 00:18:42 +08:00
|
|
|
std::string spelling = GetSpelling(type_decl);
|
2022-11-07 16:37:13 +08:00
|
|
|
if (const auto& [it, inserted] = rendered_types_.emplace(ns_name, spelling);
|
|
|
|
inserted) {
|
|
|
|
rendered_types_ordered_.push_back(&*it);
|
|
|
|
}
|
2020-09-25 16:13:50 +08:00
|
|
|
}
|
|
|
|
|
2022-11-15 00:18:42 +08:00
|
|
|
void Emitter::AddTypeDeclarations(
|
|
|
|
const std::vector<clang::TypeDecl*>& type_decls) {
|
|
|
|
for (clang::TypeDecl* type_decl : type_decls) {
|
|
|
|
EmitType(type_decl);
|
2022-11-02 15:42:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
absl::Status Emitter::AddFunction(clang::FunctionDecl* decl) {
|
2022-11-07 16:37:13 +08:00
|
|
|
if (rendered_functions_.insert(decl->getQualifiedNameAsString()).second) {
|
|
|
|
SAPI_ASSIGN_OR_RETURN(std::string function, EmitFunction(decl));
|
|
|
|
rendered_functions_ordered_.push_back(function);
|
|
|
|
}
|
2022-11-02 15:42:51 +08:00
|
|
|
return absl::OkStatus();
|
2020-09-25 16:13:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
absl::StatusOr<std::string> Emitter::EmitHeader(
|
|
|
|
const GeneratorOptions& options) {
|
2022-11-07 16:37:13 +08:00
|
|
|
SAPI_ASSIGN_OR_RETURN(const std::string header,
|
|
|
|
::sapi::EmitHeader(rendered_functions_ordered_,
|
|
|
|
rendered_types_ordered_, options));
|
2020-09-25 16:13:50 +08:00
|
|
|
return internal::ReformatGoogleStyle(options.out_file, header);
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:01:09 +08:00
|
|
|
} // namespace sapi
|