]> granicus.if.org Git - clang/commitdiff
[clang][Index] Add a knob to index function parameters in declarations
authorKadir Cetinkaya <kadircet@google.com>
Mon, 11 Feb 2019 13:02:21 +0000 (13:02 +0000)
committerKadir Cetinkaya <kadircet@google.com>
Mon, 11 Feb 2019 13:02:21 +0000 (13:02 +0000)
Summary:
Parameters in declarations are useful for clangd, so that we can
provide symbol information for them as well. It also helps clangd to be
consistent whether a function's definition is accessible or not.

Reviewers: hokein, akyrtzi

Subscribers: ilya-biryukov, ioeric, arphaman, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D57949

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

include/clang/Index/IndexingAction.h
lib/Index/IndexDecl.cpp
lib/Index/IndexingContext.cpp
lib/Index/IndexingContext.h
unittests/Index/IndexTests.cpp

index 36b3c75a65ff256cbf0e831579923a8d4804016c..8b3d5415c063e35c651c3d845d2fd051bbf876c1 100644 (file)
@@ -44,6 +44,8 @@ struct IndexingOptions {
   // callback is not available (e.g. after parsing has finished). Note that
   // macro references are not available in Proprocessor.
   bool IndexMacrosInPreprocessor = false;
+  // Has no effect if IndexFunctionLocals are false.
+  bool IndexParametersInDeclarations = false;
 };
 
 /// Creates a frontend action that indexes all symbols (macros and AST decls).
index aa73d937df315abb7bfc49bf8d700ce14a5cf1fe..eb0b3d4a70e355cb2d96f6bf5ce89330aa6bf910 100644 (file)
@@ -88,12 +88,11 @@ public:
                                  /*isBase=*/false, isIBType);
     IndexCtx.indexNestedNameSpecifierLoc(D->getQualifierLoc(), Parent);
     if (IndexCtx.shouldIndexFunctionLocalSymbols()) {
-      // Only index parameters in definitions, parameters in declarations are
-      // not useful.
       if (const ParmVarDecl *Parm = dyn_cast<ParmVarDecl>(D)) {
         auto *DC = Parm->getDeclContext();
         if (auto *FD = dyn_cast<FunctionDecl>(DC)) {
-          if (FD->isThisDeclarationADefinition())
+          if (IndexCtx.shouldIndexParametersInDeclarations() ||
+              FD->isThisDeclarationADefinition())
             IndexCtx.handleDecl(Parm);
         } else if (auto *MD = dyn_cast<ObjCMethodDecl>(DC)) {
           if (MD->isThisDeclarationADefinition())
@@ -102,7 +101,8 @@ public:
           IndexCtx.handleDecl(Parm);
         }
       } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
-        if (FD->isThisDeclarationADefinition()) {
+        if (IndexCtx.shouldIndexParametersInDeclarations() ||
+            FD->isThisDeclarationADefinition()) {
           for (auto PI : FD->parameters()) {
             IndexCtx.handleDecl(PI);
           }
index 0bbd739d741c721f07b6f662de296c851428fd3c..3d2d7d4ff02aea1b400ff3322d57f5d610efbbdc 100644 (file)
@@ -40,6 +40,10 @@ bool IndexingContext::shouldIndexImplicitInstantiation() const {
   return IndexOpts.IndexImplicitInstantiation;
 }
 
+bool IndexingContext::shouldIndexParametersInDeclarations() const {
+  return IndexOpts.IndexParametersInDeclarations;
+}
+
 bool IndexingContext::handleDecl(const Decl *D,
                                  SymbolRoleSet Roles,
                                  ArrayRef<SymbolRelation> Relations) {
index 1de38513c61ab74e4274cba2bdad80a1b2fac2cd..7765fa73f82c842f58833e9d70374ab9166a1c1b 100644 (file)
@@ -61,6 +61,8 @@ public:
 
   bool shouldIndexImplicitInstantiation() const;
 
+  bool shouldIndexParametersInDeclarations() const;
+
   static bool isTemplateImplicitInstantiation(const Decl *D);
 
   bool handleDecl(const Decl *D, SymbolRoleSet Roles = SymbolRoleSet(),
index 147f7a714ad24d1c661ca9c2f04c1b9b9e5a1e84..2b574a53fc3235d22411298c49cdace497f36267 100644 (file)
@@ -119,6 +119,21 @@ TEST(IndexTest, IndexPreprocessorMacros) {
   EXPECT_THAT(Index->Symbols, UnorderedElementsAre());
 }
 
+TEST(IndexTest, IndexParametersInDecls) {
+  std::string Code = "void foo(int bar);";
+  auto Index = std::make_shared<Indexer>();
+  IndexingOptions Opts;
+  Opts.IndexFunctionLocals = true;
+  Opts.IndexParametersInDeclarations = true;
+  tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
+  EXPECT_THAT(Index->Symbols, Contains(QName("bar")));
+
+  Opts.IndexParametersInDeclarations = false;
+  Index->Symbols.clear();
+  tooling::runToolOnCode(new IndexAction(Index, Opts), Code);
+  EXPECT_THAT(Index->Symbols, Not(Contains(QName("bar"))));
+}
+
 } // namespace
 } // namespace index
 } // namespace clang