Skip to content

Commit

Permalink
Fixed #404: Unintentional swapping of array dimensions.
Browse files Browse the repository at this point in the history
While this fix addresses the original issue, the construction of an
array as shown is invalid. This could be something resolved by #406.
  • Loading branch information
andreasfertig committed Feb 5, 2022
1 parent 511bb4e commit c5f8383
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
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]();

0 comments on commit c5f8383

Please sign in to comment.