Commit 4679f38a authored by Roberto Raggi's avatar Roberto Raggi
Browse files

Fixed code completion when using macros in the expression's code (e.g. in qApp->).

parent 7239d03e
......@@ -37,6 +37,7 @@
#include <TranslationUnit.h>
#include <cplusplus/LookupContext.h>
#include <cplusplus/ResolveExpression.h>
#include <cplusplus/pp.h>
using namespace CPlusPlus;
......@@ -53,9 +54,13 @@ void TypeOfExpression::setDocuments(const QMap<QString, Document::Ptr> &document
QList<TypeOfExpression::Result> TypeOfExpression::operator()(const QString &expression,
Document::Ptr document,
Symbol *lastVisibleSymbol)
Symbol *lastVisibleSymbol,
PreprocessMode mode)
{
Document::Ptr expressionDoc = documentForExpression(expression);
QString code = expression;
if (mode == Preprocess)
code = preprocessedExpression(expression, m_documents, document);
Document::Ptr expressionDoc = documentForExpression(code);
m_ast = extractExpressionAST(expressionDoc);
m_lookupContext = LookupContext(lastVisibleSymbol, expressionDoc,
......@@ -97,3 +102,34 @@ Document::Ptr TypeOfExpression::documentForExpression(const QString &expression)
doc->parse(Document::ParseExpression);
return doc;
}
void TypeOfExpression::processEnvironment(QMap<QString, Document::Ptr> documents,
Document::Ptr doc, Environment *env,
QSet<QString> *processed) const
{
if (processed->contains(doc->fileName()))
return;
processed->insert(doc->fileName());
foreach (const Document::Include &incl, doc->includes()) {
processEnvironment(documents,
documents.value(incl.fileName()),
env, processed);
}
foreach (const Macro &macro, doc->definedMacros())
env->bind(macro);
}
QString TypeOfExpression::preprocessedExpression(const QString &expression,
QMap<QString, Document::Ptr> documents,
Document::Ptr thisDocument) const
{
Environment env;
QSet<QString> processed;
processEnvironment(documents, thisDocument,
&env, &processed);
const QByteArray code = expression.toUtf8();
pp preproc(0, env);
QByteArray preprocessedCode;
preproc("<expression>", code, &preprocessedCode);
return QString::fromUtf8(preprocessedCode);
}
......@@ -43,6 +43,9 @@
namespace CPlusPlus {
class Environment;
class Macro;
class CPLUSPLUS_EXPORT TypeOfExpression
{
public:
......@@ -60,6 +63,11 @@ public:
*/
void setDocuments(const QMap<QString, Document::Ptr> &documents);
enum PreprocessMode {
NoPreprocess,
Preprocess
};
/**
* Returns a list of possible fully specified types associated with the
* given expression.
......@@ -73,7 +81,8 @@ public:
* @param lastVisibleSymbol The last visible symbol in the document.
*/
QList<Result> operator()(const QString &expression, Document::Ptr document,
Symbol *lastVisibleSymbol);
Symbol *lastVisibleSymbol,
PreprocessMode mode = NoPreprocess);
/**
* Returns the AST of the last evaluated expression.
......@@ -91,6 +100,14 @@ private:
ExpressionAST *extractExpressionAST(Document::Ptr doc) const;
Document::Ptr documentForExpression(const QString &expression) const;
void processEnvironment(QMap<QString, CPlusPlus::Document::Ptr> documents,
CPlusPlus::Document::Ptr doc, CPlusPlus::Environment *env,
QSet<QString> *processed) const;
QString preprocessedExpression(const QString &expression,
QMap<QString, CPlusPlus::Document::Ptr> documents,
CPlusPlus::Document::Ptr thisDocument) const;
QMap<QString, Document::Ptr> m_documents;
ExpressionAST *m_ast;
LookupContext m_lookupContext;
......
......@@ -439,7 +439,8 @@ int CppCodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
typeOfExpression.setDocuments(m_manager->documents());
QList<TypeOfExpression::Result> resolvedTypes = typeOfExpression(expression, thisDocument, symbol);
QList<TypeOfExpression::Result> resolvedTypes = typeOfExpression(expression, thisDocument, symbol,
TypeOfExpression::Preprocess);
LookupContext context = typeOfExpression.lookupContext();
if (!typeOfExpression.expressionAST() && (! m_completionOperator ||
......
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