Skip to content

Commit a51d433

Browse files
committed
[Import as member] Don't infer special CF memory management names
1 parent c5dc346 commit a51d433

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

lib/ClangImporter/IAMInference.cpp

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ STATISTIC(FailInferFunction, "# of functions unable to infer");
3939
// Specifically skipped/avoided
4040
STATISTIC(SkipLeadingUnderscore,
4141
"# of globals skipped due to leading underscore");
42+
STATISTIC(SkipCFMemoryManagement,
43+
"# of CF memory management globals skipped");
4244

4345
// Success statistics
4446
STATISTIC(SuccessImportAsTypeID, "# imported as 'typeID'");
@@ -176,6 +178,7 @@ class IAMInference {
176178
}
177179

178180
IAMResult infer(const clang::NamedDecl *);
181+
IAMResult inferVar(const clang::VarDecl *);
179182

180183
private:
181184
// typeID
@@ -617,39 +620,43 @@ bool IAMInference::validToImportAsProperty(
617620
return isValidAsInstanceProperty(getterDecl, setterDecl);
618621
}
619622

620-
IAMResult IAMInference::infer(const clang::NamedDecl *clangDecl) {
621-
if (clangDecl->getName().startswith("_")) {
622-
++SkipLeadingUnderscore;
623+
IAMResult IAMInference::inferVar(const clang::VarDecl *varDecl) {
624+
auto fail = [varDecl]() -> IAMResult {
625+
DEBUG(llvm::dbgs() << "failed to infer variable: ");
626+
DEBUG(varDecl->print(llvm::dbgs()));
627+
DEBUG(llvm::dbgs() << "\n");
628+
++FailInferVar;
623629
return {};
624-
}
630+
};
625631

626-
if (auto varDecl = dyn_cast<clang::VarDecl>(clangDecl)) {
627-
auto fail = [varDecl]() -> IAMResult {
628-
DEBUG(llvm::dbgs() << "failed to infer variable: ");
629-
DEBUG(varDecl->print(llvm::dbgs()));
630-
DEBUG(llvm::dbgs() << "\n");
631-
++FailInferVar;
632-
return {};
633-
};
632+
// Try to find a type to add this as a static property to
633+
StringRef workingName = varDecl->getName();
634+
if (workingName.empty())
635+
return fail();
634636

635-
// Try to find a type to add this as a static property to
636-
StringRef workingName = varDecl->getName();
637-
if (workingName.empty())
638-
return fail();
637+
// Special pattern: constants of the form "kFooBarBaz", extend "FooBar" with
638+
// property "Baz"
639+
if (*camel_case::getWords(workingName).begin() == "k")
640+
workingName = workingName.drop_front(1);
639641

640-
// Special pattern: constants of the form "kFooBarBaz", extend "FooBar" with
641-
// property "Baz"
642-
if (*camel_case::getWords(workingName).begin() == "k")
643-
workingName = workingName.drop_front(1);
642+
NameBuffer remainingName;
643+
if (auto effectiveDC = findTypeAndMatch(workingName, remainingName))
644644

645-
NameBuffer remainingName;
646-
if (auto effectiveDC = findTypeAndMatch(workingName, remainingName))
645+
return importAsStaticProperty(remainingName, effectiveDC);
647646

648-
return importAsStaticProperty(remainingName, effectiveDC);
647+
return fail();
648+
}
649649

650-
return fail();
650+
IAMResult IAMInference::infer(const clang::NamedDecl *clangDecl) {
651+
if (clangDecl->getName().startswith("_")) {
652+
++SkipLeadingUnderscore;
653+
return {};
651654
}
652655

656+
// Try to infer a member variable
657+
if (auto varDecl = dyn_cast<clang::VarDecl>(clangDecl))
658+
return inferVar(varDecl);
659+
653660
// Try to infer a member function
654661
auto funcDecl = dyn_cast<clang::FunctionDecl>(clangDecl);
655662
if (!funcDecl) {
@@ -675,9 +682,11 @@ IAMResult IAMInference::infer(const clang::NamedDecl *clangDecl) {
675682
auto retTy = funcDecl->getReturnType();
676683
unsigned numParams = funcDecl->getNumParams();
677684

678-
// 0) Special cases are specially handled: *GetTypeID()
685+
// 0) Special cases are specially handled
679686
//
680687
StringRef getTypeID = "GetTypeID";
688+
StringRef cfSpecials[] = {"Release", "Retain", "Autorelease"};
689+
// *GetTypeID
681690
if (numParams == 0 && workingName.endswith(getTypeID)) {
682691
NameBuffer remainingName;
683692
if (auto effectiveDC = findTypeAndMatch(
@@ -688,6 +697,19 @@ IAMResult IAMInference::infer(const clang::NamedDecl *clangDecl) {
688697
return importAsTypeID(retTy, effectiveDC);
689698
}
690699
}
700+
// *Release/*Retain/*Autorelease
701+
} else if (numParams == 1 &&
702+
std::any_of(std::begin(cfSpecials), std::end(cfSpecials),
703+
[workingName](StringRef suffix) {
704+
return workingName.endswith(suffix);
705+
})) {
706+
if (auto type =
707+
funcDecl->getParamDecl(0)->getType()->getAs<clang::TypedefType>()) {
708+
if (CFPointeeInfo::classifyTypedef(type->getDecl())) {
709+
++SkipCFMemoryManagement;
710+
return {};
711+
}
712+
}
691713
}
692714

693715
// 1) If we find an init specifier and our name matches the return type, we

0 commit comments

Comments
 (0)