Author: Daniel Grumberg Date: 2024-07-16T11:24:28+01:00 New Revision: 2c13194eab28474089841903acd5790b8b1a559a
URL: https://github.com/llvm/llvm-project/commit/2c13194eab28474089841903acd5790b8b1a559a DIFF: https://github.com/llvm/llvm-project/commit/2c13194eab28474089841903acd5790b8b1a559a.diff LOG: [clang][ExtractAPI][NFC] Remove some nullptr dereference problems (#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 Added: Modified: clang/include/clang/ExtractAPI/ExtractAPIVisitor.h clang/lib/ExtractAPI/DeclarationFragments.cpp Removed: ################################################################################ diff --git a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h index 76d7fd798bed3..1b27027621666 100644 --- a/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h +++ b/clang/include/clang/ExtractAPI/ExtractAPIVisitor.h @@ -175,22 +175,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; } @@ -352,7 +355,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 = @@ -666,8 +669,8 @@ 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), @@ -675,8 +678,8 @@ bool ExtractAPIVisitorBase<Derived>::VisitCXXMethodDecl( 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)); @@ -688,14 +691,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)); @@ -977,7 +980,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 = diff --git a/clang/lib/ExtractAPI/DeclarationFragments.cpp b/clang/lib/ExtractAPI/DeclarationFragments.cpp index 8c7c0f8a14726..6b85c7db90349 100644 --- a/clang/lib/ExtractAPI/DeclarationFragments.cpp +++ b/clang/lib/ExtractAPI/DeclarationFragments.cpp @@ -710,7 +710,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); @@ -1610,9 +1611,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; } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits