Clang: Improve ProjectPartArtefact

Empty strings were only handled by accident and wrongly formatted ones
were never handled. Now we do nothing for empty strings and throw an
exception from wrongly formatted strings. The exceptions are very helpful
in the test code because the show errors the the testing data.

Change-Id: I87d1678eda7502fdc3f74f51fad491803d28582b
Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
This commit is contained in:
Marco Bubke
2018-02-07 16:11:28 +01:00
parent 980fd54ea1
commit b5c3d5a40d
11 changed files with 203 additions and 52 deletions

View File

@@ -18,7 +18,7 @@ HEADERS += \
$$PWD/usedmacro.h \ $$PWD/usedmacro.h \
$$PWD/sourcedependency.h \ $$PWD/sourcedependency.h \
$$PWD/filestatus.h \ $$PWD/filestatus.h \
$$PWD/projectpartartefacts.h $$PWD/projectpartartefact.h
!isEmpty(LIBTOOLING_LIBS) { !isEmpty(LIBTOOLING_LIBS) {
SOURCES += \ SOURCES += \
@@ -65,4 +65,6 @@ SOURCES += \
$$PWD/sourcerangefilter.cpp \ $$PWD/sourcerangefilter.cpp \
$$PWD/symbolindexer.cpp \ $$PWD/symbolindexer.cpp \
$$PWD/symbolentry.cpp \ $$PWD/symbolentry.cpp \
$$PWD/symbolstorageinterface.cpp $$PWD/symbolstorageinterface.cpp \
$$PWD/projectpartartefact.cpp

View File

@@ -0,0 +1,105 @@
/****************************************************************************
**
** 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 "projectpartartefact.h"
#include <utils/algorithm.h>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
namespace ClangBackEnd {
ProjectPartArtefact::ProjectPartArtefact(Utils::SmallStringView compilerArgumentsText, Utils::SmallStringView compilerMacrosText, int projectPartId)
: compilerArguments(toStringVector(compilerArgumentsText)),
compilerMacros(toCompilerMacros(compilerMacrosText)),
projectPartId(projectPartId)
{
}
Utils::SmallStringVector ProjectPartArtefact::toStringVector(Utils::SmallStringView jsonText)
{
if (jsonText.isEmpty())
return {};
QJsonDocument document = createJsonDocument(jsonText, "Compiler arguments parsing error");
return Utils::transform<Utils::SmallStringVector>(document.array(), [] (const QJsonValue &value) {
return Utils::SmallString{value.toString()};
});
}
CompilerMacros ProjectPartArtefact::createCompilerMacrosFromDocument(const QJsonDocument &document)
{
QJsonObject object = document.object();
CompilerMacros macros;
macros.reserve(object.size());
for (auto current = object.constBegin(); current != object.constEnd(); ++current)
macros.emplace_back(current.key(), current.value().toString());
std::sort(macros.begin(), macros.end());
return macros;
}
CompilerMacros ProjectPartArtefact::toCompilerMacros(Utils::SmallStringView jsonText)
{
if (jsonText.isEmpty())
return {};
QJsonDocument document = createJsonDocument(jsonText, "Compiler macros parsing error");
return createCompilerMacrosFromDocument(document);
}
QJsonDocument ProjectPartArtefact::createJsonDocument(Utils::SmallStringView jsonText,
const char *whatError)
{
QJsonParseError error;
QJsonDocument document = QJsonDocument::fromJson(QByteArray::fromRawData(jsonText.data(),
jsonText.size()),
&error);
checkError(whatError, error);
return document;
}
void ProjectPartArtefact::checkError(const char *whatError, const QJsonParseError &error)
{
if (error.error != QJsonParseError::NoError) {
throw ProjectPartArtefactParseError(whatError,
error.errorString());
}
}
bool operator==(const ProjectPartArtefact &first, const ProjectPartArtefact &second)
{
return first.compilerArguments == second.compilerArguments
&& first.compilerMacros == second.compilerMacros;
}
} // namespace ClangBackEnd

View File

@@ -25,14 +25,14 @@
#pragma once #pragma once
#include <utils/algorithm.h> #include "projectpartartefactexception.h"
#include <utils/smallstringvector.h> #include <utils/smallstringvector.h>
#include <compilermacro.h> #include <compilermacro.h>
#include <QJsonArray> QT_FORWARD_DECLARE_CLASS(QJsonDocument)
#include <QJsonDocument> QT_FORWARD_DECLARE_STRUCT(QJsonParseError)
#include <QJsonObject>
namespace ClangBackEnd { namespace ClangBackEnd {
@@ -41,45 +41,21 @@ class ProjectPartArtefact
public: public:
ProjectPartArtefact(Utils::SmallStringView compilerArgumentsText, ProjectPartArtefact(Utils::SmallStringView compilerArgumentsText,
Utils::SmallStringView compilerMacrosText, Utils::SmallStringView compilerMacrosText,
int projectPartId) int projectPartId);
: compilerArguments(toStringVector(compilerArgumentsText)),
compilerMacros(toCompilerMacros(compilerMacrosText)),
projectPartId(projectPartId)
{
}
static static
Utils::SmallStringVector toStringVector(Utils::SmallStringView jsonText) Utils::SmallStringVector toStringVector(Utils::SmallStringView jsonText);
{
QJsonDocument document = QJsonDocument::fromJson(QByteArray::fromRawData(jsonText.data(),
jsonText.size()));
return Utils::transform<Utils::SmallStringVector>(document.array(), [] (const QJsonValue &value) {
return Utils::SmallString{value.toString()};
});
}
static static
CompilerMacros toCompilerMacros(Utils::SmallStringView jsonText) CompilerMacros createCompilerMacrosFromDocument(const QJsonDocument &document);
{ static
QJsonDocument document = QJsonDocument::fromJson(QByteArray::fromRawData(jsonText.data(), CompilerMacros toCompilerMacros(Utils::SmallStringView jsonText);
jsonText.size())); static
QJsonObject object = document.object(); QJsonDocument createJsonDocument(Utils::SmallStringView jsonText,
CompilerMacros macros; const char *whatError);
macros.reserve(object.size()); static
void checkError(const char *whatError, const QJsonParseError &error);
for (auto current = object.constBegin(); current != object.constEnd(); ++current)
macros.emplace_back(current.key(), current.value().toString());
return macros;
}
friend friend
bool operator==(const ProjectPartArtefact &first, const ProjectPartArtefact &second) bool operator==(const ProjectPartArtefact &first, const ProjectPartArtefact &second);
{
return first.compilerArguments == second.compilerArguments
&& first.compilerMacros == second.compilerMacros;
}
public: public:
Utils::SmallStringVector compilerArguments; Utils::SmallStringVector compilerArguments;
@@ -88,4 +64,4 @@ public:
}; };
using ProjectPartArtefacts = std::vector<ProjectPartArtefact>; using ProjectPartArtefacts = std::vector<ProjectPartArtefact>;
} } // namespace ClangBackEnd

View File

@@ -0,0 +1,40 @@
/****************************************************************************
**
** 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 <sqliteexception.h>
namespace ClangBackEnd {
class ProjectPartArtefactParseError : public Sqlite::Exception
{
public:
ProjectPartArtefactParseError(const char *whatErrorHasHappen,
Utils::SmallString &&errorMessage)
: Exception(whatErrorHasHappen, std::move(errorMessage))
{
}
};
}

View File

@@ -27,7 +27,7 @@
#include "filestatus.h" #include "filestatus.h"
#include "projectpartentry.h" #include "projectpartentry.h"
#include "projectpartartefacts.h" #include "projectpartartefact.h"
#include "sourcelocationentry.h" #include "sourcelocationentry.h"
#include "sourcedependency.h" #include "sourcedependency.h"
#include "symbolentry.h" #include "symbolentry.h"

View File

@@ -40,7 +40,7 @@
#include <fulltokeninfo.h> #include <fulltokeninfo.h>
#include <nativefilepath.h> #include <nativefilepath.h>
#include <precompiledheadersupdatedmessage.h> #include <precompiledheadersupdatedmessage.h>
#include <projectpartartefacts.h> #include <projectpartartefact.h>
#include <sourcedependency.h> #include <sourcedependency.h>
#include <sourcelocationentry.h> #include <sourcelocationentry.h>
#include <sourcelocationscontainer.h> #include <sourcelocationscontainer.h>

View File

@@ -31,7 +31,7 @@
#include <filepathstoragesources.h> #include <filepathstoragesources.h>
#include <stringcachefwd.h> #include <stringcachefwd.h>
#include <projectpartartefacts.h> #include <projectpartartefact.h>
#include <cpptools/usages.h> #include <cpptools/usages.h>

View File

@@ -25,7 +25,7 @@
#include "googletest.h" #include "googletest.h"
#include <projectpartartefacts.h> #include <projectpartartefact.h>
namespace { namespace {
@@ -38,6 +38,19 @@ TEST(ProjectPartArtefact, CompilerArguments)
ASSERT_THAT(artefact.compilerArguments, ElementsAre(Eq("-DFoo"), Eq("-DBar"))); ASSERT_THAT(artefact.compilerArguments, ElementsAre(Eq("-DFoo"), Eq("-DBar")));
} }
TEST(ProjectPartArtefact, EmptyCompilerArguments)
{
ClangBackEnd::ProjectPartArtefact artefact{"", "", 1};
ASSERT_THAT(artefact.compilerArguments, IsEmpty());
}
TEST(ProjectPartArtefact, CompilerArgumentsParseError)
{
ASSERT_THROW(ClangBackEnd::ProjectPartArtefact("\"-DFoo\",\"-DBar\"]", "", 1),
ClangBackEnd::ProjectPartArtefactParseError);
}
TEST(ProjectPartArtefact, CompilerMacros) TEST(ProjectPartArtefact, CompilerMacros)
{ {
ClangBackEnd::ProjectPartArtefact artefact{"", "{\"Foo\":\"1\",\"Bar\":\"42\"}", 1}; ClangBackEnd::ProjectPartArtefact artefact{"", "{\"Foo\":\"1\",\"Bar\":\"42\"}", 1};
@@ -45,4 +58,18 @@ TEST(ProjectPartArtefact, CompilerMacros)
ASSERT_THAT(artefact.compilerMacros, ASSERT_THAT(artefact.compilerMacros,
UnorderedElementsAre(Eq(CompilerMacro{"Foo", "1"}), Eq(CompilerMacro{"Bar", "42"}))); UnorderedElementsAre(Eq(CompilerMacro{"Foo", "1"}), Eq(CompilerMacro{"Bar", "42"})));
} }
TEST(ProjectPartArtefact, EmptyCompilerMacros)
{
ClangBackEnd::ProjectPartArtefact artefact{"", "", 1};
ASSERT_THAT(artefact.compilerMacros, IsEmpty());
}
TEST(ProjectPartArtefact, CompilerMacrosParseError)
{
ASSERT_THROW(ClangBackEnd::ProjectPartArtefact("", "\"Foo\":\"1\",\"Bar\":\"42\"}", 1),
ClangBackEnd::ProjectPartArtefactParseError);
}
} }

View File

@@ -82,12 +82,12 @@ protected:
ClangBackEnd::FilePathId generatedFilePathId{1, 21}; ClangBackEnd::FilePathId generatedFilePathId{1, 21};
ProjectPartContainer projectPart1{"project1", ProjectPartContainer projectPart1{"project1",
{"-I", TESTDATA_DIR, "-Wno-pragma-once-outside-header"}, {"-I", TESTDATA_DIR, "-Wno-pragma-once-outside-header"},
{{"DEFINE", "1"}}, {{"BAR", "1"}, {"FOO", "1"}},
{header1PathId}, {header1PathId},
{main1PathId}}; {main1PathId}};
ProjectPartContainer projectPart2{"project2", ProjectPartContainer projectPart2{"project2",
{"-I", TESTDATA_DIR, "-x", "c++-header", "-Wno-pragma-once-outside-header"}, {"-I", TESTDATA_DIR, "-x", "c++-header", "-Wno-pragma-once-outside-header"},
{{"DEFINE", "1"}}, {{"BAR", "1"}, {"FOO", "0"}},
{header2PathId}, {header2PathId},
{main2PathId}}; {main2PathId}};
FileContainers unsaved{{{TESTDATA_DIR, "query_simplefunction.h"}, FileContainers unsaved{{{TESTDATA_DIR, "query_simplefunction.h"},
@@ -99,7 +99,7 @@ protected:
UsedMacros usedMacros{{"Foo", {1, 1}}}; UsedMacros usedMacros{{"Foo", {1, 1}}};
FileStatuses fileStatus{{{1, 2}, 3, 4}}; FileStatuses fileStatus{{{1, 2}, 3, 4}};
SourceDependencies sourceDependencies{{{1, 1}, {1, 2}}, {{1, 1}, {1, 3}}}; SourceDependencies sourceDependencies{{{1, 1}, {1, 2}}, {{1, 1}, {1, 3}}};
ClangBackEnd::ProjectPartArtefact artefact{"[-DFOO]", "{\"FOO\":\"1\"}", 74}; ClangBackEnd::ProjectPartArtefact artefact{"[\"-DFOO\"]", "{\"FOO\":\"1\",\"BAR\":\"1\"}", 74};
NiceMock<MockSqliteTransactionBackend> mockSqliteTransactionBackend; NiceMock<MockSqliteTransactionBackend> mockSqliteTransactionBackend;
NiceMock<MockSymbolsCollector> mockCollector; NiceMock<MockSymbolsCollector> mockCollector;
NiceMock<MockSymbolStorage> mockStorage; NiceMock<MockSymbolStorage> mockStorage;
@@ -178,10 +178,10 @@ TEST_F(SymbolIndexer, UpdateProjectPartsCallsUpdateProjectPartsInStorage)
{ {
EXPECT_CALL(mockStorage, insertOrUpdateProjectPart(Eq("project1"), EXPECT_CALL(mockStorage, insertOrUpdateProjectPart(Eq("project1"),
ElementsAre("-I", TESTDATA_DIR, "-Wno-pragma-once-outside-header"), ElementsAre("-I", TESTDATA_DIR, "-Wno-pragma-once-outside-header"),
ElementsAre(CompilerMacro{"DEFINE", "1"}))); ElementsAre(CompilerMacro{"BAR", "1"}, CompilerMacro{"FOO", "1"})));
EXPECT_CALL(mockStorage, insertOrUpdateProjectPart(Eq("project2"), EXPECT_CALL(mockStorage, insertOrUpdateProjectPart(Eq("project2"),
ElementsAre("-I", TESTDATA_DIR, "-x", "c++-header", "-Wno-pragma-once-outside-header"), ElementsAre("-I", TESTDATA_DIR, "-x", "c++-header", "-Wno-pragma-once-outside-header"),
ElementsAre(CompilerMacro{"DEFINE", "1"}))); ElementsAre(CompilerMacro{"BAR", "1"}, CompilerMacro{"FOO", "0"})));
indexer.updateProjectParts({projectPart1, projectPart2}, Utils::clone(unsaved)); indexer.updateProjectParts({projectPart1, projectPart2}, Utils::clone(unsaved));
} }

View File

@@ -90,7 +90,7 @@ protected:
{2, {"function2USR", "function2"}}}; {2, {"function2USR", "function2"}}};
SourceLocationEntries sourceLocations{{1, {1, 3}, {42, 23}, SymbolType::Declaration}, SourceLocationEntries sourceLocations{{1, {1, 3}, {42, 23}, SymbolType::Declaration},
{2, {1, 4}, {7, 11}, SymbolType::Declaration}}; {2, {1, 4}, {7, 11}, SymbolType::Declaration}};
ClangBackEnd::ProjectPartArtefact artefact{"-DFOO", "{\"FOO\":\"1\"}", 74}; ClangBackEnd::ProjectPartArtefact artefact{"[\"-DFOO\"]", "{\"FOO\":\"1\"}", 74};
Storage storage{statementFactory, filePathCache}; Storage storage{statementFactory, filePathCache};
}; };

View File

@@ -92,6 +92,7 @@ SOURCES += \
nativefilepathview-test.cpp \ nativefilepathview-test.cpp \
mocktimer.cpp \ mocktimer.cpp \
tokenprocessor-test.cpp \ tokenprocessor-test.cpp \
projectpartartefact-test.cpp \
highlightingresultreporter-test.cpp highlightingresultreporter-test.cpp
!isEmpty(LIBCLANG_LIBS) { !isEmpty(LIBCLANG_LIBS) {