diff --git a/share/qtcreator/styles/darkvim.xml b/share/qtcreator/styles/darkvim.xml index afe1d9394244eaf00ad977aa18be624dafc47691..a7f0f924ab81970b03116d507d567b0e48c55646 100644 --- a/share/qtcreator/styles/darkvim.xml +++ b/share/qtcreator/styles/darkvim.xml @@ -14,6 +14,7 @@ <style name="Doxygen.Comment" foreground="#55ffff"/> <style name="Doxygen.Tag" foreground="#00a0a0"/> <style name="Field"/> + <style name="Function"/> <style name="Keyword" foreground="#ffff55"/> <style name="Label" foreground="#ffff55"/> <style name="LineNumber" foreground="#888888" background="#232323"/> diff --git a/share/qtcreator/styles/grayscale.xml b/share/qtcreator/styles/grayscale.xml index b31ecd7815a5d67fb60fc6e3c528500f353d2677..c1a29c205456c41bff1b8dce23605a461ac7934e 100644 --- a/share/qtcreator/styles/grayscale.xml +++ b/share/qtcreator/styles/grayscale.xml @@ -8,6 +8,7 @@ <style name="Doxygen.Comment" foreground="#808080"/> <style name="Doxygen.Tag" foreground="#808080" italic="true"/> <style name="Field"/> + <style name="Function"/> <style name="Keyword" bold="true"/> <style name="Label"/> <style name="LineNumber" foreground="#c7c4c1" background="#efebe7"/> diff --git a/share/qtcreator/styles/inkpot.xml b/share/qtcreator/styles/inkpot.xml index 7b24f8ada8ad717b714f751e1f00da25a5b2fa86..730c170f56fbd5cc1a6866b80031e14464406b36 100644 --- a/share/qtcreator/styles/inkpot.xml +++ b/share/qtcreator/styles/inkpot.xml @@ -19,6 +19,7 @@ <style name="Doxygen.Comment" foreground="#737dd5"/> <style name="Doxygen.Tag" foreground="#4e5ab3"/> <style name="Field" bold="true"/> + <style name="Function"/> <style name="Keyword" foreground="#808bed"/> <style name="Label" foreground="#e76000"/> <style name="LineNumber" foreground="#8b8bcd" background="#2e2e2e"/> diff --git a/share/qtcreator/styles/intellij.xml b/share/qtcreator/styles/intellij.xml index b98500b620c5e14838f88edc55df814ffe967eba..dfe5f7afd59e78e966a8d58e27446c82dfe84358 100644 --- a/share/qtcreator/styles/intellij.xml +++ b/share/qtcreator/styles/intellij.xml @@ -8,6 +8,7 @@ <style name="Doxygen.Comment" foreground="#808080" italic="true"/> <style name="Doxygen.Tag" foreground="#808080" bold="true" italic="true"/> <style name="Field" foreground="#660e7a" bold="true"/> + <style name="Function" foreground="#000000"/> <style name="Keyword" foreground="#000080" bold="true"/> <style name="Label" foreground="#800000" bold="true"/> <style name="Local" foreground="#000000"/> diff --git a/src/plugins/cppeditor/cppeditor.cpp b/src/plugins/cppeditor/cppeditor.cpp index b18cc1dacb90c025aaf13b914e2d9198d28c4d0f..566b5b634ce659ae3f7a37c644270dd3c8026be0 100644 --- a/src/plugins/cppeditor/cppeditor.cpp +++ b/src/plugins/cppeditor/cppeditor.cpp @@ -1047,6 +1047,11 @@ void CPPEditorWidget::finishHighlightSymbolUsages() TextEditor::SemanticHighlighter::clearExtraAdditionalFormatsUntilEnd( highlighter, m_highlighter); + + if (m_modelManager) + m_modelManager->setExtraDiagnostics(m_lastSemanticInfo.doc->fileName(), + CPlusPlus::CppModelManagerInterface::CppSemanticsDiagnostic, + m_lastSemanticInfo.doc->diagnosticMessages()); } @@ -1743,6 +1748,8 @@ void CPPEditorWidget::setFontSettings(const TextEditor::FontSettings &fs) fs.toTextCharFormat(QLatin1String(TextEditor::Constants::C_VIRTUAL_METHOD)); m_semanticHighlightFormatMap[SemanticInfo::LabelUse] = fs.toTextCharFormat(QLatin1String(TextEditor::Constants::C_LABEL)); + m_semanticHighlightFormatMap[SemanticInfo::FunctionUse] = + fs.toTextCharFormat(QLatin1String(TextEditor::Constants::C_FUNCTION)); m_keywordFormat = fs.toTextCharFormat(QLatin1String(TextEditor::Constants::C_KEYWORD)); // only set the background, we do not want to modify foreground properties set by the syntax highlighter or the link diff --git a/src/plugins/cpptools/ModelManagerInterface.h b/src/plugins/cpptools/ModelManagerInterface.h index b873e880fe9ca3ba72587a50c64c79896f04b27f..8bab7086e830a35b04dc4050ea4ad40cb12259f8 100644 --- a/src/plugins/cpptools/ModelManagerInterface.h +++ b/src/plugins/cpptools/ModelManagerInterface.h @@ -179,7 +179,8 @@ public: enum ExtraDiagnosticKind { AllExtraDiagnostics = -1, - ExportedQmlTypesDiagnostic + ExportedQmlTypesDiagnostic, + CppSemanticsDiagnostic }; public: diff --git a/src/plugins/cpptools/cppchecksymbols.cpp b/src/plugins/cpptools/cppchecksymbols.cpp index a2d1d6dcf076eb14f5676a3d513eb8a4bb0e28a8..2330e037c03fe5ffbf217c234c810bebb19c3731 100644 --- a/src/plugins/cpptools/cppchecksymbols.cpp +++ b/src/plugins/cpptools/cppchecksymbols.cpp @@ -68,6 +68,7 @@ class CollectSymbols: protected SymbolVisitor Snapshot _snapshot; QSet<QByteArray> _types; QSet<QByteArray> _members; + QSet<QByteArray> _functions; QSet<QByteArray> _virtualMethods; QSet<QByteArray> _statics; bool _mainDocument; @@ -90,6 +91,11 @@ public: return _members; } + const QSet<QByteArray> &functions() const + { + return _functions; + } + const QSet<QByteArray> &virtualMethods() const { return _virtualMethods; @@ -149,6 +155,17 @@ protected: } } + void addFunction(const Name *name) + { + if (! name) { + return; + + } else if (name->isNameId()) { + const Identifier *id = name->identifier(); + _functions.insert(QByteArray::fromRawData(id->chars(), id->size())); + } + } + void addVirtualMethod(const Name *name) { if (! name) { @@ -183,6 +200,8 @@ protected: { if (symbol->isVirtual()) addVirtualMethod(symbol->name()); + else + addFunction(symbol->name()); return true; } @@ -206,6 +225,8 @@ protected: if (Function *funTy = symbol->type()->asFunctionType()) { if (funTy->isVirtual()) addVirtualMethod(symbol->name()); + else + addFunction(symbol->name()); } if (symbol->isTypedef()) @@ -300,6 +321,7 @@ CheckSymbols::CheckSymbols(Document::Ptr doc, const LookupContext &context) _fileName = doc->fileName(); _potentialTypes = collectTypes.types(); _potentialMembers = collectTypes.members(); + _potentialFunctions = collectTypes.functions(); _potentialVirtualMethods = collectTypes.virtualMethods(); _potentialStatics = collectTypes.statics(); @@ -326,7 +348,7 @@ void CheckSymbols::run() bool CheckSymbols::warning(unsigned line, unsigned column, const QString &text, unsigned length) { Document::DiagnosticMessage m(Document::DiagnosticMessage::Warning, _fileName, line, column, text, length); - _diagnosticMessages.append(m); + _doc->addDiagnosticMessage(m); return false; } @@ -477,6 +499,8 @@ bool CheckSymbols::visit(SimpleDeclarationAST *ast) addUse(declId, SemanticInfo::VirtualMethodUse); } else if (maybeVirtualMethod(decl->name())) { addVirtualMethod(_context.lookup(decl->name(), decl->enclosingScope()), declId, funTy->argumentCount()); + } else { + addUse(declId, SemanticInfo::FunctionUse); } } } @@ -535,7 +559,7 @@ bool CheckSymbols::visit(CallAST *ast) if (MemberAccessAST *access = ast->base_expression->asMemberAccess()) { if (access->member_name && access->member_name->name) { - if (maybeVirtualMethod(access->member_name->name)) { + if (maybeVirtualMethod(access->member_name->name) || maybeFunction(access->member_name->name)) { const QByteArray expression = textOf(access); const QList<LookupItem> candidates = @@ -551,7 +575,7 @@ bool CheckSymbols::visit(CallAST *ast) } } else if (IdExpressionAST *idExpr = ast->base_expression->asIdExpression()) { if (const Name *name = idExpr->name->name) { - if (maybeVirtualMethod(name)) { + if (maybeVirtualMethod(name) || maybeFunction(name)) { NameAST *exprName = idExpr->name; if (QualifiedNameAST *q = exprName->asQualifiedName()) exprName = q->unqualified_name; @@ -651,6 +675,8 @@ void CheckSymbols::checkName(NameAST *ast, Scope *scope) Class *klass = scope->asClass(); if (hasVirtualDestructor(_context.lookupType(klass))) addUse(ast, SemanticInfo::VirtualMethodUse); + else + addUse(ast, SemanticInfo::FunctionUse); } else if (maybeType(ast->name) || maybeStatic(ast->name)) { const QList<LookupItem> candidates = _context.lookup(ast->name, scope); addTypeOrStatic(candidates, ast); @@ -679,6 +705,14 @@ bool CheckSymbols::visit(DestructorNameAST *ast) return true; } +bool CheckSymbols::visit(ParameterDeclarationAST *ast) +{ + accept(ast->type_specifier_list); + // Skip parameter name, it does not need to be colored + accept(ast->expression); + return false; +} + bool CheckSymbols::visit(QualifiedNameAST *ast) { if (ast->name) { @@ -722,6 +756,8 @@ bool CheckSymbols::visit(QualifiedNameAST *ast) if (ast->unqualified_name->asDestructorName() != 0) { if (hasVirtualDestructor(binding)) addUse(ast->unqualified_name, SemanticInfo::VirtualMethodUse); + else + addUse(ast->unqualified_name, SemanticInfo::FunctionUse); } else { addTypeOrStatic(binding->find(ast->unqualified_name->name), ast->unqualified_name); } @@ -802,6 +838,8 @@ bool CheckSymbols::visit(FunctionDefinitionAST *ast) addUse(declId, SemanticInfo::VirtualMethodUse); } else if (maybeVirtualMethod(fun->name())) { addVirtualMethod(_context.lookup(fun->name(), fun->enclosingScope()), declId, fun->argumentCount()); + } else { + addUse(declId, SemanticInfo::FunctionUse); } } } @@ -1023,30 +1061,49 @@ void CheckSymbols::addVirtualMethod(const QList<LookupItem> &candidates, NameAST if (tok.generated()) return; + enum { Match_None, Match_TooManyArgs, Match_TooFewArgs, Match_Ok } matchType = Match_None; + SemanticInfo::UseKind kind = SemanticInfo::FunctionUse; foreach (const LookupItem &r, candidates) { Symbol *c = r.declaration(); if (! c) continue; - Function *funTy = r.type()->asFunctionType(); + Function *funTy = c->type()->asFunctionType(); + if (! funTy) { + //Try to find a template function + if (Template * t = r.type()->asTemplateType()) + if ((c = t->declaration())) + funTy = c->type()->asFunctionType(); + } if (! funTy) - continue; - if (! funTy->isVirtual()) - continue; - else if (argumentCount < funTy->minimumArgumentCount()) - continue; - else if (argumentCount > funTy->argumentCount()) { - if (! funTy->isVariadic()) - continue; + continue; // TODO: add diagnostic messages and color call-operators calls too? + + kind = funTy->isVirtual() ? SemanticInfo::VirtualMethodUse : SemanticInfo::FunctionUse; + if (argumentCount < funTy->minimumArgumentCount()) { + matchType = Match_TooFewArgs; + } + else if (argumentCount > funTy->argumentCount() && ! funTy->isVariadic()) { + matchType = Match_TooManyArgs; + } + else { + matchType = Match_Ok; + break; } + } + if (matchType != Match_None) { unsigned line, column; getTokenStartPosition(startToken, &line, &column); const unsigned length = tok.length(); - const Use use(line, column, length, SemanticInfo::VirtualMethodUse); + // Add a diagnostic message if argument count does not match + if (matchType == Match_TooFewArgs) + warning(line, column, "Too few arguments", length); + else if (matchType == Match_TooManyArgs) + warning(line, column, "Too many arguments", length); + + const Use use(line, column, length, kind); addUse(use); - break; } } @@ -1120,6 +1177,19 @@ static bool sortByLinePredicate(const CheckSymbols::Use &lhs, const CheckSymbols return lhs.line < rhs.line; } +bool CheckSymbols::maybeFunction(const Name *name) const +{ + if (name) { + if (const Identifier *ident = name->identifier()) { + const QByteArray id = QByteArray::fromRawData(ident->chars(), ident->size()); + if (_potentialFunctions.contains(id)) + return true; + } + } + + return false; +} + void CheckSymbols::flush() { _lineOfLastUsage = 0; diff --git a/src/plugins/cpptools/cppchecksymbols.h b/src/plugins/cpptools/cppchecksymbols.h index 56dfc541f5e8028e1b9da6893eec63c9d25fa6b7..3efe7439950c8c8c37264bb00eb0052f34b4702e 100644 --- a/src/plugins/cpptools/cppchecksymbols.h +++ b/src/plugins/cpptools/cppchecksymbols.h @@ -107,6 +107,7 @@ protected: bool maybeType(const Name *name) const; bool maybeMember(const Name *name) const; bool maybeStatic(const Name *name) const; + bool maybeFunction(const Name *name) const; bool maybeVirtualMethod(const Name *name) const; void checkName(NameAST *ast, Scope *scope = 0); @@ -142,6 +143,7 @@ protected: virtual bool visit(SimpleNameAST *ast); virtual bool visit(DestructorNameAST *ast); + virtual bool visit(ParameterDeclarationAST *ast); virtual bool visit(QualifiedNameAST *ast); virtual bool visit(TemplateIdAST *ast); @@ -169,6 +171,7 @@ private: QList<Document::DiagnosticMessage> _diagnosticMessages; QSet<QByteArray> _potentialTypes; QSet<QByteArray> _potentialMembers; + QSet<QByteArray> _potentialFunctions; QSet<QByteArray> _potentialVirtualMethods; QSet<QByteArray> _potentialStatics; QList<AST *> _astStack; diff --git a/src/plugins/cpptools/cppmodelmanager.cpp b/src/plugins/cpptools/cppmodelmanager.cpp index 8611e4f5f3d775c6e49caef1217e4157f2039d5b..724f44affa9ed63d6d36f140612708e1b0e82169 100644 --- a/src/plugins/cpptools/cppmodelmanager.cpp +++ b/src/plugins/cpptools/cppmodelmanager.cpp @@ -1151,13 +1151,19 @@ void CppModelManager::updateEditor(Document::Ptr doc) QTextCursor c(ed->document()->findBlockByNumber(m.line() - 1)); const QString text = c.block().text(); - for (int i = 0; i < text.size(); ++i) { - if (! text.at(i).isSpace()) { - c.setPosition(c.position() + i); - break; + if (m.length() > 0 && m.column() + m.length() < (unsigned)text.size()) { + int column = m.column() > 0 ? m.column() - 1 : 0; + c.setPosition(c.position() + column); + c.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor, m.length()); + } else { + for (int i = 0; i < text.size(); ++i) { + if (! text.at(i).isSpace()) { + c.setPosition(c.position() + i); + break; + } } + c.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor); } - c.movePosition(QTextCursor::EndOfBlock, QTextCursor::KeepAnchor); sel.cursor = c; sel.format.setToolTip(m.text()); e.selections.append(sel); diff --git a/src/plugins/cpptools/cppsemanticinfo.h b/src/plugins/cpptools/cppsemanticinfo.h index cf61b745592276fda18b919b1bf3deebc86520b4..acc1705424b354b1441a0e3cc62c4083d2ac57fc 100644 --- a/src/plugins/cpptools/cppsemanticinfo.h +++ b/src/plugins/cpptools/cppsemanticinfo.h @@ -52,7 +52,8 @@ public: FieldUse, StaticUse, VirtualMethodUse, - LabelUse + LabelUse, + FunctionUse }; typedef TextEditor::SemanticHighlighter::Result Use; diff --git a/src/plugins/texteditor/texteditorconstants.h b/src/plugins/texteditor/texteditorconstants.h index 0fc099368be7e69035ee36f9c5276992f03b30f0..b442933cadf4dff3fdeec76efbfe48a4149a1857 100644 --- a/src/plugins/texteditor/texteditorconstants.h +++ b/src/plugins/texteditor/texteditorconstants.h @@ -129,6 +129,7 @@ const char C_TYPE[] = "Type"; const char C_LOCAL[] = "Local"; const char C_FIELD[] = "Field"; const char C_STATIC[] = "Static"; +const char C_FUNCTION[] = "Function"; const char C_VIRTUAL_METHOD[] = "VirtualMethod"; const char C_KEYWORD[] = "Keyword"; const char C_OPERATOR[] = "Operator"; diff --git a/src/plugins/texteditor/texteditorsettings.cpp b/src/plugins/texteditor/texteditorsettings.cpp index 717a3f267eb0e4b9e5bda13297c9ac20769bc9dd..ece074475764ce2da7028320cc0f0339daeebacd 100644 --- a/src/plugins/texteditor/texteditorsettings.cpp +++ b/src/plugins/texteditor/texteditorsettings.cpp @@ -145,6 +145,7 @@ TextEditorSettings::TextEditorSettings(QObject *parent) formatDescriptions.append(FormatDescription(QLatin1String(C_FIELD), tr("Field"), Qt::darkRed)); formatDescriptions.append(FormatDescription(QLatin1String(C_STATIC), tr("Static"), Qt::darkMagenta)); + formatDescriptions.append(FormatDescription(QLatin1String(C_FUNCTION), tr("Function"))); FormatDescription virtualMethodFormatDescriptor(QLatin1String(C_VIRTUAL_METHOD), tr("Virtual Method")); virtualMethodFormatDescriptor.format().setItalic(true); formatDescriptions.append(virtualMethodFormatDescriptor);