Skip to content

Commit

Permalink
Merge pull request #285 from andreasfertig/fixInitializerListExpansion
Browse files Browse the repository at this point in the history
Fixed std::initializer_list expansion bug when a return is before the
  • Loading branch information
andreasfertig authored Jan 8, 2020
2 parents 45fbd92 + 1d09726 commit c10540b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
20 changes: 12 additions & 8 deletions CodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2695,14 +2695,16 @@ void CodeGenerator::InsertArg(const SizeOfPackExpr* stmt)
void CodeGenerator::InsertArg(const ReturnStmt* stmt)
{
LAMBDA_SCOPE_HELPER(ReturnStmt);
UpdateCurrentPos();
mCurrentReturnPos = mOutputFormatHelper.CurrentPos();

mOutputFormatHelper.Append("return");

if(const auto* retVal = stmt->getRetValue()) {
mOutputFormatHelper.Append(' ');
InsertArg(retVal);
}

mCurrentReturnPos.reset();
}
//-----------------------------------------------------------------------------

Expand All @@ -2721,19 +2723,16 @@ void CodeGenerator::InsertArg(const CXXDefaultArgExpr* stmt)
void CodeGenerator::InsertArg(const CXXStdInitializerListExpr* stmt)
{
if(GetInsightsOptions().UseShowInitializerList) {
if(not mCurrentPos.hasValue() && not mCurrentFieldPos.hasValue()) {
if(not mCurrentPos.hasValue() && not mCurrentFieldPos.hasValue() && not mCurrentReturnPos.hasValue()) {
return;
}

std::string modifiers{};

size_t variableInsertPos = mCurrentPos.getValueOr(0);
size_t argumentInsertPos = mCurrentPos.getValueOr(0);
size_t variableInsertPos = mCurrentReturnPos.getValueOr(mCurrentPos.getValueOr(0));

auto& ofmToInsert = [&]() -> decltype(auto) {
if(not mCurrentPos.hasValue()) {
if(not mCurrentPos.hasValue() && not mCurrentReturnPos.hasValue()) {
variableInsertPos = mCurrentFieldPos.getValueOr(0);
argumentInsertPos = mCurrentPos.getValueOr(0);
mCurrentPos = variableInsertPos;
modifiers = kwStaticSpace;
modifiers += kwInlineSpace;
Expand Down Expand Up @@ -2768,7 +2767,12 @@ void CodeGenerator::InsertArg(const CXXStdInitializerListExpr* stmt)
// are not allowed there.
mOutputFormatHelper.Append(GetName(stmt->getType(), Unqualified::Yes), "{", internalListName, ", ", size, "}");

mCurrentPos = mCurrentPos.getValue() + ofm.GetString().size();
if(mCurrentReturnPos.hasValue()) {
mCurrentReturnPos = mCurrentReturnPos.getValue() + ofm.GetString().size();
} else {
mCurrentPos = mCurrentPos.getValue() + ofm.GetString().size();
}

} else {
// No qualifiers like const or volatile here. This appears in function calls or operators as a parameter. CV's
// are not allowed there.
Expand Down
10 changes: 6 additions & 4 deletions CodeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,10 +300,12 @@ class CodeGenerator
static constexpr auto MAX_FILL_VALUES_FOR_ARRAYS{
uint64_t{100}}; //!< This is the upper limit of elements which will be shown for an array when filled by \c
//!< FillConstantArray.
llvm::Optional<size_t> mCurrentPos{}; //!< The position in mOutputFormatHelper where a potential
//!< std::initializer_list expansion must be inserted.
llvm::Optional<size_t> mCurrentFieldPos{}; //!< The position in mOutputFormatHelper in a class where where a
//!< potential std::initializer_list expansion must be inserted.
llvm::Optional<size_t> mCurrentPos{}; //!< The position in mOutputFormatHelper where a potential
//!< std::initializer_list expansion must be inserted.
llvm::Optional<size_t> mCurrentReturnPos{}; //!< The position in mOutputFormatHelper from a return where a
//!< potential std::initializer_list expansion must be inserted.
llvm::Optional<size_t> mCurrentFieldPos{}; //!< The position in mOutputFormatHelper in a class where where a
//!< potential std::initializer_list expansion must be inserted.
OutputFormatHelper* mOutputFormatHelperOutside{
nullptr}; //!< Helper output buffer for std::initializer_list expansion.
};
Expand Down
9 changes: 9 additions & 0 deletions tests/StdInitializerList5Test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Source: https://twitter.com/olafurw/status/1214927552990601217?s=20
#include <initializer_list>

int f(std::initializer_list<int> l) {}

int main()
{
return f({1, 2, 3});
}
13 changes: 13 additions & 0 deletions tests/StdInitializerList5Test.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Source: https://twitter.com/olafurw/status/1214927552990601217?s=20
#include <initializer_list>

int f(std::initializer_list<int> l)
{
}


int main()
{
return f(std::initializer_list<int>{1, 2, 3});
}

0 comments on commit c10540b

Please sign in to comment.