]> granicus.if.org Git - clang/commitdiff
[libclang] Do index 'extern' declarations inside functions.
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Mon, 10 Sep 2012 22:58:04 +0000 (22:58 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Mon, 10 Sep 2012 22:58:04 +0000 (22:58 +0000)
rdar://12257073

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@163563 91177308-0d34-0410-b5e6-96231b3b80d8

test/Index/index-decls.m
tools/libclang/IndexBody.cpp
tools/libclang/IndexDecl.cpp
tools/libclang/IndexingContext.cpp
tools/libclang/IndexingContext.h

index 46d37c4345a46243c40e59e4046cfe309b1152b0..c6b14bb8fda5cb800422dfca5a227cc199fc02b7 100644 (file)
@@ -26,6 +26,13 @@ __attribute__((something)) @interface I2 @end
 }
 @end
 
+int test1() {
+  extern int extvar;
+  extvar = 2;
+  extern int extfn();
+  return extfn();
+}
+
 // RUN: c-index-test -index-file %s -target x86_64-apple-macosx10.7 > %t
 // RUN: FileCheck %s -input-file=%t
 // CHECK: [indexDeclaration]: kind: objc-class | name: I | {{.*}} | loc: 1:12
@@ -41,3 +48,9 @@ __attribute__((something)) @interface I2 @end
 
 // CHECK: [indexDeclaration]: kind: objc-ivar | name: _auto_prop | {{.*}} | loc: 20:33
 // CHECK: [indexEntityReference]: kind: objc-ivar | name: _auto_prop | {{.*}} | loc: 25:3
+
+// CHECK: [indexDeclaration]: kind: function | name: test1 | {{.*}} | loc: 29:5
+// CHECK: [indexDeclaration]: kind: variable | name: extvar | {{.*}} | loc: 30:14
+// CHECK: [indexEntityReference]: kind: variable | name: extvar | {{.*}} | loc: 31:3
+// CHECK: [indexDeclaration]: kind: function | name: extfn | {{.*}} | loc: 32:14
+// CHECK: [indexEntityReference]: kind: function | name: extfn | {{.*}} | loc: 33:10
index acf8838940985041a50be439e86951315f165f54..3614206dee9282851a0ac326e8db0b7116b5e0e5 100644 (file)
@@ -130,8 +130,20 @@ public:
   }
 
   bool VisitDeclStmt(DeclStmt *S) {
-    if (IndexCtx.shouldIndexFunctionLocalSymbols())
+    if (IndexCtx.shouldIndexFunctionLocalSymbols()) {
       IndexCtx.indexDeclGroupRef(S->getDeclGroup());
+      return true;
+    }
+
+    DeclGroupRef DG = S->getDeclGroup();
+    for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I) {
+      const Decl *D = *I;
+      if (!D)
+        continue;
+      if (!IndexCtx.isFunctionLocalDecl(D))
+        IndexCtx.indexTopLevelDecl(D);
+    }
+
     return true;
   }
 
index 4c78f5e1b5efde018416ce84605de75b138bd4cc..53a98f111ce95ad23d314e6228789ab6f9598e45 100644 (file)
@@ -325,7 +325,7 @@ void IndexingContext::indexDeclContext(const DeclContext *DC) {
   }
 }
 
-void IndexingContext::indexTopLevelDecl(Decl *D) {
+void IndexingContext::indexTopLevelDecl(const Decl *D) {
   if (isNotFromSourceFile(D->getLocation()))
     return;
 
index 6e1b04df57397dab72fb40fa338c1a2e7344d380..210dc36d522f64438c1e0dc1740e4f5cc4f70edb 100644 (file)
@@ -204,6 +204,26 @@ void IndexingContext::setPreprocessor(Preprocessor &PP) {
   static_cast<ASTUnit*>(CXTU->TUData)->setPreprocessor(&PP);
 }
 
+bool IndexingContext::isFunctionLocalDecl(const Decl *D) {
+  assert(D);
+
+  if (!D->getParentFunctionOrMethod())
+    return false;
+
+  if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) {
+    switch (ND->getLinkage()) {
+    case NoLinkage:
+    case InternalLinkage:
+      return true;
+    case UniqueExternalLinkage:
+    case ExternalLinkage:
+      return false;
+    }
+  }
+
+  return true;
+}
+
 bool IndexingContext::shouldAbort() {
   if (!CB.abortQuery)
     return false;
@@ -590,7 +610,7 @@ bool IndexingContext::handleReference(const NamedDecl *D, SourceLocation Loc,
     return false;
   if (Loc.isInvalid())
     return false;
-  if (!shouldIndexFunctionLocalSymbols() && D->getParentFunctionOrMethod())
+  if (!shouldIndexFunctionLocalSymbols() && isFunctionLocalDecl(D))
     return false;
   if (isNotFromSourceFile(D->getLocation()))
     return false;
index 00e109644c237a52a418244a2e9009f095c6eef7..ef5ed07e36f0477a7585e4e0427addb90b8bd3a7 100644 (file)
@@ -370,6 +370,8 @@ public:
     return IndexOptions & CXIndexOpt_IndexImplicitTemplateInstantiations;
   }
 
+  static bool isFunctionLocalDecl(const Decl *D);
+
   bool shouldAbort();
 
   bool hasDiagnosticCallback() const { return CB.diagnostic; }
@@ -451,7 +453,7 @@ public:
 
   bool isNotFromSourceFile(SourceLocation Loc) const;
 
-  void indexTopLevelDecl(Decl *D);
+  void indexTopLevelDecl(const Decl *D);
   void indexTUDeclsInObjCContainer();
   void indexDeclGroupRef(DeclGroupRef DG);