From e0d649789dc54c34222a4fa8c804d5c0c13767ca Mon Sep 17 00:00:00 2001 From: Argyrios Kyrtzidis Date: Mon, 8 Oct 2012 23:08:41 +0000 Subject: [PATCH] In VarDecl::getSourceRange() make sure to check that the source location of the initializer is valid before using it. Fixes rdar://12455002&12449015 where local variables of objc objects in ARC mode were not annotated because of the ImplicitValueInitExpr initializer having invalid source range, resulting in the SourceRange of the VarDecl having invalid end location. llvm-svn: 165456 --- clang/lib/AST/Decl.cpp | 7 +++++-- clang/test/Index/arc-annotate.m | 21 ++++++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index 8553e030e2bc..c34d78c8a4ed 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -1204,8 +1204,11 @@ void VarDecl::setStorageClass(StorageClass SC) { } SourceRange VarDecl::getSourceRange() const { - if (getInit()) - return SourceRange(getOuterLocStart(), getInit()->getLocEnd()); + if (const Expr *Init = getInit()) { + SourceLocation InitEnd = Init->getLocEnd(); + if (InitEnd.isValid()) + return SourceRange(getOuterLocStart(), InitEnd); + } return DeclaratorDecl::getSourceRange(); } diff --git a/clang/test/Index/arc-annotate.m b/clang/test/Index/arc-annotate.m index b836bc8b26de..95340938f0a0 100644 --- a/clang/test/Index/arc-annotate.m +++ b/clang/test/Index/arc-annotate.m @@ -4,7 +4,12 @@ @property (unsafe_unretained, nonatomic) id third_property; @end -// RUN: c-index-test -test-annotate-tokens=%s:1:1:5:1 %s -fobjc-arc -fobjc-nonfragile-abi | FileCheck %s +void foo() { + A *avar; + avar = 0; +} + +// RUN: c-index-test -test-annotate-tokens=%s:1:1:11:1 %s -fobjc-arc -fobjc-nonfragile-abi | FileCheck %s // CHECK: Punctuation: "@" [1:1 - 1:2] ObjCInterfaceDecl=A:1:12 // CHECK: Keyword: "interface" [1:2 - 1:11] ObjCInterfaceDecl=A:1:12 // CHECK: Identifier: "A" [1:12 - 1:13] ObjCInterfaceDecl=A:1:12 @@ -36,3 +41,17 @@ // CHECK: Punctuation: ")" [4:40 - 4:41] ObjCPropertyDecl=third_property:4:45 // CHECK: Identifier: "id" [4:42 - 4:44] TypeRef=id:0:0 // CHECK: Identifier: "third_property" [4:45 - 4:59] ObjCPropertyDecl=third_property:4:45 + +// CHECK: Identifier: "A" [8:3 - 8:4] ObjCClassRef=A:1:12 +// CHECK: Punctuation: "*" [8:5 - 8:6] VarDecl=avar:8:6 (Definition) +// CHECK: Identifier: "avar" [8:6 - 8:10] VarDecl=avar:8:6 (Definition) +// CHECK: Punctuation: ";" [8:10 - 8:11] DeclStmt= +// CHECK: Identifier: "avar" [9:3 - 9:7] DeclRefExpr=avar:8:6 +// CHECK: Punctuation: "=" [9:8 - 9:9] BinaryOperator= +// CHECK: Literal: "0" [9:10 - 9:11] IntegerLiteral= +// CHECK: Punctuation: ";" [9:11 - 9:12] CompoundStmt= + +// RUN: c-index-test -file-refs-at=%s:8:8 %s -fobjc-arc -fobjc-nonfragile-abi | FileCheck %s -check-prefix=CHECK-REFS +// CHECK-REFS: VarDecl=avar:8:6 (Definition) +// CHECK-REFS: VarDecl=avar:8:6 (Definition) =[8:6 - 8:10] +// CHECK-REFS: DeclRefExpr=avar:8:6 =[9:3 - 9:7]