Skip to content

Commit

Permalink
Merge pull request #47 from andreasfertig/fixIssue46
Browse files Browse the repository at this point in the history
Fixed issue #46: Lambda's in global scope.
  • Loading branch information
Andreas Fertig authored Jul 15, 2018
2 parents e74fd6d + f3c9745 commit 80260e6
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 26 deletions.
38 changes: 15 additions & 23 deletions AutoStmtHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
****************************************************************************/

#include "AutoStmtHandler.h"
#include "CodeGenerator.h"
#include "InsightsHelpers.h"
#include "InsightsMatchers.h"
#include "InsightsStaticStrings.h"
Expand All @@ -21,9 +22,18 @@ namespace clang::insights {
AutoStmtHandler::AutoStmtHandler(Rewriter& rewrite, MatchFinder& matcher)
: InsightsBase(rewrite)
{

static const auto isAutoAncestor =
hasAncestor(varDecl(anyOf(hasType(autoType().bind("autoType")),
hasType(qualType(hasDescendant(autoType().bind("autoType")))),
/* decltype and decltype(auto) */
hasType(decltypeType().bind("dt")),
hasType(qualType(hasDescendant(decltypeType().bind("dt")))))));

matcher.addMatcher(varDecl(unless(anyOf(isExpansionInSystemHeader(),
isMacroOrInvalidLocation(),
decompositionDecl(),
isAutoAncestor,
/* don't replace auto in templates */
isTemplate,
hasAncestor(functionDecl()))),
Expand All @@ -41,36 +51,18 @@ AutoStmtHandler::AutoStmtHandler(Rewriter& rewrite, MatchFinder& matcher)
void AutoStmtHandler::run(const MatchFinder::MatchResult& result)
{
if(const auto* autoDecl = result.Nodes.getNodeAs<VarDecl>("autoDecl")) {
const QualType type = [&]() {
if(const auto* declType = result.Nodes.getNodeAs<DecltypeType>("dt")) {
return declType->getUnderlyingType();
}

return autoDecl->getType();
}();

const std::string fqn{[&]() {
if(autoDecl->getType()->isFunctionPointerType()) {
const auto lineNo = result.SourceManager->getSpellingLineNumber(autoDecl->getSourceRange().getBegin());

const std::string funcPtrName{StrCat("FuncPtr_", std::to_string(lineNo))};
std::string usingStr{StrCat("using ", funcPtrName, " = ", GetName(type), ";\n ", funcPtrName)};

return StrCat((autoDecl->isConstexpr() ? kwConstExprSpace : ""), usingStr);

} else {
return StrCat((autoDecl->isConstexpr() ? kwConstExprSpace : ""), GetName(type));
}
}()};
OutputFormatHelper outputFormatHelper{};
CodeGenerator codeGenerator{outputFormatHelper};
codeGenerator.InsertArg(autoDecl);

// constexpr int* x = 5;
// ^ ^ ^
// 1 2 3
// the SourceRange starts at (1) end ends at (3). The Location in the other hand starts at (1) and ends
// at (2)
const SourceRange sr{autoDecl->getSourceRange().getBegin(), autoDecl->getLocation().getLocWithOffset(-1)};
const auto sr = GetSourceRangeAfterToken(autoDecl->getSourceRange(), tok::semi, result);

mRewrite.ReplaceText(sr, fqn);
mRewrite.ReplaceText(sr, outputFormatHelper.GetString());
}
}
//-----------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion CompilerGeneratedHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ CompilerGeneratedHandler::CompilerGeneratedHandler(Rewriter& rewrite, MatchFinde
isTemplate,
hasAncestor(functionDecl()),
isMacroOrInvalidLocation())),
hasParent(cxxRecordDecl().bind("record")));
hasParent(cxxRecordDecl(unless(isLambda())).bind("record")));

matcher.addMatcher(cxxMethodDecl(compilerProvided).bind("method"), this);
}
Expand Down
2 changes: 2 additions & 0 deletions FunctionDeclHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ FunctionDeclHandler::FunctionDeclHandler(Rewriter& rewrite, MatchFinder& matcher
: InsightsBase(rewrite)
{
matcher.addMatcher(functionDecl(unless(anyOf(cxxMethodDecl(unless(isUserProvided())),
cxxMethodDecl(hasParent(cxxRecordDecl(isLambda()))),
isExpansionInSystemHeader(),
isTemplate,
hasAncestor(friendDecl()), // friendDecl has functionDecl as child
Expand All @@ -36,6 +37,7 @@ FunctionDeclHandler::FunctionDeclHandler(Rewriter& rewrite, MatchFinder& matcher
hasDescendant(classTemplateSpecializationDecl()));

matcher.addMatcher(friendDecl(unless(anyOf(cxxMethodDecl(unless(isUserProvided())),
cxxMethodDecl(hasParent(cxxRecordDecl(isLambda()))),
isExpansionInSystemHeader(),
isTemplate,
hasTemplateDescendant,
Expand Down
6 changes: 4 additions & 2 deletions tests/AutoHandler7Test.expect
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ const char * Foo()
}


int x = 22;
static int x = 22;


static const char * c = Foo();

const char * c = Foo();


int main()
Expand Down
10 changes: 10 additions & 0 deletions tests/Issue46.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
auto x = []() {
return 5;
};

auto x2 = [a=5](int b) {
return b*a;
};

int main()
{}
37 changes: 37 additions & 0 deletions tests/Issue46.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

class __lambda_1_10
{
public: inline /*constexpr */ int operator()() const
{
return 5;
}

};

__lambda_1_10 x = __lambda_1_10{};



class __lambda_5_11
{
public: inline /*constexpr */ int operator()(int b) const
{
return b * a;
}

private:
int a;

public: __lambda_5_11(int _a)
: a{_a}
{}

};

__lambda_5_11 x2 = __lambda_5_11{5};


int main()
{
}

0 comments on commit 80260e6

Please sign in to comment.