diff --git a/src/libs/clangsupport/clangsupport-lib.pri b/src/libs/clangsupport/clangsupport-lib.pri index 1c18e89870f..9e9ad5ccc7c 100644 --- a/src/libs/clangsupport/clangsupport-lib.pri +++ b/src/libs/clangsupport/clangsupport-lib.pri @@ -172,6 +172,7 @@ HEADERS += \ $$PWD/updatevisibletranslationunitsmessage.h \ $$PWD/writemessageblock.h \ $$PWD/ipcclientprovider.h \ + $$PWD/requestsourcerangesforquerymessage.h \ $$PWD/stringcachefwd.h contains(QT_CONFIG, reduce_exports):CONFIG += hide_symbols diff --git a/src/tools/clangrefactoringbackend/source/clangrefactoringbackend-source.pri b/src/tools/clangrefactoringbackend/source/clangrefactoringbackend-source.pri index 8af6b89974c..8a33f63ed2c 100644 --- a/src/tools/clangrefactoringbackend/source/clangrefactoringbackend-source.pri +++ b/src/tools/clangrefactoringbackend/source/clangrefactoringbackend-source.pri @@ -2,7 +2,14 @@ INCLUDEPATH += $$PWD HEADERS += \ $$PWD/clangrefactoringbackend_global.h \ - $$PWD/sourcerangefilter.h + $$PWD/sourcerangefilter.h \ + $$PWD/symbolscollector.h \ + $$PWD/symbolentry.h \ + $$PWD/collectsymbolsconsumer.h \ + $$PWD/collectsymbolsaction.h \ + $$PWD/collectmacrossourcefilecallbacks.h \ + $$PWD/collectsymbolsastvisitor.h \ + $$PWD/sourcelocationentry.h !isEmpty(LIBTOOLING_LIBS) { SOURCES += \ @@ -36,4 +43,9 @@ HEADERS += \ } SOURCES += \ - $$PWD/sourcerangefilter.cpp + $$PWD/sourcerangefilter.cpp \ + $$PWD/symbolscollector.cpp \ + $$PWD/collectsymbolsaction.cpp \ + $$PWD/collectmacrossourcefilecallbacks.cpp \ + $$PWD/symbolentry.cpp \ + $$PWD/sourcelocationentry.cpp diff --git a/src/tools/clangrefactoringbackend/source/clangtool.cpp b/src/tools/clangrefactoringbackend/source/clangtool.cpp index a5ea8838fca..f52106f3947 100644 --- a/src/tools/clangrefactoringbackend/source/clangtool.cpp +++ b/src/tools/clangrefactoringbackend/source/clangtool.cpp @@ -47,15 +47,15 @@ void ClangTool::addFile(std::string &&directory, std::string &&content, std::vector &&commandLine) { - fileContents.emplace_back(toNativePath(std::move(directory)), + m_fileContents.emplace_back(toNativePath(std::move(directory)), std::move(fileName), std::move(content), std::move(commandLine)); - const auto &fileContent = fileContents.back(); + const auto &fileContent = m_fileContents.back(); - compilationDatabase.addFile(fileContent.directory, fileContent.fileName, fileContent.commandLine); - sourceFilePaths.push_back(fileContent.filePath); + m_compilationDatabase.addFile(fileContent.directory, fileContent.fileName, fileContent.commandLine); + m_sourceFilePaths.push_back(fileContent.filePath); } template @@ -86,7 +86,7 @@ void ClangTool::addFiles(const Utils::PathStringVector void ClangTool::addUnsavedFiles(const V2::FileContainers &unsavedFiles) { - unsavedFileContents.reserve(unsavedFileContents.size() + unsavedFiles.size()); + m_unsavedFileContents.reserve(m_unsavedFileContents.size() + unsavedFiles.size()); auto convertToUnsavedFileContent = [] (const V2::FileContainer &unsavedFile) { return UnsavedFileContent{toNativePath(unsavedFile.filePath().path().clone()), @@ -95,7 +95,7 @@ void ClangTool::addUnsavedFiles(const V2::FileContainers &unsavedFiles) std::transform(unsavedFiles.begin(), unsavedFiles.end(), - std::back_inserter(unsavedFileContents), + std::back_inserter(m_unsavedFileContents), convertToUnsavedFileContent); } @@ -109,14 +109,14 @@ llvm::StringRef toStringRef(const String &string) clang::tooling::ClangTool ClangTool::createTool() const { - clang::tooling::ClangTool tool(compilationDatabase, sourceFilePaths); + clang::tooling::ClangTool tool(m_compilationDatabase, m_sourceFilePaths); - for (const auto &fileContent : fileContents) { + for (const auto &fileContent : m_fileContents) { if (!fileContent.content.empty()) tool.mapVirtualFile(fileContent.filePath, fileContent.content); } - for (const auto &unsavedFileContent : unsavedFileContents) + for (const auto &unsavedFileContent : m_unsavedFileContents) tool.mapVirtualFile(toStringRef(unsavedFileContent.filePath), toStringRef(unsavedFileContent.content)); diff --git a/src/tools/clangrefactoringbackend/source/clangtool.h b/src/tools/clangrefactoringbackend/source/clangtool.h index c6439209143..7957bb70448 100644 --- a/src/tools/clangrefactoringbackend/source/clangtool.h +++ b/src/tools/clangrefactoringbackend/source/clangtool.h @@ -104,10 +104,10 @@ public: clang::tooling::ClangTool createTool() const; private: - RefactoringCompilationDatabase compilationDatabase; - std::vector fileContents; - std::vector sourceFilePaths; - std::vector unsavedFileContents; + RefactoringCompilationDatabase m_compilationDatabase; + std::vector m_fileContents; + std::vector m_sourceFilePaths; + std::vector m_unsavedFileContents; }; } // namespace ClangBackEnd diff --git a/src/tools/clangrefactoringbackend/source/collectmacrossourcefilecallbacks.cpp b/src/tools/clangrefactoringbackend/source/collectmacrossourcefilecallbacks.cpp new file mode 100644 index 00000000000..ade4904e46e --- /dev/null +++ b/src/tools/clangrefactoringbackend/source/collectmacrossourcefilecallbacks.cpp @@ -0,0 +1,35 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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. +** +****************************************************************************/ + +#include "collectmacrossourcefilecallbacks.h" + +namespace ClangBackEnd { + +CollectMacrosSourceFileCallbacks::CollectMacrosSourceFileCallbacks() +{ + +} + +} // namespace ClangBackEnd diff --git a/src/tools/clangrefactoringbackend/source/collectmacrossourcefilecallbacks.h b/src/tools/clangrefactoringbackend/source/collectmacrossourcefilecallbacks.h new file mode 100644 index 00000000000..2c28f19f7b5 --- /dev/null +++ b/src/tools/clangrefactoringbackend/source/collectmacrossourcefilecallbacks.h @@ -0,0 +1,38 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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. +** +****************************************************************************/ + +#pragma once + +#include + +namespace ClangBackEnd { + +class CollectMacrosSourceFileCallbacks : public clang::tooling::SourceFileCallbacks +{ +public: + CollectMacrosSourceFileCallbacks(); +}; + +} // namespace ClangBackEnd diff --git a/src/tools/clangrefactoringbackend/source/collectsymbolsaction.cpp b/src/tools/clangrefactoringbackend/source/collectsymbolsaction.cpp new file mode 100644 index 00000000000..011477e4bad --- /dev/null +++ b/src/tools/clangrefactoringbackend/source/collectsymbolsaction.cpp @@ -0,0 +1,39 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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. +** +****************************************************************************/ + +#include "collectsymbolsaction.h" + +#include "collectsymbolsconsumer.h" + +namespace ClangBackEnd { + +std::unique_ptr CollectSymbolsAction::newASTConsumer() +{ + return std::make_unique(m_symbolEntries, + m_sourceLocationEntries, + m_filePathCache); +} + +} // namespace ClangBackEnd diff --git a/src/tools/clangrefactoringbackend/source/collectsymbolsaction.h b/src/tools/clangrefactoringbackend/source/collectsymbolsaction.h new file mode 100644 index 00000000000..ceaf0ee85b7 --- /dev/null +++ b/src/tools/clangrefactoringbackend/source/collectsymbolsaction.h @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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. +** +****************************************************************************/ + +#pragma once + +#include "clangrefactoringbackend_global.h" +#include "sourcelocationentry.h" +#include "symbolentry.h" + +#include + +#include + +#include + +namespace ClangBackEnd { + +class CollectSymbolsAction +{ +public: + CollectSymbolsAction(FilePathCache<> &filePathCache) + : m_filePathCache(filePathCache) + {} + + std::unique_ptr newASTConsumer(); + + SymbolEntries takeSymbols() + { + return std::move(m_symbolEntries); + } + + const SymbolEntries &symbols() const + { + return m_symbolEntries; + } + + const SourceLocationEntries &sourceLocations() const + { + return m_sourceLocationEntries; + } + +private: + SymbolEntries m_symbolEntries; + SourceLocationEntries m_sourceLocationEntries; + FilePathCache<> &m_filePathCache; + +}; + +} // namespace ClangBackEnd diff --git a/src/tools/clangrefactoringbackend/source/collectsymbolsastvisitor.h b/src/tools/clangrefactoringbackend/source/collectsymbolsastvisitor.h new file mode 100644 index 00000000000..11fd41900fd --- /dev/null +++ b/src/tools/clangrefactoringbackend/source/collectsymbolsastvisitor.h @@ -0,0 +1,126 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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. +** +****************************************************************************/ + +#pragma once + +#include "symbolentry.h" +#include "sourcelocationentry.h" + +#include + +#include +#include +#include +#include +#include + +#include + +namespace ClangBackEnd { + +Utils::SmallStringView toStringView(clang::StringRef stringReference) +{ + return Utils::SmallStringView(stringReference.data(), stringReference.size()); +} + +class CollectSymbolsASTVisitor : public clang::RecursiveASTVisitor +{ +public: + CollectSymbolsASTVisitor(SymbolEntries &symbolEntries, + SourceLocationEntries &sourceLocationEntries, + FilePathCache<> &filePathCache, + const clang::SourceManager &sourceManager) + : m_symbolEntries(symbolEntries), + m_sourceLocationEntries(sourceLocationEntries), + m_filePathCache(filePathCache), + m_sourceManager(sourceManager) + {} + + bool VisitNamedDecl(const clang::NamedDecl *declaration) + { + auto globalId = declaration->getCanonicalDecl()->getLocation().getRawEncoding(); + auto sourceLocation = declaration->getLocation(); + + auto found = m_symbolEntries.find(globalId); + if (found == m_symbolEntries.end()) { + m_symbolEntries.emplace(std::piecewise_construct, + std::forward_as_tuple(globalId), + std::forward_as_tuple(generateUSR(declaration), declaration->getName())); + } + + m_sourceLocationEntries.emplace_back(globalId, + filePathId(sourceLocation), + lineColum(sourceLocation), + SymbolType::Declaration); + + return true; + } + + bool VisitDeclRefExpr(const clang::DeclRefExpr *expression) + { + auto declaration = expression->getFoundDecl(); + auto globalId = declaration->getCanonicalDecl()->getLocation().getRawEncoding(); + auto sourceLocation = expression->getLocation(); + + m_sourceLocationEntries.emplace_back(globalId, + filePathId(sourceLocation), + lineColum(sourceLocation), + SymbolType::DeclarationReference); + + return true; + } + + uint filePathId(clang::SourceLocation sourceLocation) + { + auto filePath = m_sourceManager.getFilename(sourceLocation); + + return m_filePathCache.stringId(toStringView(filePath)); + } + + LineColumn lineColum(clang::SourceLocation sourceLocation) + { + return {m_sourceManager.getSpellingLineNumber(sourceLocation), + m_sourceManager.getSpellingColumnNumber(sourceLocation)}; + } + + llvm::SmallVector generateUSR(const clang::Decl *declaration) + { + llvm::SmallVector usr; + + clang::index::generateUSRForDecl(declaration, usr); + + return usr; + } + +private: + SymbolEntries &m_symbolEntries; + SourceLocationEntries &m_sourceLocationEntries; + FilePathCache<> &m_filePathCache; + const clang::SourceManager &m_sourceManager; +}; + + +} // namespace ClangBackend + diff --git a/src/tools/clangrefactoringbackend/source/collectsymbolsconsumer.h b/src/tools/clangrefactoringbackend/source/collectsymbolsconsumer.h new file mode 100644 index 00000000000..035db37ebcb --- /dev/null +++ b/src/tools/clangrefactoringbackend/source/collectsymbolsconsumer.h @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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. +** +****************************************************************************/ + +#pragma once + +#include "collectsymbolsastvisitor.h" + +#include +#include +#include + +#include + +namespace ClangBackEnd { + +class CollectSymbolsConsumer : public clang::ASTConsumer +{ +public: + CollectSymbolsConsumer(SymbolEntries &symbolEntries, + SourceLocationEntries &sourceLocationEntries, + FilePathCache<> &filePathCache) + : m_symbolEntries(symbolEntries), + m_sourceLocationEntries(sourceLocationEntries), + m_filePathCache(filePathCache) + {} + + void HandleTranslationUnit(clang::ASTContext &astContext) override { + CollectSymbolsASTVisitor visitor{m_symbolEntries, + m_sourceLocationEntries, + m_filePathCache, + astContext.getSourceManager()}; + visitor.TraverseDecl(astContext.getTranslationUnitDecl()); + } + +private: + SymbolEntries &m_symbolEntries; + SourceLocationEntries &m_sourceLocationEntries; + FilePathCache<> &m_filePathCache; +}; +} diff --git a/src/tools/clangrefactoringbackend/source/findcursorusr.h b/src/tools/clangrefactoringbackend/source/findcursorusr.h index 0c228c67e86..549086cd3aa 100644 --- a/src/tools/clangrefactoringbackend/source/findcursorusr.h +++ b/src/tools/clangrefactoringbackend/source/findcursorusr.h @@ -25,26 +25,12 @@ #pragma once -#if defined(__GNUC__) -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wunused-parameter" -#elif defined(_MSC_VER) -# pragma warning(push) -# pragma warning( disable : 4100 ) -#endif - #include #include #include #include #include -#if defined(__GNUC__) -# pragma GCC diagnostic pop -#elif defined(_MSC_VER) -# pragma warning(pop) -#endif - #include namespace ClangBackEnd { @@ -54,8 +40,8 @@ class FindNamedDeclarationASTVisitor : public clang::RecursiveASTVisitor takeNamedDecl() { - return std::move(namedDeclarations); + return std::move(m_namedDeclarations); } private: @@ -138,7 +124,7 @@ private: bool isValid = isValidLocationWithCursorInside(startLocation, endLocation); if (isValid) - namedDeclarations.push_back(declaration); + m_namedDeclarations.push_back(declaration); return !isValid; } @@ -153,20 +139,20 @@ private: bool isCursorLocationBetween(const clang::SourceLocation startLocation, const clang::SourceLocation endLocation) { - return cursorSourceLocation == startLocation - || cursorSourceLocation == endLocation - || (sourceManager.isBeforeInTranslationUnit(startLocation, cursorSourceLocation) - && sourceManager.isBeforeInTranslationUnit(cursorSourceLocation, endLocation)); + return m_cursorSourceLocation == startLocation + || m_cursorSourceLocation == endLocation + || (m_sourceManager.isBeforeInTranslationUnit(startLocation, m_cursorSourceLocation) + && m_sourceManager.isBeforeInTranslationUnit(m_cursorSourceLocation, endLocation)); } - std::vector namedDeclarations; - const clang::SourceManager &sourceManager; - const clang::SourceLocation cursorSourceLocation; + std::vector m_namedDeclarations; + const clang::SourceManager &m_sourceManager; + const clang::SourceLocation m_cursorSourceLocation; }; inline std::vector namedDeclarationsAt(const clang::ASTContext &Context, - const clang::SourceLocation cursorSourceLocation) + const clang::SourceLocation cursorSourceLocation) { const auto &sourceManager = Context.getSourceManager(); const auto currentFile = sourceManager.getFilename(cursorSourceLocation); diff --git a/src/tools/clangrefactoringbackend/source/findusrforcursoraction.cpp b/src/tools/clangrefactoringbackend/source/findusrforcursoraction.cpp index 50f7230586c..6fa964f5318 100644 --- a/src/tools/clangrefactoringbackend/source/findusrforcursoraction.cpp +++ b/src/tools/clangrefactoringbackend/source/findusrforcursoraction.cpp @@ -27,24 +27,10 @@ #include "findcursorusr.h" -#if defined(__GNUC__) -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wunused-parameter" -#elif defined(_MSC_VER) -# pragma warning(push) -# pragma warning( disable : 4100 ) -#endif - #include #include #include -#if defined(__GNUC__) -# pragma GCC diagnostic pop -#elif defined(_MSC_VER) -# pragma warning(pop) -#endif - #include #include @@ -54,16 +40,16 @@ namespace { std::vector collectConstructorUnifiedSymbolResolutions(const clang::CXXRecordDecl *declarations) { - std::vector unifiedSymbolResolutions; + std::vector unifiedSymbolResolutions; - const auto constructorDeclarations = declarations->getDefinition()->ctors(); + const auto constructorDeclarations = declarations->getDefinition()->ctors(); - std::transform(constructorDeclarations.begin(), - constructorDeclarations.end(), - std::back_inserter(unifiedSymbolResolutions), - USROfDeclaration); + std::transform(constructorDeclarations.begin(), + constructorDeclarations.end(), + std::back_inserter(unifiedSymbolResolutions), + USROfDeclaration); - return unifiedSymbolResolutions; + return unifiedSymbolResolutions; } void addUnifiedSymbolResolutionsForDeclaration(const std::vector &declarations, @@ -87,18 +73,18 @@ public: std::vector &unifiedSymbolResolutions, uint line, uint column) - : symbolName(symbolName), - unifiedSymbolResolutions(unifiedSymbolResolutions), - line(line), - column(column) + : m_symbolName(symbolName), + m_unifiedSymbolResolutions(unifiedSymbolResolutions), + m_line(line), + m_column(column) { } void HandleTranslationUnit(clang::ASTContext &astContext) override { const auto &sourceManager = astContext.getSourceManager(); const auto cursorSourceLocation = sourceManager.translateLineCol(sourceManager.getMainFileID(), - line, - column); + m_line, + m_column); if (cursorSourceLocation.isValid()) collectUnifiedSymbolResoltions(astContext, cursorSourceLocation); @@ -114,32 +100,29 @@ public: if (const auto *constructorDecl = clang::dyn_cast(firstFoundDeclaration)) { const clang::CXXRecordDecl *foundDeclarationParent = constructorDecl->getParent(); - unifiedSymbolResolutions = collectConstructorUnifiedSymbolResolutions(foundDeclarationParent); + m_unifiedSymbolResolutions = collectConstructorUnifiedSymbolResolutions(foundDeclarationParent); } else if (const auto *destructorDecl = clang::dyn_cast(firstFoundDeclaration)) { const clang::CXXRecordDecl *foundDeclarationParent = destructorDecl->getParent(); - unifiedSymbolResolutions = collectConstructorUnifiedSymbolResolutions(foundDeclarationParent); + m_unifiedSymbolResolutions = collectConstructorUnifiedSymbolResolutions(foundDeclarationParent); } else if (const auto *recordDeclaration = clang::dyn_cast(firstFoundDeclaration)) { - unifiedSymbolResolutions = collectConstructorUnifiedSymbolResolutions(recordDeclaration); + m_unifiedSymbolResolutions = collectConstructorUnifiedSymbolResolutions(recordDeclaration); } - addUnifiedSymbolResolutionsForDeclaration(foundDeclarations, unifiedSymbolResolutions); - symbolName = firstFoundDeclaration->getNameAsString(); + addUnifiedSymbolResolutionsForDeclaration(foundDeclarations, m_unifiedSymbolResolutions); + m_symbolName = firstFoundDeclaration->getNameAsString(); } } private: - Utils::SmallString &symbolName; - std::vector &unifiedSymbolResolutions; - uint line; - uint column; + Utils::SmallString &m_symbolName; + std::vector &m_unifiedSymbolResolutions; + uint m_line; + uint m_column; }; std::unique_ptr USRFindingAction::newASTConsumer() { - std::unique_ptr Consumer( - new FindDeclarationsConsumer(symbolName, unifiedSymbolResolutions_, line, column)); - - return std::move(Consumer); + return std::make_unique(m_symbolName, m_unifiedSymbolResolutions, m_line, m_column); } } // namespace ClangBackEnd diff --git a/src/tools/clangrefactoringbackend/source/findusrforcursoraction.h b/src/tools/clangrefactoringbackend/source/findusrforcursoraction.h index 6109db187ea..bfa4a373a4a 100644 --- a/src/tools/clangrefactoringbackend/source/findusrforcursoraction.h +++ b/src/tools/clangrefactoringbackend/source/findusrforcursoraction.h @@ -29,21 +29,7 @@ #include -#if defined(__GNUC__) -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wunused-parameter" -#elif defined(_MSC_VER) -# pragma warning(push) -# pragma warning( disable : 4100 ) -#endif - -#include "clang/Frontend/FrontendAction.h" - -#if defined(__GNUC__) -# pragma GCC diagnostic pop -#elif defined(_MSC_VER) -# pragma warning(pop) -#endif +#include namespace clang { class ASTConsumer; @@ -57,8 +43,8 @@ class USRFindingAction { public: USRFindingAction(uint line, uint column) - : line(line), - column(column) + : m_line(line), + m_column(column) { } @@ -66,19 +52,19 @@ public: std::string takeSymbolName() { - return std::string(symbolName); + return std::string(m_symbolName); } std::vector takeUnifiedSymbolResolutions() { - return std::move(unifiedSymbolResolutions_); + return std::move(m_unifiedSymbolResolutions); } private: - Utils::SmallString symbolName; - std::vector unifiedSymbolResolutions_; - uint line; - uint column; + Utils::SmallString m_symbolName; + std::vector m_unifiedSymbolResolutions; + uint m_line; + uint m_column; }; } // namespace ClangBackEnd diff --git a/src/tools/clangrefactoringbackend/source/locationsourcefilecallbacks.h b/src/tools/clangrefactoringbackend/source/locationsourcefilecallbacks.h index fe23e3e5a5a..cb9560ae7d8 100644 --- a/src/tools/clangrefactoringbackend/source/locationsourcefilecallbacks.h +++ b/src/tools/clangrefactoringbackend/source/locationsourcefilecallbacks.h @@ -27,22 +27,8 @@ #include -#if defined(__GNUC__) -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wunused-parameter" -#elif defined(_MSC_VER) -# pragma warning(push) -# pragma warning( disable : 4100 ) -#endif - #include -#if defined(__GNUC__) -# pragma GCC diagnostic pop -#elif defined(_MSC_VER) -# pragma warning(pop) -#endif - namespace llvm { class StringRef; } diff --git a/src/tools/clangrefactoringbackend/source/sourcelocationentry.cpp b/src/tools/clangrefactoringbackend/source/sourcelocationentry.cpp new file mode 100644 index 00000000000..9cea242fa87 --- /dev/null +++ b/src/tools/clangrefactoringbackend/source/sourcelocationentry.cpp @@ -0,0 +1,42 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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. +** +****************************************************************************/ + +#include "sourcelocationentry.h" + +#include + +namespace ClangBackEnd { + +std::ostream &operator<<(std::ostream &out, const SourceLocationEntry &entry) +{ + out << "(" + << entry.fileId << ", " + << entry.line << ", " + << entry.column << ")"; + + return out; +} + +} // namespace ClangBackEnd diff --git a/src/tools/clangrefactoringbackend/source/sourcelocationentry.h b/src/tools/clangrefactoringbackend/source/sourcelocationentry.h new file mode 100644 index 00000000000..b4a63306362 --- /dev/null +++ b/src/tools/clangrefactoringbackend/source/sourcelocationentry.h @@ -0,0 +1,79 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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. +** +****************************************************************************/ + +#pragma once + +#include +#include +#include + +using uint = unsigned int; + +namespace ClangBackEnd { + +enum class SymbolType +{ + Declaration, + DeclarationReference +}; + +class LineColumn +{ +public: + LineColumn(uint line, uint column) + : line(line), + column(column) + {} + + uint line = 0; + uint column = 0; +}; + +class SourceLocationEntry +{ +public: + SourceLocationEntry(uint symbolId, + uint fileId, + LineColumn lineColumn, + SymbolType symbolType) + : symbolId(symbolId), + fileId(fileId), + line(lineColumn.line), + column(lineColumn.column), + symbolType(symbolType) + {} + + uint symbolId = 0; + uint fileId = std::numeric_limits::max(); + uint line = 0; + uint column = 0; + SymbolType symbolType; +}; + +using SourceLocationEntries = std::vector; + +std::ostream &operator<<(std::ostream &out, const SourceLocationEntry &entry); + +} // namespace ClangBackEnd diff --git a/src/tools/clangrefactoringbackend/source/symbolentry.cpp b/src/tools/clangrefactoringbackend/source/symbolentry.cpp new file mode 100644 index 00000000000..ee606cf9ab8 --- /dev/null +++ b/src/tools/clangrefactoringbackend/source/symbolentry.cpp @@ -0,0 +1,41 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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. +** +****************************************************************************/ + +#include "symbolentry.h" + +#include + +namespace ClangBackEnd { + +std::ostream &operator<<(std::ostream &out, const SymbolEntry &entry) +{ + out << "(" + << entry.symbolName << ", " + << entry.usr <<")"; + + return out; +} + +} // namespace ClangBackEnd diff --git a/src/tools/clangrefactoringbackend/source/symbolentry.h b/src/tools/clangrefactoringbackend/source/symbolentry.h new file mode 100644 index 00000000000..cf74418282f --- /dev/null +++ b/src/tools/clangrefactoringbackend/source/symbolentry.h @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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. +** +****************************************************************************/ + +#pragma once + +#include + +#include +#include + +#include +#include +#include + +namespace ClangBackEnd { + +class SymbolEntry +{ +public: + SymbolEntry(const llvm::SmallVector &usr, + llvm::StringRef name) + : usr(usr.data(), usr.size()), + symbolName(name.data(), name.size()) + {} + + Utils::PathString usr; + Utils::SmallString symbolName; +}; + +using SymbolEntries = std::unordered_map; + +std::ostream &operator<<(std::ostream &out, const SymbolEntry &entry); + +} // namespace ClangBackEnd diff --git a/src/tools/clangrefactoringbackend/source/symbolfinder.h b/src/tools/clangrefactoringbackend/source/symbolfinder.h index 42c7098f1e2..a76639aa26a 100644 --- a/src/tools/clangrefactoringbackend/source/symbolfinder.h +++ b/src/tools/clangrefactoringbackend/source/symbolfinder.h @@ -32,22 +32,6 @@ #include -#if defined(__GNUC__) -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wunused-parameter" -#elif defined(_MSC_VER) -# pragma warning(push) -# pragma warning( disable : 4100 ) -#endif - -#include "clang/Tooling/Refactoring.h" - -#if defined(__GNUC__) -# pragma GCC diagnostic pop -#elif defined(_MSC_VER) -# pragma warning(pop) -#endif - namespace ClangBackEnd { class SymbolFinder : public ClangTool diff --git a/src/tools/clangrefactoringbackend/source/symbollocationfinderaction.cpp b/src/tools/clangrefactoringbackend/source/symbollocationfinderaction.cpp index c5aba6ca905..2b07e893f58 100644 --- a/src/tools/clangrefactoringbackend/source/symbollocationfinderaction.cpp +++ b/src/tools/clangrefactoringbackend/source/symbollocationfinderaction.cpp @@ -39,7 +39,7 @@ class FindingSymbolsASTConsumer : public clang::ASTConsumer { public: FindingSymbolsASTConsumer(std::vector &unifiedSymbolResolutions) - : unifiedSymbolResolutions(unifiedSymbolResolutions) + : m_unifiedSymbolResolutions(unifiedSymbolResolutions) { } @@ -48,7 +48,7 @@ public: std::vector sourceLocations; - auto &&sourceLocationsOfUsr = takeLocationsOfUSRs(unifiedSymbolResolutions, context.getTranslationUnitDecl()); + auto &&sourceLocationsOfUsr = takeLocationsOfUSRs(m_unifiedSymbolResolutions, context.getTranslationUnitDecl()); sourceLocations.insert(sourceLocations.end(), sourceLocationsOfUsr.begin(), sourceLocationsOfUsr.end()); @@ -65,24 +65,24 @@ public: void updateSourceLocations(const std::vector &sourceLocations, const clang::SourceManager &sourceManager) { - appendSourceLocationsToSourceLocationsContainer(*sourceLocationsContainer, sourceLocations, sourceManager); + appendSourceLocationsToSourceLocationsContainer(*m_sourceLocationsContainer, sourceLocations, sourceManager); } void setSourceLocations(ClangBackEnd::SourceLocationsContainer *sourceLocations) { - sourceLocationsContainer = sourceLocations; + m_sourceLocationsContainer = sourceLocations; } private: - ClangBackEnd::SourceLocationsContainer *sourceLocationsContainer = nullptr; - std::vector &unifiedSymbolResolutions; + ClangBackEnd::SourceLocationsContainer *m_sourceLocationsContainer = nullptr; + std::vector &m_unifiedSymbolResolutions; }; std::unique_ptr SymbolLocationFinderAction::newASTConsumer() { - auto consumer = std::unique_ptr(new FindingSymbolsASTConsumer(unifiedSymbolResolutions_)); + auto consumer = std::unique_ptr(new FindingSymbolsASTConsumer(m_unifiedSymbolResolutions_)); - consumer->setSourceLocations(&sourceLocations); + consumer->setSourceLocations(&m_sourceLocations); return std::move(consumer); } diff --git a/src/tools/clangrefactoringbackend/source/symbollocationfinderaction.h b/src/tools/clangrefactoringbackend/source/symbollocationfinderaction.h index 7bee18f94bb..ae601d8219e 100644 --- a/src/tools/clangrefactoringbackend/source/symbollocationfinderaction.h +++ b/src/tools/clangrefactoringbackend/source/symbollocationfinderaction.h @@ -29,22 +29,8 @@ #include -#if defined(__GNUC__) -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wunused-parameter" -#elif defined(_MSC_VER) -# pragma warning(push) -# pragma warning( disable : 4100 ) -#endif - #include -#if defined(__GNUC__) -# pragma GCC diagnostic pop -#elif defined(_MSC_VER) -# pragma warning(pop) -#endif - namespace clang { class ASTConsumer; } @@ -59,22 +45,22 @@ public: SourceLocationsContainer takeSourceLocations() { - return std::move(sourceLocations); + return std::move(m_sourceLocations); } void setUnifiedSymbolResolutions(std::vector &&unifiedSymbolResolutions) { - unifiedSymbolResolutions_ = std::move(unifiedSymbolResolutions); + m_unifiedSymbolResolutions_ = std::move(unifiedSymbolResolutions); } const std::vector &unifiedSymbolResolutions() const { - return unifiedSymbolResolutions_; + return m_unifiedSymbolResolutions_; } private: - ClangBackEnd::SourceLocationsContainer sourceLocations; - std::vector unifiedSymbolResolutions_; + ClangBackEnd::SourceLocationsContainer m_sourceLocations; + std::vector m_unifiedSymbolResolutions_; }; } // namespace ClangBackEnd diff --git a/src/tools/clangrefactoringbackend/source/symbolscollector.cpp b/src/tools/clangrefactoringbackend/source/symbolscollector.cpp new file mode 100644 index 00000000000..c07a01f3ff4 --- /dev/null +++ b/src/tools/clangrefactoringbackend/source/symbolscollector.cpp @@ -0,0 +1,53 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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. +** +****************************************************************************/ + +#include "symbolscollector.h" + +namespace ClangBackEnd { + +SymbolsCollector::SymbolsCollector(FilePathCache<> &filePathCache) + : m_collectSymbolsAction(filePathCache) +{ +} + +void SymbolsCollector::collectSymbols() +{ + auto tool = createTool(); + + tool.run(clang::tooling::newFrontendActionFactory(&m_collectSymbolsAction, + &m_collectMacrosSourceFileCallbacks).get()); +} + +const SymbolEntries &SymbolsCollector::symbols() const +{ + return m_collectSymbolsAction.symbols(); +} + +const SourceLocationEntries &SymbolsCollector::sourceLocations() const +{ + return m_collectSymbolsAction.sourceLocations(); +} + +} // namespace ClangBackEnd diff --git a/src/tools/clangrefactoringbackend/source/symbolscollector.h b/src/tools/clangrefactoringbackend/source/symbolscollector.h new file mode 100644 index 00000000000..f047131d557 --- /dev/null +++ b/src/tools/clangrefactoringbackend/source/symbolscollector.h @@ -0,0 +1,51 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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. +** +****************************************************************************/ + +#pragma once + +#include "clangtool.h" +#include "collectmacrossourcefilecallbacks.h" +#include "collectsymbolsaction.h" +#include "symbolentry.h" +#include "stringcache.h" + +namespace ClangBackEnd { + +class SymbolsCollector: public ClangTool +{ +public: + SymbolsCollector(FilePathCache<> &filePathCache); + + void collectSymbols(); + + const SymbolEntries &symbols() const; + const SourceLocationEntries &sourceLocations() const; + +private: + CollectSymbolsAction m_collectSymbolsAction; + CollectMacrosSourceFileCallbacks m_collectMacrosSourceFileCallbacks; +}; + +} // namespace ClangBackEnd diff --git a/tests/unit/unittest/data/symbolscollector_simple.cpp b/tests/unit/unittest/data/symbolscollector_simple.cpp new file mode 100644 index 00000000000..494af8c8b48 --- /dev/null +++ b/tests/unit/unittest/data/symbolscollector_simple.cpp @@ -0,0 +1,15 @@ +void function(); +void function(); +void function(int x); +void function() +{ + int x; + x = 4; + int y = 4; + y = x + 3; +} + +void f() +{ + function(); +} diff --git a/tests/unit/unittest/symbolscollector-test.cpp b/tests/unit/unittest/symbolscollector-test.cpp new file mode 100644 index 00000000000..15e15ba5f14 --- /dev/null +++ b/tests/unit/unittest/symbolscollector-test.cpp @@ -0,0 +1,162 @@ +/**************************************************************************** +** +** Copyright (C) 2017 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. +** +****************************************************************************/ + +#include "googletest.h" + +#include + +#include "filesystem-utilities.h" + +using testing::PrintToString; +using testing::AllOf; +using testing::Contains; +using testing::Not; +using testing::Field; +using testing::Key; +using testing::Pair; +using testing::Value; +using testing::_; + +using ClangBackEnd::SourceLocationEntry; +using ClangBackEnd::SymbolEntry; +using ClangBackEnd::SymbolType; + +namespace { + +class SymbolsCollector : public testing::Test +{ +protected: + uint stringId(Utils::SmallStringView string) + { + return filePathCache.stringId(string); + } + + uint symbolIdForSymbolName(const Utils::SmallString &symbolName); + +protected: + ClangBackEnd::FilePathCache<> filePathCache; + ClangBackEnd::SymbolsCollector collector{filePathCache}; +}; + +TEST_F(SymbolsCollector, CollectSymbolName) +{ + collector.addFile(TESTDATA_DIR, "symbolscollector_simple.cpp", "", {"cc", TESTDATA_DIR"/symbolscollector_simple.cpp"}); + + collector.collectSymbols(); + + ASSERT_THAT(collector.symbols(), + Contains( + Pair(_, Field(&SymbolEntry::symbolName, "x")))); +} + +TEST_F(SymbolsCollector, SymbolMatchesLocation) +{ + collector.addFile(TESTDATA_DIR, "symbolscollector_simple.cpp", "", {"cc", TESTDATA_DIR"/symbolscollector_simple.cpp"}); + + collector.collectSymbols(); + + ASSERT_THAT(collector.sourceLocations(), + Contains( + AllOf(Field(&SourceLocationEntry::symbolId, symbolIdForSymbolName("function")), + Field(&SourceLocationEntry::line, 1), + Field(&SourceLocationEntry::column, 6)))); +} + +TEST_F(SymbolsCollector, OtherSymboldMatchesLocation) +{ + collector.addFile(TESTDATA_DIR, "symbolscollector_simple.cpp", "", {"cc", TESTDATA_DIR"/symbolscollector_simple.cpp"}); + + collector.collectSymbols(); + + ASSERT_THAT(collector.sourceLocations(), + Contains( + AllOf(Field(&SourceLocationEntry::symbolId, symbolIdForSymbolName("function")), + Field(&SourceLocationEntry::line, 2), + Field(&SourceLocationEntry::column, 6)))); +} + +TEST_F(SymbolsCollector, CollectFilePath) +{ + collector.addFile(TESTDATA_DIR, "symbolscollector_simple.cpp", "", {"cc", TESTDATA_DIR"/symbolscollector_simple.cpp"}); + + collector.collectSymbols(); + + ASSERT_THAT(collector.sourceLocations(), + Contains( + AllOf(Field(&SourceLocationEntry::fileId, + stringId(TESTDATA_DIR"/symbolscollector_simple.cpp")), + Field(&SourceLocationEntry::symbolType, SymbolType::Declaration)))); +} + +TEST_F(SymbolsCollector, CollectLineColumn) +{ + collector.addFile(TESTDATA_DIR, "symbolscollector_simple.cpp", "", {"cc", TESTDATA_DIR"/symbolscollector_simple.cpp"}); + + collector.collectSymbols(); + + ASSERT_THAT(collector.sourceLocations(), + Contains( + AllOf(Field(&SourceLocationEntry::line, 1), + Field(&SourceLocationEntry::column, 6), + Field(&SourceLocationEntry::symbolType, SymbolType::Declaration)))); +} + +TEST_F(SymbolsCollector, CollectReference) +{ + collector.addFile(TESTDATA_DIR, "symbolscollector_simple.cpp", "", {"cc", TESTDATA_DIR"/symbolscollector_simple.cpp"}); + + collector.collectSymbols(); + + ASSERT_THAT(collector.sourceLocations(), + Contains( + AllOf(Field(&SourceLocationEntry::line, 14), + Field(&SourceLocationEntry::column, 5), + Field(&SourceLocationEntry::symbolType, SymbolType::DeclarationReference)))); +} + +TEST_F(SymbolsCollector, ReferencedSymboldMatchesLocation) +{ + collector.addFile(TESTDATA_DIR, "symbolscollector_simple.cpp", "", {"cc", TESTDATA_DIR"/symbolscollector_simple.cpp"}); + + collector.collectSymbols(); + + ASSERT_THAT(collector.sourceLocations(), + Contains( + AllOf(Field(&SourceLocationEntry::symbolId, symbolIdForSymbolName("function")), + Field(&SourceLocationEntry::line, 14), + Field(&SourceLocationEntry::column, 5)))); +} + +uint SymbolsCollector::symbolIdForSymbolName(const Utils::SmallString &symbolName) +{ + for (const auto &entry : collector.symbols()) { + if (entry.second.symbolName == symbolName) + return entry.first; + } + + return 0; +} + +} diff --git a/tests/unit/unittest/unittest.pro b/tests/unit/unittest/unittest.pro index 8a29b173aa6..b41f8bdbfdb 100644 --- a/tests/unit/unittest/unittest.pro +++ b/tests/unit/unittest/unittest.pro @@ -68,7 +68,7 @@ SOURCES += \ spydummy.cpp \ stringcache-test.cpp \ unittests-main.cpp \ - utf8-test.cpp \ + utf8-test.cpp !isEmpty(LIBCLANG_LIBS) { SOURCES += \ @@ -144,6 +144,7 @@ SOURCES += \ refactoringengine-test.cpp \ refactoringserver-test.cpp \ sourcerangeextractor-test.cpp \ + symbolscollector-test.cpp \ symbolfinder-test.cpp \ testclangtool.cpp \ }