|
| 1 | +// |
| 2 | +// Licensed under the Apache License v2.0 with LLVM Exceptions. |
| 3 | +// See https://llvm.org/LICENSE.txt for license information. |
| 4 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 | +// |
| 6 | +// Copyright (c) 2025 Alan de Freitas (alandefreitas@gmail.com) |
| 7 | +// |
| 8 | +// Official repository: https://github.com/cppalliance/mrdocs |
| 9 | +// |
| 10 | + |
| 11 | +#include "NamespacesFinalizer.hpp" |
| 12 | + |
| 13 | +namespace clang::mrdocs { |
| 14 | + |
| 15 | +NamespacesFinalizer::FinalizerResult |
| 16 | +NamespacesFinalizer:: |
| 17 | +operator()(NamespaceInfo& I) |
| 18 | +{ |
| 19 | + report::trace( |
| 20 | + "Finalizing namespace '{}'", |
| 21 | + corpus_.Corpus::qualifiedName(I)); |
| 22 | + |
| 23 | + // 1) Finalize sub-namespaces |
| 24 | + SmallVector<SymbolID, 64> removedNamespaces; |
| 25 | + for (auto subNamespaces = corpus_.find(I.Members.Namespaces); |
| 26 | + Info& info: subNamespaces) |
| 27 | + { |
| 28 | + MRDOCS_ASSERT(info.isNamespace()); |
| 29 | + SymbolID memberID = info.id; |
| 30 | + auto& member = dynamic_cast<NamespaceInfo&>(info); |
| 31 | + if (auto const res = (*this)(member); |
| 32 | + res == FinalizerResult::Removed) |
| 33 | + { |
| 34 | + removedNamespaces.push_back(memberID); |
| 35 | + } |
| 36 | + } |
| 37 | + for (SymbolID const& memberID : removedNamespaces) |
| 38 | + { |
| 39 | + auto it = std::ranges::find(I.Members.Namespaces, memberID); |
| 40 | + MRDOCS_ASSERT(it != I.Members.Namespaces.end()); |
| 41 | + I.Members.Namespaces.erase(it); |
| 42 | + } |
| 43 | + |
| 44 | + // No more steps for the global namespace |
| 45 | + MRDOCS_CHECK_OR(I.id != SymbolID::global, FinalizerResult::None); |
| 46 | + |
| 47 | + // No more steps for documented namespaces |
| 48 | + MRDOCS_CHECK_OR(!I.javadoc, FinalizerResult::None); |
| 49 | + |
| 50 | + // 3) Remove empty undocumented namespaces |
| 51 | + auto memberIds = allMembers(I); |
| 52 | + if (std::ranges::empty(memberIds)) |
| 53 | + { |
| 54 | + if (!corpus_.config->extractEmptyNamespaces) |
| 55 | + { |
| 56 | + auto const it = corpus_.info_.find(I.id); |
| 57 | + MRDOCS_CHECK_OR(it != corpus_.info_.end(), FinalizerResult::None); |
| 58 | + corpus_.info_.erase(it); |
| 59 | + return FinalizerResult::Removed; |
| 60 | + } |
| 61 | + return FinalizerResult::None; |
| 62 | + } |
| 63 | + |
| 64 | + // 4) If the namespace is regular and undocumented, |
| 65 | + // it's extraction mode is updated according to |
| 66 | + // its members |
| 67 | + MRDOCS_CHECK_OR(I.Extraction == ExtractionMode::Regular, FinalizerResult::None); |
| 68 | + auto members = corpus_.find(memberIds); |
| 69 | + bool allDependencies = true; |
| 70 | + bool allImplementationDefined = true; |
| 71 | + bool anySeeBelow = false; |
| 72 | + for (Info const& member : members) |
| 73 | + { |
| 74 | + if (member.Extraction == ExtractionMode::Regular) |
| 75 | + { |
| 76 | + // It has a regular member, so it should remain |
| 77 | + // regular |
| 78 | + return FinalizerResult::None; |
| 79 | + } |
| 80 | + if (member.Extraction != ExtractionMode::Dependency) |
| 81 | + { |
| 82 | + allDependencies = false; |
| 83 | + } |
| 84 | + if (member.Extraction != ExtractionMode::ImplementationDefined) |
| 85 | + { |
| 86 | + allImplementationDefined = false; |
| 87 | + } |
| 88 | + if (member.Extraction == ExtractionMode::SeeBelow) |
| 89 | + { |
| 90 | + anySeeBelow = true; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if (allDependencies) |
| 95 | + { |
| 96 | + I.Extraction = ExtractionMode::Dependency; |
| 97 | + } |
| 98 | + else if (allImplementationDefined) |
| 99 | + { |
| 100 | + I.Extraction = ExtractionMode::ImplementationDefined; |
| 101 | + } |
| 102 | + else if (anySeeBelow) |
| 103 | + { |
| 104 | + I.Extraction = ExtractionMode::SeeBelow; |
| 105 | + } |
| 106 | + return FinalizerResult::Changed; |
| 107 | +} |
| 108 | + |
| 109 | +} // clang::mrdocs |
0 commit comments