| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 3 |
// Copyright (c) 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 The University of Utah |
| 4 |
// All rights reserved. |
| 5 |
// |
| 6 |
// This file is distributed under the University of Illinois Open Source |
| 7 |
// License. See the file COPYING for details. |
| 8 |
// |
| 9 |
//===----------------------------------------------------------------------===// |
| 10 |
|
| 11 |
#if HAVE_CONFIG_H |
| 12 |
# include <config.h> |
| 13 |
#endif |
| 14 |
|
| 15 |
#include "RemoveUnusedFunction.h" |
| 16 |
|
| 17 |
#include <cctype> |
| 18 |
#include <algorithm> |
| 19 |
#include "clang/AST/RecursiveASTVisitor.h" |
| 20 |
#include "clang/AST/ASTContext.h" |
| 21 |
#include "clang/Basic/SourceManager.h" |
| 22 |
#include "clang/AST/Attr.h" |
| 23 |
|
| 24 |
#include "TransformationManager.h" |
| 25 |
|
| 26 |
using namespace clang; |
| 27 |
|
| 28 |
static const char *DescriptionMsg = |
| 29 |
"Remove unused function declarations. \n"; |
| 30 |
|
| 31 |
static RegisterTransformation<RemoveUnusedFunction> |
| 32 |
Trans("remove-unused-function", DescriptionMsg); |
| 33 |
|
| 34 |
class RUFAnalysisVisitor : public RecursiveASTVisitor<RUFAnalysisVisitor> { |
| 35 |
public: |
| 36 |
|
| 37 |
explicit RUFAnalysisVisitor(RemoveUnusedFunction *Instance) |
| 38 |
: ConsumerInstance(Instance) |
| 39 |
{ } |
| 40 |
|
| 41 |
bool VisitFunctionDecl(FunctionDecl *FD); |
| 42 |
|
| 43 |
private: |
| 44 |
|
| 45 |
RemoveUnusedFunction *ConsumerInstance; |
| 46 |
}; |
| 47 |
|
| 48 |
typedef llvm::DenseMap<const clang::UsingDecl *, |
| 49 |
const clang::FunctionDecl *> |
| 50 |
UsingFunctionDeclsMap; |
| 51 |
|
| 52 |
typedef llvm::SmallPtrSet<const clang::FunctionDecl *, 32> |
| 53 |
FunctionDeclsSet; |
| 54 |
|
| 55 |
typedef llvm::SmallPtrSet<const clang::FunctionDecl *, 5> |
| 56 |
MemberSpecializationSet; |
| 57 |
|
| 58 |
namespace { |
| 59 |
|
| 60 |
class UsingDeclVisitor : public |
| 61 |
RecursiveASTVisitor<UsingDeclVisitor> { |
| 62 |
public: |
| 63 |
UsingDeclVisitor(const FunctionDecl *FD, |
| 64 |
RemoveUnusedFunction *Instance) |
| 65 |
: CurrentFD(FD), |
| 66 |
ConsumerInstance(Instance) |
| 67 |
{ } |
| 68 |
|
| 69 |
bool VisitUsingDecl(UsingDecl *D); |
| 70 |
|
| 71 |
private: |
| 72 |
const FunctionDecl *CurrentFD; |
| 73 |
|
| 74 |
RemoveUnusedFunction *ConsumerInstance; |
| 75 |
}; |
| 76 |
|
| 77 |
bool UsingDeclVisitor::VisitUsingDecl(UsingDecl *D) |
| 78 |
{ |
| 79 |
ConsumerInstance->handleOneUsingDecl(CurrentFD, D); |
| 80 |
return true; |
| 81 |
} |
| 82 |
// end of UsingDeclVisitor |
| 83 |
|
| 84 |
class SpecializationVisitor : public |
| 85 |
RecursiveASTVisitor<SpecializationVisitor> { |
| 86 |
public: |
| 87 |
explicit SpecializationVisitor(RemoveUnusedFunction *Instance) |
| 88 |
: ConsumerInstance(Instance) |
| 89 |
{ } |
| 90 |
|
| 91 |
bool VisitFunctionDecl(FunctionDecl *FD); |
| 92 |
|
| 93 |
bool VisitMemberExpr(MemberExpr *E); |
| 94 |
|
| 95 |
bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E); |
| 96 |
|
| 97 |
bool VisitCallExpr(CallExpr *E); |
| 98 |
|
| 99 |
bool shouldVisitTemplateInstantiations() { |
| 100 |
return true; |
| 101 |
} |
| 102 |
|
| 103 |
private: |
| 104 |
|
| 105 |
RemoveUnusedFunction *ConsumerInstance; |
| 106 |
}; |
| 107 |
|
| 108 |
bool SpecializationVisitor::VisitFunctionDecl(FunctionDecl *FD) |
| 109 |
{ |
| 110 |
ConsumerInstance->handleOneFunctionDecl(FD); |
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
bool SpecializationVisitor::VisitMemberExpr(MemberExpr *E) |
| 115 |
{ |
| 116 |
ConsumerInstance->handleOneMemberExpr(E); |
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
bool SpecializationVisitor::VisitCXXOperatorCallExpr( |
| 121 |
CXXOperatorCallExpr *E) |
| 122 |
{ |
| 123 |
// ConsumerInstance->handleOneCXXOperatorCallExpr(E); |
| 124 |
return true; |
| 125 |
} |
| 126 |
|
| 127 |
bool SpecializationVisitor::VisitCallExpr( |
| 128 |
CallExpr *E) |
| 129 |
{ |
| 130 |
ConsumerInstance->handleOneCallExpr(E); |
| 131 |
return true; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
// end of SpecializationVisitor |
| 136 |
|
| 137 |
class ReferencedFunctionDeclVisitor : public |
| 138 |
RecursiveASTVisitor<ReferencedFunctionDeclVisitor> { |
| 139 |
public: |
| 140 |
ReferencedFunctionDeclVisitor(const FunctionDecl *FD, |
| 141 |
RemoveUnusedFunction *Instance) |
| 142 |
: CurrentFD(FD), |
| 143 |
ConsumerInstance(Instance) |
| 144 |
{ } |
| 145 |
|
| 146 |
bool VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E); |
| 147 |
|
| 148 |
bool VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E); |
| 149 |
|
| 150 |
private: |
| 151 |
const FunctionDecl *CurrentFD; |
| 152 |
|
| 153 |
RemoveUnusedFunction *ConsumerInstance; |
| 154 |
}; |
| 155 |
|
| 156 |
bool ReferencedFunctionDeclVisitor::VisitUnresolvedLookupExpr( |
| 157 |
UnresolvedLookupExpr *E) |
| 158 |
{ |
| 159 |
ConsumerInstance->handleOneUnresolvedLookupExpr(CurrentFD, E); |
| 160 |
return true; |
| 161 |
} |
| 162 |
|
| 163 |
bool ReferencedFunctionDeclVisitor::VisitCXXDependentScopeMemberExpr( |
| 164 |
CXXDependentScopeMemberExpr *E) |
| 165 |
{ |
| 166 |
ConsumerInstance->handleOneCXXDependentScopeMemberExpr(CurrentFD, E); |
| 167 |
return true; |
| 168 |
} |
| 169 |
|
| 170 |
} // end of anon namespace |
| 171 |
|
| 172 |
class ExtraReferenceVisitorWrapper : public |
| 173 |
RecursiveASTVisitor<ExtraReferenceVisitorWrapper> { |
| 174 |
public: |
| 175 |
explicit ExtraReferenceVisitorWrapper(RemoveUnusedFunction *Instance) |
| 176 |
: ConsumerInstance(Instance) |
| 177 |
{ } |
| 178 |
|
| 179 |
bool VisitFunctionDecl(FunctionDecl *FD); |
| 180 |
|
| 181 |
private: |
| 182 |
|
| 183 |
RemoveUnusedFunction *ConsumerInstance; |
| 184 |
}; |
| 185 |
|
| 186 |
bool ExtraReferenceVisitorWrapper::VisitFunctionDecl(FunctionDecl *FD) |
| 187 |
{ |
| 188 |
if (!FD->isThisDeclarationADefinition()) |
| 189 |
return true; |
| 190 |
UsingDeclVisitor Visitor(FD, ConsumerInstance); |
| 191 |
Visitor.TraverseDecl(FD); |
| 192 |
ReferencedFunctionDeclVisitor FuncVisitor(FD, ConsumerInstance); |
| 193 |
FuncVisitor.TraverseDecl(FD); |
| 194 |
ConsumerInstance->setInlinedSystemFunctions(FD); |
| 195 |
return true; |
| 196 |
} |
| 197 |
|
| 198 |
bool RUFAnalysisVisitor::VisitFunctionDecl(FunctionDecl *FD) |
| 199 |
{ |
| 200 |
if (ConsumerInstance->isInIncludedFile(FD)) |
| 201 |
return true; |
| 202 |
const FunctionDecl *CanonicalFD = FD->getCanonicalDecl(); |
| 203 |
if (ConsumerInstance->VisitedFDs.count(CanonicalFD)) |
| 204 |
return true; |
| 205 |
ConsumerInstance->VisitedFDs.insert(CanonicalFD); |
| 206 |
|
| 207 |
FunctionDecl::TemplatedKind TK = FD->getTemplatedKind(); |
| 208 |
if (TK == FunctionDecl::TK_MemberSpecialization) { |
| 209 |
const FunctionDecl *Member = FD->getInstantiatedFromMemberFunction(); |
| 210 |
ConsumerInstance->addOneMemberSpecialization(FD, Member); |
| 211 |
return true; |
| 212 |
} |
| 213 |
else if (TK == FunctionDecl::TK_DependentFunctionTemplateSpecialization) { |
| 214 |
const DependentFunctionTemplateSpecializationInfo *Info = |
| 215 |
FD->getDependentSpecializationInfo(); |
| 216 |
// don't need to track all specs, just associate FD with one |
| 217 |
// of those |
| 218 |
const FunctionDecl *Member = nullptr; |
| 219 |
|
| 220 |
#if LLVM_VERSION_MAJOR >= 18 |
| 221 |
ArrayRef<FunctionTemplateDecl *> members = Info->getCandidates(); |
| 222 |
if (!members.empty()) { |
| 223 |
Member = members[0]->getTemplatedDecl(); |
| 224 |
} |
| 225 |
#else |
| 226 |
if (Info->getNumTemplates() > 0) { |
| 227 |
Member = |
| 228 |
Info->getTemplate(0)->getTemplatedDecl(); |
| 229 |
} |
| 230 |
#endif |
| 231 |
|
| 232 |
if (Member != nullptr) |
| 233 |
ConsumerInstance->addOneMemberSpecialization(FD, Member); |
| 234 |
|
| 235 |
return true; |
| 236 |
} |
| 237 |
|
| 238 |
TemplateSpecializationKind K = FD->getTemplateSpecializationKind(); |
| 239 |
if (K == TSK_ExplicitSpecialization) { |
| 240 |
ConsumerInstance->addFuncToExplicitSpecs(FD); |
| 241 |
return true; |
| 242 |
} |
| 243 |
// Why implicit instantiation would appear here? e.g.: |
| 244 |
// template<typename T> void foo(T &); |
| 245 |
// struct S { friend void foo<>(bool&); }; |
| 246 |
// OK, just add it into MemberSpec set. |
| 247 |
if (K == TSK_ImplicitInstantiation) { |
| 248 |
const FunctionTemplateDecl *FTD = FD->getPrimaryTemplate(); |
| 249 |
TransAssert(FTD && "NULL FunctionTemplateDecl!"); |
| 250 |
const FunctionDecl *Member = FTD->getTemplatedDecl(); |
| 251 |
ConsumerInstance->addOneMemberSpecialization(FD, Member); |
| 252 |
return true; |
| 253 |
} |
| 254 |
|
| 255 |
if (FD->isReferenced() || |
| 256 |
FD->isMain() || |
| •257 |
#if LLVM_VERSION_MAJOR >= 21 |
| 258 |
FD->hasAttr<DeviceKernelAttr>() || |
| 259 |
#else |
| 260 |
FD->hasAttr<OpenCLKernelAttr>() || |
| •261 |
#endif |
| 262 |
ConsumerInstance->hasReferencedSpecialization(CanonicalFD) || |
| 263 |
ConsumerInstance->isInlinedSystemFunction(CanonicalFD) || |
| 264 |
ConsumerInstance->isInReferencedSet(CanonicalFD) || |
| 265 |
!ConsumerInstance->hasAtLeastOneValidLocation(CanonicalFD)) |
| 266 |
return true; |
| 267 |
|
| 268 |
ConsumerInstance->addOneFunctionDecl(CanonicalFD); |
| 269 |
return true; |
| 270 |
} |
| 271 |
|
| 272 |
void RemoveUnusedFunction::Initialize(ASTContext &context) |
| 273 |
{ |
| 274 |
Transformation::Initialize(context); |
| 275 |
AnalysisVisitor = new RUFAnalysisVisitor(this); |
| 276 |
VisitorWrapper = new ExtraReferenceVisitorWrapper(this); |
| 277 |
initializeInlinedSystemFunctions(); |
| 278 |
} |
| 279 |
|
| 280 |
void RemoveUnusedFunction::HandleTranslationUnit(ASTContext &Ctx) |
| 281 |
{ |
| 282 |
VisitorWrapper->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 283 |
// visit using decls declared outsided function-scope |
| 284 |
UsingDeclVisitor Visitor(NULL, this); |
| 285 |
Visitor.TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 286 |
SpecializationVisitor SpecVisitor(this); |
| 287 |
SpecVisitor.TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 288 |
AnalysisVisitor->TraverseDecl(Ctx.getTranslationUnitDecl()); |
| 289 |
|
| 290 |
if (QueryInstanceOnly) |
| 291 |
return; |
| 292 |
|
| 293 |
if (TransformationCounter > ValidInstanceNum) { |
| 294 |
TransError = TransMaxInstanceError; |
| 295 |
return; |
| 296 |
} |
| 297 |
if (ToCounter > ValidInstanceNum) { |
| 298 |
TransError = TransToCounterTooBigError; |
| 299 |
return; |
| 300 |
} |
| 301 |
|
| 302 |
Ctx.getDiagnostics().setSuppressAllDiagnostics(false); |
| 303 |
|
| 304 |
doRewriting(); |
| 305 |
|
| 306 |
if (Ctx.getDiagnostics().hasErrorOccurred() || |
| 307 |
Ctx.getDiagnostics().hasFatalErrorOccurred()) |
| 308 |
TransError = TransInternalError; |
| 309 |
} |
| 310 |
|
| 311 |
// Each "key" function is inlined into the corresponding "value" |
| 312 |
// function, and its isReferenced flag is false... |
| 313 |
void RemoveUnusedFunction::initializeInlinedSystemFunctions() |
| 314 |
{ |
| 315 |
InlinedSystemFunctions["__fprintf_chk"] = "fprintf"; |
| 316 |
InlinedSystemFunctions["__printf_chk"] = "printf"; |
| 317 |
InlinedSystemFunctions["__dprintf_chk"] = "dprintf"; |
| 318 |
InlinedSystemFunctions["__asprintf_chk"] = "asprintf"; |
| 319 |
InlinedSystemFunctions["__obstack_printf_chk"] = "obstack_printf"; |
| 320 |
InlinedSystemFunctions["__swprintf_chk"] = "swprintf"; |
| 321 |
InlinedSystemFunctions["__swprintf_alias"] = "swprintf"; |
| 322 |
InlinedSystemFunctions["__fwprintf_chk"] = "fwprintf"; |
| 323 |
InlinedSystemFunctions["__wprintf_chk"] = "wprintf"; |
| 324 |
} |
| 325 |
|
| 326 |
void RemoveUnusedFunction::doRewriting() |
| 327 |
{ |
| 328 |
if (ToCounter <= 0) { |
| 329 |
TransAssert(TheFunctionDecl && "NULL TheFunctionDecl!"); |
| 330 |
// add FD under removal first in order to avoid recursion, e.g. |
| 331 |
// void foo() { using ::foo; } |
| 332 |
RemovedFDs.insert(TheFunctionDecl); |
| 333 |
removeOneFunctionDeclGroup(TheFunctionDecl); |
| 334 |
return; |
| 335 |
} |
| 336 |
|
| 337 |
TransAssert((TransformationCounter <= |
| 338 |
static_cast<int>(AllValidFunctionDecls.size())) && |
| 339 |
"TransformationCounter is larger than the number of defs!"); |
| 340 |
TransAssert((ToCounter <= static_cast<int>(AllValidFunctionDecls.size())) && |
| 341 |
"ToCounter is larger than the number of defs!"); |
| 342 |
for (int I = ToCounter; I >= TransformationCounter; --I) { |
| 343 |
TransAssert((I >= 1) && "Invalid Index!"); |
| 344 |
const FunctionDecl *FD = AllValidFunctionDecls[I-1]; |
| 345 |
TransAssert(FD && "NULL FunctionDecl!"); |
| 346 |
RemovedFDs.insert(FD); |
| 347 |
removeOneFunctionDeclGroup(FD); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
SourceLocation RemoveUnusedFunction::getExtensionLocStart( |
| 352 |
SourceLocation Loc) |
| 353 |
{ |
| 354 |
SourceLocation StartLoc = Loc; |
| 355 |
FileID FID = SrcManager->getMainFileID(); |
| 356 |
SourceLocation FileStartLoc = SrcManager->getLocForStartOfFile(FID); |
| 357 |
const char * const FileStartBuf = SrcManager->getCharacterData(FileStartLoc); |
| 358 |
const char *StartBuf = SrcManager->getCharacterData(StartLoc); |
| 359 |
const char *MatchedPos = NULL; |
| 360 |
bool Out = false; |
| 361 |
std::string Ext = "__noisnetxe__"; |
| 362 |
while (true) { |
| 363 |
do { |
| 364 |
StartBuf--; |
| 365 |
if (StartBuf < FileStartBuf) |
| 366 |
break; |
| 367 |
} while (isspace(*StartBuf)); |
| 368 |
|
| 369 |
int Len = static_cast<int>(Ext.length()); |
| 370 |
for (int I = 0; I < Len; I++) { |
| 371 |
if (StartBuf < FileStartBuf || *StartBuf != Ext[I]) { |
| 372 |
Out = true; |
| 373 |
break; |
| 374 |
} |
| 375 |
StartBuf--; |
| 376 |
} |
| 377 |
if (Out) |
| 378 |
break; |
| 379 |
StartBuf++; |
| 380 |
MatchedPos = StartBuf; |
| 381 |
} |
| 382 |
// no __extension__ in front |
| 383 |
if (MatchedPos == NULL) |
| 384 |
return Loc; |
| 385 |
|
| 386 |
TransAssert((MatchedPos >= FileStartBuf) && "Invalid MatchedPos!"); |
| 387 |
std::string ExtStr(MatchedPos, Ext.length()); |
| 388 |
TransAssert((ExtStr == "__extension__") && "Invalid ExtStr!"); (void)ExtStr; |
| 389 |
int Offset = MatchedPos - (SrcManager->getCharacterData(StartLoc)); |
| 390 |
return StartLoc.getLocWithOffset(Offset); |
| 391 |
} |
| 392 |
|
| 393 |
bool RemoveUnusedFunction::hasValidOuterLocStart( |
| 394 |
const FunctionTemplateDecl *FTD, const FunctionDecl *FD) |
| 395 |
{ |
| 396 |
SourceLocation FTDLocStart = FTD->getSourceRange().getBegin(); |
| 397 |
SourceLocation FDLocStart = FD->getSourceRange().getBegin(); |
| 398 |
const char *FTDStartPos = SrcManager->getCharacterData(FTDLocStart); |
| 399 |
const char *FDStartPos = SrcManager->getCharacterData(FDLocStart); |
| 400 |
return (FTDStartPos < FDStartPos); |
| 401 |
} |
| 402 |
|
| 403 |
SourceLocation RemoveUnusedFunction::getFunctionOuterLocStart( |
| 404 |
const FunctionDecl *FD) |
| 405 |
{ |
| 406 |
SourceLocation LocStart = FD->getBeginLoc(); |
| 407 |
bool RecordLoc = false; |
| 408 |
|
| 409 |
// check if FD is from a function template |
| 410 |
if (FunctionTemplateDecl *FTD = FD->getDescribedFunctionTemplate()) { |
| 411 |
// get FTD->getBeginLoc() only if it is less than FD->getBeginLoc, |
| 412 |
// for example, in the code below: |
| 413 |
// template <typename T> struct S {template <typename T1> void foo();}; |
| 414 |
// template<typename T> template<typename T1> void S<T>::foo() { } |
| 415 |
// where |
| 416 |
// FTD->getBeginLoc() points to the begining of "template<typename T1>" |
| 417 |
if (hasValidOuterLocStart(FTD, FD)) { |
| 418 |
LocStart = FTD->getBeginLoc(); |
| 419 |
RecordLoc = true; |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
if (LocStart.isMacroID()) |
| 424 |
LocStart = SrcManager->getExpansionLoc(LocStart); |
| 425 |
|
| 426 |
// this is ugly, but how do we get the location of __extension__? e.g.: |
| 427 |
// __extension__ void foo(); |
| 428 |
LocStart = getExtensionLocStart(LocStart); |
| 429 |
|
| 430 |
// In some cases where the given input is not well-formed, we may |
| 431 |
// end up with duplicate locations for the FunctionTemplateDecl |
| 432 |
// case. In such cases, we simply return an invalid SourceLocation, |
| 433 |
// which will ben skipped by the caller of getFunctionOuterLocStart. |
| 434 |
if (RecordLoc) { |
| 435 |
if (VisitedLocations.count(LocStart)) |
| 436 |
return SourceLocation(); |
| 437 |
VisitedLocations.insert(LocStart); |
| 438 |
} |
| 439 |
|
| 440 |
return LocStart; |
| 441 |
} |
| 442 |
|
| 443 |
bool RemoveUnusedFunction::isTokenOperator(SourceLocation Loc) |
| 444 |
{ |
| 445 |
const char * Buf = SrcManager->getCharacterData(Loc); |
| 446 |
std::string OStr = "operator"; |
| 447 |
std::string S(Buf, OStr.length()); |
| 448 |
return (S == OStr); |
| 449 |
} |
| 450 |
|
| 451 |
// this is ugly, but seems clang gives "bad" location for the code below: |
| 452 |
// template<typename T> void foo(T *); |
| 453 |
// template <typename T> struct S { |
| 454 |
// template<typename T1> friend void foo(T1 *); |
| 455 |
// ^ FD.end_loc points here |
| 456 |
// }; |
| 457 |
// template struct S<char>; |
| 458 |
SourceLocation RemoveUnusedFunction::getFunctionLocEnd( |
| 459 |
SourceLocation LocStart, |
| 460 |
SourceLocation LocEnd, |
| 461 |
const FunctionDecl *FD) |
| 462 |
{ |
| 463 |
if (!FD->getDescribedFunctionTemplate()) { |
| 464 |
// Remove trailing ; if function has no body |
| 465 |
if (!FD->hasBody()) { |
| 466 |
return RewriteHelper->getLocationUntil(LocEnd, ';'); |
| 467 |
} |
| 468 |
else { |
| 469 |
return LocEnd; |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
SourceLocation FDLoc = FD->getLocation(); |
| 474 |
if (FDLoc.isMacroID()) |
| 475 |
FDLoc = SrcManager->getExpansionLoc(FDLoc); |
| 476 |
const char * const FDBuf = SrcManager->getCharacterData(FDLoc); |
| 477 |
const char * LocEndBuf = SrcManager->getCharacterData(LocEnd); |
| 478 |
if ((FDBuf < LocEndBuf) && !isTokenOperator(FDLoc) && |
| 479 |
!FD->isDeleted() && !FD->isDefaulted()) |
| 480 |
return LocEnd; |
| 481 |
int Offset = 0; |
| 482 |
while (*LocEndBuf != ';') { |
| 483 |
LocEndBuf++; |
| 484 |
Offset++; |
| 485 |
} |
| 486 |
return LocEnd.getLocWithOffset(Offset-1); |
| 487 |
} |
| 488 |
|
| 489 |
void RemoveUnusedFunction::removeOneFunctionDecl(const FunctionDecl *FD) |
| 490 |
{ |
| 491 |
SourceRange FuncRange = FD->getSourceRange(); |
| 492 |
SourceLocation LocEnd = FuncRange.getEnd(); |
| 493 |
if (LocEnd.isMacroID()) |
| 494 |
LocEnd = SrcManager->getExpansionLoc(LocEnd); |
| 495 |
if (!FD->isInExternCContext() && !FD->isInExternCXXContext()) { |
| 496 |
SourceLocation FuncLocStart = getFunctionOuterLocStart(FD); |
| 497 |
|
| 498 |
if (FuncLocStart.isInvalid()) |
| 499 |
return; |
| 500 |
// This is a workaround to cases where we have function declaration |
| 501 |
// groups, e.g. |
| 502 |
// void *foo(), bar(); |
| 503 |
// It's very tricky to remove a single decl in such a case. |
| 504 |
// Let's just remove all decls for now. To avoid removing text that |
| 505 |
// has been removed, we keep track the start loc for each group so that |
| 506 |
// we only remove each group once. |
| 507 |
if (!FD->hasBody()) { |
| 508 |
if (FDGroupLocations.count(FuncLocStart)) { |
| 509 |
return; |
| 510 |
} else { |
| 511 |
FDGroupLocations.insert(FuncLocStart); |
| 512 |
} |
| 513 |
} |
| 514 |
|
| 515 |
LocEnd = getFunctionLocEnd(FuncLocStart, LocEnd, FD); |
| 516 |
if (SrcManager->isWrittenInMainFile(FuncLocStart) && |
| 517 |
SrcManager->isWrittenInMainFile(LocEnd)) |
| 518 |
TheRewriter.RemoveText(SourceRange(FuncLocStart, LocEnd)); |
| 519 |
return; |
| 520 |
} |
| 521 |
|
| 522 |
const DeclContext *Ctx = FD->getLookupParent(); |
| 523 |
const LinkageSpecDecl *Linkage = dyn_cast<LinkageSpecDecl>(Ctx); |
| 524 |
if (!Linkage) { |
| 525 |
SourceLocation FuncLocStart = getFunctionOuterLocStart(FD); |
| 526 |
if (FuncLocStart.isInvalid()) |
| 527 |
return; |
| 528 |
LocEnd = getFunctionLocEnd(FuncLocStart, LocEnd, FD); |
| 529 |
TheRewriter.RemoveText(SourceRange(FuncLocStart, LocEnd)); |
| 530 |
return; |
| 531 |
} |
| 532 |
// cases like: |
| 533 |
// extern "C++" { void foo(); } |
| 534 |
// namespace { using ::foo; } |
| 535 |
if (Linkage->hasBraces()) { |
| 536 |
SourceLocation FuncLocStart = getFunctionOuterLocStart(FD); |
| 537 |
if (FuncLocStart.isInvalid()) |
| 538 |
return; |
| 539 |
TheRewriter.RemoveText(SourceRange(FuncLocStart, LocEnd)); |
| 540 |
return; |
| 541 |
} |
| 542 |
// cases like: |
| 543 |
// extern "C++" void foo(); |
| 544 |
// it also handles cases such as extern "C++" template<typename T> ... |
| 545 |
SourceLocation LocStart = Linkage->getExternLoc(); |
| 546 |
if (LocStart.isMacroID()) |
| 547 |
LocStart = SrcManager->getExpansionLoc(LocStart); |
| 548 |
LocStart = getExtensionLocStart(LocStart); |
| 549 |
TheRewriter.RemoveText(SourceRange(LocStart, LocEnd)); |
| 550 |
} |
| 551 |
|
| 552 |
void RemoveUnusedFunction::removeOneExplicitInstantiation( |
| 553 |
const FunctionDecl *Spec) |
| 554 |
{ |
| 555 |
FileID FID = SrcManager->getMainFileID(); |
| 556 |
SourceLocation FileStartLoc = SrcManager->getLocForStartOfFile(FID); |
| 557 |
const char * const FileStartBuf = SrcManager->getCharacterData(FileStartLoc); |
| 558 |
|
| 559 |
SourceLocation Loc = Spec->getPointOfInstantiation(); |
| 560 |
if (Loc.isInvalid()) { |
| 561 |
TheRewriter.RemoveText(Spec->getSourceRange()); |
| 562 |
return; |
| 563 |
} |
| 564 |
const char *OrigStartBuf = SrcManager->getCharacterData(Loc); |
| 565 |
const char *StartBuf = OrigStartBuf; |
| 566 |
int Offset = 0; |
| 567 |
while ((*StartBuf != ';') && (*StartBuf != '{') && (*StartBuf != '}') && |
| 568 |
(StartBuf >= FileStartBuf)) { |
| 569 |
StartBuf--; |
| 570 |
Offset--; |
| 571 |
} |
| 572 |
SourceLocation LocStart = Loc.getLocWithOffset(Offset + 1); |
| 573 |
Offset = 0; |
| 574 |
StartBuf = OrigStartBuf; |
| 575 |
while (*StartBuf != ';') { |
| 576 |
StartBuf++; |
| 577 |
Offset++; |
| 578 |
} |
| 579 |
SourceLocation LocEnd = Loc.getLocWithOffset(Offset - 1); |
| 580 |
TheRewriter.RemoveText(SourceRange(LocStart, LocEnd)); |
| 581 |
} |
| 582 |
|
| 583 |
void RemoveUnusedFunction::removeRemainingExplicitSpecs( |
| 584 |
MemberSpecializationSet *ExplicitSpecs) |
| 585 |
{ |
| 586 |
if (!ExplicitSpecs) |
| 587 |
return; |
| 588 |
for (MemberSpecializationSet::iterator I = ExplicitSpecs->begin(), |
| 589 |
E = ExplicitSpecs->end(); I != E; ++I) { |
| 590 |
removeOneFunctionDecl(*I); |
| 591 |
} |
| 592 |
} |
| 593 |
|
| 594 |
// We can't handle function template explicit instantiation because |
| 595 |
// I am not sure how to analyze its source location. |
| 596 |
// For example, in the code below: |
| 597 |
// template<typename T> bool foo(const int&) {return true;} |
| 598 |
// template bool foo<char>(const int&); |
| 599 |
// ^ point of instantiation |
| 600 |
// What I could get is the point of the instantiation, but I |
| 601 |
// don't know the source range for the entire instantiation declaration. |
| 602 |
void RemoveUnusedFunction::removeFunctionExplicitInstantiations( |
| 603 |
const FunctionDecl *FD) |
| 604 |
{ |
| 605 |
const FunctionTemplateDecl *FTD = getTopDescribedTemplate(FD); |
| 606 |
if (!FTD) |
| 607 |
return; |
| 608 |
|
| 609 |
const FunctionDecl *CanonicalFD = FD->getCanonicalDecl(); |
| 610 |
MemberSpecializationSet *S = MemberToInstantiations[CanonicalFD]; |
| 611 |
MemberSpecializationSet *ExplicitSpecs = |
| 612 |
FuncToExplicitSpecs[CanonicalFD]; |
| 613 |
|
| 614 |
for (FunctionTemplateDecl::spec_iterator I = FTD->spec_begin(), |
| 615 |
E = FTD->spec_end(); I != E; ++I) { |
| 616 |
FunctionDecl *Spec = (*I); |
| 617 |
TemplateSpecializationKind K = Spec->getTemplateSpecializationKind(); |
| 618 |
if (K == TSK_ExplicitSpecialization) { |
| 619 |
// check if the explicit spec points to any dependent specs, |
| 620 |
// skip it if yes |
| 621 |
if (!ExplicitSpecs) { |
| 622 |
removeOneFunctionDecl(Spec); |
| 623 |
} |
| 624 |
else if (ExplicitSpecs->count(Spec)) { |
| 625 |
removeOneFunctionDecl(Spec); |
| 626 |
ExplicitSpecs->erase(Spec); |
| 627 |
} |
| 628 |
continue; |
| 629 |
} |
| 630 |
if (K != TSK_ExplicitInstantiationDeclaration && |
| 631 |
K != TSK_ExplicitInstantiationDefinition) |
| 632 |
continue; |
| 633 |
TransAssert(!Spec->isReferenced() && "Referenced Instantiation!"); |
| 634 |
removeOneExplicitInstantiation(Spec); |
| 635 |
if (S && S->count(Spec)) { |
| 636 |
S->erase(Spec); |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
removeRemainingExplicitSpecs(ExplicitSpecs); |
| 641 |
|
| 642 |
if (!S) |
| 643 |
return; |
| 644 |
for (MemberSpecializationSet::iterator I = S->begin(), E = S->end(); |
| 645 |
I != E; ++I) { |
| 646 |
const FunctionDecl *Spec = (*I); |
| 647 |
TemplateSpecializationKind K = Spec->getTemplateSpecializationKind(); |
| 648 |
TransAssert(((K == TSK_ExplicitInstantiationDeclaration) || |
| 649 |
(K == TSK_ExplicitInstantiationDefinition)) && |
| 650 |
"Bad Instantiation!"); (void)K; |
| 651 |
removeOneExplicitInstantiation(Spec); |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
void RemoveUnusedFunction::removeMemberSpecializations(const FunctionDecl *FD) |
| 656 |
{ |
| 657 |
MemberSpecializationSet *S = MemberToSpecs[FD->getCanonicalDecl()]; |
| 658 |
if (!S) |
| 659 |
return; |
| 660 |
for (MemberSpecializationSet::iterator I = S->begin(), E = S->end(); |
| 661 |
I != E; ++I) { |
| 662 |
const FunctionDecl *Spec = (*I); |
| 663 |
removeOneFunctionDecl(Spec); |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
void RemoveUnusedFunction::removeOneFunctionDeclGroup(const FunctionDecl *FD) |
| 668 |
{ |
| 669 |
for (FunctionDecl::redecl_iterator RI = FD->redecls_begin(), |
| 670 |
RE = FD->redecls_end(); RI != RE; ++RI) { |
| 671 |
const FunctionDecl *FD = (*RI); |
| 672 |
if (!FD->hasBody() && !FD->getTypeSourceInfo()) { |
| 673 |
continue; |
| 674 |
} |
| 675 |
removeOneFunctionDecl(FD); |
| 676 |
} |
| 677 |
|
| 678 |
removeMemberSpecializations(FD); |
| 679 |
removeFunctionExplicitInstantiations(FD); |
| 680 |
|
| 681 |
for (UsingFunctionDeclsMap::const_iterator I = UsingFDs.begin(), |
| 682 |
E = UsingFDs.end(); I != E; ++I) { |
| 683 |
if ((*I).second != FD) |
| 684 |
continue; |
| 685 |
const FunctionDecl *ParentFD = UsingParentFDs[(*I).first]; |
| 686 |
if (ParentFD && RemovedFDs.count(ParentFD->getCanonicalDecl())) |
| 687 |
continue; |
| 688 |
const UsingDecl *UD = (*I).first; |
| 689 |
SourceRange Range = UD->getSourceRange(); |
| 690 |
if (Range.getBegin().isMacroID()) { |
| 691 |
TheRewriter.RemoveText(SrcManager->getExpansionRange(Range)); |
| 692 |
} else { |
| 693 |
RewriteHelper->removeDecl((*I).first); |
| 694 |
} |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
bool RemoveUnusedFunction::hasAtLeastOneValidLocation(const FunctionDecl *FD) |
| 699 |
{ |
| 700 |
for (FunctionDecl::redecl_iterator RI = FD->redecls_begin(), |
| 701 |
RE = FD->redecls_end(); RI != RE; ++RI) { |
| 702 |
SourceRange FuncRange = FD->getSourceRange(); |
| 703 |
SourceLocation StartLoc = FuncRange.getBegin(); |
| 704 |
if (StartLoc.isMacroID()) |
| 705 |
StartLoc = SrcManager->getExpansionLoc(StartLoc); |
| 706 |
SourceLocation EndLoc = FuncRange.getEnd(); |
| 707 |
if (EndLoc.isMacroID()) |
| 708 |
EndLoc = SrcManager->getExpansionLoc(EndLoc); |
| 709 |
if (SrcManager->isWrittenInMainFile(StartLoc) && |
| 710 |
SrcManager->isWrittenInMainFile(EndLoc)) |
| 711 |
return true; |
| 712 |
} |
| 713 |
return false; |
| 714 |
} |
| 715 |
|
| 716 |
bool RemoveUnusedFunction::isInReferencedSet(const FunctionDecl *CanonicalFD) |
| 717 |
{ |
| 718 |
TransAssert(CanonicalFD && "NULL FunctionDecl!"); |
| 719 |
return ReferencedFDs.count(CanonicalFD); |
| 720 |
} |
| 721 |
|
| 722 |
const FunctionDecl * |
| 723 |
RemoveUnusedFunction::lookupFunctionDeclShallow(const DeclarationName &DName, |
| 724 |
const DeclContext *Ctx) |
| 725 |
{ |
| 726 |
if (dyn_cast<LinkageSpecDecl>(Ctx)) |
| 727 |
return NULL; |
| 728 |
DeclContext::lookup_result Result = Ctx->lookup(DName); |
| 729 |
for (auto I = Result.begin(), E = Result.end(); |
| 730 |
I != E; ++I) { |
| 731 |
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(*I)) |
| 732 |
return FD; |
| 733 |
if (const UsingShadowDecl *USD = dyn_cast<UsingShadowDecl>(*I)) { |
| 734 |
const NamedDecl *ND = USD->getTargetDecl(); |
| 735 |
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) |
| 736 |
return FD; |
| 737 |
} |
| 738 |
if (const FunctionTemplateDecl *TD = dyn_cast<FunctionTemplateDecl>(*I)) { |
| 739 |
return TD->getTemplatedDecl(); |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
bool InNamespace = Ctx->isNamespace(); |
| 744 |
for (auto *I : Ctx->using_directives()) { |
| 745 |
const NamespaceDecl *ND = I->getNominatedNamespace(); |
| 746 |
// avoid infinite recursion |
| 747 |
if ((InNamespace && dyn_cast<NamespaceDecl>(Ctx) == ND) || |
| 748 |
ND->getLookupParent() == Ctx) { |
| 749 |
return NULL; |
| 750 |
} |
| 751 |
if (const FunctionDecl *FD = lookupFunctionDeclShallow(DName, ND)) |
| 752 |
return FD; |
| 753 |
} |
| 754 |
return NULL; |
| 755 |
} |
| 756 |
|
| 757 |
const FunctionDecl *RemoveUnusedFunction::getFunctionDeclFromSpecifier( |
| 758 |
const DeclarationName &Name, const NestedNameSpecifier *NNS) |
| 759 |
{ |
| 760 |
const FunctionDecl *FD = NULL; |
| 761 |
switch (NNS->getKind()) { |
| 762 |
case NestedNameSpecifier::Namespace: |
| 763 |
FD = lookupFunctionDeclShallow(Name, |
| 764 |
NNS->getAsNamespace()); |
| 765 |
break; |
| 766 |
case NestedNameSpecifier::NamespaceAlias: |
| 767 |
FD = lookupFunctionDeclShallow(Name, |
| 768 |
NNS->getAsNamespaceAlias()->getNamespace()); |
| 769 |
break; |
| 770 |
case NestedNameSpecifier::Global: |
| 771 |
FD = lookupFunctionDeclShallow(Name, |
| 772 |
Context->getTranslationUnitDecl()); |
| 773 |
break; |
| 774 |
default: |
| 775 |
return NULL; |
| 776 |
} |
| 777 |
return FD; |
| 778 |
} |
| 779 |
|
| 780 |
void RemoveUnusedFunction::handleOneUsingDecl(const FunctionDecl *CurrentFD, |
| 781 |
const UsingDecl *UD) |
| 782 |
{ |
| 783 |
if (VisitedUsingDecls.count(UD)) |
| 784 |
return; |
| 785 |
|
| 786 |
VisitedUsingDecls.insert(UD); |
| 787 |
const NestedNameSpecifier *NNS = UD->getQualifier(); |
| 788 |
if (!NNS) |
| 789 |
return; |
| 790 |
DeclarationName Name = UD->getUnderlyingDecl()->getDeclName(); |
| 791 |
const FunctionDecl *FD = getFunctionDeclFromSpecifier(Name, NNS); |
| 792 |
if (!FD || FD->isReferenced()) |
| 793 |
return; |
| 794 |
|
| 795 |
// we don't put FD into ReferencedFD because we will |
| 796 |
// delete both the FD and the UsingDecl if FD is only referenced |
| 797 |
// by UsingDecl[s]. |
| 798 |
const FunctionDecl *CanonicalFD = FD->getCanonicalDecl(); |
| 799 |
TransAssert((UsingFDs.find(UD) == UsingFDs.end()) && |
| 800 |
"Duplicate UsingDecl to FD map!"); |
| 801 |
UsingFDs[UD] = CanonicalFD; |
| 802 |
if (CurrentFD) { |
| 803 |
TransAssert(CurrentFD->isThisDeclarationADefinition() && |
| 804 |
"CurrentFD is not a definition!"); |
| 805 |
TransAssert((UsingParentFDs.find(UD) == UsingParentFDs.end()) && |
| 806 |
"Duplicate UsingDecl to ParentFD map!"); |
| 807 |
UsingParentFDs[UD] = CurrentFD; |
| 808 |
} |
| 809 |
} |
| 810 |
|
| 811 |
void RemoveUnusedFunction::handleOneCXXDependentScopeMemberExpr( |
| 812 |
const FunctionDecl *CurrentFD, |
| 813 |
const CXXDependentScopeMemberExpr *E) |
| 814 |
{ |
| 815 |
if (E->isImplicitAccess()) |
| 816 |
return; |
| 817 |
DeclarationName DName = E->getMember(); |
| 818 |
DeclarationName::NameKind K = DName.getNameKind(); |
| 819 |
if ((K != DeclarationName::CXXOperatorName) && |
| 820 |
(K != DeclarationName::Identifier)) |
| 821 |
return; |
| 822 |
const Expr *Base = E->getBase()->IgnoreParenCasts(); |
| 823 |
const FunctionDecl *FD = NULL; |
| 824 |
if (dyn_cast<CXXThisExpr>(Base)) { |
| 825 |
TransAssert(CurrentFD && "NULL CurrentFD"); |
| 826 |
const DeclContext *Ctx = CurrentFD->getLookupParent(); |
| 827 |
TransAssert(Ctx && "Bad DeclContext!"); |
| 828 |
DeclContextSet VisitedCtxs; |
| 829 |
FD = lookupFunctionDecl(DName, Ctx, VisitedCtxs); |
| 830 |
// we may not get FD in cases where we have this->m_field |
| 831 |
if (FD) |
| 832 |
addOneReferencedFunction(FD); |
| 833 |
return; |
| 834 |
} |
| 835 |
} |
| 836 |
|
| 837 |
void RemoveUnusedFunction::handleOneUnresolvedLookupExpr( |
| 838 |
const FunctionDecl *CurrentFD, |
| 839 |
const UnresolvedLookupExpr *E) |
| 840 |
{ |
| 841 |
DeclarationName DName = E->getName(); |
| 842 |
DeclarationName::NameKind K = DName.getNameKind(); |
| 843 |
if ((K != DeclarationName::CXXOperatorName) && |
| 844 |
(K != DeclarationName::Identifier)) |
| 845 |
return; |
| 846 |
const NestedNameSpecifier *NNS = E->getQualifier(); |
| 847 |
// we fail only if UE is invoked with some qualifier or |
| 848 |
// instantiation, e.g.: |
| 849 |
// namespace NS { template<typename T> void foo(T&) { } } |
| 850 |
// template<typename T> void bar(T p) { NS::foo(p); } |
| 851 |
// removing foo would fail. |
| 852 |
// |
| 853 |
// template <typename T> void foo() { } |
| 854 |
// template <typename T> void bar() { foo<T>(); } |
| 855 |
// removing foo would faill, too |
| 856 |
// |
| 857 |
// On the other handle, the following code is ok: |
| 858 |
// template<typename T> void foo(T&) { } |
| 859 |
// template<typename T> void bar(T p) { foo(p); } |
| 860 |
const FunctionDecl *FD = NULL; |
| 861 |
if (NNS) { |
| 862 |
FD = getFunctionDeclFromSpecifier(DName, NNS); |
| 863 |
} |
| 864 |
else { |
| 865 |
const DeclContext *Ctx = CurrentFD->getLookupParent(); |
| 866 |
FD = lookupFunctionDeclShallow(DName, Ctx); |
| 867 |
} |
| 868 |
|
| 869 |
if (!FD || FD->isReferenced()) |
| 870 |
return; |
| 871 |
addOneReferencedFunction(FD); |
| 872 |
} |
| 873 |
|
| 874 |
void RemoveUnusedFunction::handleOneMemberExpr( |
| 875 |
const MemberExpr *ME) |
| 876 |
{ |
| 877 |
const ValueDecl *VD = ME->getMemberDecl(); |
| 878 |
const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(VD); |
| 879 |
if (!MD) |
| 880 |
return; |
| 881 |
if (const FunctionDecl *FD = MD->getInstantiatedFromMemberFunction()) |
| 882 |
addOneReferencedFunction(FD); |
| 883 |
} |
| 884 |
|
| 885 |
const FunctionDecl *RemoveUnusedFunction::getSourceFunctionDecl( |
| 886 |
const FunctionDecl *TheFD) |
| 887 |
{ |
| 888 |
if (FunctionTemplateDecl *FTD = TheFD->getPrimaryTemplate()) { |
| 889 |
if (const FunctionTemplateDecl *D = |
| 890 |
FTD->getInstantiatedFromMemberTemplate()) |
| 891 |
return D->getTemplatedDecl(); |
| 892 |
else |
| 893 |
return FTD->getTemplatedDecl(); |
| 894 |
} |
| 895 |
if (const FunctionDecl *FD = TheFD->getInstantiatedFromMemberFunction()) |
| 896 |
return FD; |
| 897 |
return TheFD; |
| 898 |
} |
| 899 |
|
| 900 |
// we can't handle cases like below: |
| 901 |
// template<typename T> struct S; |
| 902 |
// template<typename T> void foo(S<T>); |
| 903 |
// template<> void foo(S<char>); |
| 904 |
// template void foo(S<char>); |
| 905 |
// the second instantiation will refer to the specialization, and thus |
| 906 |
// we lose the source range of it... in fact, it points to the |
| 907 |
// original template declaration. |
| 908 |
void RemoveUnusedFunction::handleOneFunctionDecl(const FunctionDecl *TheFD) |
| 909 |
{ |
| 910 |
const FunctionDecl *FD = getSourceFunctionDecl(TheFD); |
| 911 |
if (TheFD->isReferenced()) { |
| 912 |
addOneReferencedFunction(FD); |
| 913 |
return; |
| 914 |
} |
| 915 |
|
| 916 |
FunctionDecl::TemplatedKind TK = TheFD->getTemplatedKind(); |
| 917 |
// Now it's another ugly part, with dependent function template spec, |
| 918 |
// we lose the explicit specialization in Member's spec_iterator, e.g.: |
| 919 |
// template <class T> void foo(T); // line 1 |
| 920 |
// template <class T1, class T2> class A { // line 2 |
| 921 |
// friend void foo<>(T1); // line 3 |
| 922 |
// }; // line 4 |
| 923 |
// template <> void foo(char); // line 5 |
| 924 |
// template class A<char, bool>; // line 6 |
| 925 |
// In the code above, foo's spec_iterator has an explicit spec, |
| 926 |
// but this spec's source range points to the dependent_spec at line 3, |
| 927 |
// and then we will be in trouble... |
| 928 |
if (TK == FunctionDecl::TK_DependentFunctionTemplateSpecialization) { |
| 929 |
const DependentFunctionTemplateSpecializationInfo *Info = |
| 930 |
TheFD->getDependentSpecializationInfo(); |
| 931 |
// don't need to track all specs, just associate FD with one |
| 932 |
// of those |
| 933 |
const FunctionDecl *Member = nullptr; |
| 934 |
|
| 935 |
#if LLVM_VERSION_MAJOR >= 18 |
| 936 |
ArrayRef<FunctionTemplateDecl *> members = Info->getCandidates(); |
| 937 |
if (!members.empty()) { |
| 938 |
Member = members[0]->getTemplatedDecl(); |
| 939 |
} |
| 940 |
#else |
| 941 |
if (Info->getNumTemplates() > 0) { |
| 942 |
Member = Info->getTemplate(0)->getTemplatedDecl(); |
| 943 |
} |
| 944 |
#endif |
| 945 |
|
| 946 |
if (Member != nullptr) |
| 947 |
createFuncToExplicitSpecs(Member); |
| 948 |
return; |
| 949 |
} |
| 950 |
|
| 951 |
// keep explicit instantiations that are not recored by original |
| 952 |
// template's spec_iterator, e.g.: |
| 953 |
// template<typename T> class S { |
| 954 |
// public: |
| 955 |
// template<typename V> void foo(V& v) {}; |
| 956 |
// }; |
| 957 |
// typedef S<char> SS; |
| 958 |
// template void SS::foo(unsigned short&); |
| 959 |
FunctionTemplateDecl *FTD = TheFD->getPrimaryTemplate(); |
| 960 |
if (!FTD) |
| 961 |
return; |
| 962 |
|
| 963 |
const FunctionTemplateDecl *D = FTD->getInstantiatedFromMemberTemplate(); |
| 964 |
if (!D) |
| 965 |
return; |
| 966 |
const FunctionDecl *OrigFD = D->getTemplatedDecl(); |
| 967 |
TemplateSpecializationKind K = TheFD->getTemplateSpecializationKind(); |
| 968 |
if (K != TSK_ExplicitInstantiationDeclaration && |
| 969 |
K != TSK_ExplicitInstantiationDefinition) |
| 970 |
return; |
| 971 |
|
| 972 |
MemberSpecializationSet *S = |
| 973 |
MemberToInstantiations[OrigFD->getCanonicalDecl()]; |
| 974 |
if (S == NULL) { |
| 975 |
S = new MemberSpecializationSet(); |
| 976 |
MemberToInstantiations[OrigFD->getCanonicalDecl()] = S; |
| 977 |
} |
| 978 |
S->insert(TheFD); |
| 979 |
} |
| 980 |
|
| 981 |
void RemoveUnusedFunction::handleOneCallExpr(const CallExpr *E) |
| 982 |
{ |
| 983 |
const FunctionDecl *FD = E->getDirectCallee(); |
| 984 |
if (!FD) |
| 985 |
return; |
| 986 |
const FunctionDecl *TheFD = getSourceFunctionDecl(FD); |
| 987 |
addOneReferencedFunction(TheFD); |
| 988 |
} |
| 989 |
|
| 990 |
void RemoveUnusedFunction::handleOneCXXOperatorCallExpr( |
| 991 |
const CXXOperatorCallExpr *E) |
| 992 |
{ |
| 993 |
const FunctionDecl *FD = E->getDirectCallee(); |
| 994 |
if (!FD) |
| 995 |
return; |
| 996 |
const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD); |
| 997 |
if (!MD) |
| 998 |
return; |
| 999 |
if (const FunctionDecl *OrigFD = MD->getInstantiatedFromMemberFunction()) |
| 1000 |
addOneReferencedFunction(OrigFD); |
| 1001 |
} |
| 1002 |
|
| 1003 |
void RemoveUnusedFunction::addOneFunctionDecl(const FunctionDecl *CanonicalFD) |
| 1004 |
{ |
| 1005 |
ValidInstanceNum++; |
| 1006 |
if (ToCounter > 0) { |
| 1007 |
AllValidFunctionDecls.push_back(CanonicalFD); |
| 1008 |
return; |
| 1009 |
} |
| 1010 |
if (ValidInstanceNum == TransformationCounter) { |
| 1011 |
TheFunctionDecl = CanonicalFD; |
| 1012 |
} |
| 1013 |
} |
| 1014 |
|
| 1015 |
void RemoveUnusedFunction::addOneReferencedFunction( |
| 1016 |
const FunctionDecl *FD) |
| 1017 |
{ |
| 1018 |
ReferencedFDs.insert(FD->getCanonicalDecl()); |
| 1019 |
} |
| 1020 |
|
| 1021 |
const FunctionTemplateDecl *RemoveUnusedFunction::getTopDescribedTemplate( |
| 1022 |
const FunctionDecl *FD) |
| 1023 |
{ |
| 1024 |
FunctionTemplateDecl *FTD = FD->getDescribedFunctionTemplate(); |
| 1025 |
if (!FTD) |
| 1026 |
return NULL; |
| 1027 |
if (const FunctionTemplateDecl *D = FTD->getInstantiatedFromMemberTemplate()) |
| 1028 |
return D; |
| 1029 |
else |
| 1030 |
return FTD; |
| 1031 |
} |
| 1032 |
|
| 1033 |
bool RemoveUnusedFunction::hasReferencedSpecialization(const FunctionDecl *FD) |
| 1034 |
{ |
| 1035 |
if (const FunctionTemplateDecl *FTD = getTopDescribedTemplate(FD)) { |
| 1036 |
for (FunctionTemplateDecl::spec_iterator I = FTD->spec_begin(), |
| 1037 |
E = FTD->spec_end(); I != E; ++I) { |
| 1038 |
if ((*I)->isReferenced()) |
| 1039 |
return true; |
| 1040 |
} |
| 1041 |
} |
| 1042 |
return false; |
| 1043 |
} |
| 1044 |
|
| 1045 |
void RemoveUnusedFunction::setInlinedSystemFunctions(const FunctionDecl *FD) |
| 1046 |
{ |
| 1047 |
if (!FD->isInlined()) |
| 1048 |
return; |
| 1049 |
|
| 1050 |
std::string FDNameStr = FD->getNameAsString(); |
| 1051 |
for (InlinedSystemFunctionsMap::iterator I = InlinedSystemFunctions.begin(), |
| 1052 |
E = InlinedSystemFunctions.end(); I != E; ++I) { |
| 1053 |
if (FDNameStr == (*I).second) { |
| 1054 |
ExistingSystemFunctions.insert(FDNameStr); |
| 1055 |
return; |
| 1056 |
} |
| 1057 |
} |
| 1058 |
} |
| 1059 |
|
| 1060 |
bool RemoveUnusedFunction::isInlinedSystemFunction(const FunctionDecl *FD) |
| 1061 |
{ |
| 1062 |
std::string FDNameStr = FD->getNameAsString(); |
| 1063 |
InlinedSystemFunctionsMap::iterator I = |
| 1064 |
InlinedSystemFunctions.find(FDNameStr); |
| 1065 |
if (I == InlinedSystemFunctions.end()) |
| 1066 |
return false; |
| 1067 |
return ExistingSystemFunctions.count((*I).second); |
| 1068 |
} |
| 1069 |
|
| 1070 |
void RemoveUnusedFunction::addOneMemberSpecialization( |
| 1071 |
const FunctionDecl *FD, const FunctionDecl *Member) |
| 1072 |
{ |
| 1073 |
MemberSpecializationSet *S = |
| 1074 |
MemberToSpecs[Member->getCanonicalDecl()]; |
| 1075 |
if (S == NULL) { |
| 1076 |
S = new MemberSpecializationSet(); |
| 1077 |
MemberToSpecs[Member->getCanonicalDecl()] = S; |
| 1078 |
} |
| 1079 |
S->insert(FD); |
| 1080 |
} |
| 1081 |
|
| 1082 |
void RemoveUnusedFunction::createFuncToExplicitSpecs(const FunctionDecl *FD) |
| 1083 |
{ |
| 1084 |
MemberSpecializationSet *S = |
| 1085 |
FuncToExplicitSpecs[FD->getCanonicalDecl()]; |
| 1086 |
if (S == NULL) { |
| 1087 |
S = new MemberSpecializationSet(); |
| 1088 |
FuncToExplicitSpecs[FD->getCanonicalDecl()] = S; |
| 1089 |
} |
| 1090 |
} |
| 1091 |
|
| 1092 |
void RemoveUnusedFunction::addFuncToExplicitSpecs(const FunctionDecl *FD) |
| 1093 |
{ |
| 1094 |
TransAssert((FD->getTemplateSpecializationKind() |
| 1095 |
== TSK_ExplicitSpecialization) && |
| 1096 |
"Invalid template specialization kind!"); |
| 1097 |
const FunctionTemplateDecl *FTD = FD->getPrimaryTemplate(); |
| 1098 |
TransAssert(FTD && "NULL FunctionTemplateDecl!"); |
| 1099 |
const FunctionDecl *TemplatedFD = FTD->getTemplatedDecl(); |
| 1100 |
MemberSpecializationSet *S = |
| 1101 |
FuncToExplicitSpecs[TemplatedFD->getCanonicalDecl()]; |
| 1102 |
if (S != NULL) { |
| 1103 |
S->insert(FD); |
| 1104 |
} |
| 1105 |
} |
| 1106 |
|
| 1107 |
RemoveUnusedFunction::~RemoveUnusedFunction() |
| 1108 |
{ |
| 1109 |
for (MemberToSpecializationMap::iterator I = MemberToSpecs.begin(), |
| 1110 |
E = MemberToSpecs.end(); I != E; ++I) { |
| 1111 |
delete ((*I).second); |
| 1112 |
} |
| 1113 |
for (MemberToSpecializationMap::iterator I = MemberToInstantiations.begin(), |
| 1114 |
E = MemberToInstantiations.end(); I != E; ++I) { |
| 1115 |
delete ((*I).second); |
| 1116 |
} |
| 1117 |
delete AnalysisVisitor; |
| 1118 |
delete VisitorWrapper; |
| 1119 |
} |
| 1120 |
|