2016-08-04 15:26:53 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2016-09-15 17:41:41 +02:00
|
|
|
#include "googletest.h"
|
2017-09-21 11:43:59 +02:00
|
|
|
#include "mockfilepathcaching.h"
|
2016-09-15 17:41:41 +02:00
|
|
|
|
2016-08-04 15:26:53 +02:00
|
|
|
#include <clangrefactoringbackend_global.h>
|
|
|
|
|
#include <symbolfinder.h>
|
|
|
|
|
|
2016-09-15 17:41:41 +02:00
|
|
|
#include "filesystem-utilities.h"
|
2016-08-04 15:26:53 +02:00
|
|
|
|
|
|
|
|
using ClangBackEnd::SymbolFinder;
|
|
|
|
|
using ClangBackEnd::FileContent;
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
MATCHER_P2(IsSourceLocation, line, column,
|
|
|
|
|
std::string(negation ? "isn't" : "is")
|
2016-11-15 15:38:12 +01:00
|
|
|
+ "line " + PrintToString(line)
|
2016-08-04 15:26:53 +02:00
|
|
|
+ ", column " + PrintToString(column)
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
if (arg.line() != uint(line)
|
|
|
|
|
|| arg.column() != uint(column))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MATCHER_P(StrEq, text,
|
|
|
|
|
std::string(negation ? "isn't" : "is")
|
|
|
|
|
+ " text " + PrintToString(text)
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
return std::string(arg.data(), arg.size()) == std::string(text);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
using Finder = SymbolFinder;
|
|
|
|
|
|
|
|
|
|
class SymbolFinder : public testing::Test
|
|
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
NiceMock<MockFilePathCaching> filePathCaching;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using SymbolFinderSlowTest = SymbolFinder;
|
|
|
|
|
|
|
|
|
|
TEST_F(SymbolFinder, FileContentFilePath)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
|
|
|
|
FileContent fileContent(toNativePath("/tmp"), "data.cpp", "int variable;", {"cc", "data.cpp"});
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(fileContent.filePath, toNativePath("/tmp/data.cpp"));
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
TEST_F(SymbolFinderSlowTest, FindName)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
Finder finder(1, 5, filePathCaching);
|
2016-08-04 15:26:53 +02:00
|
|
|
finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "int variable;", {"cc", "renamevariable.cpp"});
|
|
|
|
|
|
|
|
|
|
finder.findSymbol();
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(finder.takeSymbolName(), "variable");
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
TEST_F(SymbolFinderSlowTest, FindNameInUnsavedFile)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
Finder finder(1, 5, filePathCaching);
|
2016-08-04 15:26:53 +02:00
|
|
|
finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "int newVariable;", {"cc", "renamevariable.cpp"});
|
|
|
|
|
|
|
|
|
|
finder.findSymbol();
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(finder.takeSymbolName(), "newVariable");
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
TEST_F(SymbolFinderSlowTest, FindUsrs)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
Finder finder(1, 5, filePathCaching);
|
2016-08-04 19:55:54 +02:00
|
|
|
finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "int variable;", {"cc", "renamevariable.cpp", "-std=c++14"});
|
2016-08-04 15:26:53 +02:00
|
|
|
|
|
|
|
|
finder.findSymbol();
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(finder.unifiedSymbolResolutions().front(), StrEq("c:@variable"));
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
TEST_F(SymbolFinderSlowTest, VariableDeclarationSourceLocations)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
Finder finder(1, 5, filePathCaching);
|
2016-08-04 19:55:54 +02:00
|
|
|
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))));
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
TEST_F(SymbolFinderSlowTest, VariableUsageSourceLocations)
|
2016-08-04 19:55:54 +02:00
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
Finder finder(3, 9, filePathCaching);
|
2016-08-04 19:55:54 +02:00
|
|
|
finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "", {"cc", "renamevariable.cpp", "-std=c++14"});
|
2016-08-04 15:26:53 +02:00
|
|
|
|
|
|
|
|
finder.findSymbol();
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
|
|
|
|
|
AllOf(Contains(IsSourceLocation(1, 5)),
|
|
|
|
|
Contains(IsSourceLocation(3, 9))));
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
TEST_F(SymbolFinderSlowTest, TemplateMemberVariableDeclarationSourceLocations)
|
2016-08-04 19:55:54 +02:00
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
Finder finder(8, 18, filePathCaching);
|
2016-08-04 19:55:54 +02:00
|
|
|
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))));
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
TEST_F(SymbolFinderSlowTest, TemplateMemberVariableUsageSourceLocations)
|
2016-08-04 19:55:54 +02:00
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
Finder finder(15, 14, filePathCaching);
|
2016-08-04 19:55:54 +02:00
|
|
|
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))));
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
TEST_F(SymbolFinderSlowTest, TemplateMemberVariableUsageInLambdaSourceLocations)
|
2016-08-04 19:55:54 +02:00
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
Finder finder(18, 19, filePathCaching);
|
2016-08-04 19:55:54 +02:00
|
|
|
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))));
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
TEST_F(SymbolFinderSlowTest, CursorOverMacroDefintionSymbolName)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
Finder finder(1, 9, filePathCaching);
|
2016-08-04 15:26:53 +02:00
|
|
|
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
|
|
|
|
|
|
|
|
|
|
finder.findSymbol();
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(finder.takeSymbolName(), "Macro");
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
TEST_F(SymbolFinderSlowTest, CursorOverMacroExpansionSymbolName)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
Finder finder(10, 10, filePathCaching);
|
2016-08-04 15:26:53 +02:00
|
|
|
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
|
|
|
|
|
|
|
|
|
|
finder.findSymbol();
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(finder.takeSymbolName(), "Macro");
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
TEST_F(SymbolFinderSlowTest, FindMacroDefinition)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
Finder finder(1, 9, filePathCaching);
|
2016-08-04 15:26:53 +02:00
|
|
|
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
|
|
|
|
|
|
|
|
|
|
finder.findSymbol();
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
|
|
|
|
|
Contains(IsSourceLocation(1, 9)));
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
TEST_F(SymbolFinderSlowTest, FindMacroExpansion)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
Finder finder(1, 9, filePathCaching);
|
2016-08-04 15:26:53 +02:00
|
|
|
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
|
|
|
|
|
|
|
|
|
|
finder.findSymbol();
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
|
|
|
|
|
Contains(IsSourceLocation(5, 17)));
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
TEST_F(SymbolFinderSlowTest, DoNotFindUndedefinedMacroExpansion)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
Finder finder(1, 9, filePathCaching);
|
2016-08-04 15:26:53 +02:00
|
|
|
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
|
|
|
|
|
|
|
|
|
|
finder.findSymbol();
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
|
|
|
|
|
Not(Contains(IsSourceLocation(10, 10))));
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
TEST_F(SymbolFinderSlowTest, FindMacroDefinitionFromMacroExpansion)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
Finder finder(10, 10, filePathCaching);
|
2016-08-04 15:26:53 +02:00
|
|
|
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
|
|
|
|
|
|
|
|
|
|
finder.findSymbol();
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
|
|
|
|
|
Contains(IsSourceLocation(8, 9)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
TEST_F(SymbolFinderSlowTest, FindMacroExpansionBeforeMacroExpansionWithCursor)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
Finder finder(12, 10, filePathCaching);
|
2016-08-04 15:26:53 +02:00
|
|
|
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
|
|
|
|
|
|
|
|
|
|
finder.findSymbol();
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
|
|
|
|
|
Contains(IsSourceLocation(10, 10)));
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-21 11:43:59 +02:00
|
|
|
TEST_F(SymbolFinderSlowTest, FindMacroExpansionAfterMacroExpansionWithCursor)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
2017-09-21 11:43:59 +02:00
|
|
|
Finder finder(10, 10, filePathCaching);
|
2016-08-04 15:26:53 +02:00
|
|
|
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
|
|
|
|
|
|
|
|
|
|
finder.findSymbol();
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
|
|
|
|
|
Contains(IsSourceLocation(12, 10)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|