Commit da27ea4d authored by Nikolai Kosjar's avatar Nikolai Kosjar
Browse files

Clang: Introduce UnsavedFile wrapper



This simplifies UnsavedFiles and makes TemporaryModifiedUnsavedFiles
useless.

Change-Id: I1896f971215ed22ce7aa7bf21b16381862b7469d
Reviewed-by: default avatarMarco Bubke <marco.bubke@theqtcompany.com>
parent 7db37916
......@@ -7,6 +7,7 @@ HEADERS += $$PWD/clangipcserver.h \
$$PWD/codecompletefailedexception.h \
$$PWD/clangcodecompleteresults.h \
$$PWD/codecompletionsextractor.h \
$$PWD/unsavedfile.h \
$$PWD/unsavedfiles.h \
$$PWD/projects.h \
$$PWD/translationunits.h \
......@@ -31,8 +32,7 @@ HEADERS += $$PWD/clangipcserver.h \
$$PWD/highlightinginformationsiterator.h \
$$PWD/skippedsourceranges.h \
$$PWD/clangtranslationunit.h \
$$PWD/clangtype.h \
$$PWD/temporarymodifiedunsavedfiles.h
$$PWD/clangtype.h
SOURCES += $$PWD/clangipcserver.cpp \
$$PWD/codecompleter.cpp \
......@@ -41,6 +41,7 @@ SOURCES += $$PWD/clangipcserver.cpp \
$$PWD/codecompletefailedexception.cpp \
$$PWD/clangcodecompleteresults.cpp \
$$PWD/codecompletionsextractor.cpp \
$$PWD/unsavedfile.cpp \
$$PWD/unsavedfiles.cpp \
$$PWD/projects.cpp \
$$PWD/translationunits.cpp \
......@@ -63,5 +64,4 @@ SOURCES += $$PWD/clangipcserver.cpp \
$$PWD/highlightinginformation.cpp \
$$PWD/skippedsourceranges.cpp \
$$PWD/clangtranslationunit.cpp \
$$PWD/clangtype.cpp \
$$PWD/temporarymodifiedunsavedfiles.cpp
$$PWD/clangtype.cpp
......@@ -185,6 +185,11 @@ CXTranslationUnit TranslationUnit::cxTranslationUnitWithoutReparsing() const
return d->translationUnit;
}
UnsavedFile &TranslationUnit::unsavedFile() const
{
return unsavedFiles().unsavedFile(filePath());
}
const Utf8String &TranslationUnit::filePath() const
{
checkIfNull();
......@@ -498,11 +503,6 @@ CXUnsavedFile *TranslationUnit::cxUnsavedFiles() const
return unsavedFiles().cxUnsavedFiles();
}
const std::vector<CXUnsavedFile> &TranslationUnit::cxUnsavedFilesVector() const
{
return unsavedFiles().cxUnsavedFileVector();
}
TranslationUnit::~TranslationUnit() = default;
TranslationUnit::TranslationUnit(const TranslationUnit &) = default;
......
......@@ -42,6 +42,7 @@ namespace ClangBackEnd {
class TranslationUnitData;
class CodeCompleter;
class UnsavedFile;
class UnsavedFiles;
class ProjectPart;
class DiagnosticContainer;
......@@ -93,8 +94,9 @@ public:
CXIndex index() const;
CXTranslationUnit cxTranslationUnit() const;
CXTranslationUnit cxTranslationUnitWithoutReparsing() const;
UnsavedFile &unsavedFile() const;
CXUnsavedFile * cxUnsavedFiles() const;
const std::vector<CXUnsavedFile> &cxUnsavedFilesVector() const;
uint unsavedFilesCount() const;
......
......@@ -31,7 +31,7 @@
#include "codecompletefailedexception.h"
#include "codecompletionsextractor.h"
#include "sourcelocation.h"
#include "temporarymodifiedunsavedfiles.h"
#include "unsavedfile.h"
#include "clangtranslationunit.h"
#include "sourcerange.h"
......@@ -97,22 +97,18 @@ ClangCodeCompleteResults CodeCompleter::complete(uint line,
ClangCodeCompleteResults CodeCompleter::completeWithArrowInsteadOfDot(uint line, uint column)
{
TemporaryModifiedUnsavedFiles modifiedUnsavedFiles(translationUnit.cxUnsavedFilesVector());
const SourceLocation location = translationUnit.sourceLocationAtWithoutReparsing(line,
column - 1);
const bool replaced = modifiedUnsavedFiles.replaceInFile(filePath(),
location.offset(),
1,
Utf8StringLiteral("->"));
ClangCodeCompleteResults results;
const SourceLocation location = translationUnit.sourceLocationAtWithoutReparsing(line, column - 1);
const bool replaced = translationUnit.unsavedFile().replaceAt(location.offset(),
1,
Utf8StringLiteral("->"));
if (replaced) {
results = complete(line,
column + 1,
modifiedUnsavedFiles.cxUnsavedFiles(),
modifiedUnsavedFiles.count());
translationUnit.cxUnsavedFiles(),
translationUnit.unsavedFilesCount());
if (results.hasResults())
neededCorrection_ = CompletionCorrection::DotToArrowCorrection;
......
......@@ -23,65 +23,82 @@
**
****************************************************************************/
#include "temporarymodifiedunsavedfiles.h"
#include "unsavedfile.h"
#include "utf8string.h"
#include <cstring>
#include <ostream>
namespace ClangBackEnd {
TemporaryModifiedUnsavedFiles::TemporaryModifiedUnsavedFiles(
const std::vector<CXUnsavedFile> &unsavedFilesVector)
: m_unsavedFileVector(unsavedFilesVector)
UnsavedFile::UnsavedFile(const Utf8String &filePath, const Utf8String &fileContent)
{
char *cxUnsavedFilePath = new char[filePath.byteSize() + 1];
char *cxUnsavedFileContent = new char[fileContent.byteSize() + 1];
std::memcpy(cxUnsavedFilePath, filePath.constData(), filePath.byteSize() + 1);
std::memcpy(cxUnsavedFileContent, fileContent.constData(), fileContent.byteSize() + 1);
cxUnsavedFile = CXUnsavedFile{cxUnsavedFilePath,
cxUnsavedFileContent,
ulong(fileContent.byteSize())};
}
bool TemporaryModifiedUnsavedFiles::replaceInFile(const Utf8String &filePath,
uint offset,
uint length,
const Utf8String &replacement)
UnsavedFile::UnsavedFile(UnsavedFile &&other) noexcept
: cxUnsavedFile(other.cxUnsavedFile)
{
const auto isMatchingFile = [filePath] (const CXUnsavedFile &unsavedFile) {
return std::strcmp(unsavedFile.Filename, filePath.constData()) == 0;
};
const auto unsavedFileIterator = std::find_if(m_unsavedFileVector.begin(),
m_unsavedFileVector.end(),
isMatchingFile);
other.cxUnsavedFile = { nullptr, nullptr, 0UL };
}
if (unsavedFileIterator == m_unsavedFileVector.end())
return false;
UnsavedFile &UnsavedFile::operator=(UnsavedFile &&other) noexcept
{
using std::swap;
swap(this->cxUnsavedFile, other.cxUnsavedFile);
return replaceInFile_internal(*unsavedFileIterator, offset, length, replacement);
return *this;
}
CXUnsavedFile TemporaryModifiedUnsavedFiles::cxUnsavedFileAt(uint index)
const char *UnsavedFile::filePath() const
{
return m_unsavedFileVector[index];
return cxUnsavedFile.Filename;
}
CXUnsavedFile *TemporaryModifiedUnsavedFiles::cxUnsavedFiles()
bool UnsavedFile::replaceAt(uint position, uint length, const Utf8String &replacement)
{
return m_unsavedFileVector.data();
if (position < cxUnsavedFile.Length) {
Utf8String modifiedContent(cxUnsavedFile.Contents, cxUnsavedFile.Length);
modifiedContent.replace(int(position), int(length), replacement);
*this = UnsavedFile(Utf8String::fromUtf8(filePath()), modifiedContent);
return true;
}
return false;
}
uint TemporaryModifiedUnsavedFiles::count()
CXUnsavedFile *UnsavedFile::data()
{
return uint(m_unsavedFileVector.size());
return &cxUnsavedFile;
}
bool TemporaryModifiedUnsavedFiles::replaceInFile_internal(CXUnsavedFile &unsavedFile,
uint offset,
uint length,
const Utf8String &replacement)
UnsavedFile::~UnsavedFile()
{
auto modifiedContent = Utf8String::fromUtf8(unsavedFile.Contents);
modifiedContent.replace(int(offset), int(length), replacement);
unsavedFile.Contents = modifiedContent.constData();
unsavedFile.Length = uint(modifiedContent.byteSize() + 1);
m_modifiedContents.push_back(modifiedContent); // Keep the modified copy.
delete [] cxUnsavedFile.Contents;
delete [] cxUnsavedFile.Filename;
cxUnsavedFile.Contents = nullptr;
cxUnsavedFile.Filename = nullptr;
cxUnsavedFile.Length = 0;
}
return true;
void PrintTo(const UnsavedFile &unsavedFile, std::ostream *os)
{
*os << "UnsavedFile("
<< unsavedFile.cxUnsavedFile.Filename << ", "
<< unsavedFile.cxUnsavedFile.Contents << ", "
<< unsavedFile.cxUnsavedFile.Length << ")";
}
} // namespace ClangBackEnd
......@@ -23,44 +23,40 @@
**
****************************************************************************/
#ifndef CLANGBACKEND_TEMPORARYMODIFIEDUNSAVEDFILES_H
#define CLANGBACKEND_TEMPORARYMODIFIEDUNSAVEDFILES_H
#pragma once
#include <clang-c/Index.h>
#include <utf8string.h>
#include <iosfwd>
#include <vector>
class Utf8String;
namespace ClangBackEnd {
class TemporaryModifiedUnsavedFiles
using uint = unsigned int;
class UnsavedFile
{
public:
TemporaryModifiedUnsavedFiles(const std::vector<CXUnsavedFile> &unsavedFilesVector);
friend void PrintTo(const UnsavedFile &unsavedFile, std::ostream *os);
UnsavedFile(const Utf8String &filePath, const Utf8String &fileContent);
~UnsavedFile();
TemporaryModifiedUnsavedFiles(const TemporaryModifiedUnsavedFiles &) = delete;
UnsavedFile(const UnsavedFile &other) = delete;
bool operator=(const UnsavedFile &other) = delete;
bool replaceInFile(const Utf8String &filePath,
uint offset,
uint length,
const Utf8String &replacement);
UnsavedFile(UnsavedFile &&other) noexcept;
UnsavedFile &operator=(UnsavedFile &&other) noexcept;
CXUnsavedFile cxUnsavedFileAt(uint index);
CXUnsavedFile *cxUnsavedFiles();
uint count();
const char *filePath() const;
private:
bool replaceInFile_internal(CXUnsavedFile &unsavedFile,
uint offset,
uint length,
const Utf8String &replacement);
bool replaceAt(uint position, uint length, const Utf8String &replacement);
private:
std::vector<CXUnsavedFile> m_unsavedFileVector;
std::vector<Utf8String> m_modifiedContents;
CXUnsavedFile *data();
public: // for tests
CXUnsavedFile cxUnsavedFile = { nullptr, nullptr, 0UL };
};
} // namespace ClangBackEnd
#endif // CLANGBACKEND_TEMPORARYMODIFIEDUNSAVEDFILES_H
......@@ -25,8 +25,9 @@
#include "unsavedfiles.h"
#include "unsavedfile.h"
#include <algorithm>
#include <cstring>
namespace ClangBackEnd {
......@@ -34,11 +35,10 @@ class UnsavedFilesData
{
public:
UnsavedFilesData();
~UnsavedFilesData();
public:
time_point lastChangeTimePoint;
std::vector<CXUnsavedFile> cxUnsavedFiles;
std::vector<UnsavedFile> unsavedFiles;
};
UnsavedFilesData::UnsavedFilesData()
......@@ -46,14 +46,6 @@ UnsavedFilesData::UnsavedFilesData()
{
}
UnsavedFilesData::~UnsavedFilesData()
{
for (CXUnsavedFile &cxUnsavedFile : cxUnsavedFiles)
UnsavedFiles::deleteCXUnsavedFile(cxUnsavedFile);
cxUnsavedFiles.clear();
}
UnsavedFiles::UnsavedFiles()
: d(std::make_shared<UnsavedFilesData>())
{
......@@ -79,7 +71,7 @@ UnsavedFiles &UnsavedFiles::operator=(UnsavedFiles &&other)
void UnsavedFiles::createOrUpdate(const QVector<FileContainer> &fileContainers)
{
for (const FileContainer &fileContainer : fileContainers)
updateCXUnsavedFileWithFileContainer(fileContainer);
updateUnsavedFileWithFileContainer(fileContainer);
updateLastChangeTimePoint();
}
......@@ -87,33 +79,35 @@ void UnsavedFiles::createOrUpdate(const QVector<FileContainer> &fileContainers)
void UnsavedFiles::remove(const QVector<FileContainer> &fileContainers)
{
for (const FileContainer &fileContainer : fileContainers)
removeCXUnsavedFile(fileContainer);
removeUnsavedFile(fileContainer);
updateLastChangeTimePoint();
}
void UnsavedFiles::clear()
UnsavedFile &UnsavedFiles::unsavedFile(const Utf8String &filePath)
{
for (CXUnsavedFile &cxUnsavedFile : d->cxUnsavedFiles)
deleteCXUnsavedFile(cxUnsavedFile);
const auto isMatchingFile = [filePath] (const UnsavedFile &unsavedFile) {
return unsavedFile.filePath() == filePath;
};
const auto unsavedFileIterator = std::find_if(d->unsavedFiles.begin(),
d->unsavedFiles.end(),
isMatchingFile);
d->cxUnsavedFiles.clear();
updateLastChangeTimePoint();
if (unsavedFileIterator != d->unsavedFiles.end())
return *unsavedFileIterator;
static UnsavedFile defaultUnsavedFile = UnsavedFile(Utf8String(), Utf8String());
return defaultUnsavedFile;
}
uint UnsavedFiles::count() const
{
return uint(d->cxUnsavedFiles.size());
return uint(d->unsavedFiles.size());
}
CXUnsavedFile *UnsavedFiles::cxUnsavedFiles() const
{
return d->cxUnsavedFiles.data();
}
const std::vector<CXUnsavedFile> &UnsavedFiles::cxUnsavedFileVector() const
{
return d->cxUnsavedFiles;
return d->unsavedFiles.data()->data();
}
const time_point &UnsavedFiles::lastChangeTimePoint() const
......@@ -121,59 +115,35 @@ const time_point &UnsavedFiles::lastChangeTimePoint() const
return d->lastChangeTimePoint;
}
CXUnsavedFile UnsavedFiles::createCxUnsavedFile(const Utf8String &filePath, const Utf8String &fileContent)
void UnsavedFiles::updateUnsavedFileWithFileContainer(const FileContainer &fileContainer)
{
char *cxUnsavedFilePath = new char[filePath.byteSize() + 1];
char *cxUnsavedFileContent = new char[fileContent.byteSize() + 1];
std::memcpy(cxUnsavedFilePath, filePath.constData(), filePath.byteSize() + 1);
std::memcpy(cxUnsavedFileContent, fileContent.constData(), fileContent.byteSize() + 1);
return CXUnsavedFile { cxUnsavedFilePath, cxUnsavedFileContent, ulong(fileContent.byteSize())};
}
void UnsavedFiles::deleteCXUnsavedFile(CXUnsavedFile &cxUnsavedFile)
{
delete [] cxUnsavedFile.Contents;
delete [] cxUnsavedFile.Filename;
cxUnsavedFile.Contents = nullptr;
cxUnsavedFile.Filename = nullptr;
cxUnsavedFile.Length = 0;
if (fileContainer.hasUnsavedFileContent())
addOrUpdateUnsavedFile(fileContainer);
else
removeUnsavedFile(fileContainer);
}
void UnsavedFiles::updateCXUnsavedFileWithFileContainer(const FileContainer &fileContainer)
{
if (fileContainer.hasUnsavedFileContent()) {
addOrUpdateCXUnsavedFile(fileContainer);
} else {
removeCXUnsavedFile(fileContainer);
}
}
void UnsavedFiles::removeCXUnsavedFile(const FileContainer &fileContainer)
void UnsavedFiles::removeUnsavedFile(const FileContainer &fileContainer)
{
const Utf8String filePath = fileContainer.filePath();
auto removeBeginIterator = std::partition(d->cxUnsavedFiles.begin(),
d->cxUnsavedFiles.end(),
[filePath] (const CXUnsavedFile &cxUnsavedFile) { return filePath != cxUnsavedFile.Filename; });
auto removeBeginIterator = std::partition(d->unsavedFiles.begin(),
d->unsavedFiles.end(),
[filePath] (const UnsavedFile &unsavedFile) { return filePath != unsavedFile.filePath(); });
std::for_each(removeBeginIterator, d->cxUnsavedFiles.end(), UnsavedFiles::deleteCXUnsavedFile);
d->cxUnsavedFiles.erase(removeBeginIterator, d->cxUnsavedFiles.end());
d->unsavedFiles.erase(removeBeginIterator, d->unsavedFiles.end());
}
void UnsavedFiles::addOrUpdateCXUnsavedFile(const FileContainer &fileContainer)
void UnsavedFiles::addOrUpdateUnsavedFile(const FileContainer &fileContainer)
{
const Utf8String filePath = fileContainer.filePath();
const Utf8String fileContent = fileContainer.unsavedFileContent();
auto isSameFile = [filePath] (const CXUnsavedFile &cxUnsavedFile) { return filePath == cxUnsavedFile.Filename; };
auto cxUnsavedFileIterator = std::find_if(d->cxUnsavedFiles.begin(), d->cxUnsavedFiles.end(), isSameFile);
if (cxUnsavedFileIterator == d->cxUnsavedFiles.end())
d->cxUnsavedFiles.push_back(createCxUnsavedFile(filePath, fileContent));
else {
deleteCXUnsavedFile(*cxUnsavedFileIterator);
*cxUnsavedFileIterator = createCxUnsavedFile(filePath, fileContent);
}
auto isSameFile = [filePath] (const UnsavedFile &unsavedFile) { return filePath == unsavedFile.filePath(); };
auto unsavedFileIterator = std::find_if(d->unsavedFiles.begin(), d->unsavedFiles.end(), isSameFile);
if (unsavedFileIterator == d->unsavedFiles.end())
d->unsavedFiles.emplace_back(filePath, fileContent);
else
*unsavedFileIterator = UnsavedFile(filePath, fileContent);
}
void UnsavedFiles::updateLastChangeTimePoint()
......
......@@ -33,13 +33,13 @@
#include <clang-c/Index.h>
#include <chrono>
#include <vector>
#include <memory>
namespace ClangBackEnd {
using time_point = std::chrono::steady_clock::time_point;
class UnsavedFile;
class UnsavedFilesData;
class UnsavedFiles
......@@ -57,21 +57,18 @@ public:
void createOrUpdate(const QVector<FileContainer> &fileContainers);
void remove(const QVector<FileContainer> &fileContainers);
void clear();
uint count() const;
UnsavedFile &unsavedFile(const Utf8String &filePath);
uint count() const;
CXUnsavedFile *cxUnsavedFiles() const;
const std::vector<CXUnsavedFile> &cxUnsavedFileVector() const;
const time_point &lastChangeTimePoint() const;
private:
CXUnsavedFile createCxUnsavedFile(const Utf8String &filePath, const Utf8String &fileContent);
static void deleteCXUnsavedFile(CXUnsavedFile &cxUnsavedFile);
void updateCXUnsavedFileWithFileContainer(const FileContainer &fileContainer);
void removeCXUnsavedFile(const FileContainer &fileContainer);
void addOrUpdateCXUnsavedFile(const FileContainer &fileContainer);
void updateUnsavedFileWithFileContainer(const FileContainer &fileContainer);
void removeUnsavedFile(const FileContainer &fileContainer);
void addOrUpdateUnsavedFile(const FileContainer &fileContainer);
void updateLastChangeTimePoint();
private:
......
......@@ -548,7 +548,6 @@ TEST_F(CodeCompletionsExtractor, UnsavedFile)
unsavedFiles.createOrUpdate({unsavedDataFileContainer(TESTDATA_DIR"/complete_extractor_function.cpp",
TESTDATA_DIR"/complete_extractor_function_unsaved.cpp")});
ClangCodeCompleteResults completeResults(getResults(translationUnit, 20));
unsavedFiles.clear();
::CodeCompletionsExtractor extractor(completeResults.data());
......@@ -566,7 +565,6 @@ TEST_F(CodeCompletionsExtractor, ChangeUnsavedFile)
unsavedFiles.createOrUpdate({unsavedDataFileContainer(TESTDATA_DIR"/complete_extractor_function.cpp",
TESTDATA_DIR"/complete_extractor_function_unsaved_2.cpp")});
completeResults = getResults(translationUnit, 20);
unsavedFiles.clear();
::CodeCompletionsExtractor extractor(completeResults.data());
......
/****************************************************************************
**
** 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 <filecontainer.h>
#include <temporarymodifiedunsavedfiles.h>
#include <unsavedfiles.h>
#include <gmock/gmock.h>
#include <gmock/gmock-matchers.h>
#include <gtest/gtest.h>
#include "gtest-qt-printing.h"
using ClangBackEnd::FileContainer;
using ClangBackEnd::TemporaryModifiedUnsavedFiles;
using ClangBackEnd::UnsavedFiles;
using testing::Eq;
namespace {
class TemporaryModifiedUnsavedFiles : public ::testing::Test
{
protected:
ClangBackEnd::UnsavedFiles unsavedFiles;
FileContainer fileContainer{Utf8StringLiteral("file1.h"),
Utf8StringLiteral("projectPartId"),
Utf8StringLiteral("void f() { foo. }"),
true};
Utf8String someNotExistingFilePath{Utf8StringLiteral("nonExistingFile.cpp")};
};
TEST_F(TemporaryModifiedUnsavedFiles, HasZeroFilesOnCreation)
{
::TemporaryModifiedUnsavedFiles files(unsavedFiles.cxUnsavedFileVector());
ASSERT_THAT(files.count(), Eq(0L));
}