Commit 98a167c0 authored by Marco Bubke's avatar Marco Bubke
Browse files

Clang: Fix template renaming



Change-Id: I8040fe8dfc99d66e242ce2ff8589aa914838bfc9
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
parent fe3a7d64
......@@ -59,17 +59,15 @@ public:
return true;
}
bool shouldVisitImplicitCode() const
{
return true;
}
bool VisitNamedDecl(const clang::NamedDecl *declaration)
{
auto name = declaration->getNameAsString();
return setResultIfCursorIsInBetween(declaration, declaration->getLocation(),
declaration->getNameAsString().length());
bool notFound = setResultIfCursorIsInBetween(declaration,
declaration->getLocation(),
declaration->getNameAsString().length());
return true;
}
bool VisitDeclRefExpr(const clang::DeclRefExpr *expression)
......@@ -77,16 +75,18 @@ public:
if (!iterateNestedNameSpecifierLocation(expression->getQualifierLoc()))
return false;
const auto *Decl = expression->getFoundDecl();
return setResultIfCursorIsInBetween(Decl, expression->getLocation(),
Decl->getNameAsString().length());
const auto *declaration = expression->getFoundDecl();
return setResultIfCursorIsInBetween(declaration,
expression->getLocation(),
declaration->getNameAsString().length());
}
bool VisitMemberExpr(const clang::MemberExpr *Expr)
bool VisitMemberExpr(const clang::MemberExpr *expression)
{
const auto *Decl = Expr->getFoundDecl().getDecl();
return setResultIfCursorIsInBetween(Decl, Expr->getMemberLoc(),
Decl->getNameAsString().length());
const auto *declaration = expression->getFoundDecl().getDecl();
return setResultIfCursorIsInBetween(declaration,
expression->getMemberLoc(),
declaration->getNameAsString().length());
}
std::vector<const clang::NamedDecl*> takeNamedDecl()
......@@ -156,7 +156,8 @@ private:
std::vector<const clang::NamedDecl*> namedDeclarations;
const clang::SourceManager &sourceManager;
const clang::SourceLocation cursorSourceLocation; // The location to find the NamedDecl.
const clang::SourceLocation cursorSourceLocation;
bool isTemplate = false;
};
inline
......
......@@ -49,10 +49,9 @@ namespace ClangBackEnd {
class FindLocationsOfUSRsASTVisitor : public clang::RecursiveASTVisitor<FindLocationsOfUSRsASTVisitor>
{
public:
explicit FindLocationsOfUSRsASTVisitor(std::vector<USRName> &unifiedSymbolResolutions)
explicit FindLocationsOfUSRsASTVisitor(const std::vector<USRName> &unifiedSymbolResolutions)
: unifiedSymbolResolutions(unifiedSymbolResolutions)
{
std::sort(unifiedSymbolResolutions.begin(), unifiedSymbolResolutions.end());
}
bool VisitNamedDecl(const clang::NamedDecl *declaration) {
......@@ -108,15 +107,17 @@ private:
bool containsUSR(const USRName &unifiedSymbolResolution)
{
return std::binary_search(unifiedSymbolResolutions.cbegin(),
unifiedSymbolResolutions.cend(),
unifiedSymbolResolution);
auto found = std::find(unifiedSymbolResolutions.cbegin(),
unifiedSymbolResolutions.cend(),
unifiedSymbolResolution);
return found != unifiedSymbolResolutions.cend();
}
private:
// All the locations of the USR were found.
std::vector<USRName> unifiedSymbolResolutions;
const std::vector<USRName> unifiedSymbolResolutions;
std::vector<clang::SourceLocation> foundLocations;
};
......
......@@ -2,3 +2,19 @@ int variable;
int x = variable + 3;
template <typename TemplateName>
class TemplateKlasse {
public:
TemplateName value;
};
void function()
{
TemplateKlasse<int> instance;
instance.value += 5;
auto l = [x=instance] {
int k = x.value + 3;
};
}
......@@ -94,17 +94,29 @@ TEST(SymbolFinder, FindNameInUnsavedFile)
TEST(SymbolFinder, FindUsrs)
{
SymbolFinder finder(1, 5);
finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "int variable;", {"cc", "renamevariable.cpp"});
finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "int variable;", {"cc", "renamevariable.cpp", "-std=c++14"});
finder.findSymbol();
ASSERT_THAT(finder.unifiedSymbolResolutions().front(), StrEq("c:@variable"));
}
TEST(SymbolFinder, SourceLocations)
TEST(SymbolFinder, VariableDeclarationSourceLocations)
{
SymbolFinder finder(1, 5);
finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "", {"cc", "renamevariable.cpp"});
finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "", {"cc", "renamevariable.cpp", "-std=c++14"});
finder.findSymbol();
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
AllOf(Contains(IsSourceLocation(1, 5)),
Contains(IsSourceLocation(3, 9))));
}
TEST(SymbolFinder, VariableUsageSourceLocations)
{
SymbolFinder finder(3, 9);
finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "", {"cc", "renamevariable.cpp", "-std=c++14"});
finder.findSymbol();
......@@ -113,6 +125,45 @@ TEST(SymbolFinder, SourceLocations)
Contains(IsSourceLocation(3, 9))));
}
TEST(SymbolFinder, TemplateMemberVariableDeclarationSourceLocations)
{
SymbolFinder finder(8, 18);
finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "", {"cc", "renamevariable.cpp", "-std=c++14"});
finder.findSymbol();
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
AllOf(Contains(IsSourceLocation(8, 18)),
Contains(IsSourceLocation(15, 14)),
Contains(IsSourceLocation(18, 19))));
}
TEST(SymbolFinder, TemplateMemberVariableUsageSourceLocations)
{
SymbolFinder finder(15, 14);
finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "", {"cc", "renamevariable.cpp", "-std=c++14"});
finder.findSymbol();
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
AllOf(Contains(IsSourceLocation(8, 18)),
Contains(IsSourceLocation(15, 14)),
Contains(IsSourceLocation(18, 19))));
}
TEST(SymbolFinder, TemplateMemberVariableUsageInLambdaSourceLocations)
{
SymbolFinder finder(18, 19);
finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "", {"cc", "renamevariable.cpp", "-std=c++14"});
finder.findSymbol();
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
AllOf(Contains(IsSourceLocation(8, 18)),
Contains(IsSourceLocation(15, 14)),
Contains(IsSourceLocation(18, 19))));
}
TEST(SymbolFinder, CursorOverMacroDefintionSymbolName)
{
SymbolFinder finder(1, 9);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment