Skip to content

Commit ed6fd23

Browse files
refactor Cpp::GetAllCppNames
1 parent 5f1d956 commit ed6fd23

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

include/clang/Interpreter/CppInterOp.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <cassert>
1414
#include <cstdint>
15+
#include <set>
1516
#include <string>
1617
#include <vector>
1718

@@ -668,7 +669,8 @@ namespace Cpp {
668669
const std::vector<TemplateArgInfo>& explicit_types,
669670
const std::vector<TemplateArgInfo>& arg_types);
670671

671-
CPPINTEROP_API std::vector<std::string> GetAllCppNames(TCppScope_t scope);
672+
CPPINTEROP_API void GetAllCppNames(TCppScope_t scope,
673+
std::set<std::string>& names);
672674

673675
CPPINTEROP_API void DumpScope(TCppScope_t scope);
674676

lib/Interpreter/CppInterOp.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3108,7 +3108,7 @@ namespace Cpp {
31083108
return nullptr;
31093109
}
31103110

3111-
std::vector<std::string> GetAllCppNames(TCppScope_t scope) {
3111+
void GetAllCppNames(TCppScope_t scope, std::set<std::string>& names) {
31123112
auto *D = (clang::Decl *)scope;
31133113
clang::DeclContext *DC;
31143114
clang::DeclContext::decl_iterator decl;
@@ -3124,17 +3124,14 @@ namespace Cpp {
31243124
DC = clang::TranslationUnitDecl::castToDeclContext(TUD);
31253125
decl = DC->decls_begin();
31263126
} else {
3127-
return {};
3127+
return;
31283128
}
31293129

3130-
std::vector<std::string> names;
31313130
for (/* decl set above */; decl != DC->decls_end(); decl++) {
31323131
if (auto *ND = llvm::dyn_cast_or_null<NamedDecl>(*decl)) {
3133-
names.push_back(ND->getNameAsString());
3132+
names.insert(ND->getNameAsString());
31343133
}
31353134
}
3136-
3137-
return names;
31383135
}
31393136

31403137
void GetEnums(TCppScope_t scope, std::vector<std::string>& Result) {

unittests/CppInterOp/ScopeReflectionTest.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -716,14 +716,16 @@ TEST(ScopeReflectionTest, GetAllCppNames) {
716716

717717
GetAllTopLevelDecls(code, Decls);
718718

719-
auto test_get_all_cpp_names = [](Decl *D, const std::vector<std::string> &truth_names) {
720-
auto names = Cpp::GetAllCppNames(D);
721-
EXPECT_EQ(names.size(), truth_names.size());
722-
723-
for (unsigned i = 0; i < truth_names.size() && i < names.size(); i++) {
724-
EXPECT_EQ(names[i], truth_names[i]);
725-
}
726-
};
719+
auto test_get_all_cpp_names =
720+
[](Decl* D, const std::vector<std::string>& truth_names) {
721+
std::set<std::string> names;
722+
Cpp::GetAllCppNames(D, names);
723+
EXPECT_EQ(names.size(), truth_names.size());
724+
725+
for (unsigned i = 0; i < truth_names.size() && i < names.size(); i++) {
726+
EXPECT_TRUE(names.find(truth_names[i]) != names.end());
727+
}
728+
};
727729

728730
test_get_all_cpp_names(Decls[0], {"a"});
729731
test_get_all_cpp_names(Decls[1], {"b"});

0 commit comments

Comments
 (0)