Skip to content

Commit 748637c

Browse files
committed
Tidy and use version 0 for classes extracted from source
1 parent e34d72a commit 748637c

File tree

2 files changed

+46
-44
lines changed

2 files changed

+46
-44
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ public void setHasError() {
422422
* previously set by a call to {@link OdasaOutput#setCurrentSourceFile(File)}.
423423
*/
424424
public TrapLocker getTrapLockerForCurrentSourceFile() {
425-
return new TrapLocker((IrClass)null, null);
425+
return new TrapLocker((IrClass)null, null, true);
426426
}
427427

428428
/**
@@ -460,8 +460,8 @@ public TrapLocker getTrapLockerForModule(String moduleName) {
460460
*
461461
* @return a {@link TrapLocker} for the trap file corresponding to the given class symbol.
462462
*/
463-
public TrapLocker getTrapLockerForDecl(IrDeclaration sym, String signature) {
464-
return new TrapLocker(sym, signature);
463+
public TrapLocker getTrapLockerForDecl(IrDeclaration sym, String signature, boolean fromSource) {
464+
return new TrapLocker(sym, signature, fromSource);
465465
}
466466

467467
public class TrapLocker implements AutoCloseable {
@@ -472,7 +472,7 @@ public class TrapLocker implements AutoCloseable {
472472
private File trapFileBase = null;
473473
private TrapClassVersion trapFileVersion = null;
474474
private final String signature;
475-
private TrapLocker(IrDeclaration decl, String signature) {
475+
private TrapLocker(IrDeclaration decl, String signature, boolean fromSource) {
476476
this.sym = decl;
477477
this.signature = signature;
478478
if (sym==null) {
@@ -485,7 +485,10 @@ private TrapLocker(IrDeclaration decl, String signature) {
485485
} else {
486486
// We encode the metadata into the filename, so that the
487487
// TRAP filenames for different metadatas don't overlap.
488-
trapFileVersion = TrapClassVersion.fromSymbol(sym, log);
488+
if (fromSource)
489+
trapFileVersion = new TrapClassVersion(0, 0, 0, "kotlin");
490+
else
491+
trapFileVersion = TrapClassVersion.fromSymbol(sym, log);
489492
String baseName = normalTrapFile.getName().replace(".trap.gz", "");
490493
// If a class has lots of inner classes, then we get lots of files
491494
// in a single directory. This makes our directory listings later slow.

java/kotlin-extractor/src/main/kotlin/ExternalDeclExtractor.kt

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -43,40 +43,32 @@ class ExternalDeclExtractor(val logger: FileLogger, val invocationTrapFile: Stri
4343
fun extractLater(c: IrClass) = extractLater(c, "")
4444

4545
fun noteClassSourceExtractedTo(c: IrClass, sourceFile: String) {
46-
extractDecl(c, "") { trapFileBW, _, _, _ ->
47-
val tw = TrapWriter(logger.loggerBase, TrapLabelManager(), trapFileBW, diagnosticTrapWriter)
48-
tw.writeComment(".class trap file stubbed as source is extracted to $sourceFile")
49-
tw.writeComment("Part of invocation $invocationTrapFile")
46+
extractDecl(c, "", true) { trapFileBW, _, _ ->
47+
trapFileBW.write("// .class trap file stubbed because this class was extracted from source in $sourceFile\n")
48+
trapFileBW.write("// Part of invocation $invocationTrapFile\n")
5049
}
5150
}
5251

53-
fun extractDecl(irDecl: IrDeclaration, possiblyLongSignature: String, extractorFn: (BufferedWriter, String, String, OdasaOutput.TrapFileManager) -> Unit) {
52+
private fun extractDecl(irDecl: IrDeclaration, possiblyLongSignature: String, fromSource: Boolean, extractorFn: (BufferedWriter, String, OdasaOutput.TrapFileManager) -> Unit) {
5453
// In order to avoid excessively long signatures which can lead to trap file names longer than the filesystem
5554
// limit, we truncate and add a hash to preserve uniqueness if necessary.
5655
val signature = if (possiblyLongSignature.length > 100) {
5756
possiblyLongSignature.substring(0, 92) + "#" + StringDigestor.digest(possiblyLongSignature).substring(0, 8)
5857
} else { possiblyLongSignature }
59-
output.getTrapLockerForDecl(irDecl, signature).useAC { locker ->
58+
output.getTrapLockerForDecl(irDecl, signature, fromSource).useAC { locker ->
6059
locker.trapFileManager.useAC { manager ->
6160
val shortName = when(irDecl) {
6261
is IrDeclarationWithName -> irDecl.name.asString()
6362
else -> "(unknown name)"
6463
}
65-
if(manager == null) {
64+
if (manager == null) {
6665
logger.info("Skipping extracting external decl $shortName")
6766
} else {
6867
val trapFile = manager.file
6968
val trapTmpFile = File.createTempFile("${trapFile.nameWithoutExtension}.", ".${trapFile.extension}.tmp", trapFile.parentFile)
70-
71-
val containingClass = getContainingClassOrSelf(irDecl)
72-
if (containingClass == null) {
73-
logger.errorElement("Unable to get containing class", irDecl)
74-
return
75-
}
76-
val binaryPath = getIrClassBinaryPath(containingClass)
7769
try {
7870
GZIPOutputStream(trapTmpFile.outputStream()).bufferedWriter().use {
79-
extractorFn(it, binaryPath, signature, manager)
71+
extractorFn(it, signature, manager)
8072
}
8173

8274
if (!trapTmpFile.renameTo(trapFile)) {
@@ -97,33 +89,40 @@ class ExternalDeclExtractor(val logger: FileLogger, val invocationTrapFile: Stri
9789
externalDeclWorkList.clear()
9890
nextBatch.forEach { workPair ->
9991
val (irDecl, possiblyLongSignature) = workPair
100-
extractDecl(irDecl, possiblyLongSignature) { trapFileBW, binaryPath, signature, manager ->
101-
// We want our comments to be the first thing in the file,
102-
// so start off with a mere TrapWriter
103-
val tw = TrapWriter(logger.loggerBase, TrapLabelManager(), trapFileBW, diagnosticTrapWriter)
104-
tw.writeComment("Generated by the CodeQL Kotlin extractor for external dependencies")
105-
tw.writeComment("Part of invocation $invocationTrapFile")
106-
if (signature != possiblyLongSignature) {
107-
tw.writeComment("Function signature abbreviated; full signature is: $possiblyLongSignature")
108-
}
109-
// Now elevate to a SourceFileTrapWriter, and populate the
110-
// file information if needed:
111-
val ftw = tw.makeFileTrapWriter(binaryPath, true)
92+
extractDecl(irDecl, possiblyLongSignature, false) { trapFileBW, signature, manager ->
93+
val containingClass = getContainingClassOrSelf(irDecl)
94+
if (containingClass == null) {
95+
logger.errorElement("Unable to get containing class", irDecl)
96+
} else {
97+
val binaryPath = getIrClassBinaryPath(containingClass)
11298

113-
val fileExtractor = KotlinFileExtractor(logger, ftw, null, binaryPath, manager, this, primitiveTypeMapping, pluginContext, KotlinFileExtractor.DeclarationStack(), globalExtensionState)
99+
// We want our comments to be the first thing in the file,
100+
// so start off with a mere TrapWriter
101+
val tw = TrapWriter(logger.loggerBase, TrapLabelManager(), trapFileBW, diagnosticTrapWriter)
102+
tw.writeComment("Generated by the CodeQL Kotlin extractor for external dependencies")
103+
tw.writeComment("Part of invocation $invocationTrapFile")
104+
if (signature != possiblyLongSignature) {
105+
tw.writeComment("Function signature abbreviated; full signature is: $possiblyLongSignature")
106+
}
107+
// Now elevate to a SourceFileTrapWriter, and populate the
108+
// file information if needed:
109+
val ftw = tw.makeFileTrapWriter(binaryPath, true)
114110

115-
if (irDecl is IrClass) {
116-
// Populate a location and compilation-unit package for the file. This is similar to
117-
// the beginning of `KotlinFileExtractor.extractFileContents` but without an `IrFile`
118-
// to start from.
119-
val pkg = irDecl.packageFqName?.asString() ?: ""
120-
val pkgId = fileExtractor.extractPackage(pkg)
121-
ftw.writeHasLocation(ftw.fileId, ftw.getWholeFileLocation())
122-
ftw.writeCupackage(ftw.fileId, pkgId)
111+
val fileExtractor = KotlinFileExtractor(logger, ftw, null, binaryPath, manager, this, primitiveTypeMapping, pluginContext, KotlinFileExtractor.DeclarationStack(), globalExtensionState)
123112

124-
fileExtractor.extractClassSource(irDecl, extractDeclarations = !irDecl.isFileClass, extractStaticInitializer = false, extractPrivateMembers = false, extractFunctionBodies = false)
125-
} else {
126-
fileExtractor.extractDeclaration(irDecl, extractPrivateMembers = false, extractFunctionBodies = false)
113+
if (irDecl is IrClass) {
114+
// Populate a location and compilation-unit package for the file. This is similar to
115+
// the beginning of `KotlinFileExtractor.extractFileContents` but without an `IrFile`
116+
// to start from.
117+
val pkg = irDecl.packageFqName?.asString() ?: ""
118+
val pkgId = fileExtractor.extractPackage(pkg)
119+
ftw.writeHasLocation(ftw.fileId, ftw.getWholeFileLocation())
120+
ftw.writeCupackage(ftw.fileId, pkgId)
121+
122+
fileExtractor.extractClassSource(irDecl, extractDeclarations = !irDecl.isFileClass, extractStaticInitializer = false, extractPrivateMembers = false, extractFunctionBodies = false)
123+
} else {
124+
fileExtractor.extractDeclaration(irDecl, extractPrivateMembers = false, extractFunctionBodies = false)
125+
}
127126
}
128127
}
129128
}

0 commit comments

Comments
 (0)