astor icon indicating copy to clipboard operation
astor copied to clipboard

missing comments on patches

Open martinezmatias opened this issue 5 years ago • 9 comments

The modified version of the patched file does not include comments, even it keeps the original line number of elements. (it leaves empty lines). Reported by @tdurieux.

martinezmatias avatar May 20 '20 07:05 martinezmatias

Here, we disable before creating the model: https://github.com/SpoonLabs/astor/blob/b11cd3d74525cc2ed00c32119e90533fd5599920/src/main/java/fr/inria/astor/core/manipulation/MutationSupporter.java#L69

martinezmatias avatar May 20 '20 07:05 martinezmatias

Actually, if we enable the inclusion of comments

actory.getEnvironment().setCommentEnabled(true); 

we have:

ava.lang.UnsupportedOperationException: PartialSourcePosition only contains a CompilationUnit
	at spoon.reflect.cu.position.NoSourcePosition.getSourceEnd(NoSourcePosition.java:58)
	at spoon.reflect.visitor.ElementPrinterHelper.getComments(ElementPrinterHelper.java:417)
	at spoon.reflect.visitor.ElementPrinterHelper.writeComment(ElementPrinterHelper.java:408)
	at spoon.reflect.visitor.ElementPrinterHelper.writeHeader(ElementPrinterHelper.java:351)
	at spoon.reflect.visitor.DefaultJavaPrettyPrinter.writeHeader(DefaultJavaPrettyPrinter.java:1957)
	at spoon.reflect.visitor.DefaultJavaPrettyPrinter.calculate(DefaultJavaPrettyPrinter.java:1983)
	at spoon.support.JavaOutputProcessor.createJavaFile(JavaOutputProcessor.java:114)
	at spoon.support.JavaOutputProcessor.process(JavaOutputProcessor.java:150)
	at fr.inria.astor.core.manipulation.bytecode.OutputWritter.saveSourceCode(OutputWritter.java:57)
	at fr.inria.astor.core.manipulation.MutationSupporter.generateSourceCodeFromCtClass(MutationSupporter.java:141)
	at fr.inria.astor.core.manipulation.MutationSupporter.saveSourceCodeOnDiskProgramVariant(MutationSupporter.java:121)
	at fr.inria.astor.util.PatchDiffCalculator.getDiff(PatchDiffCalculator.java:57)
	at fr.inria.astor.core.solutionsearch.AstorCoreEngine.computePatchDiff(AstorCoreEngine.java:229)
	at fr.inria.astor.core.solutionsearch.AstorCoreEngine.atEnd(AstorCoreEngine.java:186)

martinezmatias avatar May 20 '20 10:05 martinezmatias

Solution: diff context = 0

martinezmatias avatar May 20 '20 13:05 martinezmatias

In the generated result file, I don't have comments either. Has this been fixed? Could you explain a bit how to keep the comments?

JakkuSakura avatar Jul 30 '21 20:07 JakkuSakura

Hi @qiujiangkun

To include comments, we should include them in the generated model. if I am not wrong, by default, those are not included. https://github.com/SpoonLabs/astor/blob/b11cd3d74525cc2ed00c32119e90533fd5599920/src/main/java/fr/inria/astor/core/manipulation/MutationSupporter.java#L69

I'd say that would be simple to modify astor to include them: add an option that indicates whether comments should be included or not, then use the value (e.g., read it using the ConfigurationProperties) when Astor creates the model here

martinezmatias avatar Aug 02 '21 13:08 martinezmatias

PRs are welcome @qiujiangkun , you are welcome if you 'd try to implement this feature. We can help you :)

martinezmatias avatar Aug 02 '21 13:08 martinezmatias

Thanks for the solution. I've implemented a very primitive algorithm based on Intellij IDEA's great PSI model, to match patch against original code structure as much as possible. https://github.com/bc2021-program-repair/intellij-idea-astor-plugin/blob/main/src/main/kotlin/org/bytecamp/program_repair/astor_plugin/code/CodeImporter.kt image

JakkuSakura avatar Aug 02 '21 14:08 JakkuSakura

That code could be very useful for future research. Do you have a pointer?

martinezmatias avatar Aug 02 '21 14:08 martinezmatias

Yes, I have modified my previous comment to include link to that code

JakkuSakura avatar Aug 02 '21 14:08 JakkuSakura