Commit b8a9e858 authored by Marco Bubke's avatar Marco Bubke

Clang: Don't set a translation unit dirty if the main file is deleted

If a file is deleted the translation unit should be not updated. It will be
later removed anyway.

Task-number: QTCREATORBUG-15070
Change-Id: I40c9c70629dab1d35b713d54442ffdbb2df3db43
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@theqtcompany.com>
parent d8ba9fa9
......@@ -213,7 +213,7 @@ const QSet<Utf8String> &TranslationUnit::dependedFilePaths() const
void TranslationUnit::setDirtyIfDependencyIsMet(const Utf8String &filePath)
{
if (d->dependedFilePaths.contains(filePath)) {
if (d->dependedFilePaths.contains(filePath) && isMainFileAndExistsOrIsOtherFile(filePath)) {
d->needsToBeReparsed = true;
d->hasNewDiagnostics = true;
}
......@@ -249,6 +249,14 @@ bool TranslationUnit::projectPartIsOutdated() const
return d->projectPart.lastChangeTimePoint() >= d->lastProjectPartChangeTimePoint;
}
bool TranslationUnit::isMainFileAndExistsOrIsOtherFile(const Utf8String &filePath) const
{
if (filePath == d->filePath)
return QFileInfo::exists(d->filePath);
return true;
}
void TranslationUnit::createTranslationUnitIfNeeded() const
{
if (!d->translationUnit) {
......
......@@ -111,6 +111,7 @@ private:
void updateLastProjectPartChangeTimePoint() const;
void removeTranslationUnitIfProjectPartWasChanged() const;
bool projectPartIsOutdated() const;
bool isMainFileAndExistsOrIsOtherFile(const Utf8String &filePath) const;
void createTranslationUnitIfNeeded() const;
void checkTranslationUnitErrorCode(CXErrorCode errorCode) const;
void reparseTranslationUnit() const;
......
......@@ -48,6 +48,8 @@
#include <gtest/gtest.h>
#include "gtest-qt-printing.h"
#include <QTemporaryFile>
#include <chrono>
#include <thread>
......@@ -68,12 +70,18 @@ namespace {
class TranslationUnit : public ::testing::Test
{
protected:
::TranslationUnit createTemporaryTranslationUnit();
QByteArray readContentFromTranslationUnitFile() const;
protected:
ClangBackEnd::ProjectParts projects;
ProjectPart projectPart{Utf8StringLiteral("/path/to/projectfile")};
Utf8String translationUnitFilePath = Utf8StringLiteral(TESTDATA_DIR"/translationunits.cpp");
ClangBackEnd::UnsavedFiles unsavedFiles;
ClangBackEnd::TranslationUnits translationUnits{projects, unsavedFiles};
::TranslationUnit translationUnit{Utf8StringLiteral(TESTDATA_DIR"/translationunits.cpp"),
ProjectPart(Utf8StringLiteral("/path/to/projectfile")),
::TranslationUnit translationUnit{translationUnitFilePath,
projectPart,
translationUnits};
};
......@@ -86,13 +94,13 @@ TEST_F(TranslationUnit, DefaultTranslationUnitIsInvalid)
TEST_F(TranslationUnit, ThrowExceptionForNonExistingFilePath)
{
ASSERT_THROW(::TranslationUnit(Utf8StringLiteral("file.cpp"), ProjectPart(Utf8StringLiteral("/path/to/projectfile")), translationUnits),
ASSERT_THROW(::TranslationUnit(Utf8StringLiteral("file.cpp"), projectPart, translationUnits),
ClangBackEnd::TranslationUnitFileNotExitsException);
}
TEST_F(TranslationUnit, ThrowNoExceptionForNonExistingFilePathIfDoNotCheckIfFileExistsIsSet)
{
ASSERT_NO_THROW(::TranslationUnit(Utf8StringLiteral("file.cpp"), ProjectPart(Utf8StringLiteral("/path/to/projectfile")), translationUnits, ::TranslationUnit::DoNotCheckIfFileExists));
ASSERT_NO_THROW(::TranslationUnit(Utf8StringLiteral("file.cpp"), projectPart, translationUnits, ::TranslationUnit::DoNotCheckIfFileExists));
}
TEST_F(TranslationUnit, TranslationUnitIsValid)
......@@ -172,7 +180,7 @@ TEST_F(TranslationUnit, DocumentRevisionInFileContainerGetter)
TEST_F(TranslationUnit, DependedFilePaths)
{
ASSERT_THAT(translationUnit.dependedFilePaths(),
AllOf(Contains(Utf8StringLiteral(TESTDATA_DIR"/translationunits.cpp")),
AllOf(Contains(translationUnitFilePath),
Contains(Utf8StringLiteral(TESTDATA_DIR"/translationunits.h"))));
}
......@@ -198,7 +206,7 @@ TEST_F(TranslationUnit, NeedsReparsingForMainFile)
{
translationUnit.cxTranslationUnit();
translationUnit.setDirtyIfDependencyIsMet(Utf8StringLiteral(TESTDATA_DIR"/translationunits.cpp"));
translationUnit.setDirtyIfDependencyIsMet(translationUnitFilePath);
ASSERT_TRUE(translationUnit.isNeedingReparse());
}
......@@ -235,7 +243,7 @@ TEST_F(TranslationUnit, HasNewDiagnosticsForMainFile)
{
translationUnit.cxTranslationUnit();
translationUnit.setDirtyIfDependencyIsMet(Utf8StringLiteral(TESTDATA_DIR"/translationunits.cpp"));
translationUnit.setDirtyIfDependencyIsMet(translationUnitFilePath);
ASSERT_TRUE(translationUnit.hasNewDiagnostics());
}
......@@ -243,20 +251,41 @@ TEST_F(TranslationUnit, HasNewDiagnosticsForMainFile)
TEST_F(TranslationUnit, HasNoNewDiagnosticsAfterGettingDiagnostics)
{
translationUnit.cxTranslationUnit();
translationUnit.setDirtyIfDependencyIsMet(Utf8StringLiteral(TESTDATA_DIR"/translationunits.cpp"));
translationUnit.setDirtyIfDependencyIsMet(translationUnitFilePath);
translationUnit.diagnostics();
ASSERT_FALSE(translationUnit.hasNewDiagnostics());
}
//TEST_F(TranslationUnit, ThrowParseErrorForWrongArguments)
//{
// ProjectPart project(Utf8StringLiteral("/path/to/projectfile"));
// project.setArguments({Utf8StringLiteral("-fblah")});
// TranslationUnit translationUnit(Utf8StringLiteral(TESTDATA_DIR"/complete_testfile_1.cpp"), unsavedFiles, project);
TEST_F(TranslationUnit, DeletedFileShouldBeNotSetDirty)
{
auto translationUnit = createTemporaryTranslationUnit();
translationUnit.setDirtyIfDependencyIsMet(translationUnit.filePath());
// ASSERT_THROW(translationUnit.cxTranslationUnit(), ClangBackEnd::TranslationUnitParseErrorException);
//}
ASSERT_FALSE(translationUnit.isNeedingReparse());
}
::TranslationUnit TranslationUnit::createTemporaryTranslationUnit()
{
QTemporaryFile temporaryFile;
EXPECT_TRUE(temporaryFile.open());
EXPECT_TRUE(temporaryFile.write(readContentFromTranslationUnitFile()));
::TranslationUnit translationUnit(temporaryFile.fileName(),
projectPart,
translationUnits);
return translationUnit;
}
QByteArray TranslationUnit::readContentFromTranslationUnitFile() const
{
QFile contentFile(translationUnitFilePath);
EXPECT_TRUE(contentFile.open(QIODevice::ReadOnly));
return contentFile.readAll();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment