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
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// 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/generator.h"
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "absl/status/status.h"
|
2020-09-02 23:46:48 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2020-09-25 16:13:50 +08:00
|
|
|
#include "clang/AST/Type.h"
|
2019-06-25 20:48:56 +08:00
|
|
|
#include "clang/Format/Format.h"
|
|
|
|
#include "sandboxed_api/tools/clang_generator/diagnostics.h"
|
|
|
|
#include "sandboxed_api/tools/clang_generator/emitter.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/util/fileops.h"
|
2019-06-25 20:48:56 +08:00
|
|
|
#include "sandboxed_api/util/status_macros.h"
|
|
|
|
|
|
|
|
namespace sapi {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Replaces the file extension of a path name.
|
|
|
|
std::string ReplaceFileExtension(absl::string_view path,
|
|
|
|
absl::string_view new_extension) {
|
2020-05-28 22:01:09 +08:00
|
|
|
auto last_slash = path.rfind('/');
|
|
|
|
auto pos = path.rfind('.', last_slash);
|
|
|
|
if (pos != absl::string_view::npos && last_slash != absl::string_view::npos) {
|
2019-06-25 20:48:56 +08:00
|
|
|
pos += last_slash;
|
|
|
|
}
|
|
|
|
return absl::StrCat(path.substr(0, pos), new_extension);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline absl::string_view ToStringView(llvm::StringRef ref) {
|
|
|
|
return absl::string_view(ref.data(), ref.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2020-09-25 16:13:50 +08:00
|
|
|
std::string GetOutputFilename(absl::string_view source_file) {
|
|
|
|
return ReplaceFileExtension(source_file, ".sapi.h");
|
|
|
|
}
|
|
|
|
|
2019-06-25 20:48:56 +08:00
|
|
|
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
|
2020-09-25 16:13:50 +08:00
|
|
|
(options_.function_names.empty() ||
|
|
|
|
options_.function_names.count(ToStringView(decl->getName())) > 0)) {
|
2019-06-25 20:48:56 +08:00
|
|
|
functions_.push_back(decl);
|
2020-09-25 16:13:50 +08:00
|
|
|
|
2020-10-06 16:04:11 +08:00
|
|
|
collector_.CollectRelatedTypes(decl->getDeclaredReturnType());
|
2019-06-25 20:48:56 +08:00
|
|
|
for (const clang::ParmVarDecl* param : decl->parameters()) {
|
2020-10-06 16:04:11 +08:00
|
|
|
collector_.CollectRelatedTypes(param->getType());
|
2019-06-25 20:48:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GeneratorASTConsumer::HandleTranslationUnit(clang::ASTContext& context) {
|
2020-09-25 16:13:50 +08:00
|
|
|
std::cout << "Processing " << in_file_ << "\n";
|
2019-06-25 20:48:56 +08:00
|
|
|
if (!visitor_.TraverseDecl(context.getTranslationUnitDecl())) {
|
2020-09-25 16:13:50 +08:00
|
|
|
ReportFatalError(context.getDiagnostics(),
|
|
|
|
context.getTranslationUnitDecl()->getBeginLoc(),
|
|
|
|
"AST traversal exited early");
|
2019-06-25 20:48:56 +08:00
|
|
|
}
|
|
|
|
|
2020-10-06 16:04:11 +08:00
|
|
|
for (clang::QualType qual : visitor_.collector_.collected()) {
|
2020-09-25 16:13:50 +08:00
|
|
|
emitter_.CollectType(qual);
|
|
|
|
}
|
|
|
|
for (clang::FunctionDecl* func : visitor_.functions_) {
|
|
|
|
emitter_.CollectFunction(func);
|
2019-06-25 20:48:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 22:01:09 +08:00
|
|
|
} // namespace sapi
|