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"
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
using testing::PrintToString;
|
|
|
|
|
using testing::AllOf;
|
|
|
|
|
using testing::Contains;
|
|
|
|
|
using testing::Not;
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST(SymbolFinder, FileContentFilePath)
|
|
|
|
|
{
|
|
|
|
|
FileContent fileContent(toNativePath("/tmp"), "data.cpp", "int variable;", {"cc", "data.cpp"});
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(fileContent.filePath, toNativePath("/tmp/data.cpp"));
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-04 11:39:17 +01:00
|
|
|
TEST(SymbolFinderSlowTest, FindName)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
|
|
|
|
SymbolFinder finder(1, 5);
|
|
|
|
|
finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "int variable;", {"cc", "renamevariable.cpp"});
|
|
|
|
|
|
|
|
|
|
finder.findSymbol();
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(finder.takeSymbolName(), "variable");
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-04 11:39:17 +01:00
|
|
|
TEST(SymbolFinderSlowTest, FindNameInUnsavedFile)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
|
|
|
|
SymbolFinder finder(1, 5);
|
|
|
|
|
finder.addFile(TESTDATA_DIR, "renamevariable.cpp", "int newVariable;", {"cc", "renamevariable.cpp"});
|
|
|
|
|
|
|
|
|
|
finder.findSymbol();
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(finder.takeSymbolName(), "newVariable");
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-04 11:39:17 +01:00
|
|
|
TEST(SymbolFinderSlowTest, FindUsrs)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
|
|
|
|
SymbolFinder finder(1, 5);
|
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-01-04 11:39:17 +01:00
|
|
|
TEST(SymbolFinderSlowTest, VariableDeclarationSourceLocations)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
|
|
|
|
SymbolFinder finder(1, 5);
|
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-01-04 11:39:17 +01:00
|
|
|
TEST(SymbolFinderSlowTest, VariableUsageSourceLocations)
|
2016-08-04 19:55:54 +02:00
|
|
|
{
|
|
|
|
|
SymbolFinder finder(3, 9);
|
|
|
|
|
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-01-04 11:39:17 +01:00
|
|
|
TEST(SymbolFinderSlowTest, TemplateMemberVariableDeclarationSourceLocations)
|
2016-08-04 19:55:54 +02:00
|
|
|
{
|
|
|
|
|
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))));
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-04 11:39:17 +01:00
|
|
|
TEST(SymbolFinderSlowTest, TemplateMemberVariableUsageSourceLocations)
|
2016-08-04 19:55:54 +02:00
|
|
|
{
|
|
|
|
|
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))));
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-04 11:39:17 +01:00
|
|
|
TEST(SymbolFinderSlowTest, TemplateMemberVariableUsageInLambdaSourceLocations)
|
2016-08-04 19:55:54 +02:00
|
|
|
{
|
|
|
|
|
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))));
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-04 11:39:17 +01:00
|
|
|
TEST(SymbolFinderSlowTest, CursorOverMacroDefintionSymbolName)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
|
|
|
|
SymbolFinder finder(1, 9);
|
|
|
|
|
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
|
|
|
|
|
|
|
|
|
|
finder.findSymbol();
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(finder.takeSymbolName(), "Macro");
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-04 11:39:17 +01:00
|
|
|
TEST(SymbolFinderSlowTest, CursorOverMacroExpansionSymbolName)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
|
|
|
|
SymbolFinder finder(10, 10);
|
|
|
|
|
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
|
|
|
|
|
|
|
|
|
|
finder.findSymbol();
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(finder.takeSymbolName(), "Macro");
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-04 11:39:17 +01:00
|
|
|
TEST(SymbolFinderSlowTest, FindMacroDefinition)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
|
|
|
|
SymbolFinder finder(1, 9);
|
|
|
|
|
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
|
|
|
|
|
|
|
|
|
|
finder.findSymbol();
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
|
|
|
|
|
Contains(IsSourceLocation(1, 9)));
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-04 11:39:17 +01:00
|
|
|
TEST(SymbolFinderSlowTest, FindMacroExpansion)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
|
|
|
|
SymbolFinder finder(1, 9);
|
|
|
|
|
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
|
|
|
|
|
|
|
|
|
|
finder.findSymbol();
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
|
|
|
|
|
Contains(IsSourceLocation(5, 17)));
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-04 11:39:17 +01:00
|
|
|
TEST(SymbolFinderSlowTest, DoNotFindUndedefinedMacroExpansion)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
|
|
|
|
SymbolFinder finder(1, 9);
|
|
|
|
|
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
|
|
|
|
|
|
|
|
|
|
finder.findSymbol();
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
|
|
|
|
|
Not(Contains(IsSourceLocation(10, 10))));
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-04 11:39:17 +01:00
|
|
|
TEST(SymbolFinderSlowTest, FindMacroDefinitionFromMacroExpansion)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
|
|
|
|
SymbolFinder finder(10, 10);
|
|
|
|
|
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
|
|
|
|
|
|
|
|
|
|
finder.findSymbol();
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
|
|
|
|
|
Contains(IsSourceLocation(8, 9)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-01-04 11:39:17 +01:00
|
|
|
TEST(SymbolFinderSlowTest, FindMacroExpansionBeforeMacroExpansionWithCursor)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
|
|
|
|
SymbolFinder finder(12, 10);
|
|
|
|
|
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
|
|
|
|
|
|
|
|
|
|
finder.findSymbol();
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
|
|
|
|
|
Contains(IsSourceLocation(10, 10)));
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-04 11:39:17 +01:00
|
|
|
TEST(SymbolFinderSlowTest, FindMacroExpansionAfterMacroExpansionWithCursor)
|
2016-08-04 15:26:53 +02:00
|
|
|
{
|
|
|
|
|
SymbolFinder finder(10, 10);
|
|
|
|
|
finder.addFile(TESTDATA_DIR, "symbolfinder_macro.cpp", "", {"cc", "symbolfinder_macro.cpp"});
|
|
|
|
|
|
|
|
|
|
finder.findSymbol();
|
|
|
|
|
|
|
|
|
|
ASSERT_THAT(finder.sourceLocations().sourceLocationContainers(),
|
|
|
|
|
Contains(IsSourceLocation(12, 10)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|