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.
|
|
|
|
|
|
|
|
#ifndef SANDBOXED_API_TOOLS_CLANG_GENERATOR_EMITTER_H_
|
|
|
|
#define SANDBOXED_API_TOOLS_CLANG_GENERATOR_EMITTER_H_
|
|
|
|
|
|
|
|
#include <string>
|
2020-09-25 16:13:50 +08:00
|
|
|
#include <vector>
|
2019-06-25 20:48:56 +08:00
|
|
|
|
2022-11-07 16:37:13 +08:00
|
|
|
#include "absl/container/flat_hash_set.h"
|
|
|
|
#include "absl/container/node_hash_set.h"
|
2019-06-25 20:48:56 +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/string_view.h"
|
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/Type.h"
|
|
|
|
#include "sandboxed_api/tools/clang_generator/types.h"
|
|
|
|
|
|
|
|
namespace sapi {
|
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 = -1);
|
2020-09-25 16:13:50 +08:00
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
class GeneratorOptions;
|
|
|
|
|
2022-11-07 16:37:13 +08:00
|
|
|
class RenderedType {
|
|
|
|
public:
|
|
|
|
RenderedType(std::string ns_name, std::string spelling)
|
|
|
|
: ns_name(std::move(ns_name)), spelling(std::move(spelling)) {}
|
|
|
|
|
|
|
|
bool operator==(const RenderedType& other) const {
|
|
|
|
return ns_name == other.ns_name && spelling == other.spelling;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename H>
|
|
|
|
friend H AbslHashValue(H h, RenderedType rt) {
|
|
|
|
return H::combine(std::move(h), rt.ns_name, rt.spelling);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ns_name;
|
|
|
|
std::string spelling;
|
|
|
|
};
|
|
|
|
|
2022-11-02 15:42:51 +08:00
|
|
|
// Responsible for emitting the actual textual representation of the generated
|
|
|
|
// Sandboxed API header.
|
2020-09-25 16:13:50 +08:00
|
|
|
class Emitter {
|
|
|
|
public:
|
2022-11-15 00:18:42 +08:00
|
|
|
// Adds the declarations of previously collected types to the emitter,
|
|
|
|
// recording the spelling of each one. Types/declarations that are not
|
|
|
|
// supported by the current generator settings or that are unwanted or
|
|
|
|
// unnecessary are skipped. Other filtered types include C++ constructs or
|
|
|
|
// well-known standard library elements. The latter can be replaced by
|
|
|
|
// including the correct headers in the emitted header.
|
|
|
|
void AddTypeDeclarations(const std::vector<clang::TypeDecl*>& type_decls);
|
2022-11-02 15:42:51 +08:00
|
|
|
|
|
|
|
absl::Status AddFunction(clang::FunctionDecl* decl);
|
2020-09-25 16:13:50 +08:00
|
|
|
|
|
|
|
// Outputs a formatted header for a list of functions and their related types.
|
|
|
|
absl::StatusOr<std::string> EmitHeader(const GeneratorOptions& options);
|
|
|
|
|
2022-11-02 15:42:51 +08:00
|
|
|
private:
|
2022-11-15 00:18:42 +08:00
|
|
|
void EmitType(clang::TypeDecl* type_decl);
|
2022-11-02 15:42:51 +08:00
|
|
|
|
2020-09-25 16:13:50 +08:00
|
|
|
protected:
|
2022-11-07 16:37:13 +08:00
|
|
|
// Stores namespaces and a list of spellings for types. Keeps track of types
|
|
|
|
// that have been rendered so far. Using a node_hash_set for pointer
|
|
|
|
// stability.
|
|
|
|
absl::node_hash_set<RenderedType> rendered_types_;
|
|
|
|
|
|
|
|
// A vector to preserve the order of type declarations needs to be preserved.
|
|
|
|
std::vector<const RenderedType*> rendered_types_ordered_;
|
|
|
|
|
|
|
|
// Fully qualified names of functions for the sandboxed API. Keeps track of
|
|
|
|
// functions that have been rendered so far.
|
|
|
|
absl::flat_hash_set<std::string> rendered_functions_;
|
2020-09-25 16:13:50 +08:00
|
|
|
|
2022-11-07 16:37:13 +08:00
|
|
|
// Rendered function bodies, as a vector to preserve source order. This is
|
|
|
|
// not strictly necessary, but makes the output look less surprising.
|
|
|
|
std::vector<std::string> rendered_functions_ordered_;
|
2020-09-25 16:13:50 +08:00
|
|
|
};
|
2019-06-25 20:48:56 +08:00
|
|
|
|
|
|
|
// Constructs an include guard name for the given filename. The name is of the
|
2022-11-02 15:42:51 +08:00
|
|
|
// same form as the include guards in this project and conforms to the Google
|
|
|
|
// C++ style. For example,
|
2019-06-25 20:48:56 +08:00
|
|
|
// sandboxed_api/examples/zlib/zlib-sapi.sapi.h
|
|
|
|
// will be mapped to
|
|
|
|
// SANDBOXED_API_EXAMPLES_ZLIB_ZLIB_SAPI_SAPI_H_
|
|
|
|
std::string GetIncludeGuard(absl::string_view filename);
|
|
|
|
|
|
|
|
} // namespace sapi
|
|
|
|
|
2020-05-28 22:01:09 +08:00
|
|
|
#endif // SANDBOXED_API_TOOLS_CLANG_GENERATOR_EMITTER_H_
|