17 clang::SourceManager& SM = Ctx.getSourceManager();
21 loc = clang::Lexer::getLocForEndOfToken(loc, 0, SM, Ctx.getLangOpts());
24 std::pair<clang::FileID, unsigned> locInfo = SM.getDecomposedLoc(loc);
27 bool invalidTemp =
false;
28 clang::StringRef file = SM.getBufferData(locInfo.first, &invalidTemp);
30 std::cout <<
"invalidtemp" << std::endl;
31 return std::make_pair(
false, clang::SourceLocation());
34 const char* tokenBegin = file.data() + locInfo.second;
37 clang::Lexer lexer(SM.getLocForStartOfFile(locInfo.first), Ctx.getLangOpts(), file.begin(), tokenBegin, file.end());
39 lexer.LexFromRawLexer(tok);
40 if(tok.isNot(clang::tok::semi)) {
41 if(!IsDecl) {
return std::make_pair(
false, tok.getLocation()); }
47 return std::make_pair(
true, tok.getLocation());
62auto getBeginLoc(clang::SourceLocation begin, clang::ASTContext& Ctx)
64 clang::SourceManager& sm = Ctx.getSourceManager();
65 auto line = sm.getSpellingLineNumber(begin);
66 auto col = sm.getSpellingColumnNumber(begin);
67 auto previous_loc = begin;
68 auto previous2_loc = begin;
69 auto previous3_loc = begin;
71 while(tok.isNot(clang::tok::semi) and tok.isNot(clang::tok::r_brace)) {
72 auto curr_loc = sm.translateLineCol(sm.getMainFileID(), line, col);
73 auto beginCurrToken = clang::Lexer::GetBeginningOfToken(curr_loc, sm, Ctx.getLangOpts());
74 line = sm.getSpellingLineNumber(beginCurrToken);
75 col = sm.getSpellingColumnNumber(beginCurrToken);
76 [[maybe_unused]]
auto res = clang::Lexer::getRawToken(beginCurrToken, tok, sm, Ctx.getLangOpts());
77 assert(col >= 1 and
"Invalid column in getBeginLoc()");
82 auto [fid, b_offset] = sm.getDecomposedSpellingLoc(curr_loc);
83 auto offsetEndOfNextLine = b_offset - 1;
84 col = sm.getColumnNumber(fid, offsetEndOfNextLine);
86 previous3_loc = previous2_loc;
87 previous2_loc = previous_loc;
88 previous_loc = beginCurrToken;
95 clang::PrintingPolicy pp(Result.Context->getLangOpts());
96 pp.PrintInjectedClassNameWithArguments =
true;
97 pp.PrintCanonicalTypes =
true;
98 pp.SuppressDefaultTemplateArgs =
true;
99 pp.FullyQualifiedName =
true;
100 pp.SuppressScope =
false;
102 clang::Rewriter::RewriteOptions opts;
103 opts.IncludeInsertsAtBeginOfRange =
true;
104 opts.IncludeInsertsAtEndOfRange =
true;
105 opts.RemoveLineIfEmpty =
false;
107 clang::SourceManager& sm = Result.Context->getSourceManager();
109 if(
const clang::CXXMethodDecl* MFS = Result.Nodes.getNodeAs<clang::CXXMethodDecl>(
"explicit_instantiation")) {
110 if(
const clang::MemberSpecializationInfo* MSI = MFS->getMemberSpecializationInfo()) {
111 auto template_kind = MSI->getTemplateSpecializationKind();
112 if(template_kind == clang::TSK_ExplicitInstantiationDefinition) {
113 auto loc = MSI->getPointOfInstantiation();
115 spdlog::debug(
"Delete {} at: {} ({})", MFS->getNameAsString(), loc.printToString(sm), sm.getSpellingLineNumber(loc));
116 auto begin_loc =
getBeginLoc(loc, *Result.Context);
117 auto end_loc =
getEndLoc(begin_loc, *Result.Context);
118 rewriter->RemoveText(clang::SourceRange(begin_loc, end_loc), opts);
121 }
else if(
const clang::FunctionTemplateSpecializationInfo* TSI = MFS->getTemplateSpecializationInfo()) {
122 if(TSI->getTemplateSpecializationKind() == clang::TSK_ExplicitInstantiationDefinition) {
123 auto loc = TSI->getPointOfInstantiation();
125 spdlog::debug(
"Delete {} at: {} ({})", MFS->getNameAsString(), loc.printToString(sm), sm.getSpellingLineNumber(loc));
126 auto begin_loc =
getBeginLoc(loc, *Result.Context);
127 auto end_loc =
getEndLoc(begin_loc, *Result.Context);
128 rewriter->RemoveText(clang::SourceRange(begin_loc, end_loc), opts);
132 }
else if(
const clang::FunctionDecl* FS = Result.Nodes.getNodeAs<clang::FunctionDecl>(
"explicit_instantiation")) {
133 if(
const clang::FunctionTemplateSpecializationInfo* TSI = FS->getTemplateSpecializationInfo()) {
134 if(TSI->getTemplateSpecializationKind() == clang::TSK_ExplicitInstantiationDefinition) {
135 auto loc = TSI->getPointOfInstantiation();
137 spdlog::debug(
"Delete {} at: {} ({})", FS->getNameAsString(), loc.printToString(sm), sm.getSpellingLineNumber(loc));
138 auto begin_loc =
getBeginLoc(loc, *Result.Context);
139 auto end_loc =
getEndLoc(begin_loc, *Result.Context);
140 rewriter->RemoveText(clang::SourceRange(begin_loc, end_loc), opts);