Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang][ExtractAPI][NFC] Remove some nullptr dereference problems (#98914) #8987

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 27 additions & 24 deletions clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,22 +174,25 @@ class ExtractAPIVisitorBase : public RecursiveASTVisitor<Derived> {
// skip classes not inherited as public
if (BaseSpecifier.getAccessSpecifier() != AccessSpecifier::AS_public)
continue;
SymbolReference BaseClass;
if (BaseSpecifier.getType().getTypePtr()->isTemplateTypeParmType()) {
BaseClass.Name = API.copyString(BaseSpecifier.getType().getAsString());
if (auto *TTPTD = BaseSpecifier.getType()
->getAs<TemplateTypeParmType>()
->getDecl()) {
SmallString<128> USR;
index::generateUSRForDecl(TTPTD, USR);
BaseClass.USR = API.copyString(USR);
BaseClass.Source = API.copyString(getOwningModuleName(*TTPTD));
}
if (auto *BaseDecl = BaseSpecifier.getType()->getAsTagDecl()) {
Bases.emplace_back(createSymbolReferenceForDecl(*BaseDecl));
} else {
BaseClass = createSymbolReferenceForDecl(
*BaseSpecifier.getType().getTypePtr()->getAsCXXRecordDecl());
SymbolReference BaseClass;
BaseClass.Name = API.copyString(BaseSpecifier.getType().getAsString(
Decl->getASTContext().getPrintingPolicy()));

if (BaseSpecifier.getType().getTypePtr()->isTemplateTypeParmType()) {
if (auto *TTPTD = BaseSpecifier.getType()
->getAs<TemplateTypeParmType>()
->getDecl()) {
SmallString<128> USR;
index::generateUSRForDecl(TTPTD, USR);
BaseClass.USR = API.copyString(USR);
BaseClass.Source = API.copyString(getOwningModuleName(*TTPTD));
}
}
Bases.emplace_back(BaseClass);
}
Bases.emplace_back(BaseClass);
}
return Bases;
}
Expand Down Expand Up @@ -326,7 +329,7 @@ bool ExtractAPIVisitorBase<Derived>::VisitFunctionDecl(
return true;

// Collect symbol information.
StringRef Name = Decl->getName();
auto Name = Decl->getNameAsString();
SmallString<128> USR;
index::generateUSRForDecl(Decl, USR);
PresumedLoc Loc =
Expand Down Expand Up @@ -639,17 +642,17 @@ bool ExtractAPIVisitorBase<Derived>::VisitCXXMethodDecl(
if (FunctionTemplateDecl *TemplateDecl =
Decl->getDescribedFunctionTemplate()) {
API.createRecord<CXXMethodTemplateRecord>(
USR, Decl->getName(), createHierarchyInformationForDecl(*Decl), Loc,
AvailabilityInfo::createFromDecl(Decl), Comment,
USR, Decl->getNameAsString(), createHierarchyInformationForDecl(*Decl),
Loc, AvailabilityInfo::createFromDecl(Decl), Comment,
DeclarationFragmentsBuilder::getFragmentsForFunctionTemplate(
TemplateDecl),
SubHeading, DeclarationFragmentsBuilder::getFunctionSignature(Decl),
DeclarationFragmentsBuilder::getAccessControl(TemplateDecl),
Template(TemplateDecl), isInSystemHeader(Decl));
} else if (Decl->getTemplateSpecializationInfo())
API.createRecord<CXXMethodTemplateSpecializationRecord>(
USR, Decl->getName(), createHierarchyInformationForDecl(*Decl), Loc,
AvailabilityInfo::createFromDecl(Decl), Comment,
USR, Decl->getNameAsString(), createHierarchyInformationForDecl(*Decl),
Loc, AvailabilityInfo::createFromDecl(Decl), Comment,
DeclarationFragmentsBuilder::
getFragmentsForFunctionTemplateSpecialization(Decl),
SubHeading, Signature, Access, isInSystemHeader(Decl));
Expand All @@ -661,14 +664,14 @@ bool ExtractAPIVisitorBase<Derived>::VisitCXXMethodDecl(
SubHeading, Signature, Access, isInSystemHeader(Decl));
else if (Decl->isStatic())
API.createRecord<CXXStaticMethodRecord>(
USR, Decl->getName(), createHierarchyInformationForDecl(*Decl), Loc,
AvailabilityInfo::createFromDecl(Decl), Comment,
USR, Decl->getNameAsString(), createHierarchyInformationForDecl(*Decl),
Loc, AvailabilityInfo::createFromDecl(Decl), Comment,
DeclarationFragmentsBuilder::getFragmentsForCXXMethod(Decl), SubHeading,
Signature, Access, isInSystemHeader(Decl));
else
API.createRecord<CXXInstanceMethodRecord>(
USR, Decl->getName(), createHierarchyInformationForDecl(*Decl), Loc,
AvailabilityInfo::createFromDecl(Decl), Comment,
USR, Decl->getNameAsString(), createHierarchyInformationForDecl(*Decl),
Loc, AvailabilityInfo::createFromDecl(Decl), Comment,
DeclarationFragmentsBuilder::getFragmentsForCXXMethod(Decl), SubHeading,
Signature, Access, isInSystemHeader(Decl));

Expand Down Expand Up @@ -950,7 +953,7 @@ bool ExtractAPIVisitorBase<Derived>::VisitFunctionTemplateDecl(
return true;

// Collect symbol information.
StringRef Name = Decl->getName();
auto Name = Decl->getNameAsString();
SmallString<128> USR;
index::generateUSRForDecl(Decl, USR);
PresumedLoc Loc =
Expand Down
8 changes: 6 additions & 2 deletions clang/lib/ExtractAPI/DeclarationFragments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,8 @@ DeclarationFragmentsBuilder::getFragmentsForFunction(const FunctionDecl *Func) {

Fragments.append(std::move(ReturnValueFragment))
.appendSpace()
.append(Func->getName(), DeclarationFragments::FragmentKind::Identifier);
.append(Func->getNameAsString(),
DeclarationFragments::FragmentKind::Identifier);

if (Func->getTemplateSpecializationInfo()) {
Fragments.append("<", DeclarationFragments::FragmentKind::Text);
Expand Down Expand Up @@ -1592,9 +1593,12 @@ DeclarationFragmentsBuilder::getSubHeading(const NamedDecl *Decl) {
cast<CXXMethodDecl>(Decl)->isOverloadedOperator()) {
Fragments.append(Decl->getNameAsString(),
DeclarationFragments::FragmentKind::Identifier);
} else if (!Decl->getName().empty())
} else if (Decl->getIdentifier()) {
Fragments.append(Decl->getName(),
DeclarationFragments::FragmentKind::Identifier);
} else
Fragments.append(Decl->getDeclName().getAsString(),
DeclarationFragments::FragmentKind::Identifier);
return Fragments;
}

Expand Down