@@ -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