r342484 - [index] Enhance indexing for module references

Argyrios Kyrtzidis via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 18 08:02:56 PDT 2018


Author: akirtzidis
Date: Tue Sep 18 08:02:56 2018
New Revision: 342484

URL: http://llvm.org/viewvc/llvm-project?rev=342484&view=rev
Log:
[index] Enhance indexing for module references

* Create a USR for the occurrences of the 'module' symbol kind
* Record module references for each identifier in an import declaration

Added:
    cfe/trunk/test/Index/Core/Inputs/module/SubModA.h
    cfe/trunk/test/Index/Core/Inputs/module/SubSubModA.h
Modified:
    cfe/trunk/include/clang/Index/IndexDataConsumer.h
    cfe/trunk/include/clang/Index/USRGeneration.h
    cfe/trunk/lib/Index/IndexingAction.cpp
    cfe/trunk/lib/Index/IndexingContext.cpp
    cfe/trunk/lib/Index/USRGeneration.cpp
    cfe/trunk/test/Index/Core/Inputs/module/module.modulemap
    cfe/trunk/test/Index/Core/index-with-module.m
    cfe/trunk/tools/c-index-test/core_main.cpp
    cfe/trunk/tools/libclang/CXIndexDataConsumer.cpp
    cfe/trunk/tools/libclang/CXIndexDataConsumer.h

Modified: cfe/trunk/include/clang/Index/IndexDataConsumer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Index/IndexDataConsumer.h?rev=342484&r1=342483&r2=342484&view=diff
==============================================================================
--- cfe/trunk/include/clang/Index/IndexDataConsumer.h (original)
+++ cfe/trunk/include/clang/Index/IndexDataConsumer.h Tue Sep 18 08:02:56 2018
@@ -50,7 +50,12 @@ public:
                                     SourceLocation Loc);
 
   /// \returns true to continue indexing, or false to abort.
+  ///
+  /// This will be called for each module reference in an import decl.
+  /// For "@import MyMod.SubMod", there will be a call for 'MyMod' with the
+  /// 'reference' role, and a call for 'SubMod' with the 'declaration' role.
   virtual bool handleModuleOccurence(const ImportDecl *ImportD,
+                                     const Module *Mod,
                                      SymbolRoleSet Roles, SourceLocation Loc);
 
   virtual void finish() {}

Modified: cfe/trunk/include/clang/Index/USRGeneration.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Index/USRGeneration.h?rev=342484&r1=342483&r2=342484&view=diff
==============================================================================
--- cfe/trunk/include/clang/Index/USRGeneration.h (original)
+++ cfe/trunk/include/clang/Index/USRGeneration.h Tue Sep 18 08:02:56 2018
@@ -16,6 +16,7 @@
 namespace clang {
 class Decl;
 class MacroDefinitionRecord;
+class Module;
 class SourceLocation;
 class SourceManager;
 
@@ -70,6 +71,22 @@ bool generateUSRForMacro(const MacroDefi
 bool generateUSRForMacro(StringRef MacroName, SourceLocation Loc,
                          const SourceManager &SM, SmallVectorImpl<char> &Buf);
 
+/// Generate a USR for a module, including the USR prefix.
+/// \returns true on error, false on success.
+bool generateFullUSRForModule(const Module *Mod, raw_ostream &OS);
+
+/// Generate a USR for a top-level module name, including the USR prefix.
+/// \returns true on error, false on success.
+bool generateFullUSRForTopLevelModuleName(StringRef ModName, raw_ostream &OS);
+
+/// Generate a USR fragment for a module.
+/// \returns true on error, false on success.
+bool generateUSRFragmentForModule(const Module *Mod, raw_ostream &OS);
+
+/// Generate a USR fragment for a module name.
+/// \returns true on error, false on success.
+bool generateUSRFragmentForModuleName(StringRef ModName, raw_ostream &OS);
+
 } // namespace index
 } // namespace clang
 

Modified: cfe/trunk/lib/Index/IndexingAction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/IndexingAction.cpp?rev=342484&r1=342483&r2=342484&view=diff
==============================================================================
--- cfe/trunk/lib/Index/IndexingAction.cpp (original)
+++ cfe/trunk/lib/Index/IndexingAction.cpp Tue Sep 18 08:02:56 2018
@@ -37,6 +37,7 @@ bool IndexDataConsumer::handleMacroOccur
 }
 
 bool IndexDataConsumer::handleModuleOccurence(const ImportDecl *ImportD,
+                                              const Module *Mod,
                                               SymbolRoleSet Roles,
                                               SourceLocation Loc) {
   return true;

Modified: cfe/trunk/lib/Index/IndexingContext.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/IndexingContext.cpp?rev=342484&r1=342483&r2=342484&view=diff
==============================================================================
--- cfe/trunk/lib/Index/IndexingContext.cpp (original)
+++ cfe/trunk/lib/Index/IndexingContext.cpp Tue Sep 18 08:02:56 2018
@@ -80,11 +80,27 @@ bool IndexingContext::handleReference(co
                               RefE, RefD, DC);
 }
 
+static void reportModuleReferences(const Module *Mod,
+                                   ArrayRef<SourceLocation> IdLocs,
+                                   const ImportDecl *ImportD,
+                                   IndexDataConsumer &DataConsumer) {
+  if (!Mod)
+    return;
+  reportModuleReferences(Mod->Parent, IdLocs.drop_back(), ImportD,
+                         DataConsumer);
+  DataConsumer.handleModuleOccurence(ImportD, Mod,
+                                     (SymbolRoleSet)SymbolRole::Reference,
+                                     IdLocs.back());
+}
+
 bool IndexingContext::importedModule(const ImportDecl *ImportD) {
+  if (ImportD->isInvalidDecl())
+    return true;
+
   SourceLocation Loc;
   auto IdLocs = ImportD->getIdentifierLocs();
   if (!IdLocs.empty())
-    Loc = IdLocs.front();
+    Loc = IdLocs.back();
   else
     Loc = ImportD->getLocation();
 
@@ -108,11 +124,17 @@ bool IndexingContext::importedModule(con
     }
   }
 
+  const Module *Mod = ImportD->getImportedModule();
+  if (!ImportD->isImplicit() && Mod->Parent && !IdLocs.empty()) {
+    reportModuleReferences(Mod->Parent, IdLocs.drop_back(), ImportD,
+                           DataConsumer);
+  }
+
   SymbolRoleSet Roles = (unsigned)SymbolRole::Declaration;
   if (ImportD->isImplicit())
     Roles |= (unsigned)SymbolRole::Implicit;
 
-  return DataConsumer.handleModuleOccurence(ImportD, Roles, Loc);
+  return DataConsumer.handleModuleOccurence(ImportD, Mod, Roles, Loc);
 }
 
 bool IndexingContext::isTemplateImplicitInstantiation(const Decl *D) {

Modified: cfe/trunk/lib/Index/USRGeneration.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Index/USRGeneration.cpp?rev=342484&r1=342483&r2=342484&view=diff
==============================================================================
--- cfe/trunk/lib/Index/USRGeneration.cpp (original)
+++ cfe/trunk/lib/Index/USRGeneration.cpp Tue Sep 18 08:02:56 2018
@@ -1094,3 +1094,29 @@ bool clang::index::generateUSRForMacro(S
   Out << MacroName;
   return false;
 }
+
+bool clang::index::generateFullUSRForModule(const Module *Mod,
+                                            raw_ostream &OS) {
+  if (!Mod->Parent)
+    return generateFullUSRForTopLevelModuleName(Mod->Name, OS);
+  if (generateFullUSRForModule(Mod->Parent, OS))
+    return true;
+  return generateUSRFragmentForModule(Mod, OS);
+}
+
+bool clang::index::generateFullUSRForTopLevelModuleName(StringRef ModName,
+                                                        raw_ostream &OS) {
+  OS << getUSRSpacePrefix();
+  return generateUSRFragmentForModuleName(ModName, OS);
+}
+
+bool clang::index::generateUSRFragmentForModule(const Module *Mod,
+                                                raw_ostream &OS) {
+  return generateUSRFragmentForModuleName(Mod->Name, OS);
+}
+
+bool clang::index::generateUSRFragmentForModuleName(StringRef ModName,
+                                                    raw_ostream &OS) {
+  OS << "@M@" << ModName;
+  return false;
+}

Added: cfe/trunk/test/Index/Core/Inputs/module/SubModA.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/Core/Inputs/module/SubModA.h?rev=342484&view=auto
==============================================================================
--- cfe/trunk/test/Index/Core/Inputs/module/SubModA.h (added)
+++ cfe/trunk/test/Index/Core/Inputs/module/SubModA.h Tue Sep 18 08:02:56 2018
@@ -0,0 +1,2 @@
+
+void SubModA_func(void);

Added: cfe/trunk/test/Index/Core/Inputs/module/SubSubModA.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/Core/Inputs/module/SubSubModA.h?rev=342484&view=auto
==============================================================================
--- cfe/trunk/test/Index/Core/Inputs/module/SubSubModA.h (added)
+++ cfe/trunk/test/Index/Core/Inputs/module/SubSubModA.h Tue Sep 18 08:02:56 2018
@@ -0,0 +1,2 @@
+
+void SubSubModA_func(void);

Modified: cfe/trunk/test/Index/Core/Inputs/module/module.modulemap
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/Core/Inputs/module/module.modulemap?rev=342484&r1=342483&r2=342484&view=diff
==============================================================================
--- cfe/trunk/test/Index/Core/Inputs/module/module.modulemap (original)
+++ cfe/trunk/test/Index/Core/Inputs/module/module.modulemap Tue Sep 18 08:02:56 2018
@@ -1 +1,11 @@
-module ModA { header "ModA.h" export * }
+module ModA {
+  header "ModA.h" export *
+
+  module SubModA {
+    header "SubModA.h"
+
+    module SubSubModA {
+      header "SubSubModA.h"
+    }
+  }
+}

Modified: cfe/trunk/test/Index/Core/index-with-module.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Index/Core/index-with-module.m?rev=342484&r1=342483&r2=342484&view=diff
==============================================================================
--- cfe/trunk/test/Index/Core/index-with-module.m (original)
+++ cfe/trunk/test/Index/Core/index-with-module.m Tue Sep 18 08:02:56 2018
@@ -1,11 +1,17 @@
 // RUN: rm -rf %t.mcp
 // RUN: c-index-test core -print-source-symbols -dump-imported-module-files -- %s -I %S/Inputs/module -fmodules -fmodules-cache-path=%t.mcp | FileCheck %s
 
-// CHECK: [[@LINE+1]]:9 | module/C | ModA | Decl |
+// CHECK: [[@LINE+1]]:9 | module/C | ModA | [[ModA_USR:c:@M at ModA]] | Decl |
 @import ModA;
-// CHECK: [[@LINE+1]]:1 | module/C | ModA | Decl,Impl |
+// CHECK: [[@LINE+1]]:1 | module/C | ModA | [[ModA_USR]] | Decl,Impl |
 #include "ModA.h"
 
+ at import ModA.SubModA.SubSubModA;
+// CHECK: [[@LINE-1]]:9 | module/C | ModA | [[ModA_USR]] | Ref |
+// CHECK: [[@LINE-2]]:14 | module/C | ModA.SubModA | c:@M at ModA@M at SubModA | Ref |
+// CHECK: [[@LINE-3]]:22 | module/C | ModA.SubModA.SubSubModA | [[SubSubModA_USR:c:@M at ModA@M at SubModA@M at SubSubModA]] | Decl |
+#include "SubSubModA.h" // CHECK: [[@LINE]]:1 | module/C | ModA.SubModA.SubSubModA | [[SubSubModA_USR]] | Decl,Impl |
+
 void foo() {
   // CHECK: [[@LINE+1]]:3 | function/C | ModA_func | c:@F at ModA_func | {{.*}} | Ref,Call,RelCall,RelCont | rel: 1
   ModA_func();

Modified: cfe/trunk/tools/c-index-test/core_main.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/c-index-test/core_main.cpp?rev=342484&r1=342483&r2=342484&view=diff
==============================================================================
--- cfe/trunk/tools/c-index-test/core_main.cpp (original)
+++ cfe/trunk/tools/c-index-test/core_main.cpp Tue Sep 18 08:02:56 2018
@@ -74,6 +74,7 @@ static cl::opt<std::string>
 static void printSymbolInfo(SymbolInfo SymInfo, raw_ostream &OS);
 static void printSymbolNameAndUSR(const Decl *D, ASTContext &Ctx,
                                   raw_ostream &OS);
+static void printSymbolNameAndUSR(const clang::Module *Mod, raw_ostream &OS);
 
 namespace {
 
@@ -132,8 +133,9 @@ public:
     return true;
   }
 
-  bool handleModuleOccurence(const ImportDecl *ImportD, SymbolRoleSet Roles,
-                             SourceLocation Loc) override {
+  bool handleModuleOccurence(const ImportDecl *ImportD,
+                             const clang::Module *Mod,
+                             SymbolRoleSet Roles, SourceLocation Loc) override {
     ASTContext &Ctx = ImportD->getASTContext();
     SourceManager &SM = Ctx.getSourceManager();
 
@@ -146,7 +148,8 @@ public:
     printSymbolInfo(getSymbolInfo(ImportD), OS);
     OS << " | ";
 
-    OS << ImportD->getImportedModule()->getFullModuleName() << " | ";
+    printSymbolNameAndUSR(Mod, OS);
+    OS << " | ";
 
     printSymbolRoles(Roles, OS);
     OS << " |\n";
@@ -308,6 +311,12 @@ static void printSymbolNameAndUSR(const
   }
 }
 
+static void printSymbolNameAndUSR(const clang::Module *Mod, raw_ostream &OS) {
+  assert(Mod);
+  OS << Mod->getFullModuleName() << " | ";
+  generateFullUSRForModule(Mod, OS);
+}
+
 //===----------------------------------------------------------------------===//
 // Command line processing.
 //===----------------------------------------------------------------------===//

Modified: cfe/trunk/tools/libclang/CXIndexDataConsumer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXIndexDataConsumer.cpp?rev=342484&r1=342483&r2=342484&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXIndexDataConsumer.cpp (original)
+++ cfe/trunk/tools/libclang/CXIndexDataConsumer.cpp Tue Sep 18 08:02:56 2018
@@ -222,9 +222,11 @@ bool CXIndexDataConsumer::handleDeclOccu
 }
 
 bool CXIndexDataConsumer::handleModuleOccurence(const ImportDecl *ImportD,
+                                                const Module *Mod,
                                                 SymbolRoleSet Roles,
                                                 SourceLocation Loc) {
-  IndexingDeclVisitor(*this, SourceLocation(), nullptr).Visit(ImportD);
+  if (Roles & (SymbolRoleSet)SymbolRole::Declaration)
+    IndexingDeclVisitor(*this, SourceLocation(), nullptr).Visit(ImportD);
   return !shouldAbort();
 }
 

Modified: cfe/trunk/tools/libclang/CXIndexDataConsumer.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/libclang/CXIndexDataConsumer.h?rev=342484&r1=342483&r2=342484&view=diff
==============================================================================
--- cfe/trunk/tools/libclang/CXIndexDataConsumer.h (original)
+++ cfe/trunk/tools/libclang/CXIndexDataConsumer.h Tue Sep 18 08:02:56 2018
@@ -467,7 +467,7 @@ private:
                            ArrayRef<index::SymbolRelation> Relations,
                            SourceLocation Loc, ASTNodeInfo ASTNode) override;
 
-  bool handleModuleOccurence(const ImportDecl *ImportD,
+  bool handleModuleOccurence(const ImportDecl *ImportD, const Module *Mod,
                              index::SymbolRoleSet Roles,
                              SourceLocation Loc) override;
 




More information about the cfe-commits mailing list