diff options
author | Marco Bubke <marco.bubke@qt.io> | 2016-08-04 19:55:54 +0200 |
---|---|---|
committer | Marco Bubke <marco.bubke@qt.io> | 2016-08-08 13:50:25 +0000 |
commit | 98a167c0d51fb2f4f79f3203050ca8087abf955a (patch) | |
tree | ba3bc3989e1792ceeb076cf5568fd1a379b43925 | |
parent | fe3a7d6479884089bcb7d47823d086e0e74c1c30 (diff) | |
download | qt-creator-98a167c0d51fb2f4f79f3203050ca8087abf955a.tar.gz |
Clang: Fix template renaming
Change-Id: I8040fe8dfc99d66e242ce2ff8589aa914838bfc9
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
-rw-r--r-- | src/tools/clangrefactoringbackend/source/findcursorusr.h | 31 | ||||
-rw-r--r-- | src/tools/clangrefactoringbackend/source/findlocationsofusrs.h | 13 | ||||
-rw-r--r-- | tests/unit/unittest/data/renamevariable.cpp | 16 | ||||
-rw-r--r-- | tests/unit/unittest/symbolfindertest.cpp | 57 |
4 files changed, 93 insertions, 24 deletions
diff --git a/src/tools/clangrefactoringbackend/source/findcursorusr.h b/src/tools/clangrefactoringbackend/source/findcursorusr.h index 563f1d5816..9e7fec9eee 100644 --- a/src/tools/clangrefactoringbackend/source/findcursorusr.h +++ b/src/tools/clangrefactoringbackend/source/findcursorusr.h @@ -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 diff --git a/src/tools/clangrefactoringbackend/source/findlocationsofusrs.h b/src/tools/clangrefactoringbackend/source/findlocationsofusrs.h index 480709f260..9362d9c2f8 100644 --- a/src/tools/clangrefactoringbackend/source/findlocationsofusrs.h +++ b/src/tools/clangrefactoringbackend/source/findlocationsofusrs.h @@ -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; }; diff --git a/tests/unit/unittest/data/renamevariable.cpp b/tests/unit/unittest/data/renamevariable.cpp index 0738d32dc1..638fddf56c 100644 --- a/tests/unit/unittest/data/renamevariable.cpp +++ b/tests/unit/unittest/data/renamevariable.cpp @@ -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; + }; +} diff --git a/tests/unit/unittest/symbolfindertest.cpp b/tests/unit/unittest/symbolfindertest.cpp index 75f1cf803b..9d8ac2ed94 100644 --- a/tests/unit/unittest/symbolfindertest.cpp +++ b/tests/unit/unittest/symbolfindertest.cpp @@ -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); |