sandboxed-api/sandboxed_api/tools/clang_generator/BUILD.bazel
Christian Blichmann 1ae04ac332 clang_generator: Use fully-qualified names, emit in source order
When generating headers from libraries that export functions as `extern "C"`
but still use namespaces (C-compatible C++ libraries), we want to generate
a Sandboxed API that includes fully-qualified namespace names as well.

In addition, we want the generated API to have the same source order as the
original library. Not only is this less surprising when reading the generated
code, it's also more accurate. Previously, we'd bundle all definitions in a
namespace and sort those alphabetically, but for code that relies on symbols
from another namespace to be available, generation will fail:

```c++
namespace zzz {
using entity_count_t = uint64_t;
}  // namespace zzz
namespace sheep_counter {
using sheep_count_t = :💤:entity_count_t;
extern "C" void IncreaseSheepCounter(sheep_count_t increment);
}  // namespace sheep_counter
```

PiperOrigin-RevId: 486586024
Change-Id: I419c9db8e9cb5b904364b353e2dc3d7f1030fab3
2022-11-07 00:37:53 -08:00

105 lines
3.2 KiB
Python

# Copyright 2022 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
#
# https://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.
load("//sandboxed_api/bazel:build_defs.bzl", "sapi_platform_copts")
licenses(["notice"])
cc_library(
name = "generator",
srcs = [
"diagnostics.cc",
"emitter.cc",
"generator.cc",
"types.cc",
],
hdrs = [
"diagnostics.h",
"emitter.h",
"generator.h",
"types.h",
],
copts = sapi_platform_copts(),
deps = [
"//sandboxed_api/util:status",
"@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/container:node_hash_set",
"@com_google_absl//absl/random",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:cord",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/types:optional",
"@llvm-project//clang:ast",
"@llvm-project//clang:basic",
"@llvm-project//clang:format",
"@llvm-project//clang:frontend",
"@llvm-project//clang:lex",
"@llvm-project//clang:tooling",
"@llvm-project//llvm:Support",
],
)
cc_test(
name = "generator_test",
srcs = [
"emitter_test.cc",
"frontend_action_test_util.cc",
"frontend_action_test_util.h",
],
copts = sapi_platform_copts(),
deps = [
":generator",
"//sandboxed_api:testing",
"//sandboxed_api/util:status_matchers",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_googletest//:gtest_main",
"@llvm-project//clang:basic",
"@llvm-project//clang:frontend",
"@llvm-project//clang:tooling",
"@llvm-project//llvm:Support",
],
)
# Clang tool that generates Sandboxed API headers
cc_binary(
name = "generator_tool",
srcs = [
"compilation_database.cc",
"compilation_database.h",
"generator_tool.cc",
],
copts = sapi_platform_copts(),
visibility = ["//visibility:public"],
deps = [
":generator",
"//sandboxed_api/util:file_helpers",
"//sandboxed_api/util:fileops",
"//sandboxed_api/util:status",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@llvm-project//clang:ast",
"@llvm-project//clang:driver",
"@llvm-project//clang:frontend",
"@llvm-project//clang:tooling",
"@llvm-project//llvm:Support",
],
)