diff --git a/src/plugins/clangcodemodel/clangcodemodel.pro b/src/plugins/clangcodemodel/clangcodemodel.pro index c07e0382976..49bd6ffcc13 100644 --- a/src/plugins/clangcodemodel/clangcodemodel.pro +++ b/src/plugins/clangcodemodel/clangcodemodel.pro @@ -53,6 +53,7 @@ HEADERS += \ clangfixitoperationsextractor.h \ clangfunctionhintmodel.h \ clanghighlightingmarksreporter.h \ + clangisdiagnosticrelatedtolocation.h \ clangmodelmanagersupport.h \ clangpreprocessorassistproposalitem.h \ clangtextmark.h \ diff --git a/src/plugins/clangcodemodel/clangcodemodel.qbs b/src/plugins/clangcodemodel/clangcodemodel.qbs index 8d72501868f..2b7c6f480af 100644 --- a/src/plugins/clangcodemodel/clangcodemodel.qbs +++ b/src/plugins/clangcodemodel/clangcodemodel.qbs @@ -77,6 +77,7 @@ QtcPlugin { "clangfunctionhintmodel.h", "clanghighlightingmarksreporter.cpp", "clanghighlightingmarksreporter.h", + "clangisdiagnosticrelatedtolocation.h", "clangmodelmanagersupport.cpp", "clangmodelmanagersupport.h", "clangpreprocessorassistproposalitem.cpp", diff --git a/src/plugins/clangcodemodel/clangcodemodelunittestfiles.pri b/src/plugins/clangcodemodel/clangcodemodelunittestfiles.pri index 0515875a4ac..165dc1cd7f4 100644 --- a/src/plugins/clangcodemodel/clangcodemodelunittestfiles.pri +++ b/src/plugins/clangcodemodel/clangcodemodelunittestfiles.pri @@ -16,4 +16,5 @@ HEADERS += \ $$PWD/clangcompletioncontextanalyzer.h \ $$PWD/clangdiagnosticfilter.h \ $$PWD/clangfixitoperation.h \ - $$PWD/clanghighlightingmarksreporter.h + $$PWD/clanghighlightingmarksreporter.h \ + $$PWD/clangisdiagnosticrelatedtolocation.h diff --git a/src/plugins/clangcodemodel/clangdiagnosticmanager.cpp b/src/plugins/clangcodemodel/clangdiagnosticmanager.cpp index 6e4b7496de3..5bfdd54765d 100644 --- a/src/plugins/clangcodemodel/clangdiagnosticmanager.cpp +++ b/src/plugins/clangcodemodel/clangdiagnosticmanager.cpp @@ -25,7 +25,9 @@ #include "clangdiagnosticfilter.h" #include "clangdiagnosticmanager.h" +#include "clangisdiagnosticrelatedtolocation.h" +#include #include #include #include @@ -38,14 +40,12 @@ namespace { QTextEdit::ExtraSelection createExtraSelections(const QTextCharFormat &mainformat, - const QTextCursor &cursor, - const QString &diagnosticText) + const QTextCursor &cursor) { QTextEdit::ExtraSelection extraSelection; extraSelection.format = mainformat; extraSelection.cursor = cursor; - extraSelection.format.setToolTip(diagnosticText); return extraSelection; } @@ -61,7 +61,6 @@ int positionInText(QTextDocument *textDocument, void addRangeSelections(const ClangBackEnd::DiagnosticContainer &diagnostic, QTextDocument *textDocument, const QTextCharFormat &contextFormat, - const QString &diagnosticText, QList &extraSelections) { for (auto &&range : diagnostic.ranges()) { @@ -69,7 +68,7 @@ void addRangeSelections(const ClangBackEnd::DiagnosticContainer &diagnostic, cursor.setPosition(positionInText(textDocument, range.start())); cursor.setPosition(positionInText(textDocument, range.end()), QTextCursor::KeepAnchor); - auto extraSelection = createExtraSelections(contextFormat, cursor, diagnosticText); + auto extraSelection = createExtraSelections(contextFormat, cursor); extraSelections.push_back(std::move(extraSelection)); } @@ -90,37 +89,6 @@ QTextCursor createSelectionCursor(QTextDocument *textDocument, return cursor; } -bool isHelpfulChildDiagnostic(const ClangBackEnd::DiagnosticContainer &parentDiagnostic, - const ClangBackEnd::DiagnosticContainer &childDiagnostic) -{ - auto parentLocation = parentDiagnostic.location(); - auto childLocation = childDiagnostic.location(); - - return parentLocation == childLocation; -} - -QString diagnosticText(const ClangBackEnd::DiagnosticContainer &diagnostic) -{ - QString text = diagnostic.category().toString() - + QStringLiteral("\n\n") - + diagnostic.text().toString(); - -#ifdef QT_DEBUG - if (!diagnostic.disableOption().isEmpty()) { - text += QStringLiteral(" (disable with ") - + diagnostic.disableOption().toString() - + QStringLiteral(")"); - } -#endif - - for (auto &&childDiagnostic : diagnostic.children()) { - if (isHelpfulChildDiagnostic(diagnostic, childDiagnostic)) - text += QStringLiteral("\n ") + childDiagnostic.text().toString(); - } - - return text; -} - void addSelections(const QVector &diagnostics, QTextDocument *textDocument, const QTextCharFormat &mainFormat, @@ -129,11 +97,9 @@ void addSelections(const QVector &diagnostics { for (auto &&diagnostic : diagnostics) { auto cursor = createSelectionCursor(textDocument, diagnostic.location()); + auto extraSelection = createExtraSelections(mainFormat, cursor); - auto text = diagnosticText(diagnostic); - auto extraSelection = createExtraSelections(mainFormat, cursor, text); - - addRangeSelections(diagnostic, textDocument, contextFormat, text, extraSelections); + addRangeSelections(diagnostic, textDocument, contextFormat, extraSelections); extraSelections.push_back(std::move(extraSelection)); } @@ -164,6 +130,69 @@ void addErrorSelections(const QVector &diagno addSelections(diagnostics, textDocument, errorFormat, errorContextFormat, extraSelections); } +ClangBackEnd::SourceLocationContainer toSourceLocation(QTextDocument *textDocument, int position) +{ + int line, column; + if (TextEditor::Convenience::convertPosition(textDocument, position, &line, &column)) + return ClangBackEnd::SourceLocationContainer(Utf8String(), line, column); + + return ClangBackEnd::SourceLocationContainer(); +} + +ClangBackEnd::SourceRangeContainer toSourceRange(const QTextCursor &cursor) +{ + using namespace ClangBackEnd; + + QTextDocument *textDocument = cursor.document(); + + return SourceRangeContainer(toSourceLocation(textDocument, cursor.anchor()), + toSourceLocation(textDocument, cursor.position())); +} + +bool isDiagnosticAtLocation(const ClangBackEnd::DiagnosticContainer &diagnostic, + uint line, + uint column, + QTextDocument *textDocument) +{ + using namespace ClangCodeModel::Internal; + + const ClangBackEnd::SourceLocationContainer &location = diagnostic.location(); + const QTextCursor cursor = createSelectionCursor(textDocument, location); + const ClangBackEnd::SourceRangeContainer cursorRange = toSourceRange(cursor); + + return isDiagnosticRelatedToLocation(diagnostic, {cursorRange}, line, column); +} + +QVector +filteredDiagnosticsAtLocation(const QVector &diagnostics, + uint line, + uint column, + QTextDocument *textDocument) +{ + QVector filteredDiagnostics; + + foreach (const auto &diagnostic, diagnostics) { + if (isDiagnosticAtLocation(diagnostic, line, column, textDocument)) + filteredDiagnostics.append(diagnostic); + } + + return filteredDiagnostics; +} + +bool editorDocumentProcessorHasDiagnosticAt( + const QVector &diagnostics, + uint line, + uint column, + QTextDocument *textDocument) +{ + foreach (const auto &diagnostic, diagnostics) { + if (isDiagnosticAtLocation(diagnostic, line, column, textDocument)) + return true; + } + + return false; +} + } // anonymous namespace ClangCodeModel { @@ -192,6 +221,26 @@ QList ClangDiagnosticManager::takeExtraSelections() return extraSelections; } +bool ClangDiagnosticManager::hasDiagnosticsAt(uint line, uint column) const +{ + QTextDocument *textDocument = m_textDocument->document(); + + return editorDocumentProcessorHasDiagnosticAt(m_errorDiagnostics, line, column, textDocument) + || editorDocumentProcessorHasDiagnosticAt(m_warningDiagnostics, line, column, textDocument); +} + +QVector +ClangDiagnosticManager::diagnosticsAt(uint line, uint column) const +{ + QTextDocument *textDocument = m_textDocument->document(); + + QVector diagnostics; + diagnostics += filteredDiagnosticsAtLocation(m_errorDiagnostics, line, column, textDocument); + diagnostics += filteredDiagnosticsAtLocation(m_warningDiagnostics, line, column, textDocument); + + return diagnostics; +} + void ClangDiagnosticManager::clearDiagnosticsWithFixIts() { m_fixItdiagnostics.clear(); @@ -213,8 +262,6 @@ void ClangDiagnosticManager::processNewDiagnostics( generateTextMarks(); generateEditorSelections(); - - clearWarningsAndErrors(); } const QVector & @@ -241,12 +288,6 @@ void ClangDiagnosticManager::addClangTextMarks( } } -void ClangDiagnosticManager::clearWarningsAndErrors() -{ - m_warningDiagnostics.clear(); - m_errorDiagnostics.clear(); -} - QString ClangDiagnosticManager::filePath() const { return m_textDocument->filePath().toString(); diff --git a/src/plugins/clangcodemodel/clangdiagnosticmanager.h b/src/plugins/clangcodemodel/clangdiagnosticmanager.h index 498f4ec88ff..5e7eb0561aa 100644 --- a/src/plugins/clangcodemodel/clangdiagnosticmanager.h +++ b/src/plugins/clangcodemodel/clangdiagnosticmanager.h @@ -51,6 +51,9 @@ public: const QVector &diagnosticsWithFixIts() const; QList takeExtraSelections(); + bool hasDiagnosticsAt(uint line, uint column) const; + QVector diagnosticsAt(uint line, uint column) const; + void clearDiagnosticsWithFixIts(); private: @@ -59,7 +62,6 @@ private: void generateEditorSelections(); void generateTextMarks(); void addClangTextMarks(const QVector &diagnostics); - void clearWarningsAndErrors(); private: TextEditor::TextDocument *m_textDocument; diff --git a/src/plugins/clangcodemodel/clangeditordocumentprocessor.cpp b/src/plugins/clangcodemodel/clangeditordocumentprocessor.cpp index b793db109dc..8cd28501f12 100644 --- a/src/plugins/clangcodemodel/clangeditordocumentprocessor.cpp +++ b/src/plugins/clangcodemodel/clangeditordocumentprocessor.cpp @@ -50,6 +50,7 @@ #include #include +#include #include #include @@ -233,6 +234,73 @@ TextEditor::QuickFixOperations ClangEditorDocumentProcessor::extraRefactoringOpe return extractor.extract(assistInterface.fileName(), currentLine(assistInterface)); } +bool ClangEditorDocumentProcessor::hasDiagnosticsAt(uint line, uint column) const +{ + return m_diagnosticManager.hasDiagnosticsAt(line, column); +} + +namespace { + +bool isHelpfulChildDiagnostic(const ClangBackEnd::DiagnosticContainer &parentDiagnostic, + const ClangBackEnd::DiagnosticContainer &childDiagnostic) +{ + auto parentLocation = parentDiagnostic.location(); + auto childLocation = childDiagnostic.location(); + + return parentLocation == childLocation; +} + +QString diagnosticText(const ClangBackEnd::DiagnosticContainer &diagnostic) +{ + QString text = diagnostic.category().toString() + + QStringLiteral("\n\n") + + diagnostic.text().toString(); + +#ifdef QT_DEBUG + if (!diagnostic.disableOption().isEmpty()) { + text += QStringLiteral(" (disable with ") + + diagnostic.disableOption().toString() + + QStringLiteral(")"); + } +#endif + + for (auto &&childDiagnostic : diagnostic.children()) { + if (isHelpfulChildDiagnostic(diagnostic, childDiagnostic)) + text += QStringLiteral("\n ") + childDiagnostic.text().toString(); + } + + return text; +} + +QString generateTooltipText(const QVector &diagnostics) +{ + QString text; + + foreach (const ClangBackEnd::DiagnosticContainer &diagnostic, diagnostics) { + if (text.isEmpty()) + text += diagnosticText(diagnostic); + else + text += QStringLiteral("\n\n\n") + diagnosticText(diagnostic); + } + + return text; +} + +} // anonymous namespace + +void ClangEditorDocumentProcessor::showDiagnosticTooltip(const QPoint &point, + QWidget *parent, + uint line, + uint column) const +{ + const QVector diagnostics + = m_diagnosticManager.diagnosticsAt(line, column); + + const QString tooltipText = generateTooltipText(diagnostics); + + ::Utils::ToolTip::show(point, tooltipText, parent); +} + ClangBackEnd::FileContainer ClangEditorDocumentProcessor::fileContainerWithArguments() const { return fileContainerWithArguments(m_projectPart.data()); diff --git a/src/plugins/clangcodemodel/clangeditordocumentprocessor.h b/src/plugins/clangcodemodel/clangeditordocumentprocessor.h index e5e72b3b4b2..a2a9755e25f 100644 --- a/src/plugins/clangcodemodel/clangeditordocumentprocessor.h +++ b/src/plugins/clangcodemodel/clangeditordocumentprocessor.h @@ -76,6 +76,12 @@ public: TextEditor::QuickFixOperations extraRefactoringOperations(const TextEditor::AssistInterface &assistInterface) override; + bool hasDiagnosticsAt(uint line, uint column) const override; + void showDiagnosticTooltip(const QPoint &point, + QWidget *parent, + uint line, + uint column) const override; + ClangBackEnd::FileContainer fileContainerWithArguments() const; void clearDiagnosticsWithFixIts(); diff --git a/src/plugins/clangcodemodel/clangisdiagnosticrelatedtolocation.h b/src/plugins/clangcodemodel/clangisdiagnosticrelatedtolocation.h new file mode 100644 index 00000000000..8d44634bacd --- /dev/null +++ b/src/plugins/clangcodemodel/clangisdiagnosticrelatedtolocation.h @@ -0,0 +1,78 @@ +/**************************************************************************** +** +** 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. +** +****************************************************************************/ + +#pragma once + +#include + +namespace ClangCodeModel { +namespace Internal { + +bool isWithinRange(const ClangBackEnd::SourceRangeContainer &range, + uint line, + uint column) +{ + const ClangBackEnd::SourceLocationContainer startLocation = range.start(); + const ClangBackEnd::SourceLocationContainer endLocation = range.end(); + + return startLocation.line() <= line + && startLocation.column() <= column + && line <= endLocation.line() + && column <= endLocation.column(); +} + +bool isWithinOneRange(const QVector &ranges, + uint line, + uint column) +{ + foreach (const ClangBackEnd::SourceRangeContainer &range, ranges) { + if (isWithinRange(range, line, column)) + return true; + } + + return false; +} + +bool isDiagnosticRelatedToLocation(const ClangBackEnd::DiagnosticContainer &diagnostic, + const QVector &additionalRanges, + uint line, + uint column) +{ + const ClangBackEnd::SourceLocationContainer location = diagnostic.location(); + + if (location.line() == line && location.column() == column) + return true; + + if (isWithinOneRange(additionalRanges, line, column)) + return true; + + if (isWithinOneRange(diagnostic.ranges(), line, column)) + return true; + + return false; +} + +} // namespace Internal +} // namespace ClangCodeModel diff --git a/src/plugins/cppeditor/cpphoverhandler.cpp b/src/plugins/cppeditor/cpphoverhandler.cpp index 8cbe8d08913..addeb81c759 100644 --- a/src/plugins/cppeditor/cpphoverhandler.cpp +++ b/src/plugins/cppeditor/cpphoverhandler.cpp @@ -29,6 +29,10 @@ #include "cppelementevaluator.h" #include +#include +#include +#include +#include #include #include @@ -39,12 +43,55 @@ using namespace Core; using namespace TextEditor; +namespace { + +CppTools::BaseEditorDocumentProcessor *editorDocumentProcessor(TextEditorWidget *editorWidget) +{ + const QString filePath = editorWidget->textDocument()->filePath().toString(); + auto cppModelManager = CppTools::CppModelManager::instance(); + CppTools::CppEditorDocumentHandle *editorHandle = cppModelManager->cppEditorDocument(filePath); + + if (editorHandle) + return editorHandle->processor(); + + return 0; +} + +bool editorDocumentProcessorHasDiagnosticAt(TextEditorWidget *editorWidget, int pos) +{ + if (CppTools::BaseEditorDocumentProcessor *processor = editorDocumentProcessor(editorWidget)) { + int line, column; + if (Convenience::convertPosition(editorWidget->document(), pos, &line, &column)) + return processor->hasDiagnosticsAt(line, column); + } + + return false; +} + +void processWithEditorDocumentProcessor(TextEditorWidget *editorWidget, + const QPoint &point, + int position) +{ + if (CppTools::BaseEditorDocumentProcessor *processor = editorDocumentProcessor(editorWidget)) { + int line, column; + if (Convenience::convertPosition(editorWidget->document(), position, &line, &column)) + processor->showDiagnosticTooltip(point, editorWidget, line, column); + } +} + +} // anonymous namespace + namespace CppEditor { namespace Internal { void CppHoverHandler::identifyMatch(TextEditorWidget *editorWidget, int pos) { - if (!editorWidget->extraSelectionTooltip(pos).isEmpty()) { + m_positionForEditorDocumentProcessor = -1; + + if (editorDocumentProcessorHasDiagnosticAt(editorWidget, pos)) { + setIsDiagnosticTooltip(true); + m_positionForEditorDocumentProcessor = pos; + } else if (!editorWidget->extraSelectionTooltip(pos).isEmpty()) { setToolTip(editorWidget->extraSelectionTooltip(pos)); } else { QTextCursor tc(editorWidget->document()); @@ -82,6 +129,9 @@ void CppHoverHandler::identifyMatch(TextEditorWidget *editorWidget, int pos) void CppHoverHandler::decorateToolTip() { + if (m_positionForEditorDocumentProcessor != -1) + return; + if (Qt::mightBeRichText(toolTip())) setToolTip(toolTip().toHtmlEscaped()); @@ -114,5 +164,14 @@ void CppHoverHandler::decorateToolTip() } } +void CppHoverHandler::operateTooltip(TextEditor::TextEditorWidget *editorWidget, + const QPoint &point) +{ + if (m_positionForEditorDocumentProcessor != -1) + processWithEditorDocumentProcessor(editorWidget, point, m_positionForEditorDocumentProcessor); + else + BaseHoverHandler::operateTooltip(editorWidget, point); +} + } // namespace Internal } // namespace CppEditor diff --git a/src/plugins/cppeditor/cpphoverhandler.h b/src/plugins/cppeditor/cpphoverhandler.h index eeda39a93d1..7562f42e257 100644 --- a/src/plugins/cppeditor/cpphoverhandler.h +++ b/src/plugins/cppeditor/cpphoverhandler.h @@ -36,6 +36,10 @@ class CppHoverHandler : public TextEditor::BaseHoverHandler private: void identifyMatch(TextEditor::TextEditorWidget *editorWidget, int pos) override; void decorateToolTip() override; + void operateTooltip(TextEditor::TextEditorWidget *editorWidget, const QPoint &point) override; + +private: + int m_positionForEditorDocumentProcessor = -1; }; } // namespace Internal diff --git a/src/plugins/cpptools/baseeditordocumentprocessor.cpp b/src/plugins/cpptools/baseeditordocumentprocessor.cpp index d892d22847c..c437bceb80f 100644 --- a/src/plugins/cpptools/baseeditordocumentprocessor.cpp +++ b/src/plugins/cpptools/baseeditordocumentprocessor.cpp @@ -58,6 +58,15 @@ BaseEditorDocumentProcessor::extraRefactoringOperations(const TextEditor::Assist return TextEditor::QuickFixOperations(); } +bool BaseEditorDocumentProcessor::hasDiagnosticsAt(uint, uint) const +{ + return false; +} + +void BaseEditorDocumentProcessor::showDiagnosticTooltip(const QPoint &, QWidget *, uint, uint) const +{ +} + void BaseEditorDocumentProcessor::runParser(QFutureInterface &future, BaseEditorDocumentParser::Ptr parser, const WorkingCopy workingCopy) diff --git a/src/plugins/cpptools/baseeditordocumentprocessor.h b/src/plugins/cpptools/baseeditordocumentprocessor.h index 927fd0defb0..886e6bc98c2 100644 --- a/src/plugins/cpptools/baseeditordocumentprocessor.h +++ b/src/plugins/cpptools/baseeditordocumentprocessor.h @@ -65,6 +65,12 @@ public: virtual TextEditor::QuickFixOperations extraRefactoringOperations(const TextEditor::AssistInterface &assistInterface); + virtual bool hasDiagnosticsAt(uint line, uint column) const; + virtual void showDiagnosticTooltip(const QPoint &point, + QWidget *parent, + uint line, + uint column) const; + signals: // Signal interface to implement void codeWarningsUpdated(unsigned revision, diff --git a/tests/unit/unittest/clangisdiagnosticrelatedtolocationtest.cpp b/tests/unit/unittest/clangisdiagnosticrelatedtolocationtest.cpp new file mode 100644 index 00000000000..3ef5d4310f4 --- /dev/null +++ b/tests/unit/unittest/clangisdiagnosticrelatedtolocationtest.cpp @@ -0,0 +1,146 @@ +/**************************************************************************** +** +** 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. +** +****************************************************************************/ + +#include +#include + +#include +#include +#include +#include "gtest-qt-printing.h" + +using ClangBackEnd::DiagnosticContainer; +using ClangBackEnd::SourceLocationContainer; +using ClangBackEnd::SourceRangeContainer; +using ClangCodeModel::Internal::isDiagnosticRelatedToLocation; + +namespace { + +SourceRangeContainer createRange(uint startLine, + uint startColumn, + uint endLine, + uint endColumn) +{ + const SourceLocationContainer startLocation(Utf8String(), startLine, startColumn); + const SourceLocationContainer endLocation(Utf8String(), endLine, endColumn); + + return SourceRangeContainer(startLocation, endLocation); +} + +DiagnosticContainer createDiagnosticWithLocation(uint line, uint column) +{ + const SourceLocationContainer location(Utf8String(), line, column); + + return DiagnosticContainer(Utf8String(), + Utf8String(), + std::pair(), + ClangBackEnd::DiagnosticSeverity::Error, + location, + QVector(), + QVector(), + QVector()); +} + +DiagnosticContainer createDiagnosticWithRange(uint startLine, + uint startColumn, + uint endLine, + uint endColumn) +{ + const SourceRangeContainer range = createRange(startLine, startColumn, endLine, endColumn); + + return DiagnosticContainer(Utf8String(), + Utf8String(), + std::pair(), + ClangBackEnd::DiagnosticSeverity::Error, + SourceLocationContainer(), + {range}, + QVector(), + QVector()); +} + +class ClangIsDiagnosticRelatedToLocation : public ::testing::Test +{ +protected: + const QVector emptyRanges; +}; + +TEST_F(ClangIsDiagnosticRelatedToLocation, MatchLocation) +{ + const DiagnosticContainer diagnostic = createDiagnosticWithLocation(5, 5); + + ASSERT_TRUE(isDiagnosticRelatedToLocation(diagnostic, emptyRanges, 5, 5)); +} + +TEST_F(ClangIsDiagnosticRelatedToLocation, DoNotMatchLocation) +{ + const DiagnosticContainer diagnostic = createDiagnosticWithLocation(5, 5); + + ASSERT_FALSE(isDiagnosticRelatedToLocation(diagnostic, emptyRanges, 5, 4)); +} + +TEST_F(ClangIsDiagnosticRelatedToLocation, MatchStartRange) +{ + const DiagnosticContainer diagnostic = createDiagnosticWithRange(5, 5, 5, 10); + + ASSERT_TRUE(isDiagnosticRelatedToLocation(diagnostic, emptyRanges, 5, 5)); +} + +TEST_F(ClangIsDiagnosticRelatedToLocation, MatchWithinRange) +{ + const DiagnosticContainer diagnostic = createDiagnosticWithRange(5, 5, 5, 10); + + ASSERT_TRUE(isDiagnosticRelatedToLocation(diagnostic, emptyRanges, 5, 7)); +} + +TEST_F(ClangIsDiagnosticRelatedToLocation, MatchEndRange) +{ + const DiagnosticContainer diagnostic = createDiagnosticWithRange(5, 5, 5, 10); + + ASSERT_TRUE(isDiagnosticRelatedToLocation(diagnostic, emptyRanges, 5, 10)); +} + +TEST_F(ClangIsDiagnosticRelatedToLocation, DoNoMatchBeforeRangeStart) +{ + const DiagnosticContainer diagnostic = createDiagnosticWithRange(5, 5, 5, 10); + + ASSERT_FALSE(isDiagnosticRelatedToLocation(diagnostic, emptyRanges, 5, 4)); +} + +TEST_F(ClangIsDiagnosticRelatedToLocation, DoNoMatchAfterRangeEnd) +{ + const DiagnosticContainer diagnostic = createDiagnosticWithRange(5, 5, 5, 10); + + ASSERT_FALSE(isDiagnosticRelatedToLocation(diagnostic, emptyRanges, 5, 11)); +} + +TEST_F(ClangIsDiagnosticRelatedToLocation, MatchInAdditionalRange) +{ + const QVector additionalRanges{ createRange(5, 5, 5, 10) }; + const DiagnosticContainer diagnostic = createDiagnosticWithLocation(1, 1); + + ASSERT_TRUE(isDiagnosticRelatedToLocation(diagnostic, additionalRanges, 5, 7)); +} + +} // anonymous diff --git a/tests/unit/unittest/unittest.pro b/tests/unit/unittest/unittest.pro index 89c101a0632..df91f1d9a4d 100644 --- a/tests/unit/unittest/unittest.pro +++ b/tests/unit/unittest/unittest.pro @@ -60,7 +60,8 @@ SOURCES += \ skippedsourcerangestest.cpp \ highlightingmarksreportertest.cpp \ chunksreportedmonitor.cpp \ - unsavedfiletest.cpp + unsavedfiletest.cpp \ + clangisdiagnosticrelatedtolocationtest.cpp HEADERS += \ gtest-qt-printing.h \