Skip to content

Commit

Permalink
Fix scope-based lookup when more than one function is found.
Browse files Browse the repository at this point in the history
When multiple functions are found by name, lldb removes duplicate entries of
functions with the same type, so the first function in the symbol context list
is chosen, even if it isn't in scope. This patch uses the declaration context
of the execution context to select the function which is in scope.

This fixes cases like the following:

    int func();
    namespace ns {
	int func();
	void here() {
	    // Run to BP here and eval 'p func()';
	    // lldb used to find ::func(), now finds ns::func().
	}
    }

Reviewed by: clayborg
Subscribers: lldb-commits
Differential Revision: http://reviews.llvm.org/D15312

llvm-svn: 255439
  • Loading branch information
Dawn Perchik committed Dec 12, 2015
1 parent 9315392 commit b592578
Show file tree
Hide file tree
Showing 9 changed files with 417 additions and 2 deletions.
9 changes: 9 additions & 0 deletions lldb/include/lldb/Symbol/ClangASTContext.h
Expand Up @@ -944,6 +944,15 @@ class ClangASTContext : public TypeSystem
CompilerType
GetTypeForFormatters (void* type) override;

#define LLDB_INVALID_DECL_LEVEL UINT32_MAX
// LLDB_INVALID_DECL_LEVEL is returned by CountDeclLevels if
// child_decl_ctx could not be found in decl_ctx.
uint32_t
CountDeclLevels (clang::DeclContext *frame_decl_ctx,
clang::DeclContext *child_decl_ctx,
ConstString *child_name = nullptr,
CompilerType *child_type = nullptr);

//----------------------------------------------------------------------
// Modifying RecordType
//----------------------------------------------------------------------
Expand Down
@@ -1,5 +1,5 @@
LEVEL = ../../../make

CXX_SOURCES := main.cpp
CXX_SOURCES := main.cpp ns.cpp ns2.cpp ns3.cpp

include $(LEVEL)/Makefile.rules
Expand Up @@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//

#include <cstdarg>
#include "ns.h"

namespace {
typedef unsigned int my_uint_t;
Expand Down Expand Up @@ -80,7 +81,6 @@ namespace ns2 {
int value = 200;
}

#include <stdio.h>
void test_namespace_scopes() {
do {
using namespace ns1;
Expand Down Expand Up @@ -113,5 +113,12 @@ int Foo::myfunc(int a)
int
main (int argc, char const *argv[])
{
test_lookup_at_global_scope();
test_lookup_at_file_scope();
A::test_lookup_at_ns_scope();
A::B::test_lookup_at_nested_ns_scope();
A::B::test_lookup_at_nested_ns_scope_after_using();
test_lookup_before_using_directive();
test_lookup_after_using_directive();
return Foo::myfunc(12);
}
32 changes: 32 additions & 0 deletions lldb/packages/Python/lldbsuite/test/lang/cpp/namespace/ns.cpp
@@ -0,0 +1,32 @@
//===-- ns.cpp ------------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "ns.h"

int foo()
{
printf("global foo()\n");
return 42;
}
int func()
{
printf("global func()\n");
return 1;
}
int func(int a)
{
printf("global func(int)\n");
return a + 1;
}
void test_lookup_at_global_scope()
{
// BP_global_scope
printf("at global scope: foo() = %d\n", foo()); // eval foo(), exp: 42
printf("at global scope: func() = %d\n", func()); // eval func(), exp: 1
}
36 changes: 36 additions & 0 deletions lldb/packages/Python/lldbsuite/test/lang/cpp/namespace/ns.h
@@ -0,0 +1,36 @@
//===-- ns.h ------------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include <stdio.h>

void test_lookup_at_global_scope();
void test_lookup_at_file_scope();
void test_lookup_before_using_directive();
void test_lookup_after_using_directive();
int func(int a);
namespace A {
int foo();
int func(int a);
inline int func()
{
printf("A::func()\n");
return 3;
}
inline int func2()
{
printf("A::func2()\n");
return 3;
}
void test_lookup_at_ns_scope();
namespace B {
int func();
void test_lookup_at_nested_ns_scope();
void test_lookup_at_nested_ns_scope_after_using();
}
}
65 changes: 65 additions & 0 deletions lldb/packages/Python/lldbsuite/test/lang/cpp/namespace/ns2.cpp
@@ -0,0 +1,65 @@
//===-- ns2.cpp ------------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "ns.h"

static int func()
{
printf("static m2.cpp func()\n");
return 2;
}
void test_lookup_at_file_scope()
{
// BP_file_scope
printf("at file scope: func() = %d\n", func()); // eval func(), exp: 2
printf("at file scope: func(10) = %d\n", func(10)); // eval func(10), exp: 11
}
namespace A {
namespace B {
int func()
{
printf("A::B::func()\n");
return 4;
}
void test_lookup_at_nested_ns_scope()
{
// BP_nested_ns_scope
printf("at nested ns scope: func() = %d\n", func()); // eval func(), exp: 4

//printf("func(10) = %d\n", func(10)); // eval func(10), exp: 13
// NOTE: Under the rules of C++, this test would normally get an error
// because A::B::func() hides A::func(), but lldb intentionally
// disobeys these rules so that the intended overload can be found
// by only removing duplicates if they have the same type.
}
void test_lookup_at_nested_ns_scope_after_using()
{
// BP_nested_ns_scope_after_using
using A::func;
printf("at nested ns scope after using: func() = %d\n", func()); // eval func(), exp: 3
}
}
}
int A::foo()
{
printf("A::foo()\n");
return 42;
}
int A::func(int a)
{
printf("A::func(int)\n");
return a + 3;
}
void A::test_lookup_at_ns_scope()
{
// BP_ns_scope
printf("at nested ns scope: func() = %d\n", func()); // eval func(), exp: 3
printf("at nested ns scope: func(10) = %d\n", func(10)); // eval func(10), exp: 13
printf("at nested ns scope: foo() = %d\n", foo()); // eval foo(), exp: 42
}
27 changes: 27 additions & 0 deletions lldb/packages/Python/lldbsuite/test/lang/cpp/namespace/ns3.cpp
@@ -0,0 +1,27 @@
//===-- ns3.cpp ------------------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "ns.h"
extern int func();

// Note: the following function must be before the using.
void test_lookup_before_using_directive()
{
// BP_before_using_directive
printf("before using directive: func() = %d\n", func()); // eval func(), exp: 1
}
using namespace A;
void test_lookup_after_using_directive()
{
// BP_after_using_directive
//printf("func() = %d\n", func()); // eval func(), exp: error, amiguous
printf("after using directive: func2() = %d\n", func2()); // eval func2(), exp: 3
printf("after using directive: ::func() = %d\n", ::func()); // eval ::func(), exp: 1
printf("after using directive: B::func() = %d\n", B::func()); // eval B::func(), exp: 4
}
122 changes: 122 additions & 0 deletions lldb/source/Plugins/ExpressionParser/Clang/ClangExpressionDeclMap.cpp
Expand Up @@ -1424,6 +1424,128 @@ ClangExpressionDeclMap::FindExternalVisibleDecls (NameSearchContext &context,
sc_list);
}

// If we found more than one function, see if we can use the
// frame's decl context to remove functions that are shadowed
// by other functions which match in type but are nearer in scope.
//
// AddOneFunction will not add a function whose type has already been
// added, so if there's another function in the list with a matching
// type, check to see if their decl context is a parent of the current
// frame's or was imported via a and using statement, and pick the
// best match according to lookup rules.
if (sc_list.GetSize() > 1)
{
// Collect some info about our frame's context.
StackFrame *frame = m_parser_vars->m_exe_ctx.GetFramePtr();
SymbolContext frame_sym_ctx;
if (frame != nullptr)
frame_sym_ctx = frame->GetSymbolContext(lldb::eSymbolContextFunction|lldb::eSymbolContextBlock);
CompilerDeclContext frame_decl_context = frame_sym_ctx.block != nullptr ? frame_sym_ctx.block->GetDeclContext() : CompilerDeclContext();

// We can't do this without a compiler decl context for our frame.
if (frame_decl_context)
{
clang::DeclContext *frame_decl_ctx = (clang::DeclContext *)frame_decl_context.GetOpaqueDeclContext();
ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(frame_decl_context.GetTypeSystem());

// Structure to hold the info needed when comparing function
// declarations.
struct FuncDeclInfo
{
ConstString m_name;
CompilerType m_copied_type;
uint32_t m_decl_lvl;
SymbolContext m_sym_ctx;
};

// First, symplify things by looping through the symbol contexts
// to remove unwanted functions and separate out the functions we
// want to compare and prune into a separate list.
// Cache the info needed about the function declarations in a
// vector for efficiency.
SymbolContextList sc_sym_list;
uint32_t num_indices = sc_list.GetSize();
std::vector<FuncDeclInfo> fdi_cache;
fdi_cache.reserve(num_indices);
for (uint32_t index = 0; index < num_indices; ++index)
{
FuncDeclInfo fdi;
SymbolContext sym_ctx;
sc_list.GetContextAtIndex(index, sym_ctx);

// We don't know enough about symbols to compare them,
// but we should keep them in the list.
Function *function = sym_ctx.function;
if (!function)
{
sc_sym_list.Append(sym_ctx);
continue;
}
// Filter out functions without declaration contexts, as well as
// class/instance methods, since they'll be skipped in the
// code that follows anyway.
CompilerDeclContext func_decl_context = function->GetDeclContext();
if (!func_decl_context || func_decl_context.IsClassMethod(nullptr, nullptr, nullptr))
continue;
// We can only prune functions for which we can copy the type.
CompilerType func_clang_type = function->GetType()->GetFullCompilerType();
CompilerType copied_func_type = GuardedCopyType(func_clang_type);
if (!copied_func_type)
{
sc_sym_list.Append(sym_ctx);
continue;
}

fdi.m_sym_ctx = sym_ctx;
fdi.m_name = function->GetName();
fdi.m_copied_type = copied_func_type;
fdi.m_decl_lvl = LLDB_INVALID_DECL_LEVEL;
if (fdi.m_copied_type && func_decl_context)
{
// Call CountDeclLevels to get the number of parent scopes we
// have to look through before we find the function declaration.
// When comparing functions of the same type, the one with a
// lower count will be closer to us in the lookup scope and
// shadows the other.
clang::DeclContext *func_decl_ctx = (clang::DeclContext *)func_decl_context.GetOpaqueDeclContext();
fdi.m_decl_lvl = ast->CountDeclLevels(frame_decl_ctx,
func_decl_ctx,
&fdi.m_name,
&fdi.m_copied_type);
}
fdi_cache.emplace_back(fdi);
}

// Loop through the functions in our cache looking for matching types,
// then compare their scope levels to see which is closer.
std::multimap<CompilerType, const FuncDeclInfo*> matches;
for (const FuncDeclInfo &fdi : fdi_cache)
{
const CompilerType t = fdi.m_copied_type;
auto q = matches.find(t);
if (q != matches.end())
{
if (q->second->m_decl_lvl > fdi.m_decl_lvl)
// This function is closer; remove the old set.
matches.erase(t);
else if (q->second->m_decl_lvl < fdi.m_decl_lvl)
// The functions in our set are closer - skip this one.
continue;
}
matches.insert(std::make_pair(t, &fdi));
}

// Loop through our matches and add their symbol contexts to our list.
SymbolContextList sc_func_list;
for (const auto &q : matches)
sc_func_list.Append(q.second->m_sym_ctx);

// Rejoin the lists with the functions in front.
sc_list = sc_func_list;
sc_list.Append(sc_sym_list);
}
}

if (sc_list.GetSize())
{
Symbol *extern_symbol = NULL;
Expand Down

0 comments on commit b592578

Please sign in to comment.