Skip to content

Commit

Permalink
[index] Don't add relation to a NamedDecl with no name
Browse files Browse the repository at this point in the history
Unless it's one of the special cases (tag, category) that we can handle.
This syncs up the check between handling a decl and handling a relation.

This would cause invalid nameless decls to end up in relations despite
having no name or USR.

rdar://problem/32474406

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@307855 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
benlangmuir committed Jul 12, 2017
1 parent 15f6571 commit ea63750
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/Index/IndexingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ static bool isDeclADefinition(const Decl *D, const DeclContext *ContainerDC, AST
return false;
}

/// Whether the given NamedDecl should be skipped because it has no name.
static bool shouldSkipNamelessDecl(const NamedDecl *ND) {
return ND->getDeclName().isEmpty() && !isa<TagDecl>(ND) &&
!isa<ObjCCategoryDecl>(ND);
}

static const Decl *adjustParent(const Decl *Parent) {
if (!Parent)
return nullptr;
Expand All @@ -288,8 +294,8 @@ static const Decl *adjustParent(const Decl *Parent) {
} else if (auto RD = dyn_cast<RecordDecl>(Parent)) {
if (RD->isAnonymousStructOrUnion())
continue;
} else if (auto FD = dyn_cast<FieldDecl>(Parent)) {
if (FD->getDeclName().isEmpty())
} else if (auto ND = dyn_cast<NamedDecl>(Parent)) {
if (shouldSkipNamelessDecl(ND))
continue;
}
return Parent;
Expand Down Expand Up @@ -359,9 +365,7 @@ bool IndexingContext::handleDeclOccurrence(const Decl *D, SourceLocation Loc,
const DeclContext *ContainerDC) {
if (D->isImplicit() && !(isa<ObjCMethodDecl>(D) || isa<ObjCIvarDecl>(D)))
return true;
if (!isa<NamedDecl>(D) ||
(cast<NamedDecl>(D)->getDeclName().isEmpty() &&
!isa<TagDecl>(D) && !isa<ObjCCategoryDecl>(D)))
if (!isa<NamedDecl>(D) || shouldSkipNamelessDecl(cast<NamedDecl>(D)))
return true;

SourceManager &SM = Ctx->getSourceManager();
Expand Down
13 changes: 13 additions & 0 deletions test/Index/Core/index-source-invalid-name.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: c-index-test core -print-source-symbols -- %s -std=c++1z -target x86_64-apple-macosx10.7 | FileCheck %s

namespace rdar32474406 {
// CHECK: [[@LINE+1]]:6 | function/C | foo | c:@N@rdar32474406@F@foo# | __ZN12rdar324744063fooEv | Decl,RelChild | rel: 1
void foo();
// CHECK: [[@LINE+1]]:16 | type-alias/C | Func_t | c:index-source-invalid-name.cpp@N@rdar32474406@T@Func_t | <no-cgname> | Def,RelChild | rel: 1
typedef void (*Func_t)();
// CHECK: [[@LINE+4]]:1 | type-alias/C | Func_t | c:index-source-invalid-name.cpp@N@rdar32474406@T@Func_t | <no-cgname> | Ref,RelCont | rel: 1
// CHECK-NEXT: RelCont | rdar32474406 | c:@N@rdar32474406
// CHECK: [[@LINE+2]]:14 | function/C | foo | c:@N@rdar32474406@F@foo# | __ZN12rdar324744063fooEv | Ref,RelCont | rel: 1
// CHECK-NEXT: RelCont | rdar32474406 | c:@N@rdar32474406
Func_t[] = { foo }; // invalid decomposition
}

0 comments on commit ea63750

Please sign in to comment.