Commit fd9293dd authored by Nikolai Kosjar's avatar Nikolai Kosjar

CppTools: CppPreprocessor: Track also unresolved includes

Change-Id: Ia36e7e7142dbc030a428369ed04e76e70e8eef0b
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@digia.com>
parent 2fe1fec1
...@@ -341,7 +341,7 @@ QString Document::fileName() const ...@@ -341,7 +341,7 @@ QString Document::fileName() const
QStringList Document::includedFiles() const QStringList Document::includedFiles() const
{ {
QStringList files; QStringList files;
foreach (const Include &i, _includes) foreach (const Include &i, _resolvedIncludes)
files.append(i.resolvedFileName()); files.append(i.resolvedFileName());
files.removeDuplicates(); files.removeDuplicates();
return files; return files;
...@@ -350,7 +350,10 @@ QStringList Document::includedFiles() const ...@@ -350,7 +350,10 @@ QStringList Document::includedFiles() const
// This assumes to be called with a QDir::cleanPath cleaned fileName. // This assumes to be called with a QDir::cleanPath cleaned fileName.
void Document::addIncludeFile(const Document::Include &include) void Document::addIncludeFile(const Document::Include &include)
{ {
_includes.append(include); if (include.resolvedFileName().isEmpty())
_unresolvedIncludes.append(include);
else
_resolvedIncludes.append(include);
} }
void Document::appendMacro(const Macro &macro) void Document::appendMacro(const Macro &macro)
...@@ -740,7 +743,8 @@ Document::Ptr Snapshot::preprocessedDocument(const QString &source, const QStrin ...@@ -740,7 +743,8 @@ Document::Ptr Snapshot::preprocessedDocument(const QString &source, const QStrin
newDoc->_revision = thisDocument->_revision; newDoc->_revision = thisDocument->_revision;
newDoc->_editorRevision = thisDocument->_editorRevision; newDoc->_editorRevision = thisDocument->_editorRevision;
newDoc->_lastModified = thisDocument->_lastModified; newDoc->_lastModified = thisDocument->_lastModified;
newDoc->_includes = thisDocument->_includes; newDoc->_resolvedIncludes = thisDocument->_resolvedIncludes;
newDoc->_unresolvedIncludes = thisDocument->_unresolvedIncludes;
} }
FastPreprocessor pp(*this); FastPreprocessor pp(*this);
...@@ -758,7 +762,8 @@ Document::Ptr Snapshot::documentFromSource(const QByteArray &preprocessedCode, ...@@ -758,7 +762,8 @@ Document::Ptr Snapshot::documentFromSource(const QByteArray &preprocessedCode,
newDoc->_revision = thisDocument->_revision; newDoc->_revision = thisDocument->_revision;
newDoc->_editorRevision = thisDocument->_editorRevision; newDoc->_editorRevision = thisDocument->_editorRevision;
newDoc->_lastModified = thisDocument->_lastModified; newDoc->_lastModified = thisDocument->_lastModified;
newDoc->_includes = thisDocument->_includes; newDoc->_resolvedIncludes = thisDocument->_resolvedIncludes;
newDoc->_unresolvedIncludes = thisDocument->_unresolvedIncludes;
newDoc->_definedMacros = thisDocument->_definedMacros; newDoc->_definedMacros = thisDocument->_definedMacros;
newDoc->_macroUses = thisDocument->_macroUses; newDoc->_macroUses = thisDocument->_macroUses;
} }
......
...@@ -312,8 +312,11 @@ public: ...@@ -312,8 +312,11 @@ public:
QStringList includedFiles() const; QStringList includedFiles() const;
void addIncludeFile(const Include &include); void addIncludeFile(const Include &include);
QList<Include> includes() const QList<Include> resolvedIncludes() const
{ return _includes; } { return _resolvedIncludes; }
QList<Include> unresolvedIncludes() const
{ return _unresolvedIncludes; }
QList<Block> skippedBlocks() const QList<Block> skippedBlocks() const
{ return _skippedBlocks; } { return _skippedBlocks; }
...@@ -348,7 +351,8 @@ private: ...@@ -348,7 +351,8 @@ private:
/// All messages generated during lexical/syntactic/semantic analysis. /// All messages generated during lexical/syntactic/semantic analysis.
QList<DiagnosticMessage> _diagnosticMessages; QList<DiagnosticMessage> _diagnosticMessages;
QList<Include> _includes; QList<Include> _resolvedIncludes;
QList<Include> _unresolvedIncludes;
QList<Macro> _definedMacros; QList<Macro> _definedMacros;
QList<Block> _skippedBlocks; QList<Block> _skippedBlocks;
QList<MacroUse> _macroUses; QList<MacroUse> _macroUses;
......
...@@ -52,7 +52,7 @@ QByteArray FastPreprocessor::run(Document::Ptr newDoc, const QString &source) ...@@ -52,7 +52,7 @@ QByteArray FastPreprocessor::run(Document::Ptr newDoc, const QString &source)
_merged.insert(fileName); _merged.insert(fileName);
mergeEnvironment(Preprocessor::configurationFileName); mergeEnvironment(Preprocessor::configurationFileName);
foreach (const Document::Include &i, doc->includes()) foreach (const Document::Include &i, doc->resolvedIncludes())
mergeEnvironment(i.resolvedFileName()); mergeEnvironment(i.resolvedFileName());
} }
...@@ -78,7 +78,7 @@ void FastPreprocessor::mergeEnvironment(const QString &fileName) ...@@ -78,7 +78,7 @@ void FastPreprocessor::mergeEnvironment(const QString &fileName)
_merged.insert(fileName); _merged.insert(fileName);
if (Document::Ptr doc = _snapshot.document(fileName)) { if (Document::Ptr doc = _snapshot.document(fileName)) {
foreach (const Document::Include &i, doc->includes()) foreach (const Document::Include &i, doc->resolvedIncludes())
mergeEnvironment(i.resolvedFileName()); mergeEnvironment(i.resolvedFileName());
_env.addMacros(doc->definedMacros()); _env.addMacros(doc->definedMacros());
......
...@@ -1454,7 +1454,7 @@ void CreateBindings::process(Document::Ptr doc) ...@@ -1454,7 +1454,7 @@ void CreateBindings::process(Document::Ptr doc)
if (! _processed.contains(globalNamespace)) { if (! _processed.contains(globalNamespace)) {
_processed.insert(globalNamespace); _processed.insert(globalNamespace);
foreach (const Document::Include &i, doc->includes()) { foreach (const Document::Include &i, doc->resolvedIncludes()) {
if (Document::Ptr incl = _snapshot.document(i.resolvedFileName())) if (Document::Ptr incl = _snapshot.document(i.resolvedFileName()))
process(incl); process(incl);
} }
......
...@@ -49,7 +49,7 @@ void SnapshotSymbolVisitor::accept(Document::Ptr doc, QSet<QString> *processed) ...@@ -49,7 +49,7 @@ void SnapshotSymbolVisitor::accept(Document::Ptr doc, QSet<QString> *processed)
if (doc && doc->globalNamespace() && ! processed->contains(doc->fileName())) { if (doc && doc->globalNamespace() && ! processed->contains(doc->fileName())) {
processed->insert(doc->fileName()); processed->insert(doc->fileName());
foreach (const Document::Include &i, doc->includes()) { foreach (const Document::Include &i, doc->resolvedIncludes()) {
if (Document::Ptr incl = _snapshot.document(i.resolvedFileName())) if (Document::Ptr incl = _snapshot.document(i.resolvedFileName()))
accept(incl, processed); accept(incl, processed);
} }
......
...@@ -177,7 +177,7 @@ void TypeOfExpression::processEnvironment(Document::Ptr doc, Environment *env, ...@@ -177,7 +177,7 @@ void TypeOfExpression::processEnvironment(Document::Ptr doc, Environment *env,
if (doc && ! processed->contains(doc->fileName())) { if (doc && ! processed->contains(doc->fileName())) {
processed->insert(doc->fileName()); processed->insert(doc->fileName());
foreach (const Document::Include &incl, doc->includes()) foreach (const Document::Include &incl, doc->resolvedIncludes())
processEnvironment(m_snapshot.document(incl.resolvedFileName()), env, processed); processEnvironment(m_snapshot.document(incl.resolvedFileName()), env, processed);
foreach (const Macro &macro, doc->definedMacros()) foreach (const Macro &macro, doc->definedMacros())
......
...@@ -1387,7 +1387,7 @@ CPPEditorWidget::Link CPPEditorWidget::findMacroLink(const QByteArray &name, ...@@ -1387,7 +1387,7 @@ CPPEditorWidget::Link CPPEditorWidget::findMacroLink(const QByteArray &name,
} }
} }
const QList<Document::Include> includes = doc->includes(); const QList<Document::Include> includes = doc->resolvedIncludes();
for (int index = includes.size() - 1; index != -1; --index) { for (int index = includes.size() - 1; index != -1; --index) {
const Document::Include &i = includes.at(index); const Document::Include &i = includes.at(index);
Link link = findMacroLink(name, snapshot.document(i.resolvedFileName()), snapshot, Link link = findMacroLink(name, snapshot.document(i.resolvedFileName()), snapshot,
...@@ -1519,7 +1519,7 @@ CPPEditorWidget::Link CPPEditorWidget::findLinkAt(const QTextCursor &cursor, boo ...@@ -1519,7 +1519,7 @@ CPPEditorWidget::Link CPPEditorWidget::findLinkAt(const QTextCursor &cursor, boo
// Handle include directives // Handle include directives
if (tk.is(T_STRING_LITERAL) || tk.is(T_ANGLE_STRING_LITERAL)) { if (tk.is(T_STRING_LITERAL) || tk.is(T_ANGLE_STRING_LITERAL)) {
const unsigned lineno = cursor.blockNumber() + 1; const unsigned lineno = cursor.blockNumber() + 1;
foreach (const Document::Include &incl, doc->includes()) { foreach (const Document::Include &incl, doc->resolvedIncludes()) {
if (incl.line() == lineno) { if (incl.line() == lineno) {
link.targetFileName = incl.resolvedFileName(); link.targetFileName = incl.resolvedFileName();
link.linkTextStart = beginOfToken + 1; link.linkTextStart = beginOfToken + 1;
......
...@@ -130,7 +130,7 @@ void CppElementEvaluator::checkDiagnosticMessage(int pos) ...@@ -130,7 +130,7 @@ void CppElementEvaluator::checkDiagnosticMessage(int pos)
bool CppElementEvaluator::matchIncludeFile(const CPlusPlus::Document::Ptr &document, unsigned line) bool CppElementEvaluator::matchIncludeFile(const CPlusPlus::Document::Ptr &document, unsigned line)
{ {
foreach (const Document::Include &includeFile, document->includes()) { foreach (const Document::Include &includeFile, document->resolvedIncludes()) {
if (includeFile.line() == line) { if (includeFile.line() == line) {
m_element = QSharedPointer<CppElement>(new CppInclude(includeFile)); m_element = QSharedPointer<CppElement>(new CppInclude(includeFile));
return true; return true;
......
...@@ -1335,7 +1335,7 @@ QList<Include> includesForSource(const QByteArray &source) ...@@ -1335,7 +1335,7 @@ QList<Include> includesForSource(const QByteArray &source)
pp.run(fileName); pp.run(fileName);
Document::Ptr document = cmm->snapshot().document(fileName); Document::Ptr document = cmm->snapshot().document(fileName);
return document->includes(); return document->resolvedIncludes();
} }
/// Check: Detection of include groups separated by new lines /// Check: Detection of include groups separated by new lines
......
...@@ -232,7 +232,7 @@ void insertNewIncludeDirective(const QString &include, CppRefactoringFilePtr fil ...@@ -232,7 +232,7 @@ void insertNewIncludeDirective(const QString &include, CppRefactoringFilePtr fil
{ {
// Find optimal position // Find optimal position
using namespace IncludeUtils; using namespace IncludeUtils;
LineForNewIncludeDirective finder(file->document(), file->cppDocument()->includes(), LineForNewIncludeDirective finder(file->document(), file->cppDocument()->resolvedIncludes(),
LineForNewIncludeDirective::IgnoreMocIncludes, LineForNewIncludeDirective::IgnoreMocIncludes,
LineForNewIncludeDirective::AutoDetect); LineForNewIncludeDirective::AutoDetect);
unsigned newLinesToPrepend = 0; unsigned newLinesToPrepend = 0;
...@@ -1530,7 +1530,7 @@ public: ...@@ -1530,7 +1530,7 @@ public:
fwdHeaders.insert(snapshot().document(headerFile)); fwdHeaders.insert(snapshot().document(headerFile));
foreach (Document::Ptr doc, snapshot()) { foreach (Document::Ptr doc, snapshot()) {
QFileInfo headerFileInfo(doc->fileName()); QFileInfo headerFileInfo(doc->fileName());
if (doc->globalSymbolCount() == 0 && doc->includes().size() == 1) if (doc->globalSymbolCount() == 0 && doc->resolvedIncludes().size() == 1)
fwdHeaders.insert(doc); fwdHeaders.insert(doc);
else if (headerFileInfo.suffix().isEmpty()) else if (headerFileInfo.suffix().isEmpty())
fwdHeaders.insert(doc); fwdHeaders.insert(doc);
......
...@@ -99,7 +99,7 @@ protected: ...@@ -99,7 +99,7 @@ protected:
if (!processed->contains(doc->globalNamespace())) { if (!processed->contains(doc->globalNamespace())) {
processed->insert(doc->globalNamespace()); processed->insert(doc->globalNamespace());
foreach (const Document::Include &i, doc->includes()) foreach (const Document::Include &i, doc->resolvedIncludes())
process(_snapshot.document(i.resolvedFileName()), processed); process(_snapshot.document(i.resolvedFileName()), processed);
_mainDocument = (doc == _doc); // ### improve _mainDocument = (doc == _doc); // ### improve
......
...@@ -1732,7 +1732,7 @@ void CppCompletionAssistProcessor::addMacros_helper(const CPlusPlus::Snapshot &s ...@@ -1732,7 +1732,7 @@ void CppCompletionAssistProcessor::addMacros_helper(const CPlusPlus::Snapshot &s
processed->insert(doc->fileName()); processed->insert(doc->fileName());
foreach (const Document::Include &i, doc->includes()) { foreach (const Document::Include &i, doc->resolvedIncludes()) {
addMacros_helper(snapshot, i.resolvedFileName(), processed, definedMacros); addMacros_helper(snapshot, i.resolvedFileName(), processed, definedMacros);
} }
......
...@@ -332,7 +332,7 @@ void CppPreprocessor::mergeEnvironment(Document::Ptr doc) ...@@ -332,7 +332,7 @@ void CppPreprocessor::mergeEnvironment(Document::Ptr doc)
m_processed.insert(fn); m_processed.insert(fn);
foreach (const Document::Include &incl, doc->includes()) { foreach (const Document::Include &incl, doc->resolvedIncludes()) {
const QString includedFile = incl.resolvedFileName(); const QString includedFile = incl.resolvedFileName();
if (Document::Ptr includedDoc = m_snapshot.document(includedFile)) if (Document::Ptr includedDoc = m_snapshot.document(includedFile))
...@@ -363,7 +363,7 @@ void CppPreprocessor::sourceNeeded(unsigned line, const QString &fileName, Inclu ...@@ -363,7 +363,7 @@ void CppPreprocessor::sourceNeeded(unsigned line, const QString &fileName, Inclu
QString absoluteFileName = resolveFile(fileName, type); QString absoluteFileName = resolveFile(fileName, type);
absoluteFileName = QDir::cleanPath(absoluteFileName); absoluteFileName = QDir::cleanPath(absoluteFileName);
if (m_currentDoc && !absoluteFileName.isEmpty()) if (m_currentDoc)
m_currentDoc->addIncludeFile(Document::Include(fileName, absoluteFileName, line, type)); m_currentDoc->addIncludeFile(Document::Include(fileName, absoluteFileName, line, type));
if (m_included.contains(absoluteFileName)) if (m_included.contains(absoluteFileName))
return; // we've already seen this file. return; // we've already seen this file.
......
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** 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 Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "cpptoolsplugin.h"
#include "cppmodelmanager.h"
#include "cpppreprocessertesthelper.h"
#include "cpppreprocessor.h"
#include <cplusplus/CppDocument.h>
#include <utils/fileutils.h>
#include <QtTest>
using namespace CPlusPlus;
using namespace CppTools;
using namespace CppTools::Internal;
typedef Document::Include Include;
class SourcePreprocessor
{
public:
SourcePreprocessor()
: m_cmm(CppModelManager::instance())
{
cleanUp();
}
Document::Ptr run(const QByteArray &source)
{
const QString fileName = TestIncludePaths::directoryOfTestFile()
+ QLatin1String("/file.cpp");
Utils::FileSaver srcSaver(fileName);
srcSaver.write(source);
srcSaver.finalize();
CppPreprocessor pp((QPointer<CppModelManager>(m_cmm)));
pp.setIncludePaths(QStringList(TestIncludePaths::directoryOfTestFile()));
pp.run(fileName);
return m_cmm->snapshot().document(fileName);
}
~SourcePreprocessor()
{
cleanUp();
}
private:
void cleanUp()
{
m_cmm->GC();
QVERIFY(m_cmm->snapshot().isEmpty());
}
private:
CppModelManager *m_cmm;
};
void CppToolsPlugin::test_cpppreprocessor_includes()
{
QByteArray source =
"#include \"header.h\"\n"
"#include \"notresolvable.h\"\n"
"\n"
;
SourcePreprocessor processor;
Document::Ptr document = processor.run(source);
QVERIFY(document);
const QList<Document::Include> resolvedIncludes = document->resolvedIncludes();
QVERIFY(resolvedIncludes.size() == 1);
QVERIFY(resolvedIncludes.at(0).type() == Client::IncludeLocal);
QCOMPARE(resolvedIncludes.at(0).unresolvedFileName(), QLatin1String("header.h"));
const QString expectedResolvedFileName
= TestIncludePaths::directoryOfTestFile() + QLatin1String("/header.h");
QCOMPARE(resolvedIncludes.at(0).resolvedFileName(), expectedResolvedFileName);
const QList<Document::Include> unresolvedIncludes = document->unresolvedIncludes();
QVERIFY(unresolvedIncludes.size() == 1);
QVERIFY(unresolvedIncludes.at(0).type() == Client::IncludeLocal);
QCOMPARE(unresolvedIncludes.at(0).unresolvedFileName(), QLatin1String("notresolvable.h"));
QVERIFY(unresolvedIncludes.at(0).resolvedFileName().isEmpty());
}
...@@ -97,14 +97,15 @@ FORMS += completionsettingspage.ui \ ...@@ -97,14 +97,15 @@ FORMS += completionsettingspage.ui \
equals(TEST, 1) { equals(TEST, 1) {
SOURCES += \ SOURCES += \
cpppreprocessertesthelper.cpp \
cppcodegen_test.cpp \ cppcodegen_test.cpp \
cppcompletion_test.cpp \ cppcompletion_test.cpp \
cppmodelmanager_test.cpp \ cppmodelmanager_test.cpp \
modelmanagertesthelper.cpp \ modelmanagertesthelper.cpp \
cpppointerdeclarationformatter_test.cpp \ cpppointerdeclarationformatter_test.cpp \
cpplocatorfilter_test.cpp \ cpplocatorfilter_test.cpp \
symbolsearcher_test.cpp symbolsearcher_test.cpp \
cpppreprocessor_test.cpp \
cpppreprocessertesthelper.cpp
HEADERS += \ HEADERS += \
cpppreprocessertesthelper.h \ cpppreprocessertesthelper.h \
......
...@@ -116,12 +116,13 @@ QtcPlugin { ...@@ -116,12 +116,13 @@ QtcPlugin {
files: [ files: [
"cppcodegen_test.cpp", "cppcodegen_test.cpp",
"cppcompletion_test.cpp", "cppcompletion_test.cpp",
"cpppreprocessertesthelper.cpp", "cpppreprocessertesthelper.h",
"cppmodelmanager_test.cpp", "cppmodelmanager_test.cpp",
"modelmanagertesthelper.cpp", "modelmanagertesthelper.h", "modelmanagertesthelper.cpp", "modelmanagertesthelper.h",
"cpppointerdeclarationformatter_test.cpp", "cpppointerdeclarationformatter_test.cpp",
"cpplocatorfilter_test.cpp", "cpplocatorfilter_test.cpp",
"symbolsearcher_test.cpp" "symbolsearcher_test.cpp",
"cpppreprocessor_test.cpp",
"cpppreprocessertesthelper.cpp", "cpppreprocessertesthelper.h"
] ]
cpp.defines: outer.concat(['SRCDIR="' + FileInfo.path(filePath) + '"']) cpp.defines: outer.concat(['SRCDIR="' + FileInfo.path(filePath) + '"'])
......
...@@ -189,6 +189,8 @@ private slots: ...@@ -189,6 +189,8 @@ private slots:
void test_format_pointerdeclaration_macros(); void test_format_pointerdeclaration_macros();
void test_format_pointerdeclaration_macros_data(); void test_format_pointerdeclaration_macros_data();
void test_cpppreprocessor_includes();
void test_modelmanager_paths_are_clean(); void test_modelmanager_paths_are_clean();
void test_modelmanager_framework_headers(); void test_modelmanager_framework_headers();
void test_modelmanager_refresh_also_includes_of_project_files(); void test_modelmanager_refresh_also_includes_of_project_files();
......
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