Skip to content

Commit 626a9aa

Browse files
[clang][ExtractAPI][NFC] Remove some nullptr dereference problems (llvm#98914)
A places try to get a NamedDecl's name using getName when it isn't a simple identifier, migrate these areas to getNameAsString. rdar://125315602
1 parent 991967e commit 626a9aa

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

clang/include/clang/ExtractAPI/ExtractAPIVisitor.h

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -175,22 +175,25 @@ class ExtractAPIVisitorBase : public RecursiveASTVisitor<Derived> {
175175
// skip classes not inherited as public
176176
if (BaseSpecifier.getAccessSpecifier() != AccessSpecifier::AS_public)
177177
continue;
178-
SymbolReference BaseClass;
179-
if (BaseSpecifier.getType().getTypePtr()->isTemplateTypeParmType()) {
180-
BaseClass.Name = API.copyString(BaseSpecifier.getType().getAsString());
181-
if (auto *TTPTD = BaseSpecifier.getType()
182-
->getAs<TemplateTypeParmType>()
183-
->getDecl()) {
184-
SmallString<128> USR;
185-
index::generateUSRForDecl(TTPTD, USR);
186-
BaseClass.USR = API.copyString(USR);
187-
BaseClass.Source = API.copyString(getOwningModuleName(*TTPTD));
188-
}
178+
if (auto *BaseDecl = BaseSpecifier.getType()->getAsTagDecl()) {
179+
Bases.emplace_back(createSymbolReferenceForDecl(*BaseDecl));
189180
} else {
190-
BaseClass = createSymbolReferenceForDecl(
191-
*BaseSpecifier.getType().getTypePtr()->getAsCXXRecordDecl());
181+
SymbolReference BaseClass;
182+
BaseClass.Name = API.copyString(BaseSpecifier.getType().getAsString(
183+
Decl->getASTContext().getPrintingPolicy()));
184+
185+
if (BaseSpecifier.getType().getTypePtr()->isTemplateTypeParmType()) {
186+
if (auto *TTPTD = BaseSpecifier.getType()
187+
->getAs<TemplateTypeParmType>()
188+
->getDecl()) {
189+
SmallString<128> USR;
190+
index::generateUSRForDecl(TTPTD, USR);
191+
BaseClass.USR = API.copyString(USR);
192+
BaseClass.Source = API.copyString(getOwningModuleName(*TTPTD));
193+
}
194+
}
195+
Bases.emplace_back(BaseClass);
192196
}
193-
Bases.emplace_back(BaseClass);
194197
}
195198
return Bases;
196199
}
@@ -352,7 +355,7 @@ bool ExtractAPIVisitorBase<Derived>::VisitFunctionDecl(
352355
return true;
353356

354357
// Collect symbol information.
355-
StringRef Name = Decl->getName();
358+
auto Name = Decl->getNameAsString();
356359
SmallString<128> USR;
357360
index::generateUSRForDecl(Decl, USR);
358361
PresumedLoc Loc =
@@ -666,17 +669,17 @@ bool ExtractAPIVisitorBase<Derived>::VisitCXXMethodDecl(
666669
if (FunctionTemplateDecl *TemplateDecl =
667670
Decl->getDescribedFunctionTemplate()) {
668671
API.createRecord<CXXMethodTemplateRecord>(
669-
USR, Decl->getName(), createHierarchyInformationForDecl(*Decl), Loc,
670-
AvailabilityInfo::createFromDecl(Decl), Comment,
672+
USR, Decl->getNameAsString(), createHierarchyInformationForDecl(*Decl),
673+
Loc, AvailabilityInfo::createFromDecl(Decl), Comment,
671674
DeclarationFragmentsBuilder::getFragmentsForFunctionTemplate(
672675
TemplateDecl),
673676
SubHeading, DeclarationFragmentsBuilder::getFunctionSignature(Decl),
674677
DeclarationFragmentsBuilder::getAccessControl(TemplateDecl),
675678
Template(TemplateDecl), isInSystemHeader(Decl));
676679
} else if (Decl->getTemplateSpecializationInfo())
677680
API.createRecord<CXXMethodTemplateSpecializationRecord>(
678-
USR, Decl->getName(), createHierarchyInformationForDecl(*Decl), Loc,
679-
AvailabilityInfo::createFromDecl(Decl), Comment,
681+
USR, Decl->getNameAsString(), createHierarchyInformationForDecl(*Decl),
682+
Loc, AvailabilityInfo::createFromDecl(Decl), Comment,
680683
DeclarationFragmentsBuilder::
681684
getFragmentsForFunctionTemplateSpecialization(Decl),
682685
SubHeading, Signature, Access, isInSystemHeader(Decl));
@@ -688,14 +691,14 @@ bool ExtractAPIVisitorBase<Derived>::VisitCXXMethodDecl(
688691
SubHeading, Signature, Access, isInSystemHeader(Decl));
689692
else if (Decl->isStatic())
690693
API.createRecord<CXXStaticMethodRecord>(
691-
USR, Decl->getName(), createHierarchyInformationForDecl(*Decl), Loc,
692-
AvailabilityInfo::createFromDecl(Decl), Comment,
694+
USR, Decl->getNameAsString(), createHierarchyInformationForDecl(*Decl),
695+
Loc, AvailabilityInfo::createFromDecl(Decl), Comment,
693696
DeclarationFragmentsBuilder::getFragmentsForCXXMethod(Decl), SubHeading,
694697
Signature, Access, isInSystemHeader(Decl));
695698
else
696699
API.createRecord<CXXInstanceMethodRecord>(
697-
USR, Decl->getName(), createHierarchyInformationForDecl(*Decl), Loc,
698-
AvailabilityInfo::createFromDecl(Decl), Comment,
700+
USR, Decl->getNameAsString(), createHierarchyInformationForDecl(*Decl),
701+
Loc, AvailabilityInfo::createFromDecl(Decl), Comment,
699702
DeclarationFragmentsBuilder::getFragmentsForCXXMethod(Decl), SubHeading,
700703
Signature, Access, isInSystemHeader(Decl));
701704

@@ -977,7 +980,7 @@ bool ExtractAPIVisitorBase<Derived>::VisitFunctionTemplateDecl(
977980
return true;
978981

979982
// Collect symbol information.
980-
StringRef Name = Decl->getName();
983+
auto Name = Decl->getNameAsString();
981984
SmallString<128> USR;
982985
index::generateUSRForDecl(Decl, USR);
983986
PresumedLoc Loc =

clang/lib/ExtractAPI/DeclarationFragments.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,8 @@ DeclarationFragmentsBuilder::getFragmentsForFunction(const FunctionDecl *Func) {
710710

711711
Fragments.append(std::move(ReturnValueFragment))
712712
.appendSpace()
713-
.append(Func->getName(), DeclarationFragments::FragmentKind::Identifier);
713+
.append(Func->getNameAsString(),
714+
DeclarationFragments::FragmentKind::Identifier);
714715

715716
if (Func->getTemplateSpecializationInfo()) {
716717
Fragments.append("<", DeclarationFragments::FragmentKind::Text);
@@ -1610,9 +1611,12 @@ DeclarationFragmentsBuilder::getSubHeading(const NamedDecl *Decl) {
16101611
cast<CXXMethodDecl>(Decl)->isOverloadedOperator()) {
16111612
Fragments.append(Decl->getNameAsString(),
16121613
DeclarationFragments::FragmentKind::Identifier);
1613-
} else if (!Decl->getName().empty())
1614+
} else if (Decl->getIdentifier()) {
16141615
Fragments.append(Decl->getName(),
16151616
DeclarationFragments::FragmentKind::Identifier);
1617+
} else
1618+
Fragments.append(Decl->getDeclName().getAsString(),
1619+
DeclarationFragments::FragmentKind::Identifier);
16161620
return Fragments;
16171621
}
16181622

0 commit comments

Comments
 (0)