|
41 | 41 | #include "llvm/ADT/DenseMap.h"
|
42 | 42 | #include "llvm/ADT/SmallVector.h"
|
43 | 43 | #include "llvm/ADT/StringRef.h"
|
| 44 | +#include "llvm/Support/Casting.h" |
44 | 45 | #include "llvm/Support/ErrorHandling.h"
|
45 | 46 | #include "llvm/Support/FileSystem.h"
|
46 | 47 | #include "llvm/Support/Path.h"
|
@@ -75,18 +76,62 @@ bool isPrivateProtoDecl(const NamedDecl &ND) {
|
75 | 76 | if (ND.getIdentifier() == nullptr)
|
76 | 77 | return false;
|
77 | 78 | auto Name = ND.getIdentifier()->getName();
|
78 |
| - if (!Name.contains('_')) |
79 |
| - return false; |
80 |
| - // Nested proto entities (e.g. Message::Nested) have top-level decls |
81 |
| - // that shouldn't be used (Message_Nested). Ignore them completely. |
82 |
| - // The nested entities are dangling type aliases, we may want to reconsider |
83 |
| - // including them in the future. |
84 |
| - // For enum constants, SOME_ENUM_CONSTANT is not private and should be |
85 |
| - // indexed. Outer_INNER is private. This heuristic relies on naming style, it |
86 |
| - // will include OUTER_INNER and exclude some_enum_constant. |
87 |
| - // FIXME: the heuristic relies on naming style (i.e. no underscore in |
88 |
| - // user-defined names) and can be improved. |
89 |
| - return (ND.getKind() != Decl::EnumConstant) || llvm::any_of(Name, islower); |
| 79 | + // There are some internal helpers like _internal_set_foo(); |
| 80 | + if (Name.contains("_internal_")) |
| 81 | + return true; |
| 82 | + |
| 83 | + // https://protobuf.dev/reference/cpp/cpp-generated/#nested-types |
| 84 | + // Nested entities (messages/enums) has two names, one at the top-level scope, |
| 85 | + // with a mangled name created by prepending all the outer types. These names |
| 86 | + // are almost never preferred by the developers, so exclude them from index. |
| 87 | + // e.g. |
| 88 | + // message Foo { |
| 89 | + // message Bar {} |
| 90 | + // enum E { A } |
| 91 | + // } |
| 92 | + // |
| 93 | + // yields: |
| 94 | + // class Foo_Bar {}; |
| 95 | + // enum Foo_E { Foo_E_A }; |
| 96 | + // class Foo { |
| 97 | + // using Bar = Foo_Bar; |
| 98 | + // static constexpr Foo_E A = Foo_E_A; |
| 99 | + // }; |
| 100 | + |
| 101 | + // We get rid of Foo_Bar and Foo_E by discarding any top-level entries with |
| 102 | + // `_` in the name. This relies on original message/enum not having `_` in the |
| 103 | + // name. Hence might go wrong in certain cases. |
| 104 | + if (ND.getDeclContext()->isNamespace()) { |
| 105 | + // Strip off some known public suffix helpers for enums, rest of the helpers |
| 106 | + // are generated inside record decls so we don't care. |
| 107 | + // https://protobuf.dev/reference/cpp/cpp-generated/#enum |
| 108 | + Name.consume_back("_descriptor"); |
| 109 | + Name.consume_back("_IsValid"); |
| 110 | + Name.consume_back("_Name"); |
| 111 | + Name.consume_back("_Parse"); |
| 112 | + Name.consume_back("_MIN"); |
| 113 | + Name.consume_back("_MAX"); |
| 114 | + Name.consume_back("_ARRAYSIZE"); |
| 115 | + return Name.contains('_'); |
| 116 | + } |
| 117 | + |
| 118 | + // EnumConstantDecls need some special attention, despite being nested in a |
| 119 | + // TagDecl, they might still have mangled names. We filter those by checking |
| 120 | + // if it has parent's name as a prefix. |
| 121 | + // This might go wrong if a nested entity has a name that starts with parent's |
| 122 | + // name, e.g: enum Foo { Foo_X }. |
| 123 | + if (llvm::isa<EnumConstantDecl>(&ND)) { |
| 124 | + auto *DC = llvm::cast<EnumDecl>(ND.getDeclContext()); |
| 125 | + if (!DC || !DC->getIdentifier()) |
| 126 | + return false; |
| 127 | + auto CtxName = DC->getIdentifier()->getName(); |
| 128 | + return !CtxName.empty() && Name.consume_front(CtxName) && |
| 129 | + Name.consume_front("_"); |
| 130 | + } |
| 131 | + |
| 132 | + // Now we're only left with fields/methods without an `_internal_` in the |
| 133 | + // name, they're intended for public use. |
| 134 | + return false; |
90 | 135 | }
|
91 | 136 |
|
92 | 137 | // We only collect #include paths for symbols that are suitable for global code
|
|
0 commit comments