package jadx.core.dex.visitors;

import jadx.core.dex.instructions.args.ArgType;
import jadx.core.dex.instructions.args.RegisterArg;
import jadx.core.dex.nodes.BlockNode;
import jadx.core.dex.nodes.InsnNode;
import jadx.core.dex.nodes.MethodNode;
import jadx.core.dex.nodes.parser.DebugInfoParser;
import jadx.core.utils.BlockUtils;
import jadx.core.utils.exceptions.JadxException;

public class DebugInfoVisitor extends AbstractVisitor {
	@Override
	public void visit(MethodNode mth) throws JadxException {
		int debugOffset = mth.getDebugInfoOffset();
		if (debugOffset > 0) {
			InsnNode[] insnArr = mth.getInstructions();
			DebugInfoParser debugInfoParser = new DebugInfoParser(mth, debugOffset, insnArr);
			debugInfoParser.process();

			if (insnArr.length != 0) {
				int line = insnArr[0].getSourceLine();
				if (line != 0) {
					mth.setSourceLine(line - 1);
				}
			}
			if (!mth.getReturnType().equals(ArgType.VOID)) {
				// fix debug for splitter 'return' instructions
				for (BlockNode exit : mth.getExitBlocks()) {
					InsnNode ret = BlockUtils.getLastInsn(exit);
					if (ret == null) {
						continue;
					}
					InsnNode oldRet = insnArr[ret.getOffset()];
					if (oldRet == ret) {
						continue;
					}
					RegisterArg oldArg = (RegisterArg) oldRet.getArg(0);
					RegisterArg newArg = (RegisterArg) ret.getArg(0);
					newArg.mergeDebugInfo(oldArg.getType(), oldArg.getName());
					ret.setSourceLine(oldRet.getSourceLine());
				}
			}
		}
		mth.unloadInsnArr();
	}
}

@@ -761,24 +761,87 @@
 gth != 0) %7B%0A
+%09%09%09%09for (InsnNode insn : insnArr) %7B%0A%09%09%09%09%09if (insn != null) %7B%0A%09%09
 %09%09%09%09int line
@@ -851,14 +851,8 @@
 insn
-Arr%5B0%5D
 .get
@@ -865,24 +865,26 @@
 Line();%0A%09%09%09%09
+%09
+%09
 if (line != 
@@ -884,24 +884,26 @@
 ine != 0) %7B%0A
+%09%09
 %09%09%09%09%09mth.set
@@ -924,16 +924,44 @@
 e - 1);%0A
+%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09break;%0A%09%09%09%09%09%7D%0A
 %09%09%09%09%7D%0A%09%09

fix debug info processing NPE