Skip to content

Add documentation members to Query#1090

Merged
ursachec merged 1 commit into
masterfrom
claudiu/query-debug
Jan 12, 2021
Merged

Add documentation members to Query#1090
ursachec merged 1 commit into
masterfrom
claudiu/query-debug

Conversation

@ursachec
Copy link
Copy Markdown
Contributor

@ursachec ursachec commented Jan 12, 2021

Example usage in the query-database:

object CopyLoops extends QueryBundle {

  @q
  def isCopyLoop(): Query = Query(
    name = "copy-loop",
    author = Crew.fabs,
    title = "Copy loop detected",
    description =
      """
        |For (buf, indices) pairs, determine those inside control structures (for, while, if ...)
        |where any of the calls made outside of the body (block) are Inc operations. Determine
        |the first argument of that Inc operation and check if they are used as indices for
        |the write operation into the buffer.
        |""".stripMargin,
    score = 2,
    startLine = sourcecode.Line(),
    traversal = { cpg =>
      cpg.assignment.target.isArrayAccess
        .map { access =>
          (access.array, access.subscripts.code.toSet)
        }
        .filter {
          case (buf, subscripts) =>
            val incIdentifiers = buf.inAst.isControlStructure.astChildren
              .filterNot(_.isBlock)
              .assignments
              .target
              .code
              .toSet
            (incIdentifiers & subscripts).nonEmpty
        }
        .map(_._1)
    },
    endLine = sourcecode.Line(),
    fileName = sourcecode.FileName()
  )

}

Example usage in the `query-database`:

```
object CopyLoops extends QueryBundle {

  @q
  def isCopyLoop(): Query = Query(
    name = "copy-loop",
    author = Crew.fabs,
    title = "Copy loop detected",
    description =
      """
        |For (buf, indices) pairs, determine those inside control structures (for, while, if ...)
        |where any of the calls made outside of the body (block) are Inc operations. Determine
        |the first argument of that Inc operation and check if they are used as indices for
        |the write operation into the buffer.
        |""".stripMargin,
    score = 2,
    startLine = sourcecode.Line(),
    traversal = { cpg =>
      cpg.assignment.target.isArrayAccess
        .map { access =>
          (access.array, access.subscripts.code.toSet)
        }
        .filter {
          case (buf, subscripts) =>
            val incIdentifiers = buf.inAst.isControlStructure.astChildren
              .filterNot(_.isBlock)
              .assignments
              .target
              .code
              .toSet
            (incIdentifiers & subscripts).nonEmpty
        }
        .map(_._1)
    },
    endLine = sourcecode.Line(),
    fileName = sourcecode.FileName()
  )

}
```
@ursachec ursachec force-pushed the claudiu/query-debug branch from f3a2143 to 71e42f1 Compare January 12, 2021 19:32
@ursachec ursachec merged commit c0f2cb8 into master Jan 12, 2021
@delete-merged-branch delete-merged-branch Bot deleted the claudiu/query-debug branch January 12, 2021 19:46
@fabsx00
Copy link
Copy Markdown
Contributor

fabsx00 commented Jan 13, 2021

So, this means that in call queries we need to add startLine = sourcecode.Line(), right? Would it be possible to introduce this as a default argument?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants