Skip to content
Merged
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
36 changes: 21 additions & 15 deletions lib/CppInterOp/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1951,19 +1951,30 @@
*/
return QualType();
}
static std::optional<QualType> GetTypeInternal(Decl* 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: 'GetTypeInternal' is a static definition in anonymous namespace; static is redundant here [readability-static-definition-in-anonymous-namespace]

Suggested change
static std::optional<QualType> GetTypeInternal(Decl* D) {
std::optional<QualType> GetTypeInternal(Decl* 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: no header providing "std::optional" is directly included [misc-include-cleaner]

lib/CppInterOp/CppInterOp.cpp:52:

- #if CLANG_VERSION_MAJOR >= 20
+ #include <optional>
+ #if CLANG_VERSION_MAJOR >= 20

if (!D)
return {};

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

View check run for this annotation

Codecov / codecov/patch

lib/CppInterOp/CppInterOp.cpp#L1956

Added line #L1956 was not covered by tests
// Even though typedefs derive from TypeDecl, their getTypeForDecl()
// returns a nullptr.
if (const auto* TND = llvm::dyn_cast_or_null<TypedefNameDecl>(D))
return TND->getUnderlyingType();

if (auto* VD = dyn_cast<ValueDecl>(D))
return VD->getType();

if (const auto* TD = llvm::dyn_cast_or_null<TypeDecl>(D))
return QualType(TD->getTypeForDecl(), 0);

return {};
}
} // namespace

TCppType_t GetType(const std::string& name) {
QualType builtin = findBuiltinType(name, getASTContext());
if (!builtin.isNull())
return builtin.getAsOpaquePtr();

auto* D = (Decl*)GetNamed(name, /* Within= */ 0);
if (auto* TD = llvm::dyn_cast_or_null<TypeDecl>(D)) {
return QualType(TD->getTypeForDecl(), 0).getAsOpaquePtr();
}

return (TCppType_t)0;
return GetTypeFromScope((Decl*)GetNamed(name, /*Within=*/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: argument name 'Within' in comment does not match parameter name 'parent' [bugprone-argument-comment]

  return GetTypeFromScope((Decl*)GetNamed(name, /*Within=*/nullptr));
                                                ^
Additional context

include/CppInterOp/CppInterOp.h:489: 'parent' declared here

                                    TCppScope_t parent = nullptr);
                                                ^

lib/CppInterOp/CppInterOp.cpp:779: actual callee ('GetNamed') is declared here

TCppScope_t GetNamed(const std::string& name,
            ^

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]

  return GetTypeFromScope((Decl*)GetNamed(name, /*Within=*/nullptr));
                          ^

}

TCppType_t GetComplexType(TCppType_t type) {
Expand All @@ -1974,17 +1985,12 @@

TCppType_t GetTypeFromScope(TCppScope_t klass) {
if (!klass)
return 0;

auto* D = (Decl*)klass;

if (auto* VD = dyn_cast<ValueDecl>(D))
return VD->getType().getAsOpaquePtr();
return nullptr;

if (auto* TD = dyn_cast<TypeDecl>(D))
return getASTContext().getTypeDeclType(TD).getAsOpaquePtr();
if (auto QT = GetTypeInternal((Decl*)klass))

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]

  if (auto QT = GetTypeInternal((Decl*)klass))
                                ^

return QT->getAsOpaquePtr();

return (TCppType_t) nullptr;
return nullptr;
}

// Internal functions that are not needed outside the library are
Expand Down
13 changes: 13 additions & 0 deletions unittests/CppInterOp/ScopeReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@ using namespace TestUtils;
using namespace llvm;
using namespace clang;

TYPED_TEST(CPPINTEROP_TEST_MODE, ScopeReflection_GetTypeOfTypedef) {
std::string code = R"(
typedef int Type_t;
)";

std::vector<Decl*> Decls;
GetAllTopLevelDecls(code, Decls);
auto Ty = Cpp::GetType("Type_t");

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: 'auto Ty' can be declared as 'auto *Ty' [llvm-qualified-auto]

Suggested change
auto Ty = Cpp::GetType("Type_t");
auto *Ty = Cpp::GetType("Type_t");

EXPECT_TRUE(Ty);
EXPECT_TRUE(Ty == Cpp::GetTypeFromScope(Decls[0]));
EXPECT_FALSE(Cpp::GetTypeFromScope(nullptr));
}

TYPED_TEST(CPPINTEROP_TEST_MODE, ScopeReflection_IsEnumScope) {
std::vector<Decl *> Decls;
std::vector<Decl *> SubDecls;
Expand Down
Loading