Skip to content

Commit 5bbcb7b

Browse files
authored
Merge pull request #8987 from daniel-grumberg/origin/cherry-picks/swift/6.0/extract-api-nullptr
[clang][ExtractAPI][NFC] Remove some nullptr dereference problems (llvm#98914)
2 parents 713644c + dab8e79 commit 5bbcb7b

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
@@ -174,22 +174,25 @@ class ExtractAPIVisitorBase : public RecursiveASTVisitor<Derived> {
174174
// skip classes not inherited as public
175175
if (BaseSpecifier.getAccessSpecifier() != AccessSpecifier::AS_public)
176176
continue;
177-
SymbolReference BaseClass;
178-
if (BaseSpecifier.getType().getTypePtr()->isTemplateTypeParmType()) {
179-
BaseClass.Name = API.copyString(BaseSpecifier.getType().getAsString());
180-
if (auto *TTPTD = BaseSpecifier.getType()
181-
->getAs<TemplateTypeParmType>()
182-
->getDecl()) {
183-
SmallString<128> USR;
184-
index::generateUSRForDecl(TTPTD, USR);
185-
BaseClass.USR = API.copyString(USR);
186-
BaseClass.Source = API.copyString(getOwningModuleName(*TTPTD));
187-
}
177+
if (auto *BaseDecl = BaseSpecifier.getType()->getAsTagDecl()) {
178+
Bases.emplace_back(createSymbolReferenceForDecl(*BaseDecl));
188179
} else {
189-
BaseClass = createSymbolReferenceForDecl(
190-
*BaseSpecifier.getType().getTypePtr()->getAsCXXRecordDecl());
180+
SymbolReference BaseClass;
181+
BaseClass.Name = API.copyString(BaseSpecifier.getType().getAsString(
182+
Decl->getASTContext().getPrintingPolicy()));
183+
184+
if (BaseSpecifier.getType().getTypePtr()->isTemplateTypeParmType()) {
185+
if (auto *TTPTD = BaseSpecifier.getType()
186+
->getAs<TemplateTypeParmType>()
187+
->getDecl()) {
188+
SmallString<128> USR;
189+
index::generateUSRForDecl(TTPTD, USR);
190+
BaseClass.USR = API.copyString(USR);
191+
BaseClass.Source = API.copyString(getOwningModuleName(*TTPTD));
192+
}
193+
}
194+
Bases.emplace_back(BaseClass);
191195
}
192-
Bases.emplace_back(BaseClass);
193196
}
194197
return Bases;
195198
}
@@ -326,7 +329,7 @@ bool ExtractAPIVisitorBase<Derived>::VisitFunctionDecl(
326329
return true;
327330

328331
// Collect symbol information.
329-
StringRef Name = Decl->getName();
332+
auto Name = Decl->getNameAsString();
330333
SmallString<128> USR;
331334
index::generateUSRForDecl(Decl, USR);
332335
PresumedLoc Loc =
@@ -639,17 +642,17 @@ bool ExtractAPIVisitorBase<Derived>::VisitCXXMethodDecl(
639642
if (FunctionTemplateDecl *TemplateDecl =
640643
Decl->getDescribedFunctionTemplate()) {
641644
API.createRecord<CXXMethodTemplateRecord>(
642-
USR, Decl->getName(), createHierarchyInformationForDecl(*Decl), Loc,
643-
AvailabilityInfo::createFromDecl(Decl), Comment,
645+
USR, Decl->getNameAsString(), createHierarchyInformationForDecl(*Decl),
646+
Loc, AvailabilityInfo::createFromDecl(Decl), Comment,
644647
DeclarationFragmentsBuilder::getFragmentsForFunctionTemplate(
645648
TemplateDecl),
646649
SubHeading, DeclarationFragmentsBuilder::getFunctionSignature(Decl),
647650
DeclarationFragmentsBuilder::getAccessControl(TemplateDecl),
648651
Template(TemplateDecl), isInSystemHeader(Decl));
649652
} else if (Decl->getTemplateSpecializationInfo())
650653
API.createRecord<CXXMethodTemplateSpecializationRecord>(
651-
USR, Decl->getName(), createHierarchyInformationForDecl(*Decl), Loc,
652-
AvailabilityInfo::createFromDecl(Decl), Comment,
654+
USR, Decl->getNameAsString(), createHierarchyInformationForDecl(*Decl),
655+
Loc, AvailabilityInfo::createFromDecl(Decl), Comment,
653656
DeclarationFragmentsBuilder::
654657
getFragmentsForFunctionTemplateSpecialization(Decl),
655658
SubHeading, Signature, Access, isInSystemHeader(Decl));
@@ -661,14 +664,14 @@ bool ExtractAPIVisitorBase<Derived>::VisitCXXMethodDecl(
661664
SubHeading, Signature, Access, isInSystemHeader(Decl));
662665
else if (Decl->isStatic())
663666
API.createRecord<CXXStaticMethodRecord>(
664-
USR, Decl->getName(), createHierarchyInformationForDecl(*Decl), Loc,
665-
AvailabilityInfo::createFromDecl(Decl), Comment,
667+
USR, Decl->getNameAsString(), createHierarchyInformationForDecl(*Decl),
668+
Loc, AvailabilityInfo::createFromDecl(Decl), Comment,
666669
DeclarationFragmentsBuilder::getFragmentsForCXXMethod(Decl), SubHeading,
667670
Signature, Access, isInSystemHeader(Decl));
668671
else
669672
API.createRecord<CXXInstanceMethodRecord>(
670-
USR, Decl->getName(), createHierarchyInformationForDecl(*Decl), Loc,
671-
AvailabilityInfo::createFromDecl(Decl), Comment,
673+
USR, Decl->getNameAsString(), createHierarchyInformationForDecl(*Decl),
674+
Loc, AvailabilityInfo::createFromDecl(Decl), Comment,
672675
DeclarationFragmentsBuilder::getFragmentsForCXXMethod(Decl), SubHeading,
673676
Signature, Access, isInSystemHeader(Decl));
674677

@@ -950,7 +953,7 @@ bool ExtractAPIVisitorBase<Derived>::VisitFunctionTemplateDecl(
950953
return true;
951954

952955
// Collect symbol information.
953-
StringRef Name = Decl->getName();
956+
auto Name = Decl->getNameAsString();
954957
SmallString<128> USR;
955958
index::generateUSRForDecl(Decl, USR);
956959
PresumedLoc Loc =

clang/lib/ExtractAPI/DeclarationFragments.cpp

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

709709
Fragments.append(std::move(ReturnValueFragment))
710710
.appendSpace()
711-
.append(Func->getName(), DeclarationFragments::FragmentKind::Identifier);
711+
.append(Func->getNameAsString(),
712+
DeclarationFragments::FragmentKind::Identifier);
712713

713714
if (Func->getTemplateSpecializationInfo()) {
714715
Fragments.append("<", DeclarationFragments::FragmentKind::Text);
@@ -1592,9 +1593,12 @@ DeclarationFragmentsBuilder::getSubHeading(const NamedDecl *Decl) {
15921593
cast<CXXMethodDecl>(Decl)->isOverloadedOperator()) {
15931594
Fragments.append(Decl->getNameAsString(),
15941595
DeclarationFragments::FragmentKind::Identifier);
1595-
} else if (!Decl->getName().empty())
1596+
} else if (Decl->getIdentifier()) {
15961597
Fragments.append(Decl->getName(),
15971598
DeclarationFragments::FragmentKind::Identifier);
1599+
} else
1600+
Fragments.append(Decl->getDeclName().getAsString(),
1601+
DeclarationFragments::FragmentKind::Identifier);
15981602
return Fragments;
15991603
}
16001604

0 commit comments

Comments
 (0)