2013-07-25 11:21:31 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
|
** Contact: http://www.qt.io/licensing
|
2013-07-25 11:21:31 +02:00
|
|
|
**
|
|
|
|
|
** 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
|
2015-01-14 18:07:15 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms and
|
|
|
|
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
2014-10-01 13:21:18 +02:00
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2013-07-25 11:21:31 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2013-07-25 11:21:31 +02:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2013-07-25 11:21:31 +02:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "cpptoolsplugin.h"
|
|
|
|
|
|
2014-11-28 12:03:58 +01:00
|
|
|
#include "baseeditordocumentprocessor.h"
|
2013-07-25 11:21:31 +02:00
|
|
|
#include "cppmodelmanager.h"
|
2014-05-16 15:51:04 -04:00
|
|
|
#include "cppsourceprocessertesthelper.h"
|
|
|
|
|
#include "cppsourceprocessor.h"
|
2013-12-16 16:02:45 +01:00
|
|
|
#include "cpptoolstestcase.h"
|
2014-08-19 15:59:29 +02:00
|
|
|
#include "editordocumenthandle.h"
|
2013-07-25 11:21:31 +02:00
|
|
|
|
2014-12-05 11:56:06 +01:00
|
|
|
#include <coreplugin/testdatadir.h>
|
2014-09-26 09:14:03 +02:00
|
|
|
#include <texteditor/texteditor.h>
|
2014-02-19 11:39:06 -03:00
|
|
|
|
2013-07-25 11:21:31 +02:00
|
|
|
#include <cplusplus/CppDocument.h>
|
|
|
|
|
#include <utils/fileutils.h>
|
|
|
|
|
|
2013-09-04 13:04:29 +02:00
|
|
|
#include <QFile>
|
|
|
|
|
#include <QFileInfo>
|
2013-07-25 11:21:31 +02:00
|
|
|
#include <QtTest>
|
|
|
|
|
|
|
|
|
|
using namespace CPlusPlus;
|
|
|
|
|
using namespace CppTools;
|
2013-12-19 01:30:49 +01:00
|
|
|
using namespace CppTools::Tests;
|
2013-07-25 11:21:31 +02:00
|
|
|
using namespace CppTools::Internal;
|
|
|
|
|
|
|
|
|
|
typedef Document::Include Include;
|
|
|
|
|
|
|
|
|
|
class SourcePreprocessor
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
SourcePreprocessor()
|
|
|
|
|
: m_cmm(CppModelManager::instance())
|
|
|
|
|
{
|
|
|
|
|
cleanUp();
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-09 18:42:15 +01:00
|
|
|
Document::Ptr run(const QString &filePath)
|
2013-07-25 11:21:31 +02:00
|
|
|
{
|
2014-06-06 14:41:19 +02:00
|
|
|
QScopedPointer<CppSourceProcessor> sourceProcessor(
|
|
|
|
|
CppModelManager::createSourceProcessor());
|
2014-06-25 17:23:19 +02:00
|
|
|
const ProjectPart::HeaderPath hp(TestIncludePaths::directoryOfTestFile(),
|
|
|
|
|
ProjectPart::HeaderPath::IncludePath);
|
|
|
|
|
sourceProcessor->setHeaderPaths(ProjectPart::HeaderPaths() << hp);
|
2014-12-09 18:42:15 +01:00
|
|
|
sourceProcessor->run(filePath);
|
2013-07-25 11:21:31 +02:00
|
|
|
|
2014-12-09 18:42:15 +01:00
|
|
|
Document::Ptr document = m_cmm->document(filePath);
|
2013-09-04 13:04:29 +02:00
|
|
|
return document;
|
2013-07-25 11:21:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~SourcePreprocessor()
|
|
|
|
|
{
|
|
|
|
|
cleanUp();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
void cleanUp()
|
|
|
|
|
{
|
|
|
|
|
m_cmm->GC();
|
|
|
|
|
QVERIFY(m_cmm->snapshot().isEmpty());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
CppModelManager *m_cmm;
|
|
|
|
|
};
|
|
|
|
|
|
2014-02-19 09:05:29 -03:00
|
|
|
/// Check: Resolved and unresolved includes are properly tracked.
|
2014-05-16 15:51:04 -04:00
|
|
|
void CppToolsPlugin::test_cppsourceprocessor_includes_resolvedUnresolved()
|
2013-07-25 11:21:31 +02:00
|
|
|
{
|
2014-12-09 18:42:15 +01:00
|
|
|
const QString testFilePath
|
|
|
|
|
= TestIncludePaths::testFilePath(QLatin1String("test_main_resolvedUnresolved.cpp"));
|
2013-07-25 11:21:31 +02:00
|
|
|
|
|
|
|
|
SourcePreprocessor processor;
|
2014-12-09 18:42:15 +01:00
|
|
|
Document::Ptr document = processor.run(testFilePath);
|
2013-07-25 11:21:31 +02:00
|
|
|
QVERIFY(document);
|
|
|
|
|
|
|
|
|
|
const QList<Document::Include> resolvedIncludes = document->resolvedIncludes();
|
2014-02-19 09:05:29 -03:00
|
|
|
QCOMPARE(resolvedIncludes.size(), 1);
|
|
|
|
|
QCOMPARE(resolvedIncludes.at(0).type(), Client::IncludeLocal);
|
2013-07-25 11:21:31 +02:00
|
|
|
QCOMPARE(resolvedIncludes.at(0).unresolvedFileName(), QLatin1String("header.h"));
|
|
|
|
|
const QString expectedResolvedFileName
|
2013-12-19 01:30:49 +01:00
|
|
|
= TestIncludePaths::testFilePath(QLatin1String("header.h"));
|
2013-07-25 11:21:31 +02:00
|
|
|
QCOMPARE(resolvedIncludes.at(0).resolvedFileName(), expectedResolvedFileName);
|
|
|
|
|
|
|
|
|
|
const QList<Document::Include> unresolvedIncludes = document->unresolvedIncludes();
|
2014-02-19 09:05:29 -03:00
|
|
|
QCOMPARE(unresolvedIncludes.size(), 1);
|
|
|
|
|
QCOMPARE(unresolvedIncludes.at(0).type(), Client::IncludeLocal);
|
2013-07-25 11:21:31 +02:00
|
|
|
QCOMPARE(unresolvedIncludes.at(0).unresolvedFileName(), QLatin1String("notresolvable.h"));
|
|
|
|
|
QVERIFY(unresolvedIncludes.at(0).resolvedFileName().isEmpty());
|
|
|
|
|
}
|
2014-02-19 11:39:06 -03:00
|
|
|
|
|
|
|
|
/// Check: Avoid self-include entries due to cyclic includes.
|
2014-05-16 15:51:04 -04:00
|
|
|
void CppToolsPlugin::test_cppsourceprocessor_includes_cyclic()
|
2014-02-19 11:39:06 -03:00
|
|
|
{
|
|
|
|
|
const QString fileName1 = TestIncludePaths::testFilePath(QLatin1String("cyclic1.h"));
|
|
|
|
|
const QString fileName2 = TestIncludePaths::testFilePath(QLatin1String("cyclic2.h"));
|
2014-09-04 14:59:50 +02:00
|
|
|
const QSet<QString> sourceFiles = QSet<QString>() << fileName1 << fileName2;
|
2014-02-19 11:39:06 -03:00
|
|
|
|
2014-08-19 15:46:50 +02:00
|
|
|
// Create global snapshot (needed in BuiltinEditorDocumentParser)
|
2014-02-19 11:39:06 -03:00
|
|
|
TestCase testCase;
|
|
|
|
|
testCase.parseFiles(sourceFiles);
|
|
|
|
|
|
|
|
|
|
// Open editor
|
|
|
|
|
TextEditor::BaseTextEditor *editor;
|
|
|
|
|
QVERIFY(testCase.openBaseTextEditor(fileName1, &editor));
|
|
|
|
|
testCase.closeEditorAtEndOfTestCase(editor);
|
|
|
|
|
|
2014-08-19 15:59:29 +02:00
|
|
|
// Check editor snapshot
|
2014-12-21 21:54:30 +02:00
|
|
|
const QString filePath = editor->document()->filePath().toString();
|
2014-11-28 12:03:58 +01:00
|
|
|
auto *processor = BaseEditorDocumentProcessor::get(filePath);
|
|
|
|
|
QVERIFY(processor);
|
|
|
|
|
Snapshot snapshot = processor->snapshot();
|
2014-02-19 11:39:06 -03:00
|
|
|
QCOMPARE(snapshot.size(), 3); // Configuration file included
|
|
|
|
|
|
|
|
|
|
// Check includes
|
|
|
|
|
Document::Ptr doc1 = snapshot.document(fileName1);
|
|
|
|
|
QVERIFY(doc1);
|
|
|
|
|
Document::Ptr doc2 = snapshot.document(fileName2);
|
|
|
|
|
QVERIFY(doc2);
|
|
|
|
|
|
|
|
|
|
QCOMPARE(doc1->unresolvedIncludes().size(), 0);
|
|
|
|
|
QCOMPARE(doc1->resolvedIncludes().size(), 1);
|
|
|
|
|
QCOMPARE(doc1->resolvedIncludes().first().resolvedFileName(), fileName2);
|
|
|
|
|
|
|
|
|
|
QCOMPARE(doc2->unresolvedIncludes().size(), 0);
|
|
|
|
|
QCOMPARE(doc2->resolvedIncludes().size(), 1);
|
|
|
|
|
QCOMPARE(doc2->resolvedIncludes().first().resolvedFileName(), fileName1);
|
|
|
|
|
}
|
2014-02-19 09:11:59 -03:00
|
|
|
|
|
|
|
|
/// Check: All include errors are reported as diagnostic messages.
|
2014-05-16 15:51:04 -04:00
|
|
|
void CppToolsPlugin::test_cppsourceprocessor_includes_allDiagnostics()
|
2014-02-19 09:11:59 -03:00
|
|
|
{
|
2014-12-09 18:42:15 +01:00
|
|
|
const QString testFilePath
|
|
|
|
|
= TestIncludePaths::testFilePath(QLatin1String("test_main_allDiagnostics.cpp"));
|
2014-02-19 09:11:59 -03:00
|
|
|
|
|
|
|
|
SourcePreprocessor processor;
|
2014-12-09 18:42:15 +01:00
|
|
|
Document::Ptr document = processor.run(testFilePath);
|
2014-02-19 09:11:59 -03:00
|
|
|
QVERIFY(document);
|
|
|
|
|
|
|
|
|
|
QCOMPARE(document->resolvedIncludes().size(), 0);
|
2014-02-24 12:12:14 -03:00
|
|
|
QCOMPARE(document->unresolvedIncludes().size(), 3);
|
|
|
|
|
QCOMPARE(document->diagnosticMessages().size(), 3);
|
2014-02-19 09:11:59 -03:00
|
|
|
}
|
2014-06-02 16:06:26 -04:00
|
|
|
|
2014-05-16 15:51:04 -04:00
|
|
|
void CppToolsPlugin::test_cppsourceprocessor_macroUses()
|
2014-06-02 16:06:26 -04:00
|
|
|
{
|
2014-12-09 18:42:15 +01:00
|
|
|
const QString testFilePath
|
|
|
|
|
= TestIncludePaths::testFilePath(QLatin1String("test_main_macroUses.cpp"));
|
2014-06-02 16:06:26 -04:00
|
|
|
|
|
|
|
|
SourcePreprocessor processor;
|
2014-12-09 18:42:15 +01:00
|
|
|
Document::Ptr document = processor.run(testFilePath);
|
2014-06-02 16:06:26 -04:00
|
|
|
QVERIFY(document);
|
|
|
|
|
const QList<Document::MacroUse> macroUses = document->macroUses();
|
|
|
|
|
QCOMPARE(macroUses.size(), 1);
|
|
|
|
|
const Document::MacroUse macroUse = macroUses.at(0);
|
|
|
|
|
QCOMPARE(macroUse.bytesBegin(), 25U);
|
|
|
|
|
QCOMPARE(macroUse.bytesEnd(), 35U);
|
|
|
|
|
QCOMPARE(macroUse.utf16charsBegin(), 25U);
|
|
|
|
|
QCOMPARE(macroUse.utf16charsEnd(), 35U);
|
|
|
|
|
QCOMPARE(macroUse.beginLine(), 2U);
|
|
|
|
|
}
|
2014-12-05 11:56:06 +01:00
|
|
|
|
|
|
|
|
static bool isMacroDefinedInDocument(const QByteArray ¯oName, const Document::Ptr &document)
|
|
|
|
|
{
|
|
|
|
|
foreach (const Macro ¯o, document->definedMacros()) {
|
|
|
|
|
if (macro.name() == macroName)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline QString _(const QByteArray &ba) { return QString::fromLatin1(ba, ba.size()); }
|
|
|
|
|
|
|
|
|
|
void CppToolsPlugin::test_cppsourceprocessor_includeNext()
|
|
|
|
|
{
|
|
|
|
|
const Core::Tests::TestDataDir data(
|
|
|
|
|
_(SRCDIR "/../../../tests/auto/cplusplus/preprocessor/data/include_next-data/"));
|
|
|
|
|
const QString mainFilePath = data.file(QLatin1String("main.cpp"));
|
|
|
|
|
const QString customHeaderPath = data.directory(QLatin1String("customIncludePath"));
|
|
|
|
|
const QString systemHeaderPath = data.directory(QLatin1String("systemIncludePath"));
|
|
|
|
|
|
2015-02-04 17:01:07 +02:00
|
|
|
CppSourceProcessor::DocumentCallback documentCallback = [](const Document::Ptr &){};
|
2014-12-05 11:56:06 +01:00
|
|
|
CppSourceProcessor sourceProcessor(Snapshot(), documentCallback);
|
|
|
|
|
ProjectPart::HeaderPaths headerPaths = ProjectPart::HeaderPaths()
|
|
|
|
|
<< ProjectPart::HeaderPath(customHeaderPath, ProjectPart::HeaderPath::IncludePath)
|
|
|
|
|
<< ProjectPart::HeaderPath(systemHeaderPath, ProjectPart::HeaderPath::IncludePath);
|
|
|
|
|
sourceProcessor.setHeaderPaths(headerPaths);
|
|
|
|
|
|
|
|
|
|
sourceProcessor.run(mainFilePath);
|
|
|
|
|
const Snapshot snapshot = sourceProcessor.snapshot();
|
|
|
|
|
QVERIFY(!snapshot.isEmpty());
|
|
|
|
|
const Document::Ptr mainDocument = snapshot.document(mainFilePath);
|
|
|
|
|
QVERIFY(mainDocument);
|
|
|
|
|
QVERIFY(isMacroDefinedInDocument("OK_FEATURE_X_ENABLED", mainDocument));
|
|
|
|
|
}
|