-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][ExtractAPI][NFC] Remove some nullptr dereference problems #98914
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
Merged
daniel-grumberg
merged 1 commit into
llvm:main
from
daniel-grumberg:wip-extract-api-cxx-fixes
Jul 16, 2024
Merged
[clang][ExtractAPI][NFC] Remove some nullptr dereference problems #98914
daniel-grumberg
merged 1 commit into
llvm:main
from
daniel-grumberg:wip-extract-api-cxx-fixes
Jul 16, 2024
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
@llvm/pr-subscribers-clang Author: Daniel Grumberg (daniel-grumberg) ChangesA places try to get a NamedDecl's name using getName when it isn't a simple identifier, migrate these areas to getNameAsString. rdar://125315602 Full diff: https://github.com/llvm/llvm-project/pull/98914.diff 2 Files Affected:
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;
}
|
QuietMisdreavus
approved these changes
Jul 15, 2024
daniel-grumberg
added a commit
to daniel-grumberg/llvm-project
that referenced
this pull request
Jul 16, 2024
…vm#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
daniel-grumberg
added a commit
to daniel-grumberg/llvm-project
that referenced
this pull request
Jul 16, 2024
…vm#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
daniel-grumberg
added a commit
to swiftlang/llvm-project
that referenced
this pull request
Jul 16, 2024
…vm#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
fredriss
added a commit
to swiftlang/llvm-project
that referenced
this pull request
Jul 17, 2024
…ft/6.0/extract-api-nullptr [clang][ExtractAPI][NFC] Remove some nullptr dereference problems (llvm#98914)
yuxuanchen1997
pushed a commit
that referenced
this pull request
Jul 25, 2024
…8914) Summary: 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 Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60251691
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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