Skip to content

Instantly share code, notes, and snippets.

@dmikurube
Created January 10, 2013 10:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmikurube/4501181 to your computer and use it in GitHub Desktop.
Save dmikurube/4501181 to your computer and use it in GitHub Desktop.
if (getLangOpts().InterceptAllocationFunctions &&
getLangOpts().RTTI) {
bool UseSize = false;
IdentifierInfo *DeleteInterceptInfo =
&PP.getIdentifierTable().get("__op_delete_intercept__");
DeclareGlobalAllocatorInterceptFunctions(
DeclarationName(DeleteInterceptInfo), UseSize);
LookupResult R(*this, DeleteInterceptInfo, SourceLocation(),
LookupOrdinaryName);
LookupQualifiedName(R, Context.getTranslationUnitDecl());
RecordDecl* TypeInfoDecl = getOrCreateCXXTypeInfoDecl();
if (!R.empty() && !R.isAmbiguous() && TypeInfoDecl) {
R.suppressDiagnostics();
QualType TypeInfoType = Context.getTypeDeclType(TypeInfoDecl);
TypeSourceInfo* TemporaryTypeSourceInfo =
Context.getTrivialTypeSourceInfo(Context.VoidPtrTy);
CXXTypeidExpr* TemporaryTypeidExpr =
new (Context) CXXTypeidExpr(TypeInfoType.withConst(),
TemporaryTypeSourceInfo,
SourceRange(StartLoc, StartLoc));
OverloadCandidateSet Candidates(StartLoc);
for (LookupResult::iterator Intercept = R.begin(), InterceptEnd = R.end();
Intercept != InterceptEnd; ++Intercept) {
// Even member operator new/delete are implicitly treated as
// static, so don't use AddMemberCandidate.
NamedDecl *D = (*Intercept)->getUnderlyingDecl();
FunctionDecl *Fn = cast<FunctionDecl>(D);
CXXNullPtrLiteralExpr Null(Context.VoidPtrTy, SourceLocation());
IntegerLiteral Size(Context,
llvm::APInt::getNullValue(Context.getTargetInfo().getPointerWidth(0)),
Context.getSizeType(), SourceLocation());
Expr* InterceptArgs[3];
InterceptArgs[0] = &Null;
InterceptArgs[1] = TemporaryTypeidExpr;
if (UseSize) {
InterceptArgs[2] = &Size;
AddOverloadCandidate(Fn, Intercept.getPair(),
llvm::makeArrayRef(InterceptArgs, 3), Candidates,
/*SuppressUserConversions=*/false);
} else {
AddOverloadCandidate(Fn, Intercept.getPair(),
llvm::makeArrayRef(InterceptArgs, 2), Candidates,
/*SuppressUserConversions=*/false);
}
}
// Do the resolution.
OverloadCandidateSet::iterator Best;
switch (Candidates.BestViableFunction(*this, StartLoc, Best)) {
case OR_Success: {
FunctionDecl* Fn = Best->Function;
Result->setOperatorDeleteIntercept(Fn);
Result->setInterceptWantsSize(UseSize);
MarkFunctionReferenced(StartLoc, Fn);
}
default:
;
}
}
}
if (getLangOpts().InterceptAllocationFunctions &&
getLangOpts().RTTI) {
bool UseSize = false;
IdentifierInfo *NewInterceptInfo =
&PP.getIdentifierTable().get("__op_new_intercept__");
DeclareGlobalAllocatorInterceptFunctions(
DeclarationName(NewInterceptInfo), true);
LookupResult R(*this, NewInterceptInfo, SourceLocation(),
LookupOrdinaryName);
LookupQualifiedName(R, Context.getTranslationUnitDecl());
RecordDecl* TypeInfoDecl = getOrCreateCXXTypeInfoDecl();
if (!R.empty() && !R.isAmbiguous() && TypeInfoDecl) {
R.suppressDiagnostics();
QualType TypeInfoType = Context.getTypeDeclType(TypeInfoDecl);
TypeSourceInfo* TemporaryTypeSourceInfo =
Context.getTrivialTypeSourceInfo(Context.VoidPtrTy);
CXXTypeidExpr* TemporaryTypeidExpr =
new (Context) CXXTypeidExpr(TypeInfoType.withConst(),
TemporaryTypeSourceInfo,
SourceRange(StartLoc, StartLoc));
OverloadCandidateSet Candidates(StartLoc);
for (LookupResult::iterator Intercept = R.begin(), InterceptEnd = R.end();
Intercept != InterceptEnd; ++Intercept) {
// Even member operator new/delete are implicitly treated as
// static, so don't use AddMemberCandidate.
NamedDecl *D = (*Intercept)->getUnderlyingDecl();
FunctionDecl *Fn = cast<FunctionDecl>(D);
CXXNullPtrLiteralExpr Null(Context.VoidPtrTy, SourceLocation());
IntegerLiteral Size(Context,
llvm::APInt::getNullValue(Context.getTargetInfo().getPointerWidth(0)),
Context.getSizeType(),
SourceLocation());
Expr* InterceptArgs[3];
InterceptArgs[0] = &Null;
InterceptArgs[1] = TemporaryTypeidExpr;
InterceptArgs[2] = &Size;
AddOverloadCandidate(Fn, Intercept.getPair(),
llvm::makeArrayRef(InterceptArgs, 3), Candidates,
/*SuppressUserConversions=*/false);
}
// Do the resolution.
OverloadCandidateSet::iterator Best;
switch (Candidates.BestViableFunction(*this, StartLoc, Best)) {
case OR_Success: {
FunctionDecl* Fn = Best->Function;
Result->setOperatorNewIntercept(Fn);
Result->setInterceptWantsSize(UseSize);
MarkFunctionReferenced(StartLoc, Fn);
}
default:
;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment