Skip to content

Commit dff1cf4

Browse files
committed
Kotlin: Don't write TRAP files that are already out-of-date
1 parent e6e0fe0 commit dff1cf4

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

java/kotlin-extractor/src/main/java/com/semmle/extractor/java/OdasaOutput.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ private void deleteTrapFileAndDependencies(IrDeclaration sym, String signature)
269269
* Any unique suffix needed to distinguish `sym` from other declarations with the same name.
270270
* For functions for example, this means its parameter signature.
271271
*/
272-
private TrapFileManager getMembersWriterForDecl(File trap, IrDeclaration sym, String signature) {
272+
private TrapFileManager getMembersWriterForDecl(File trap, File trapFileBase, TrapClassVersion trapFileVersion, IrDeclaration sym, String signature) {
273273
if (use_trap_locking) {
274274
TrapClassVersion currVersion = TrapClassVersion.fromSymbol(sym, log);
275275
String shortName = sym instanceof IrDeclarationWithName ? ((IrDeclarationWithName)sym).getName().asString() : "(name unknown)";
@@ -298,11 +298,30 @@ private TrapFileManager getMembersWriterForDecl(File trap, IrDeclaration sym, St
298298
// then renamed to its trap-old name, then we
299299
// don't need to rewrite it only to rename it
300300
// again.
301-
File trapOld = new File(trap.getParentFile(), trap.getName().replace(".trap.gz", ".trap-old.gz"));
301+
File trapFileDir = trap.getParentFile();
302+
File trapOld = new File(trapFileDir, trap.getName().replace(".trap.gz", ".trap-old.gz"));
302303
if (trapOld.exists()) {
303304
log.warn("Not rewriting trap file for " + trap.toString() + " as the trap-old exists");
304305
return null;
305306
}
307+
// Otherwise, if any newer TRAP file has already
308+
// been written then we don't need to write
309+
// anything.
310+
if (trapFileBase != null && trapFileVersion != null && trapFileDir.exists()) {
311+
String trapFileBaseName = trapFileBase.getName();
312+
313+
for (File f: FileUtil.list(trapFileDir)) {
314+
String name = f.getName();
315+
Matcher m = selectClassVersionComponents.matcher(name);
316+
if (m.matches() && m.group(1).equals(trapFileBaseName)) {
317+
TrapClassVersion v = new TrapClassVersion(Integer.valueOf(m.group(2)), Integer.valueOf(m.group(3)), Long.valueOf(m.group(4)), m.group(5));
318+
if (v.newerThan(trapFileVersion)) {
319+
log.warn("Not rewriting trap file for " + trap.toString() + " as " + f.toString() + " exists");
320+
return null;
321+
}
322+
}
323+
}
324+
}
306325
}
307326
return trapWriter(trap, sym, signature);
308327
}
@@ -390,6 +409,8 @@ public void setHasError() {
390409
* Trap file locking.
391410
*/
392411

412+
private final Pattern selectClassVersionComponents = Pattern.compile("(.*)#(-?[0-9]+)\\.(-?[0-9]+)-(-?[0-9]+)-(.*)\\.trap\\.gz");
413+
393414
/**
394415
* <b>CAUTION</b>: to avoid the potential for deadlock between multiple concurrent extractor processes,
395416
* only one source file {@link TrapLocker} may be open at any time, and the lock must be obtained
@@ -485,14 +506,12 @@ public TrapFileManager getTrapFileManager() {
485506
if (use_trap_locking) {
486507
lockTrapFile(trapFile);
487508
}
488-
return getMembersWriterForDecl(trapFile, sym, signature);
509+
return getMembersWriterForDecl(trapFile, trapFileBase, trapFileVersion, sym, signature);
489510
} else {
490511
return null;
491512
}
492513
}
493514

494-
private final Pattern selectClassVersionComponents = Pattern.compile("(.*)#(-?[0-9]+)\\.(-?[0-9]+)-(-?[0-9]+)-(.*)\\.trap\\.gz");
495-
496515
@Override
497516
public void close() {
498517
if (trapFile!=null) {

0 commit comments

Comments
 (0)