Skip to content
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

Fixed #404: Unintentional swapping of array dimensions. #445

Merged
merged 1 commit into from
Feb 5, 2022
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
21 changes: 18 additions & 3 deletions InsightsHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,9 @@ class SimpleTypePrinter
std::string mDataAfter{};
bool mHasData{false};
bool mSkipSpace{false};
std::string mScope{}; //!< A scope coming from an ElaboratedType which is used for a
//!< ClassTemplateSpecializationDecl if there is no other scope
bool mScanningArrayDimension{}; //!< Only the outer most ConstantArrayType handles the array dimensions and size
std::string mScope{}; //!< A scope coming from an ElaboratedType which is used for a
//!< ClassTemplateSpecializationDecl if there is no other scope

bool HandleType(const TemplateTypeParmType* type)
{
Expand Down Expand Up @@ -735,9 +736,23 @@ class SimpleTypePrinter

bool HandleType(const ConstantArrayType* type)
{
// Only the outer most ConstantArrayType generates the aary dimensions, block others.
bool scanningArrayDimension = false;
if(not mScanningArrayDimension) {
mScanningArrayDimension = true;
scanningArrayDimension = true;
}

const bool ret = HandleType(type->getElementType().getTypePtrOrNull());

mData.Append("["sv, type->getSize().getZExtValue(), "]"sv);
// Handle the array dimension after the type has been parsed.
if(scanningArrayDimension) {
do {
mData.Append("["sv, type->getSize().getZExtValue(), "]"sv);
} while((type = dyn_cast_or_null<ConstantArrayType>(type->getElementType().getTypePtrOrNull())));

mScanningArrayDimension = false;
}

return ret;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Issue404.cerr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.tmp.cpp:22:21: error: 'S' does not refer to a value
S arr2[3][2] = S [3][2]();
^
.tmp.cpp:1:8: note: declared here
struct S
^
.tmp.cpp:24:27: error: expected '(' for function-style cast or type construction
T<int> arr3[3][2] = T<int>[3][2]();
~~~~~~^
2 errors generated.
6 changes: 6 additions & 0 deletions tests/Issue404.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
struct S {};
template<typename> struct T {};

int arr1[3][2];
S arr2[3][2];
T<int> arr3[3][2];
25 changes: 25 additions & 0 deletions tests/Issue404.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
struct S
{
// inline constexpr S() noexcept = default;
};


template<typename> struct T {};

/* First instantiated from: Issue404.cpp:6 */
#ifdef INSIGHTS_USE_TEMPLATE
template<>
struct T<int>
{
// inline constexpr T() noexcept = default;
};

#endif


int arr1[3][2];

S arr2[3][2] = S [3][2]();

T<int> arr3[3][2] = T<int>[3][2]();