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_GENERATOR_H_
|
|
|
|
#define SANDBOXED_API_TOOLS_CLANG_GENERATOR_GENERATOR_H_
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "absl/container/flat_hash_set.h"
|
2020-06-10 14:30:55 +08:00
|
|
|
#include "absl/memory/memory.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"
|
2022-03-02 21:17:10 +08:00
|
|
|
#include "absl/strings/string_view.h"
|
2019-06-25 20:48:56 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
2022-03-17 17:31:29 +08:00
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
2019-06-25 20:48:56 +08:00
|
|
|
#include "clang/Frontend/FrontendAction.h"
|
|
|
|
#include "clang/Tooling/Tooling.h"
|
2020-09-25 16:13:50 +08:00
|
|
|
#include "sandboxed_api/tools/clang_generator/emitter.h"
|
2019-06-25 20:48:56 +08:00
|
|
|
#include "sandboxed_api/tools/clang_generator/types.h"
|
|
|
|
|
|
|
|
namespace sapi {
|
|
|
|
|
|
|
|
struct GeneratorOptions {
|
|
|
|
template <typename ContainerT>
|
|
|
|
GeneratorOptions& set_function_names(const ContainerT& value) {
|
|
|
|
function_names.clear();
|
|
|
|
function_names.insert(std::begin(value), std::end(value));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool has_namespace() const { return !namespace_name.empty(); }
|
|
|
|
|
|
|
|
absl::flat_hash_set<std::string> function_names;
|
|
|
|
|
|
|
|
// Output options
|
|
|
|
std::string work_dir;
|
|
|
|
std::string name; // Name of the Sandboxed API
|
|
|
|
std::string namespace_name; // Namespace to wrap the SAPI in
|
2020-09-25 16:13:50 +08:00
|
|
|
// Output path of the generated header. Used to build the header include
|
|
|
|
// guard.
|
|
|
|
std::string out_file;
|
|
|
|
std::string embed_dir; // Directory with embedded includes
|
|
|
|
std::string embed_name; // Identifier of the embed object
|
2019-06-25 20:48:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class GeneratorASTVisitor
|
|
|
|
: public clang::RecursiveASTVisitor<GeneratorASTVisitor> {
|
|
|
|
public:
|
2020-09-25 16:13:50 +08:00
|
|
|
explicit GeneratorASTVisitor(const GeneratorOptions& options)
|
|
|
|
: options_(options) {}
|
|
|
|
|
2019-06-25 20:48:56 +08:00
|
|
|
bool VisitFunctionDecl(clang::FunctionDecl* decl);
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class GeneratorASTConsumer;
|
|
|
|
|
2020-10-06 16:04:11 +08:00
|
|
|
TypeCollector collector_;
|
|
|
|
|
2019-06-25 20:48:56 +08:00
|
|
|
std::vector<clang::FunctionDecl*> functions_;
|
|
|
|
|
2020-09-25 16:13:50 +08:00
|
|
|
const GeneratorOptions& options_;
|
|
|
|
};
|
2019-06-25 20:48:56 +08:00
|
|
|
|
|
|
|
class GeneratorASTConsumer : public clang::ASTConsumer {
|
|
|
|
public:
|
2020-09-25 16:13:50 +08:00
|
|
|
GeneratorASTConsumer(std::string in_file, Emitter& emitter,
|
|
|
|
const GeneratorOptions& options)
|
|
|
|
: in_file_(std::move(in_file)), visitor_(options), emitter_(emitter) {}
|
2019-06-25 20:48:56 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
void HandleTranslationUnit(clang::ASTContext& context) override;
|
|
|
|
|
|
|
|
std::string in_file_;
|
|
|
|
GeneratorASTVisitor visitor_;
|
2020-09-25 16:13:50 +08:00
|
|
|
|
|
|
|
Emitter& emitter_;
|
2019-06-25 20:48:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class GeneratorAction : public clang::ASTFrontendAction {
|
|
|
|
public:
|
2020-09-25 16:13:50 +08:00
|
|
|
GeneratorAction(Emitter& emitter, const GeneratorOptions& options)
|
|
|
|
: emitter_(emitter), options_(options) {}
|
2019-06-25 20:48:56 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(
|
|
|
|
clang::CompilerInstance&, llvm::StringRef in_file) override {
|
|
|
|
return absl::make_unique<GeneratorASTConsumer>(std::string(in_file),
|
2020-09-25 16:13:50 +08:00
|
|
|
emitter_, options_);
|
2019-06-25 20:48:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool hasCodeCompletionSupport() const override { return false; }
|
|
|
|
|
2020-09-25 16:13:50 +08:00
|
|
|
Emitter& emitter_;
|
|
|
|
const GeneratorOptions& options_;
|
2019-06-25 20:48:56 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class GeneratorFactory : public clang::tooling::FrontendActionFactory {
|
|
|
|
public:
|
2020-09-25 16:13:50 +08:00
|
|
|
// Does not take ownership
|
|
|
|
GeneratorFactory(Emitter& emitter, const GeneratorOptions& options)
|
|
|
|
: emitter_(emitter), options_(options) {}
|
2019-06-25 20:48:56 +08:00
|
|
|
|
|
|
|
private:
|
2020-06-10 14:30:55 +08:00
|
|
|
#if LLVM_VERSION_MAJOR >= 10
|
|
|
|
std::unique_ptr<clang::FrontendAction> create() override {
|
2020-09-25 16:13:50 +08:00
|
|
|
return absl::make_unique<GeneratorAction>(emitter_, options_);
|
2020-06-10 14:30:55 +08:00
|
|
|
}
|
|
|
|
#else
|
2019-06-25 20:48:56 +08:00
|
|
|
clang::FrontendAction* create() override {
|
2020-09-25 16:13:50 +08:00
|
|
|
return new GeneratorAction(emitter_, options_);
|
2019-06-25 20:48:56 +08:00
|
|
|
}
|
2020-06-10 14:30:55 +08:00
|
|
|
#endif
|
2019-06-25 20:48:56 +08:00
|
|
|
|
2022-03-17 17:31:29 +08:00
|
|
|
bool runInvocation(
|
|
|
|
std::shared_ptr<clang::CompilerInvocation> invocation,
|
|
|
|
clang::FileManager* files,
|
|
|
|
std::shared_ptr<clang::PCHContainerOperations> pch_container_ops,
|
|
|
|
clang::DiagnosticConsumer* diag_consumer) override;
|
|
|
|
|
2020-09-25 16:13:50 +08:00
|
|
|
Emitter& emitter_;
|
|
|
|
const GeneratorOptions& options_;
|
2019-06-25 20:48:56 +08:00
|
|
|
};
|
|
|
|
|
2020-09-25 16:13:50 +08:00
|
|
|
std::string GetOutputFilename(absl::string_view source_file);
|
|
|
|
|
2022-03-02 21:17:10 +08:00
|
|
|
inline absl::string_view ToStringView(llvm::StringRef ref) {
|
|
|
|
return absl::string_view(ref.data(), ref.size());
|
|
|
|
}
|
|
|
|
|
2019-06-25 20:48:56 +08:00
|
|
|
} // namespace sapi
|
|
|
|
|
2020-05-28 22:01:09 +08:00
|
|
|
#endif // SANDBOXED_API_TOOLS_CLANG_GENERATOR_GENERATOR_H_
|