Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
73 changes: 71 additions & 2 deletions lib/CppInterOp/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,39 @@
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);
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 @@ -1228,7 +1261,43 @@
// null, so TU-level using-directives are already handled there.
auto* ND = CppInternal::utils::Lookup::Named(&getSema(), name, Within);
if (ND && ND != (clang::NamedDecl*)-1)
return INTEROP_RETURN(ND->getCanonicalDecl());
return INTEROP_RETURN(ND);

// 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)) {

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 (!FoundND)
FoundND = Named;
else
return INTEROP_RETURN(nullptr);
}
}
if (FoundND)
return INTEROP_RETURN((FoundND));

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

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.
                  ^

// inside the class to traverse base classes and find the member.
auto* ND2 = LookupUnqualified(getSema(), DName, Within);
if (ND2 == reinterpret_cast<clang::NamedDecl*>(-1))

Check warning on line 1290 in lib/CppInterOp/CppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/CppInterOp/CppInterOp.cpp#L1290

Added line #L1290 was not covered by tests
return INTEROP_RETURN(nullptr);
if (ND2) {
if (auto* Shadow = CreateInheritedUsingShadow(RD, ND2))
return INTEROP_RETURN(Shadow);
return INTEROP_RETURN(ND2);

Check warning on line 1295 in lib/CppInterOp/CppInterOp.cpp

View check run for this annotation

Codecov / codecov/patch

lib/CppInterOp/CppInterOp.cpp#L1295

Added line #L1295 was not covered by tests
}
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
Expand All @@ -1240,7 +1309,7 @@
clang::DeclarationName DName = &getSema().Context.Idents.get(name);
ND = LookupUnqualified(getSema(), DName, Within);
if (ND && ND != (clang::NamedDecl*)-1)
return INTEROP_RETURN(ND->getCanonicalDecl());
return INTEROP_RETURN(ND);

return INTEROP_RETURN(nullptr);
}
Expand Down
19 changes: 19 additions & 0 deletions unittests/CppInterOp/ScopeReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,25 @@ 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::DeclRef strt_S = Cpp::GetNamed("S", nullptr);
Cpp::DeclRef strt_S_Val = Cpp::GetNamed("Val", strt_S);
Cpp::DeclRef strt_S1 = Cpp::GetNamed("S1", nullptr);
Cpp::DeclRef 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