forked from qt-creator/qt-creator
C++: Fix Find Usages false positive results for function arguments
Code snippet: void bar(); // call find usages for bar from here void foo(int bar); // bar from here should not be in results Add test for member function false positives, that is part of QTCREATORBUG-2176. That was already fixed before. Fixes: QTCREATORBUG-2176 Change-Id: I9a079caa83bbaea1edb7ba6aeb151d4d4c77952f Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
@@ -2170,6 +2170,7 @@ bool FindUsages::visit(NestedDeclaratorAST *ast)
|
|||||||
bool FindUsages::visit(FunctionDeclaratorAST *ast)
|
bool FindUsages::visit(FunctionDeclaratorAST *ast)
|
||||||
{
|
{
|
||||||
// unsigned lparen_token = ast->lparen_token;
|
// unsigned lparen_token = ast->lparen_token;
|
||||||
|
Scope *previousScope = switchScope(ast->symbol);
|
||||||
this->parameterDeclarationClause(ast->parameter_declaration_clause);
|
this->parameterDeclarationClause(ast->parameter_declaration_clause);
|
||||||
// unsigned rparen_token = ast->rparen_token;
|
// unsigned rparen_token = ast->rparen_token;
|
||||||
for (SpecifierListAST *it = ast->cv_qualifier_list; it; it = it->next) {
|
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->exceptionSpecification(ast->exception_specification);
|
||||||
this->trailingReturnType(ast->trailing_return_type);
|
this->trailingReturnType(ast->trailing_return_type);
|
||||||
this->expression(ast->as_cpp_initializer);
|
this->expression(ast->as_cpp_initializer);
|
||||||
// Function *symbol = ast->symbol;
|
(void) switchScope(previousScope);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -84,6 +84,9 @@ private Q_SLOTS:
|
|||||||
void shadowedNames_2();
|
void shadowedNames_2();
|
||||||
void staticVariables();
|
void staticVariables();
|
||||||
|
|
||||||
|
void functionNameFoundInArguments();
|
||||||
|
void memberFunctionFalsePositives_QTCREATORBUG2176();
|
||||||
|
|
||||||
// Qt keywords
|
// Qt keywords
|
||||||
void qproperty_1();
|
void qproperty_1();
|
||||||
|
|
||||||
@@ -352,6 +355,116 @@ void tst_FindUsages::staticVariables()
|
|||||||
QCOMPARE(findUsages.usages().size(), 5);
|
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
|
#if 0
|
||||||
@interface Clazz {} +(void)method:(int)arg; @end
|
@interface Clazz {} +(void)method:(int)arg; @end
|
||||||
@implementation Clazz +(void)method:(int)arg {
|
@implementation Clazz +(void)method:(int)arg {
|
||||||
|
Reference in New Issue
Block a user