@@ -334,17 +334,28 @@ private static class Context {
334
334
private final Label parent ;
335
335
private final int childIndex ;
336
336
private final IdContext idcontext ;
337
+ private final boolean binopOperand ;
337
338
338
339
public Context (Label parent , int childIndex , IdContext idcontext ) {
340
+ this (parent , childIndex , idcontext , false );
341
+ }
342
+
343
+ public Context (Label parent , int childIndex , IdContext idcontext , boolean binopOperand ) {
339
344
this .parent = parent ;
340
345
this .childIndex = childIndex ;
341
346
this .idcontext = idcontext ;
347
+ this .binopOperand = binopOperand ;
342
348
}
343
349
344
350
/** True if the visited AST node occurs as part of a type annotation. */
345
351
public boolean isInsideType () {
346
352
return idcontext .isInsideType ();
347
353
}
354
+
355
+ /** True if the visited AST node occurs as one of the operands of a binary operation. */
356
+ public boolean isBinopOperand () {
357
+ return binopOperand ;
358
+ }
348
359
}
349
360
350
361
private class V extends DefaultVisitor <Context , Label > {
@@ -360,16 +371,24 @@ public V(Platform platform, SourceType sourceType) {
360
371
}
361
372
362
373
private Label visit (INode child , Label parent , int childIndex ) {
363
- return visit (child , parent , childIndex , IdContext .VAR_BIND );
374
+ return visit (child , parent , childIndex , IdContext .VAR_BIND , false );
364
375
}
365
376
366
377
private Label visitAll (List <? extends INode > children , Label parent ) {
367
378
return visitAll (children , parent , IdContext .VAR_BIND , 0 );
368
379
}
369
380
370
381
private Label visit (INode child , Label parent , int childIndex , IdContext idContext ) {
382
+ return visit (child , parent , childIndex , idContext , false );
383
+ }
384
+
385
+ private Label visit (INode child , Label parent , int childIndex , boolean binopOperand ) {
386
+ return visit (child , parent , childIndex , IdContext .VAR_BIND , binopOperand );
387
+ }
388
+
389
+ private Label visit (INode child , Label parent , int childIndex , IdContext idContext , boolean binopOperand ) {
371
390
if (child == null ) return null ;
372
- return child .accept (this , new Context (parent , childIndex , idContext ));
391
+ return child .accept (this , new Context (parent , childIndex , idContext , binopOperand ));
373
392
}
374
393
375
394
private Label visitAll (
@@ -381,7 +400,7 @@ private Label visitAll(
381
400
List <? extends INode > children , Label parent , IdContext idContext , int index , int step ) {
382
401
Label res = null ;
383
402
for (INode child : children ) {
384
- res = visit (child , parent , index , idContext );
403
+ res = visit (child , parent , index , idContext , false );
385
404
index += step ;
386
405
}
387
406
return res ;
@@ -821,33 +840,33 @@ public Label visit(AssignmentExpression nd, Context c) {
821
840
return key ;
822
841
}
823
842
824
- // set to determine which BinaryExpression has been extracted as regexp
825
- private Set <Expression > extractedAsRegexp = new HashSet <>();
826
-
827
843
@ Override
828
844
public Label visit (BinaryExpression nd , Context c ) {
829
845
Label key = super .visit (nd , c );
830
- extractedAsRegexp .add (nd .getLeft ());
831
- extractedAsRegexp .add (nd .getRight ());
832
- visit (nd .getLeft (), key , 0 );
833
- visit (nd .getRight (), key , 1 );
834
- if (extractedAsRegexp .contains (nd )) {
835
- return key ;
846
+ visit (nd .getLeft (), key , 0 , true );
847
+ visit (nd .getRight (), key , 1 , true );
848
+ extractRegxpFromBinop (nd , c );
849
+ return key ;
850
+ }
851
+
852
+ private void extractRegxpFromBinop (BinaryExpression nd , Context c ) {
853
+ if (c .isBinopOperand ()) {
854
+ return ;
836
855
}
837
856
Pair <String , OffsetTranslation > concatResult = getStringConcatResult (nd );
838
857
if (concatResult == null ) {
839
- return key ;
858
+ return ;
840
859
}
841
860
String rawString = concatResult .fst ();
842
861
if (rawString .length () > 1000 && !rawString .trim ().isEmpty ()) {
843
- return key ;
862
+ return ;
844
863
}
845
864
OffsetTranslation offsets = concatResult .snd ();
846
865
Position start = nd .getLoc ().getStart ();
847
866
com .semmle .util .locations .Position startPos = new com .semmle .util .locations .Position (start .getLine (), start .getColumn (), start .getOffset ());
848
867
SourceMap sourceMap = SourceMap .legacyWithStartPos (SourceMap .fromString (nd .getLoc ().getSource ()).offsetBy (0 , offsets ), startPos );
849
868
regexpExtractor .extract (rawString , sourceMap , nd , true );
850
- return key ;
869
+ return ;
851
870
}
852
871
853
872
@ Override
0 commit comments