diff --git a/src/libs/cplusplus/FindUsages.cpp b/src/libs/cplusplus/FindUsages.cpp index 95a5035a624..9dac6cd9183 100644 --- a/src/libs/cplusplus/FindUsages.cpp +++ b/src/libs/cplusplus/FindUsages.cpp @@ -2170,6 +2170,7 @@ bool FindUsages::visit(NestedDeclaratorAST *ast) bool FindUsages::visit(FunctionDeclaratorAST *ast) { // unsigned lparen_token = ast->lparen_token; + Scope *previousScope = switchScope(ast->symbol); this->parameterDeclarationClause(ast->parameter_declaration_clause); // unsigned rparen_token = ast->rparen_token; for (SpecifierListAST *it = ast->cv_qualifier_list; it; it = it->next) { @@ -2178,7 +2179,7 @@ bool FindUsages::visit(FunctionDeclaratorAST *ast) this->exceptionSpecification(ast->exception_specification); this->trailingReturnType(ast->trailing_return_type); this->expression(ast->as_cpp_initializer); - // Function *symbol = ast->symbol; + (void) switchScope(previousScope); return false; } diff --git a/tests/auto/cplusplus/findusages/tst_findusages.cpp b/tests/auto/cplusplus/findusages/tst_findusages.cpp index 9af88ad39cc..b09e2cc47fe 100644 --- a/tests/auto/cplusplus/findusages/tst_findusages.cpp +++ b/tests/auto/cplusplus/findusages/tst_findusages.cpp @@ -84,6 +84,9 @@ private Q_SLOTS: void shadowedNames_2(); void staticVariables(); + void functionNameFoundInArguments(); + void memberFunctionFalsePositives_QTCREATORBUG2176(); + // Qt keywords void qproperty_1(); @@ -352,6 +355,116 @@ void tst_FindUsages::staticVariables() QCOMPARE(findUsages.usages().size(), 5); } +void tst_FindUsages::functionNameFoundInArguments() +{ + const QByteArray src = + R"( +void bar(); // call find usages for bar from here. This is 1st result +void foo(int bar); // should not be found +void foo(int bar){} // should not be found +void foo2(int b=bar()); // 2nd result +void foo2(int b=bar()){} // 3rd result +)"; + + Document::Ptr doc = Document::create("functionNameFoundInArguments"); + doc->setUtf8Source(src); + doc->parse(); + doc->check(); + + QVERIFY(doc->diagnosticMessages().isEmpty()); + QVERIFY(doc->globalSymbolCount() >= 1); + + Symbol *s = doc->globalSymbolAt(0); + QCOMPARE(s->name()->identifier()->chars(), "bar"); + + Snapshot snapshot; + snapshot.insert(doc); + + FindUsages find(src, doc, snapshot); + find(s); + + QCOMPARE(find.usages().size(), 3); + + QCOMPARE(find.usages()[0].line, 1); + QCOMPARE(find.usages()[0].col, 5); + + QCOMPARE(find.usages()[1].line, 4); + QCOMPARE(find.usages()[1].col, 16); + + QCOMPARE(find.usages()[2].line, 5); + QCOMPARE(find.usages()[2].col, 16); +} + +void tst_FindUsages::memberFunctionFalsePositives_QTCREATORBUG2176() +{ + const QByteArray src = + R"( +void otherFunction(int value){} +struct Struct{ + static int foo(){return 1;} + int bar(){ + int foo=Struct::foo(); + otherFunction(foo); + } +}; +)"; + + Document::Ptr doc = Document::create("memberFunctionFalsePositives"); + doc->setUtf8Source(src); + doc->parse(); + doc->check(); + + QVERIFY(doc->diagnosticMessages().isEmpty()); + QCOMPARE(doc->globalSymbolCount(), 2); + + Class *s = doc->globalSymbolAt(1)->asClass(); + QVERIFY(s); + QCOMPARE(s->name()->identifier()->chars(), "Struct"); + QCOMPARE(s->memberCount(), 2); + + Symbol *memberFunctionFoo = s->memberAt(0); + QVERIFY(memberFunctionFoo); + QCOMPARE(memberFunctionFoo->name()->identifier()->chars(), "foo"); + QVERIFY(memberFunctionFoo->asFunction()); + + Function* bar = s->memberAt(1)->asFunction(); + QVERIFY(bar); + QCOMPARE(bar->name()->identifier()->chars(), "bar"); + QCOMPARE(bar->memberCount(), 1); + + Block* block = bar->memberAt(0)->asBlock(); + QVERIFY(block); + QCOMPARE(block->memberCount(), 1); + + Symbol *variableFoo = block->memberAt(0); + QVERIFY(variableFoo); + QCOMPARE(variableFoo->name()->identifier()->chars(), "foo"); + QVERIFY(variableFoo->asDeclaration()); + + Snapshot snapshot; + snapshot.insert(doc); + + FindUsages find(src, doc, snapshot); + + find(memberFunctionFoo); + QCOMPARE(find.usages().size(), 2); + + QCOMPARE(find.usages()[0].line, 3); + QCOMPARE(find.usages()[0].col, 15); + + QCOMPARE(find.usages()[1].line, 5); + QCOMPARE(find.usages()[1].col, 24); + + find(variableFoo); + QCOMPARE(find.usages().size(), 2); + + QCOMPARE(find.usages()[0].line, 5); + QCOMPARE(find.usages()[0].col, 12); + + QCOMPARE(find.usages()[1].line, 6); + QCOMPARE(find.usages()[1].col, 22); +} + #if 0 @interface Clazz {} +(void)method:(int)arg; @end @implementation Clazz +(void)method:(int)arg {