-
Notifications
You must be signed in to change notification settings - Fork 60
Teach GetType and friends about typedef names. #885
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1951,19 +1951,30 @@ | |
| */ | ||
| return QualType(); | ||
| } | ||
| static std::optional<QualType> GetTypeInternal(Decl* D) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 {}; | ||
| // 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)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 contextinclude/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,
^
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
|
@@ -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)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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"); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
| 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; | ||||||
|
|
||||||
There was a problem hiding this comment.
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]