Skip to content

Commit 8738a27

Browse files
refactor Cpp::GetAllCppNames
1 parent 5f1d956 commit 8738a27

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed

include/clang/Interpreter/CppInterOp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <cstdint>
1515
#include <string>
1616
#include <vector>
17+
#include <set>
1718

1819
// The cross-platform CPPINTEROP_API macro definition
1920
#if defined _WIN32 || defined __CYGWIN__
@@ -668,7 +669,7 @@ 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, std::set<std::string> &names);
672673

673674
CPPINTEROP_API void DumpScope(TCppScope_t scope);
674675

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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -717,11 +717,12 @@ TEST(ScopeReflectionTest, GetAllCppNames) {
717717
GetAllTopLevelDecls(code, Decls);
718718

719719
auto test_get_all_cpp_names = [](Decl *D, const std::vector<std::string> &truth_names) {
720-
auto names = Cpp::GetAllCppNames(D);
720+
std::set<std::string> names;
721+
Cpp::GetAllCppNames(D, names);
721722
EXPECT_EQ(names.size(), truth_names.size());
722723

723724
for (unsigned i = 0; i < truth_names.size() && i < names.size(); i++) {
724-
EXPECT_EQ(names[i], truth_names[i]);
725+
EXPECT_TRUE(names.find(truth_names[i]) != names.end());
725726
}
726727
};
727728

0 commit comments

Comments
 (0)