Skip to content

Codemod design improvement#28

Merged
nahsra merged 2 commits into
mainfrom
add_pointer_collection
Mar 17, 2023
Merged

Codemod design improvement#28
nahsra merged 2 commits into
mainfrom
add_pointer_collection

Conversation

@nahsra

@nahsra nahsra commented Mar 17, 2023

Copy link
Copy Markdown
Contributor

Although we didn't end up choosing to do the "AST pointer collection" strategy, we still liked some of the API design we built as part of brainstorming, so we decided to implement it.

@nahsra nahsra requested a review from andrecsilva March 17, 2023 18:40
@nahsra nahsra self-assigned this Mar 17, 2023
@pixeebot

pixeebot Bot commented Mar 17, 2023

Copy link
Copy Markdown
Contributor

We found no opportunities for hardening in your pull request. Good work!
Thanks!

docs | feedback

@nahsra nahsra merged commit cbbbe6e into main Mar 17, 2023
@nahsra nahsra deleted the add_pointer_collection branch March 17, 2023 18:52
if (imports.contains(newImport)) {
return;
}
for (ImportDeclaration existingImport : imports) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this since we have ASTTransforms::addImportIfMissing.

logger.error("Unexpected node encountered in {}:{}", context.path(), region);
return;
}
if (JavaParserUtils.regionMatchesNodeStart(node, region)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may match undesired nodes since it only checks for overlapping. For example in n1().n2().n3() the range of n1() overlaps n1().n2().n3(). This may be a particular problem if n1(), n2() and n3() are calls to the same method. I'd rather do exact matching.

Also you need to check if semgrep's region in the SARIF matches JavaParser ranges exactly. I know CodeQL does not (it includes an extra column).

List<Result> results = sarif.getResultsByPath(context.path());
List<? extends Node> allNodes = cu.findAll(nodeType);

for (Result result : results) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine this way but a small suggestion using streams:

List<Region> regions = results.stream().map(r -> r.getLocations().get(0).getPhysicalLocation().getRegion()).collect(Collectors.toList())
var allMatchedNodes = allNodes.stream().filter(n -> regions.stream().anyMatch(r -> JavaParserUtils.regionMatchesNode(n, r)));      

Then iterate/filter/collect the resulting Stream allMatchedNodes.

}

/**
* Creates a visitor for the given context and locations.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update the javadoc here, It does not create a visitor anymore.

*/
public abstract ModifierVisitor<FileWeavingContext> createVisitor(
CodemodInvocationContext context, List<Region> regions);
public abstract void onSemgrepResultFound(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small suggestion: make it return CompilationUnit. This makes it clear that the main goal is change the AST.

ryandens pushed a commit that referenced this pull request Aug 24, 2023
* Fully offline resolution

* cleanup

* [no ci] Autogenerated JaCoCo coverage badge

---------

Co-authored-by: Jacoco Coverage Update Action <pixee@users.noreply.github.com>
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.

2 participants