@@ -43,40 +43,32 @@ class ExternalDeclExtractor(val logger: FileLogger, val invocationTrapFile: Stri
43
43
fun extractLater (c : IrClass ) = extractLater(c, " " )
44
44
45
45
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 " )
50
49
}
51
50
}
52
51
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 ) {
54
53
// In order to avoid excessively long signatures which can lead to trap file names longer than the filesystem
55
54
// limit, we truncate and add a hash to preserve uniqueness if necessary.
56
55
val signature = if (possiblyLongSignature.length > 100 ) {
57
56
possiblyLongSignature.substring(0 , 92 ) + " #" + StringDigestor .digest(possiblyLongSignature).substring(0 , 8 )
58
57
} else { possiblyLongSignature }
59
- output.getTrapLockerForDecl(irDecl, signature).useAC { locker ->
58
+ output.getTrapLockerForDecl(irDecl, signature, fromSource ).useAC { locker ->
60
59
locker.trapFileManager.useAC { manager ->
61
60
val shortName = when (irDecl) {
62
61
is IrDeclarationWithName -> irDecl.name.asString()
63
62
else -> " (unknown name)"
64
63
}
65
- if (manager == null ) {
64
+ if (manager == null ) {
66
65
logger.info(" Skipping extracting external decl $shortName " )
67
66
} else {
68
67
val trapFile = manager.file
69
68
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)
77
69
try {
78
70
GZIPOutputStream (trapTmpFile.outputStream()).bufferedWriter().use {
79
- extractorFn(it, binaryPath, signature, manager)
71
+ extractorFn(it, signature, manager)
80
72
}
81
73
82
74
if (! trapTmpFile.renameTo(trapFile)) {
@@ -97,33 +89,40 @@ class ExternalDeclExtractor(val logger: FileLogger, val invocationTrapFile: Stri
97
89
externalDeclWorkList.clear()
98
90
nextBatch.forEach { workPair ->
99
91
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)
112
98
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 )
114
110
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)
123
112
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
+ }
127
126
}
128
127
}
129
128
}
0 commit comments