Skip to content

I can't dechiper error message for query. #5291

@BengtGustafsson

Description

@BengtGustafsson

This query is apparently wrong in some way. If I replace FunctionDeclarationEntry with Function it works, which is mysterious to me. The error message is not helpful, to say the least:

import cpp

from FunctionDeclarationEntry decl
where decl.getParentScope() instanceof BlockStmt
select decl, "This function declaration is extern in a function body."

Possibly you can see this query for yourself here: https://lgtm.com/query/6168120573077511864/

I could not figure out a way to copy paste the error message, so unless the link works you'd have to paste the query from here to see it. It is long and ends with the ominous "and 139 more disjuncts". I could not figure out the cause, both Function and FunctionDeclarationEntry inherit Element which contains the implementation of getParentScope so I don't see how the error could arise.

FYI, what I wanted to do was to find function declarations inside function bodies. This is allowed by an old C rule inherited by C++, but is very unfortunate syntactically in that it is very hard to distinguish from a local variable declaration. This is called the most vexing parse in C++. As there is no real need for such extern declarations inside functions (without the extern keyword anyway) I have wanted to scan large source code repositories for such declarations for a long time, but never came around to writing a clang tool for the purpose. Then I stumbled on CodeQL and it seemed to be able to do what I wanted (except maybe detect if there was an extern keyword). I suspect it would find very few instances, which could motivate a deprecation from the standard.

While this is not the prime application for CodeQL I think that removing archaic and unsafe constructs from C++ would benefit the main purpose. As the C++ committee is very careful about backwards compatibility N occurances in all of github would be a very strong argument, provided a low enough N. Note that using the comma operator inside an array indexing bracket was deprecated in C++20 after a similar code search revealed that it was almost never used, and when it was, it was mostly a bug.

Metadata

Metadata

Assignees

Labels

C++questionFurther information is requested

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions