22 clang::PrintingPolicy pp(Result.Context->getLangOpts());
23 pp.PrintInjectedClassNameWithArguments =
true;
24 pp.PrintCanonicalTypes =
true;
25 pp.SuppressDefaultTemplateArgs =
true;
26 pp.FullyQualifiedName =
true;
27 pp.SuppressScope =
false;
30 if(
const clang::CXXMethodDecl* MFS = Result.Nodes.getNodeAs<clang::CXXMethodDecl>(
"func_definition")) {
31 spdlog::debug(
"Processing memfunc {}", MFS->getNameAsString());
32 spdlog::debug(
"TI={}, CTI={}", MFS->isTemplateInstantiation(), (MFS->getParent()->getMemberSpecializationInfo() !=
nullptr));
33 if(MFS->isTemplateInstantiation() or (MFS->getParent()->getMemberSpecializationInfo() !=
nullptr)) {
34 if(
const clang::MemberSpecializationInfo* MSI = MFS->getMemberSpecializationInfo()) {
35 if(MSI->getTemplateSpecializationKind() != clang::TSK_ExplicitInstantiationDefinition) {
return; }
36 }
else if(
const clang::FunctionTemplateSpecializationInfo* TSI = MFS->getTemplateSpecializationInfo()) {
37 if(TSI->getTemplateSpecializationKind() != clang::TSK_ExplicitInstantiationDefinition) {
return; }
43 spdlog::debug(
"Check if the instantiation is already present.");
47 if(candidate.match(toDo)) {
48 spdlog::debug(
"Erasing element from toDolist:\n {} because of: {}", toDo, candidate);
57 spdlog::debug(
"Check for match.");
59 spdlog::debug(
"Processing candidate: {}", candidate);
62 spdlog::debug(
"CHecking toDo entry: {}", toDo);
63 if(candidate.isTemplateFor(toDo)) {
65 rewriter->InsertText(MFS->getBodyRBrace().getLocWithOffset(1), llvm::StringRef(it->getInstantiation()),
true,
true);
67 spdlog::debug(
"Match!!! Call the rewriter and delete entry from toDoList.");
68 auto sc = MFS->getBodyRBrace().getLocWithOffset(1);
69 auto fid =
rewriter->getSourceMgr().getFileID(sc);
70 auto fileentry =
rewriter->getSourceMgr().getFileEntryRefForID(fid);
71 auto fname_ =
rewriter->getSourceMgr().getFileManager().getCanonicalName(*fileentry);
72 std::string fname(fname_.data(), fname_.size());
73 auto new_name = std::filesystem::path(fname);
74 new_name.replace_extension(
"gen.cpp");
76 auto& fm = sm.getFileManager();
77 auto new_name_str = new_name.string();
78 llvm::StringRef gen_name(new_name_str);
79 auto file_ref = fm.getFileRef(gen_name,
true);
80 auto new_fid = sm.getOrCreateFileID(*file_ref, clang::SrcMgr::C_User);
81 auto new_loc = sm.getLocForEndOfFile(new_fid);
82 rewriter->InsertText(new_loc, llvm::StringRef(it->getInstantiation()),
true,
true);
90 }
else if(
const clang::FunctionDecl* FS = Result.Nodes.getNodeAs<clang::FunctionDecl>(
"func_definition")) {
91 spdlog::debug(
"Processing func {}", FS->getNameAsString());
92 if(FS->isTemplateInstantiation()) {
93 if(
const clang::FunctionTemplateSpecializationInfo* TSI = FS->getTemplateSpecializationInfo()) {
94 if(TSI->getTemplateSpecializationKind() != clang::TSK_ExplicitInstantiationDefinition) {
return; }
99 spdlog::debug(
"Check if the instantiation is already present.");
102 if(candidate.match(toDo)) {
103 spdlog::debug(
"Erase from toDolist.");
112 spdlog::debug(
"Check if the correct prototype is present for explicit instantiation.");
116 if(candidate.isTemplateFor(toDo)) {
117 spdlog::debug(
"Match!!! Call the rewriter and delete entry from toDoList.");
118 spdlog::debug(
"Injection: {}", it->getInstantiation());
120 rewriter->InsertText(FS->getBodyRBrace().getLocWithOffset(1), llvm::StringRef(it->getInstantiation()),
true,
true);
122 auto sc = FS->getBodyRBrace().getLocWithOffset(1);
123 auto fid =
rewriter->getSourceMgr().getFileID(sc);
124 auto fileentry =
rewriter->getSourceMgr().getFileEntryRefForID(fid);
125 auto fname_ =
rewriter->getSourceMgr().getFileManager().getCanonicalName(*fileentry);
126 std::string fname(fname_.data(), fname_.size());
127 auto new_name = std::filesystem::path(fname);
128 new_name.replace_extension(
"gen.cpp");
129 std::cout <<
"Injecting in file " << new_name << std::endl;
130 auto& sm =
rewriter->getSourceMgr();
131 auto& fm = sm.getFileManager();
132 auto new_name_str = new_name.string();
133 llvm::StringRef gen_name(new_name_str);
134 auto file_ref = fm.getFileRef(gen_name,
true);
135 auto new_fid = sm.getOrCreateFileID(*file_ref, clang::SrcMgr::C_User);
136 auto new_loc = sm.getLocForEndOfFile(new_fid);
137 rewriter->InsertText(new_loc, llvm::StringRef(it->getInstantiation()),
true,
true);