Skip to content

Commit

Permalink
Merge pull request #287 from andreasfertig/fixIssue286
Browse files Browse the repository at this point in the history
Fixed #286: Desugar decltype(x) even when it is an LValueReference.
  • Loading branch information
andreasfertig authored Jan 15, 2020
2 parents c10540b + 9bcbc4e commit 626830d
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 37 deletions.
62 changes: 30 additions & 32 deletions InsightsHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,20 @@ class SimpleTypePrinter
return ret;
}

bool HandleType(const DecltypeType* type)
{
const bool skipSpace{mSkipSpace};
mSkipSpace = true;

HandleType(type->desugar().getTypePtrOrNull());

mSkipSpace = skipSpace;

// if we hit a DecltypeType always use the expanded version to support things like a DecltypeType wrapped in an
// LValueReferenceType
return true;
}

bool HandleType(const Type* type)
{
#define HANDLE_TYPE(t) \
Expand Down Expand Up @@ -744,6 +758,7 @@ class SimpleTypePrinter
HANDLE_TYPE(InjectedClassNameType);
HANDLE_TYPE(DependentTemplateSpecializationType);
HANDLE_TYPE(PackExpansionType);
HANDLE_TYPE(DecltypeType);

#undef HANDLE_TYPE
return false;
Expand Down Expand Up @@ -789,16 +804,29 @@ class SimpleTypePrinter
AddCVQualifiers(splitted.Quals);
}

mHasData = HandleType(mType.getTypePtrOrNull());
const auto* typePtr = mType.getTypePtrOrNull();
mHasData = HandleType(typePtr);
mData.Append(mDataAfter);

// Take care of 'char* const'
if(mType.getQualifiers().hasFastQualifiers()) {
const QualType fastQualifierType{typePtr, mType.getQualifiers().getFastQualifiers()};

mSkipSpace = true;
AddCVQualifiers(fastQualifierType.getCanonicalType()->getPointeeType().getLocalQualifiers());
}

return mHasData;
}
};
//-----------------------------------------------------------------------------

static std::string GetNameInternal(const QualType& t, const CppInsightsPrintingPolicy& printingPolicy)
static std::string GetName(const QualType& t,
const Unqualified unqualified = Unqualified::No,
const InsightsSuppressScope supressScope = InsightsSuppressScope::No)
{
const CppInsightsPrintingPolicy printingPolicy{unqualified, supressScope};

if(SimpleTypePrinter st{t, printingPolicy}; st.GetTypeString()) {
return ScopeHandler::RemoveCurrentScope(st.GetString());

Expand All @@ -808,36 +836,6 @@ static std::string GetNameInternal(const QualType& t, const CppInsightsPrintingP

return ScopeHandler::RemoveCurrentScope(GetAsCPPStyleString(t, printingPolicy));
}
//-----------------------------------------------------------------------------

static bool IsDecltypeType(const QualType& t)
{
if(t.getTypePtrOrNull()) {
if(isa<clang::DecltypeType>(t)) {
return true;
}
}

return false;
}
//-----------------------------------------------------------------------------

static std::string GetName(const QualType& t,
const Unqualified unqualified = Unqualified::No,
const InsightsSuppressScope supressScope = InsightsSuppressScope::No)
{
const auto desugaredType = GetDesugarType(t);
const auto* autoType = desugaredType->getContainedAutoType();
const bool isAutoType{autoType && autoType->isSugared()};
const CppInsightsPrintingPolicy printingPolicy{unqualified, supressScope};

// Handle decltype(var)
if(not isAutoType && IsDecltypeType(t)) {
return GetNameInternal(desugaredType, printingPolicy);
}

return GetNameInternal(t, printingPolicy);
}
} // namespace details
//-----------------------------------------------------------------------------

Expand Down
4 changes: 2 additions & 2 deletions tests/AutoHandlerTest.expect
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ int main()
float f = 1.0F;
char c = 'c';
unsigned int u = 0U;
decltype(u) uu = u;
unsigned int uu = u;
unsigned int mu = 0U;
decltype(u) muu = u;
unsigned int muu = u;
}


2 changes: 1 addition & 1 deletion tests/Issue255.expect
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ void test()
for(; __begin1.operator!=(__end1); __begin1.operator++())
{
int element = __begin1.operator*();
/* PASSED: static_assert(std::is_same_v<decltype(element), int>); */
/* PASSED: static_assert(std::is_same_v<int, int>); */
}

}
Expand Down
8 changes: 8 additions & 0 deletions tests/Issue286.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <vector>
struct S {
std::vector<int> v{};
decltype(v)& getV () {
return v;
}
};

13 changes: 13 additions & 0 deletions tests/Issue286.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <vector>
struct S
{
std::vector<int, std::allocator<int> > v = std::vector<int, std::allocator<int> >{};
inline std::vector<int, std::allocator<int> > & getV()
{
return this->v;
}

};



9 changes: 9 additions & 0 deletions tests/Issue286_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
int main()
{
int x= 2;

decltype(x)& y = x;

//return y;
}

7 changes: 7 additions & 0 deletions tests/Issue286_2.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
int main()
{
int x = 2;
int & y = x;
}


4 changes: 2 additions & 2 deletions tests/StructuredBindingsHandler3Test.expect
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace std
}
namespace std
{
template<size_t , typename type_parameter_0_1>
template<unsigned long , typename type_parameter_0_1>
struct tuple_element;
/* First instantiated from: StructuredBindingsHandler3Test.cpp:17 */
#ifdef INSIGHTS_USE_TEMPLATE
Expand Down Expand Up @@ -94,7 +94,7 @@ struct std::tuple_size<constant::Q>
};


template<size_t N>
template<unsigned long N>
struct std::tuple_element<N, constant::Q>
{
using type = int;
Expand Down

0 comments on commit 626830d

Please sign in to comment.