From aa796a9fc06dc1817b99355a333d2b0edce7215b Mon Sep 17 00:00:00 2001 From: Nikolai Kosjar Date: Wed, 19 Feb 2014 09:11:59 -0300 Subject: CppTools: Add all diagnostic messages ...of type 'No such file or directory' to the CPlusPlus::Document. Only the first one was added and as a result, the editor only wavely underlined that one. Add also diagnostic messages if it's not possible to get the file contents. Change-Id: I8389d8e6af9480ea6712759ce5e130e1dd8912f1 Reviewed-by: Erik Verbruggen --- src/plugins/cpptools/cpppreprocessor.cpp | 65 ++++++++++++++------------- src/plugins/cpptools/cpppreprocessor.h | 3 +- src/plugins/cpptools/cpppreprocessor_test.cpp | 18 ++++++++ src/plugins/cpptools/cpptoolsplugin.h | 1 + 4 files changed, 54 insertions(+), 33 deletions(-) diff --git a/src/plugins/cpptools/cpppreprocessor.cpp b/src/plugins/cpptools/cpppreprocessor.cpp index 229d06885f..dd185a7e39 100644 --- a/src/plugins/cpptools/cpppreprocessor.cpp +++ b/src/plugins/cpptools/cpppreprocessor.cpp @@ -172,32 +172,31 @@ void CppPreprocessor::resetEnvironment() m_included.clear(); } -void CppPreprocessor::getFileContents(const QString &absoluteFilePath, +bool CppPreprocessor::getFileContents(const QString &absoluteFilePath, QByteArray *contents, unsigned *revision) const { - if (absoluteFilePath.isEmpty()) - return; + if (absoluteFilePath.isEmpty() || !contents || !revision) + return false; + // Get from working copy if (m_workingCopy.contains(absoluteFilePath)) { const QPair entry = m_workingCopy.get(absoluteFilePath); - if (contents) - *contents = entry.first; - if (revision) - *revision = entry.second; - return; + *contents = entry.first; + *revision = entry.second; + return true; } - if (contents) { - QString error; - if (Utils::TextFileFormat::readFileUTF8(absoluteFilePath, m_defaultCodec, contents, &error) - != Utils::TextFileFormat::ReadSuccess) { - qWarning("Error reading file \"%s\": \"%s\".", qPrintable(absoluteFilePath), - qPrintable(error)); - } + // Get from file + *revision = 0; + QString error; + if (Utils::TextFileFormat::readFileUTF8(absoluteFilePath, m_defaultCodec, contents, &error) + != Utils::TextFileFormat::ReadSuccess) { + qWarning("Error reading file \"%s\": \"%s\".", qPrintable(absoluteFilePath), + qPrintable(error)); + return false; } - if (revision) - *revision = 0; + return true; } bool CppPreprocessor::checkFile(const QString &absoluteFilePath) const @@ -388,13 +387,22 @@ static QByteArray convertToLatin1(const QByteArray &contents) void CppPreprocessor::sourceNeeded(unsigned line, const QString &fileName, IncludeType type) { + typedef Document::DiagnosticMessage Message; if (fileName.isEmpty()) return; QString absoluteFileName = resolveFile(fileName, type); absoluteFileName = QDir::cleanPath(absoluteFileName); - if (m_currentDoc) + if (m_currentDoc) { m_currentDoc->addIncludeFile(Document::Include(fileName, absoluteFileName, line, type)); + if (absoluteFileName.isEmpty()) { + const QString text = QCoreApplication::translate( + "CppPreprocessor", "%1: No such file or directory").arg(fileName); + Message message(Message::Warning, m_currentDoc->fileName(), line, /*column =*/ 0, text); + m_currentDoc->addDiagnosticMessage(message); + return; + } + } if (m_included.contains(absoluteFileName)) return; // we've already seen this file. if (absoluteFileName != modelManager()->configurationFileName()) @@ -402,21 +410,14 @@ void CppPreprocessor::sourceNeeded(unsigned line, const QString &fileName, Inclu unsigned editorRevision = 0; QByteArray contents; - getFileContents(absoluteFileName, &contents, &editorRevision); + const bool gotFileContents = getFileContents(absoluteFileName, &contents, &editorRevision); contents = convertToLatin1(contents); - if (m_currentDoc) { - if (contents.isEmpty() && !QFileInfo(absoluteFileName).isAbsolute()) { - QString msg = QCoreApplication::translate( - "CppPreprocessor", "%1: No such file or directory").arg(fileName); - - Document::DiagnosticMessage d(Document::DiagnosticMessage::Warning, - m_currentDoc->fileName(), - line, /*column = */ 0, - msg); - - m_currentDoc->addDiagnosticMessage(d); - return; - } + if (m_currentDoc && !gotFileContents) { + const QString text = QCoreApplication::translate( + "CppPreprocessor", "%1: Could not get file contents").arg(fileName); + Message message(Message::Warning, m_currentDoc->fileName(), line, /*column =*/ 0, text); + m_currentDoc->addDiagnosticMessage(message); + return; } if (m_dumpFileNameWhileParsing) { diff --git a/src/plugins/cpptools/cpppreprocessor.h b/src/plugins/cpptools/cpppreprocessor.h index 40a9509701..f8b076be8a 100644 --- a/src/plugins/cpptools/cpppreprocessor.h +++ b/src/plugins/cpptools/cpppreprocessor.h @@ -55,7 +55,8 @@ public: protected: CPlusPlus::Document::Ptr switchDocument(CPlusPlus::Document::Ptr doc); - void getFileContents(const QString &absoluteFilePath, QByteArray *contents, unsigned *revision) const; + bool getFileContents(const QString &absoluteFilePath, QByteArray *contents, + unsigned *revision) const; bool checkFile(const QString &absoluteFilePath) const; QString resolveFile(const QString &fileName, IncludeType type); QString resolveFile_helper(const QString &fileName, IncludeType type); diff --git a/src/plugins/cpptools/cpppreprocessor_test.cpp b/src/plugins/cpptools/cpppreprocessor_test.cpp index 17ab615b02..3178aa9771 100644 --- a/src/plugins/cpptools/cpppreprocessor_test.cpp +++ b/src/plugins/cpptools/cpppreprocessor_test.cpp @@ -161,3 +161,21 @@ void CppToolsPlugin::test_cpppreprocessor_includes_cyclic() QCOMPARE(doc2->resolvedIncludes().size(), 1); QCOMPARE(doc2->resolvedIncludes().first().resolvedFileName(), fileName1); } + +/// Check: All include errors are reported as diagnostic messages. +void CppToolsPlugin::test_cpppreprocessor_includes_allDiagnostics() +{ + QByteArray source = + "#include \n" + "#include \n" + "\n" + ; + + SourcePreprocessor processor; + Document::Ptr document = processor.run(source); + QVERIFY(document); + + QCOMPARE(document->resolvedIncludes().size(), 0); + QCOMPARE(document->unresolvedIncludes().size(), 2); + QCOMPARE(document->diagnosticMessages().size(), 2); +} diff --git a/src/plugins/cpptools/cpptoolsplugin.h b/src/plugins/cpptools/cpptoolsplugin.h index 05a137dc6e..ce55cf668e 100644 --- a/src/plugins/cpptools/cpptoolsplugin.h +++ b/src/plugins/cpptools/cpptoolsplugin.h @@ -124,6 +124,7 @@ private slots: void test_cpppreprocessor_includes_resolvedUnresolved(); void test_cpppreprocessor_includes_cyclic(); + void test_cpppreprocessor_includes_allDiagnostics(); void test_functionutils_virtualFunctions(); void test_functionutils_virtualFunctions_data(); -- cgit v1.2.1