Commit aa796a9f authored by Nikolai Kosjar's avatar Nikolai Kosjar

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: default avatarErik Verbruggen <erik.verbruggen@digia.com>
parent 16e16dc4
......@@ -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<QByteArray, unsigned> 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) {
......
......@@ -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);
......
......@@ -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 <NotResolvable1>\n"
"#include <NotResolvable2>\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);
}
......@@ -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();
......
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