-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for regex based CGPatterns
ghstack-source-id: 1f8b03f76102b2b6b97797b0d1cdc2ff126cb366 Pull Request resolved: #529
- Loading branch information
1 parent
e1c44bf
commit 05a2f29
Showing
12 changed files
with
279 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
Copyright (c) 2023 Uber Technologies, Inc. | ||
<p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file | ||
except in compliance with the License. You may obtain a copy of the License at | ||
<p>http://www.apache.org/licenses/LICENSE-2.0 | ||
<p>Unless required by applicable law or agreed to in writing, software distributed under the | ||
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
express or implied. See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
use crate::models::matches::Match; | ||
use itertools::Itertools; | ||
use regex::Regex; | ||
use std::collections::HashMap; | ||
use tree_sitter::Node; | ||
|
||
/// Applies the query upon the given `node`, and gets the first match | ||
/// # Arguments | ||
/// * `node` - the root node to apply the query upon | ||
/// * `source_code` - the corresponding source code string for the node. | ||
/// * `query` - the query to be applied | ||
/// * `recursive` - if `true` it matches the query to `self` and `self`'s sub-ASTs, else it matches the `query` only to `self`. | ||
/// | ||
/// # Returns | ||
/// The range of the match in the source code and the corresponding mapping from tags to code snippets. | ||
pub(crate) fn get_all_matches_for_regex( | ||
node: &Node, source_code: String, regex: &Regex, recursive: bool, replace_node: Option<String>, | ||
) -> Vec<Match> { | ||
// let code_snippet = node.utf8_text(source_code.as_bytes()).unwrap(); | ||
let all_captures = regex.captures_iter(&source_code).collect_vec(); | ||
let names = regex.capture_names().collect_vec(); | ||
let mut all_matches = vec![]; | ||
for captures in all_captures { | ||
// Check if the range of the self (node), and the range of outermost node captured by the query are equal. | ||
let range_matches_node = node.start_byte() == captures.get(0).unwrap().start() | ||
&& node.end_byte() == captures.get(0).unwrap().end(); | ||
let range_matches_inside_node = node.start_byte() <= captures.get(0).unwrap().start() | ||
&& node.end_byte() >= captures.get(0).unwrap().end(); | ||
if (recursive && range_matches_inside_node) || range_matches_node { | ||
let group_by_tag = if let Some(ref rn) = replace_node { | ||
captures | ||
.name(rn) | ||
.unwrap_or_else(|| panic!("the tag {rn} provided in the replace node is not present")) | ||
} else { | ||
captures.get(0).unwrap() | ||
}; | ||
let matches = extract_captures(&captures, &names); | ||
all_matches.push(Match::from_regex(&group_by_tag, matches, &source_code)); | ||
} | ||
} | ||
all_matches | ||
} | ||
|
||
fn extract_captures( | ||
captures: ®ex::Captures<'_>, names: &Vec<Option<&str>>, | ||
) -> HashMap<String, String> { | ||
names | ||
.iter() | ||
.flatten() | ||
.flat_map(|x| { | ||
captures | ||
.name(x) | ||
.map(|v| (x.to_string(), v.as_str().to_string())) | ||
}) | ||
.collect() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
test-resources/java/regex_based_matcher/configurations/edges.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copyright (c) 2023 Uber Technologies, Inc. | ||
# | ||
# <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file | ||
# except in compliance with the License. You may obtain a copy of the License at | ||
# <p>http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# <p>Unless required by applicable law or agreed to in writing, software distributed under the | ||
# License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
# express or implied. See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
[[edges]] | ||
scope = "File" | ||
from = "update_import" | ||
to = ["update_list_int"] | ||
|
||
[[edges]] | ||
scope = "Method" | ||
from = "update_list_int" | ||
to = ["update_add"] |
59 changes: 59 additions & 0 deletions
59
test-resources/java/regex_based_matcher/configurations/rules.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Copyright (c) 2023 Uber Technologies, Inc. | ||
# | ||
# <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file | ||
# except in compliance with the License. You may obtain a copy of the License at | ||
# <p>http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# <p>Unless required by applicable law or agreed to in writing, software distributed under the | ||
# License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
# express or implied. See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
# Replace foo().bar().baz() with `true` inside methods not nnotated as @DoNotCleanup | ||
[[rules]] | ||
name = "replace_call" | ||
query = """rgx (?P<n1>foo\\(\\)\\.bar\\(\\)\\.baz\\(\\))""" | ||
replace_node = "n1" | ||
replace = "true" | ||
groups = ["replace_expression_with_boolean_literal"] | ||
[[rules.filters]] | ||
enclosing_node = """(method_declaration) @md""" | ||
not_contains = ["""rgx @DoNotCleanup"""] | ||
|
||
# Before: | ||
# abc().def().ghi() | ||
# abc().fed().ghi() | ||
[[rules]] | ||
name = "replace_call_def_fed" | ||
query = """rgx (?P<n>abc\\(\\)\\.(?P<m_def>def)\\(\\)\\.ghi\\(\\))""" | ||
replace_node = "m_def" | ||
replace = "fed" | ||
|
||
|
||
# The below three rules do a dummy type migration from List<Integer> to NewList | ||
|
||
# Updates the import statement from `java.util.List` to `com.uber.NEwList` | ||
[[rules]] | ||
name = "update_import" | ||
query = """rgx (?P<n>java\\.util\\.List)""" | ||
replace_node = "n" | ||
replace = "com.uber.NewList" | ||
|
||
# Updates the type of local variables from `List<Integer>` to `com.uber.NewList` | ||
[[rules]] | ||
name = "update_list_int" | ||
query = """rgx (?P<var_decl>(?P<type>List<Integer>)\\s*(?P<name>\\w+)\\s*=.*;)""" | ||
replace_node = "type" | ||
replace = "NewList" | ||
is_seed_rule = false | ||
[[rules.filter]] | ||
enclosing_node = "(method_declaration) @cmd" | ||
|
||
# Updates the relevant callsite from `add` to `addToNewList` | ||
[[rules]] | ||
name = "update_add" | ||
query = """rgx (?P<call>@name\\.(?P<m_name>add)\\(\\w+\\))""" | ||
replace_node = "m_name" | ||
replace = "addToNewList" | ||
holes = ["name"] | ||
is_seed_rule = false |
34 changes: 34 additions & 0 deletions
34
test-resources/java/regex_based_matcher/expected/Sample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.uber.piranha; | ||
|
||
import com.uber.NewList; | ||
|
||
class A { | ||
|
||
void foobar() { | ||
System.out.println("Hello World!"); | ||
System.out.println(true); | ||
} | ||
|
||
@DoNotCleanup | ||
void barfn() { | ||
boolean b = foo().bar().baz(); | ||
System.out.println(b); | ||
} | ||
|
||
void foofn() { | ||
int total = abc().fed().ghi(); | ||
} | ||
|
||
void someTypeChange() { | ||
// Will get updated | ||
NewList a = getList(); | ||
Integer item = getItem(); | ||
a.addToNewList(item); | ||
|
||
// Will not get updated | ||
List<String> b = getListStr(); | ||
Integer item = getItemStr(); | ||
b.add(item); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.uber.piranha; | ||
|
||
import java.util.List; | ||
|
||
class A { | ||
|
||
void foobar() { | ||
boolean b = foo().bar().baz(); | ||
if (b) { | ||
System.out.println("Hello World!"); | ||
} | ||
System.out.println(b); | ||
} | ||
|
||
@DoNotCleanup | ||
void barfn() { | ||
boolean b = foo().bar().baz(); | ||
System.out.println(b); | ||
} | ||
|
||
void foofn() { | ||
int total = abc().def().ghi(); | ||
} | ||
|
||
void someTypeChange() { | ||
// Will get updated | ||
List<Integer> a = getList(); | ||
Integer item = getItem(); | ||
a.add(item); | ||
|
||
// Will not get updated | ||
List<String> b = getListStr(); | ||
Integer item = getItemStr(); | ||
b.add(item); | ||
} | ||
|
||
} |