From ba2d7a4fa7c29ea2d62da3d6f6835a091f604656 Mon Sep 17 00:00:00 2001 From: Erik Verbruggen Date: Mon, 19 Aug 2013 16:05:29 +0200 Subject: C++: Only parse with appropriate defines for open editors. If two files from different (sub-)projects include the same header file, and the defined macros differ for both files, the header file will be parsed with only the appropriate macros for the including file. Task-number: QTCREATORBUG-9802 Task-number: QTCREATORBUG-1249 Change-Id: I560490afa287b3bb1e863bce1bb4f57af36ad56e Reviewed-by: Nikolai Kosjar --- src/plugins/cpptools/cpppreprocessor.cpp | 47 +++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 4 deletions(-) (limited to 'src/plugins/cpptools/cpppreprocessor.cpp') diff --git a/src/plugins/cpptools/cpppreprocessor.cpp b/src/plugins/cpptools/cpppreprocessor.cpp index 48cb7fc77c..1e3f5b1bf0 100644 --- a/src/plugins/cpptools/cpppreprocessor.cpp +++ b/src/plugins/cpptools/cpppreprocessor.cpp @@ -6,6 +6,7 @@ #include #include +#include /*! * \class CppTools::Internal::CppPreprocessor @@ -34,6 +35,17 @@ CppPreprocessor::CppPreprocessor(QPointer modelManager, m_preprocess.setKeepComments(true); } +CppPreprocessor::CppPreprocessor(QPointer modelManager, const Snapshot &snapshot, + bool dumpFileNameWhileParsing) + : m_snapshot(snapshot), + m_modelManager(modelManager), + m_dumpFileNameWhileParsing(dumpFileNameWhileParsing), + m_preprocess(this, &m_env), + m_revision(0) +{ + m_preprocess.setKeepComments(true); +} + CppPreprocessor::~CppPreprocessor() { } @@ -129,10 +141,10 @@ public: { _doc->check(_mode); - if (_modelManager) + if (_modelManager) { _modelManager->emitDocumentUpdated(_doc); - - _doc->releaseSourceAndAST(); + _doc->releaseSourceAndAST(); + } } }; } // end of anonymous namespace @@ -398,7 +410,7 @@ void CppPreprocessor::sourceNeeded(unsigned line, const QString &fileName, Inclu if (m_dumpFileNameWhileParsing) { qDebug() << "Parsing file:" << absoluteFileName - << "contents:" << contents.size() + << "contents:" << contents.size() << "bytes"; ; } @@ -426,6 +438,33 @@ void CppPreprocessor::sourceNeeded(unsigned line, const QString &fileName, Inclu // b.constData()); // } + QCryptographicHash hash(QCryptographicHash::Sha1); + hash.addData(preprocessedCode); + foreach (const Macro ¯o, doc->definedMacros()) { + if (macro.isHidden()) { + static const QByteArray undef("#undef "); + hash.addData(undef); + hash.addData(macro.name()); + } else { + static const QByteArray def("#define "); + hash.addData(macro.name()); + hash.addData(" ", 1); + hash.addData(def); + hash.addData(macro.definitionText()); + } + hash.addData("\n", 1); + } + doc->setFingerprint(hash.result()); + + Document::Ptr anotherDoc = m_globalSnapshot.document(absoluteFileName); + if (anotherDoc && anotherDoc->fingerprint() == doc->fingerprint()) { + switchDocument(previousDoc); + mergeEnvironment(anotherDoc); + m_snapshot.insert(anotherDoc); + m_todo.remove(absoluteFileName); + return; + } + doc->setUtf8Source(preprocessedCode); doc->keepSourceAndAST(); doc->tokenize(); -- cgit v1.2.1