117 clang::CXXRecordDecl *decl,
118 std::vector<InstanceDeclType> &found_instances) {
123 LLVM_DEBUG(llvm::dbgs() <<
"\n[findInstance] instance size: "
126 LLVM_DEBUG(llvm::dbgs()
127 <<
"find decl name: " << decl->getName() <<
" " << decl <<
"\n");
131 auto p_field_var_decl{element.first};
132 auto instance{element.second};
135 if (
auto *p_field{clang::dyn_cast<clang::FieldDecl>(p_field_var_decl)}) {
136 auto qtype{p_field->getType().getTypePtr()};
137 if (qtype->isRecordType()) {
138 auto rt{qtype->getAsCXXRecordDecl()};
139 LLVM_DEBUG(llvm::dbgs() <<
"- fd " << rt <<
" "
140 << p_field->getCanonicalDecl() <<
" ");
141 LLVM_DEBUG(instance.dump());
144 LLVM_DEBUG(llvm::dbgs()
145 <<
"- Insert fieldDecl into found instance\n");
146 found_instances.push_back(
152 auto p_var{clang::dyn_cast<clang::VarDecl>(p_field_var_decl)};
153 auto qtype{p_var->getType().getTypePtr()};
155 std::string dbg{
"[InstanceMatcher] VarDecl"};
156 if (qtype->isRecordType()) {
157 auto rt{qtype->getAsCXXRecordDecl()};
158 LLVM_DEBUG(llvm::dbgs() <<
"- vd " << rt <<
" "
160 LLVM_DEBUG(instance.dump());
162 LLVM_DEBUG(llvm::dbgs()
163 <<
"- Insert vardecl into found instance\n");
164 found_instances.push_back(
170 LLVM_DEBUG(llvm::dbgs()
171 <<
"=> found_instances: " << found_instances.size() <<
"\n");
173 return (found_instances.size() != 0);
205 auto match_cxx_ctor_init =
209 forEachConstructorInitializer(
211 isMemberInitializer(),
224 hasUnqualifiedDesugaredType(
236 hasUnqualifiedDesugaredType(
238 hasElementType(hasUnqualifiedDesugaredType(
253 hasUnqualifiedDesugaredType(
255 hasElementType(hasUnqualifiedDesugaredType(
256 arrayType(hasElementType(hasUnqualifiedDesugaredType(
275 fieldDecl().bind(
"ctor_fd")
286 auto match_with_parent =
288 hasUnqualifiedDesugaredType(
290 hasDeclaration(match_cxx_ctor_init))))).bind(
"parent_fd");
293 auto match_instances_decl =
317 hasUnqualifiedDesugaredType(
319 hasElementType(hasUnqualifiedDesugaredType(
336 ).bind(
"instance_vd");
338 auto match_base_decl = match_cxx_ctor_init;
343 finder.addMatcher(match_instances_decl,
this);
344 finder.addMatcher(match_with_parent,
this);
345 finder.addMatcher(match_base_decl,
this);
348 void parseVarDecl(clang::VarDecl *instance_decl, std::string &instance_name) {
349 std::string name{instance_decl->getIdentifier()->getNameStart()};
352 auto var_name{instance_decl->getNameAsString()};
355 auto var_type_name{instance_decl->getType().getAsString()};
358 std::string parent_name{};
359 clang::ValueDecl *parent_rdecl{
nullptr};
361 LLVM_DEBUG(llvm::dbgs()
362 <<
"=> VD: var_name " << var_name <<
" var_type_name "
363 << var_type_name <<
" parent_name " << parent_name
367 parsed_instance.
var_name = var_name;
368 parsed_instance.var_type_name = var_type_name;
369 parsed_instance.instance_name = instance_name;
370 parsed_instance.add_instance_name(instance_name);
372 parsed_instance.type_decl =
373 instance_decl->getType().getTypePtr()->getAsCXXRecordDecl();
375 parsed_instance.instance_decl = instance_decl;
376 parsed_instance.is_field_decl =
false;
378 parsed_instance.is_array =
false;
379 parsed_instance.parent_name = parent_name;
380 parsed_instance.parent_decl = parent_rdecl;
382 LLVM_DEBUG(parsed_instance.dump(););
386 LLVM_DEBUG(llvm::dbgs() <<
"Inserting VD instance"
389 std::pair<Decl *, ModuleInstanceType>(instance_decl, parsed_instance));
394 clang::ValueDecl *parent_decl, std::string instance_name,
396 std::string name{instance_decl->getIdentifier()->getNameStart()};
399 auto var_name{instance_decl->getNameAsString()};
402 auto var_type_name{instance_decl->getType().getAsString()};
404 std::string parent_name{};
406 parent_name = parent_decl->getName().str();
409 auto array_indices{index_map[instance_name]};
410 LLVM_DEBUG(llvm::dbgs()
411 <<
"=> FD: var_name: " << var_name <<
" var_type_name, "
412 << var_type_name <<
" parent_name " << parent_name
413 <<
", instance name " << instance_name <<
"[ "
414 << std::get<0>(array_indices) <<
", "
415 << std::get<1>(array_indices) <<
", "
416 << std::get<2>(array_indices)
421 parsed_instance.
var_name = var_name;
422 parsed_instance.var_type_name = var_type_name;
423 LLVM_DEBUG(instance_decl->getType()->dump(););
426 const ArrayType *array_1d{
427 instance_decl->getType().getTypePtrOrNull()->getAsArrayTypeUnsafe()};
428 const ArrayType *array_2d{
nullptr};
429 const ArrayType *array_3d{
nullptr};
430 const ArrayType *array_type{
nullptr};
437 array_type = array_1d;
439 array_1d->getElementType().getTypePtrOrNull()->getAsArrayTypeUnsafe();
441 array_type = array_2d;
442 array_3d = array_2d->getElementType()
444 ->getAsArrayTypeUnsafe();
447 array_type = array_3d;
452 LLVM_DEBUG(llvm::outs() <<
" All dim. arrays: " << array_1d <<
" "
453 << array_2d <<
" " << array_3d <<
"\n";);
459 auto element_type{array_type->getElementType().getTypePtr()};
460 parsed_instance.type_decl = element_type->getAsCXXRecordDecl();
463 <<
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IS ARRAY type decl null: "
464 << parsed_instance.type_decl <<
"\n";
465 element_type->dump(););
466 parsed_instance.setArrayType();
468 parsed_instance.setArrayParameters(index_map[instance_name]);
469 LLVM_DEBUG(llvm::outs() <<
"Dimension of array: "
470 << parsed_instance.getArrayDimension() <<
"\n";);
473 parsed_instance.type_decl =
474 instance_decl->getType().getTypePtr()->getAsCXXRecordDecl();
477 <<
"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ NOT ARRAY type decl null: "
478 << parsed_instance.type_decl <<
"\n";);
482 instance_decl->dump();
484 parsed_instance.instance_decl = instance_decl;
485 parsed_instance.is_field_decl =
true;
486 parsed_instance.parent_name = parent_name;
487 parsed_instance.parent_decl = parent_decl;
488 parsed_instance.instance_name = instance_name;
489 parsed_instance.add_instance_name(instance_name);
491 LLVM_DEBUG(parsed_instance.dump(););
493 LLVM_DEBUG(llvm::dbgs() <<
"Inserting FD instance\n");
503 instance_decl, parsed_instance));
507 exists_instance->second.add_instance_name(instance_name);
511 virtual void run(
const MatchFinder::MatchResult &result) {
512 LLVM_DEBUG(llvm::dbgs()
513 <<
" ================== INSTANCE MATCHER ================= \n");
515 auto instance_vd =
const_cast<clang::VarDecl *
>(
516 result.Nodes.getNodeAs<clang::VarDecl>(
"instance_vd"));
517 auto ctor_init =
const_cast<clang::CXXCtorInitializer *
>(
518 result.Nodes.getNodeAs<clang::CXXCtorInitializer>(
"ctor_init"));
519 auto ctor_fd =
const_cast<clang::FieldDecl *
>(
520 result.Nodes.getNodeAs<FieldDecl>(
"ctor_fd"));
526 parent_fd =
const_cast<clang::ValueDecl *
>(
527 result.Nodes.getNodeAs<clang::ValueDecl>(
"parent_fd"));
530 auto ctor_arg =
const_cast<clang::Stmt *
>(
531 result.Nodes.getNodeAs<clang::Stmt>(
"ctor_arg"));
537 if (ctor_fd && ctor_init && parent_fd) {
538 LLVM_DEBUG(llvm::dbgs()
539 <<
"#### CTOR_FD: parent_fd " << parent_fd->getNameAsString()
540 <<
" ctor_fd " << ctor_fd->getNameAsString() <<
"\n");
541 LLVM_DEBUG(ctor_fd->dump());
545 clang::Expr *expr = ctor_init->getInit()->IgnoreImplicit();
546 clang::CXXConstructExpr *cexpr{
547 clang::dyn_cast<clang::CXXConstructExpr>(expr)};
548 clang::InitListExpr *iexpr{clang::dyn_cast<clang::InitListExpr>(expr)};
557 if ((iexpr !=
nullptr) && (cexpr ==
nullptr)) {
558 LLVM_DEBUG(llvm::dbgs() <<
"### IEXPR is not NULL\n";
560 llvm::dbgs() <<
"######## Going through index map: "
561 << index_map.size() <<
"\n";);
562 for (
auto const &init : index_map) {
563 auto submodule_instance_name{init.first};
566 LLVM_DEBUG(llvm::dbgs() <<
"#==> submodule_instance_name "
567 << submodule_instance_name <<
"\n");
571 MatchFinder iarg_registry{};
576 if (ctor_fd->hasInClassInitializer()) {
577 LLVM_DEBUG(llvm::dbgs() <<
"### In-class initializer\n";);
578 const Expr *in_class_init{ctor_fd->getInClassInitializer()};
579 iarg_registry.match(*in_class_init->IgnoreImplicit(),
582 LLVM_DEBUG(llvm::dbgs() <<
"### Constructor initializer\n";);
583 iarg_registry.match(*cexpr, *result.Context);
586 LLVM_DEBUG(iarg_matcher.dump();
588 <<
"#### IndexMap: " << index_map.size() <<
"\n";);
590 if (
auto inst_literal = iarg_matcher.getInstanceLiteral()) {
591 auto submodule_instance_name = inst_literal->getString().str();
594 LLVM_DEBUG(llvm::dbgs() <<
"=> submodule_instance_name "
595 << submodule_instance_name <<
"\n");
603 auto instance_name{cast<StringLiteral>(ctor_arg)->getString().str()};
604 LLVM_DEBUG(llvm::dbgs()
605 <<
"## VD: " << instance_vd->getNameAsString() <<
" "
606 << instance_vd <<
" instance_name " << instance_name <<
"\n");