Clang tool: Prevent extra nesting of namespaces

When specifying the `namespace` argument in Bazel (`NAMESPACE` in CMake), the
Clang tool used to put _all_ dependent types in that namespace.

For a declaration of `namespace a::b { struct S {...};` and a `namespace`
argument of `a::b`, this means that the header output was similar to
```
namespace a::b {
namespace a::b {
struct S { ...
```

This was never intended and also does not match the Python based header
generator. The Clang tool now "merges" those same namespaces. This is
correct, as it processes `namespace`d spellings with their full namespace
path.

PiperOrigin-RevId: 557393076
Change-Id: I1474dd30b6c4150d0ae3c1c48579f88060974980
This commit is contained in:
Christian Blichmann 2023-08-16 01:17:11 -07:00 committed by Copybara-Service
parent 1c2596785b
commit c501379056

View File

@ -401,14 +401,15 @@ absl::StatusOr<std::string> EmitHeader(
// Emit type dependencies
if (!rendered_types.empty()) {
absl::StrAppend(&out, "// Types this API depends on\n");
std::string last_ns_name;
std::string last_ns_name = options.namespace_name;
for (const RenderedType* rt : rendered_types) {
const auto& [ns_name, spelling] = *rt;
if (last_ns_name != ns_name) {
if (!last_ns_name.empty()) {
if (!last_ns_name.empty() && last_ns_name != options.namespace_name) {
absl::StrAppend(&out, "} // namespace ", last_ns_name, "\n\n");
}
if (!ns_name.empty()) {
if (!ns_name.empty() && ns_name != options.namespace_name) {
absl::StrAppend(&out, "namespace ", ns_name, " {\n");
}
last_ns_name = ns_name;
@ -416,7 +417,7 @@ absl::StatusOr<std::string> EmitHeader(
absl::StrAppend(&out, spelling, ";\n");
}
if (!last_ns_name.empty()) {
if (!last_ns_name.empty() && last_ns_name != options.namespace_name) {
absl::StrAppend(&out, "} // namespace ", last_ns_name, "\n\n");
}
}