Skip to content

Commit 717f333

Browse files
Vipul-Cariappavgvassilev
authored andcommitted
refactor Cpp::GetAllCppNames
1 parent 5f1d956 commit 717f333

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-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: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "llvm/Support/Debug.h"
3535
#include "llvm/Support/raw_os_ostream.h"
3636

37+
#include <set>
3738
#include <sstream>
3839
#include <string>
3940

@@ -3108,7 +3109,7 @@ namespace Cpp {
31083109
return nullptr;
31093110
}
31103111

3111-
std::vector<std::string> GetAllCppNames(TCppScope_t scope) {
3112+
void GetAllCppNames(TCppScope_t scope, std::set<std::string>& names) {
31123113
auto *D = (clang::Decl *)scope;
31133114
clang::DeclContext *DC;
31143115
clang::DeclContext::decl_iterator decl;
@@ -3124,17 +3125,14 @@ namespace Cpp {
31243125
DC = clang::TranslationUnitDecl::castToDeclContext(TUD);
31253126
decl = DC->decls_begin();
31263127
} else {
3127-
return {};
3128+
return;
31283129
}
31293130

3130-
std::vector<std::string> names;
31313131
for (/* decl set above */; decl != DC->decls_end(); decl++) {
31323132
if (auto *ND = llvm::dyn_cast_or_null<NamedDecl>(*decl)) {
3133-
names.push_back(ND->getNameAsString());
3133+
names.insert(ND->getNameAsString());
31343134
}
31353135
}
3136-
3137-
return names;
31383136
}
31393137

31403138
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)