Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions lib/CppInterOp/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,39 @@ clang::NamedDecl* LookupUnqualified(clang::Sema& S,
return (clang::NamedDecl*)-1;
}


static clang::UsingShadowDecl *CreateInheritedUsingShadow(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: 'CreateInheritedUsingShadow' is a static definition in anonymous namespace; static is redundant here [readability-static-definition-in-anonymous-namespace]

Suggested change
static clang::UsingShadowDecl *CreateInheritedUsingShadow(
;

clang::CXXRecordDecl *Record, clang::NamedDecl *Target) {
if (!Record || !Target)
return nullptr;

if (auto *Shadow = llvm::dyn_cast<clang::UsingShadowDecl>(Target))
Target = Shadow->getTargetDecl();

if (!Target)
return nullptr;

if (Target->getDeclContext() == Record)
return llvm::dyn_cast<clang::UsingShadowDecl>(Target);

clang::ASTContext &C = Record->getASTContext();
clang::DeclarationNameInfo NameInfo(Target->getDeclName(),
clang::SourceLocation());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: use nullptr [modernize-use-nullptr]

Suggested change
clang::SourceLocation());
= nullptr;

auto *Using = clang::UsingDecl::Create(C, Record, clang::SourceLocation(),
clang::NestedNameSpecifierLoc(),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: use nullptr [modernize-use-nullptr]

Suggested change
clang::NestedNameSpecifierLoc(),
= nullptr;

NameInfo, false);
Using->setImplicit(true);

auto *Shadow = clang::UsingShadowDecl::Create(
C, Record, clang::SourceLocation(), Target->getDeclName(), Using,
Target);
Using->addShadowDecl(Shadow);
Shadow->setAccess(Target->getAccess());
Record->addDecl(Shadow);

return Shadow;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no header providing "CppInternal::utils::Lookup::Named" is directly included [misc-include-cleaner]

lib/CppInterOp/CppInterOp.cpp:10:

- #include "Unwrap.h"
+ #include "CppInterOpInterpreter.h"
+ #include "Unwrap.h"


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use C-style cast to convert between unrelated types [cppcoreguidelines-pro-type-cstyle-cast]

hin);
                        ^

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: integer to pointer cast pessimizes optimization opportunities [performance-no-int-to-ptr]

hin);
                        ^

// Cheap probe: does any namespace from `DC` up to TU carry at least
// one using-directive? Gates the synthetic-DRef-chain build below so
// the common case (no using-directives anywhere on the path) doesn't
Expand Down Expand Up @@ -1230,6 +1263,40 @@ DeclRef GetNamed(const std::string& name, ConstDeclRef parent /*= nullptr*/) {
if (ND && ND != (clang::NamedDecl*)-1)
return INTEROP_RETURN(ND->getCanonicalDecl());

// Qualified lookup can miss inherited members in class scope. Try the
// record's own lookup, which includes direct members, and then fall back
// to unqualified lookup inside the record to honor base-class member
// visibility semantics.
if (Within) {
if (auto* RD = llvm::dyn_cast<clang::CXXRecordDecl>(Within)) {
clang::DeclarationName DName = &getSema().Context.Idents.get(name);
auto Decls = RD->lookup(DName);
clang::NamedDecl* FoundND = nullptr;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use C-style cast to convert between unrelated types [cppcoreguidelines-pro-type-cstyle-cast]

hin);
                        ^

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: integer to pointer cast pessimizes optimization opportunities [performance-no-int-to-ptr]

hin);
                        ^

for (auto* D : Decls) {
if (auto* Named = llvm::dyn_cast<clang::NamedDecl>(D)) {
if (!FoundND)
FoundND = Named;
else
return INTEROP_RETURN(nullptr);
}
}
if (FoundND)
return INTEROP_RETURN((FoundND->getCanonicalDecl()));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use const_cast to remove const qualifier [cppcoreguidelines-pro-type-const-cast]

DRef.
                  ^

// Qualified lookup may still miss inherited class members in some
// record contexts. Use unqualified lookup from a synthesized point
// inside the class to traverse base classes and find the member.
auto* ND2 = LookupUnqualified(getSema(), DName, Within);
if (ND2 && ND2 != (clang::NamedDecl*)-1) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use C-style cast to convert between unrelated types [cppcoreguidelines-pro-type-cstyle-cast]

hin);
                              ^

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: integer to pointer cast pessimizes optimization opportunities [performance-no-int-to-ptr]

hin);
                              ^

if (auto* Shadow = CreateInheritedUsingShadow(RD, ND2))
return INTEROP_RETURN((Shadow->getCanonicalDecl()));
return INTEROP_RETURN((ND2->getCanonicalDecl()));
}
if (ND2 == (clang::NamedDecl*)-1)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: do not use C-style cast to convert between unrelated types [cppcoreguidelines-pro-type-cstyle-cast]

    }
                       ^

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: integer to pointer cast pessimizes optimization opportunities [performance-no-int-to-ptr]

    }
                       ^

return INTEROP_RETURN(nullptr);
}
}

// Slow path: only when qualified lookup missed AND `Within` is a
// namespace whose enclosing chain carries at least one using-directive
// (the only reason qualified-vs-unqualified disagree at namespace
Expand Down
20 changes: 20 additions & 0 deletions unittests/CppInterOp/ScopeReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,26 @@ TYPED_TEST(CPPINTEROP_TEST_MODE, ScopeReflection_GetNamed) {
EXPECT_EQ(Cpp::GetQualifiedName(std_ns), "std");
EXPECT_EQ(Cpp::GetQualifiedName(std_string_class), "std::string");
EXPECT_EQ(Cpp::GetQualifiedName(std_string_npos_var), "std::basic_string<char>::npos");

Interp->declare(R"(
struct S {
typedef int Val;
};

struct S1 : public S {
/* empty */
};
)");

Cpp::TCppScope_t strt_S = Cpp::GetNamed("S", nullptr);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no type named 'TCppScope_t' in namespace 'Cpp' [clang-diagnostic-error]

Cpp::TCppScope_t strt_S = Cpp::GetNamed("S", nullptr);
     ^

Cpp::TCppScope_t strt_S_Val = Cpp::GetNamed("Val", strt_S);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no type named 'TCppScope_t' in namespace 'Cpp' [clang-diagnostic-error]

Cpp::TCppScope_t strt_S_Val = Cpp::GetNamed("Val", strt_S);
     ^

Cpp::TCppScope_t strt_S1 = Cpp::GetNamed("S1", nullptr);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: expected ';' after expression [clang-diagnostic-error]

Suggested change
Cpp::TCppScope_t strt_S1 = Cpp::GetNamed("S1", nullptr);
Cpp::TCppScope_t; strt_S1 = Cpp::GetNamed("S1", nullptr);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: no member named 'TCppScope_t' in namespace 'Cpp' [clang-diagnostic-error]

Cpp::TCppScope_t strt_S1 = Cpp::GetNamed("S1", nullptr);
     ^

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: use of undeclared identifier 'strt_S1'; did you mean 'strt_S'? [clang-diagnostic-error]

Suggested change
Cpp::TCppScope_t strt_S1 = Cpp::GetNamed("S1", nullptr);
Cpp::TCppScope_t strt_S = Cpp::GetNamed("S1", nullptr);
Additional context

unittests/CppInterOp/ScopeReflectionTest.cpp:715: 'strt_S' declared here

Cpp::TCppScope_t strt_S = Cpp::GetNamed("S", nullptr);
                 ^

Cpp::TCppScope_t strt_S1_Val = Cpp::GetNamed("Val", strt_S1);

EXPECT_EQ(Cpp::GetQualifiedName(strt_S), "S");
EXPECT_EQ(Cpp::GetQualifiedName(strt_S_Val), "S::Val");
EXPECT_EQ(Cpp::GetQualifiedName(strt_S1), "S1");
EXPECT_EQ(Cpp::GetQualifiedName(strt_S1_Val), "S1::Val");
}

TYPED_TEST(CPPINTEROP_TEST_MODE, ScopeReflection_GetNamedWithUsing) {
Expand Down
Loading