From: Ben Langmuir Date: Wed, 12 Jul 2017 22:05:30 +0000 (+0000) Subject: [index] Don't add relation to a NamedDecl with no name X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2d77c354f16dda7be663553232555b0d61eab112;p=clang [index] Don't add relation to a NamedDecl with no name 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 --- diff --git a/lib/Index/IndexingContext.cpp b/lib/Index/IndexingContext.cpp index 754bc84ff4..c4aa51d62f 100644 --- a/lib/Index/IndexingContext.cpp +++ b/lib/Index/IndexingContext.cpp @@ -229,6 +229,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(ND) && + !isa(ND); +} + static const Decl *adjustParent(const Decl *Parent) { if (!Parent) return nullptr; @@ -243,8 +249,8 @@ static const Decl *adjustParent(const Decl *Parent) { } else if (auto RD = dyn_cast(Parent)) { if (RD->isAnonymousStructOrUnion()) continue; - } else if (auto FD = dyn_cast(Parent)) { - if (FD->getDeclName().isEmpty()) + } else if (auto ND = dyn_cast(Parent)) { + if (shouldSkipNamelessDecl(ND)) continue; } return Parent; @@ -315,9 +321,7 @@ bool IndexingContext::handleDeclOccurrence(const Decl *D, SourceLocation Loc, const DeclContext *ContainerDC) { if (D->isImplicit() && !isa(D)) return true; - if (!isa(D) || - (cast(D)->getDeclName().isEmpty() && - !isa(D) && !isa(D))) + if (!isa(D) || shouldSkipNamelessDecl(cast(D))) return true; SourceManager &SM = Ctx->getSourceManager(); diff --git a/test/Index/Core/index-source-invalid-name.cpp b/test/Index/Core/index-source-invalid-name.cpp new file mode 100644 index 0000000000..1b4b059cd1 --- /dev/null +++ b/test/Index/Core/index-source-invalid-name.cpp @@ -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 | | 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 | | 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 +}