ProtoLog: Fix broken hash computation

Fixes a bug caused by inconsistent paths - when transforming sources, we took the
path of the file, while when generating the viewer json, we used the path relative
to the root package.

Test: enable protolog, verify winscope can parse it.
Change-Id: Ib636515d70ca5b66da687542a84107465e51053d
This commit is contained in:
Adrian Roos
2019-10-30 18:12:46 +01:00
parent 07e10e3a96
commit 6599f2dea4
3 changed files with 27 additions and 14 deletions

View File

@@ -60,7 +60,7 @@ object ProtoLogTool {
val outSrc = try {
val code = tryParse(text, path)
if (containsProtoLogText(text, command.protoLogClassNameArg)) {
transformer.processClass(text, path, code)
transformer.processClass(text, path, packagePath(file, code), code)
} else {
text
}
@@ -157,10 +157,7 @@ ${updates.replaceIndent(" ")}
if (containsProtoLogText(text, command.protoLogClassNameArg)) {
try {
val code = tryParse(text, path)
val pack = if (code.packageDeclaration.isPresent) code.packageDeclaration
.get().nameAsString else ""
val newPath = pack.replace('.', '/') + '/' + file.name
builder.findLogCalls(code, newPath)
builder.findLogCalls(code, path, packagePath(file, code))
} catch (ex: ParsingException) {
// If we cannot parse this file, skip it (and log why). Compilation will fail
// in a subsequent build step.
@@ -182,6 +179,13 @@ ${updates.replaceIndent(" ")}
out.close()
}
private fun packagePath(file: File, code: CompilationUnit): String {
val pack = if (code.packageDeclaration.isPresent) code.packageDeclaration
.get().nameAsString else ""
val packagePath = pack.replace('.', '/') + '/' + file.name
return packagePath
}
private fun read(command: CommandOptions) {
LogParser(ViewerConfigParser())
.parse(FileInputStream(command.logProtofileArg),

View File

@@ -72,7 +72,7 @@ class SourceTransformer(
}
val ifStmt: IfStmt
if (group.enabled) {
val hash = CodeUtils.hash(fileName, messageString, level, group)
val hash = CodeUtils.hash(packagePath, messageString, level, group)
val newCall = call.clone()
if (!group.textEnabled) {
// Remove message string if text logging is not enabled by default.
@@ -97,7 +97,7 @@ class SourceTransformer(
if (argTypes.size != call.arguments.size - 2) {
throw InvalidProtoLogCallException(
"Number of arguments (${argTypes.size} does not mach format" +
" string in: $call", ParsingContext(fileName, call))
" string in: $call", ParsingContext(path, call))
}
val blockStmt = BlockStmt()
if (argTypes.isNotEmpty()) {
@@ -214,18 +214,23 @@ class SourceTransformer(
StaticJavaParser.parseExpression<FieldAccessExpr>(protoLogCacheClassName)
private var processedCode: MutableList<String> = mutableListOf()
private var offsets: IntArray = IntArray(0)
private var fileName: String = ""
/** The path of the file being processed, relative to $ANDROID_BUILD_TOP */
private var path: String = ""
/** The path of the file being processed, relative to the root package */
private var packagePath: String = ""
fun processClass(
code: String,
path: String,
packagePath: String,
compilationUnit: CompilationUnit =
StaticJavaParser.parse(code)
): String {
fileName = path
this.path = path
this.packagePath = packagePath
processedCode = code.split('\n').toMutableList()
offsets = IntArray(processedCode.size)
protoLogCallProcessor.process(compilationUnit, this, fileName)
protoLogCallProcessor.process(compilationUnit, this, path)
return processedCode.joinToString("\n")
}
}

View File

@@ -46,7 +46,11 @@ class ViewerConfigBuilder(
private val statements: MutableMap<Int, LogCall> = mutableMapOf()
private val groups: MutableSet<LogGroup> = mutableSetOf()
fun findLogCalls(unit: CompilationUnit, fileName: String): List<Pair<LogCall, ParsingContext>> {
fun findLogCalls(
unit: CompilationUnit,
path: String,
packagePath: String
): List<Pair<LogCall, ParsingContext>> {
val calls = mutableListOf<Pair<LogCall, ParsingContext>>()
val visitor = object : ProtoLogCallVisitor {
override fun processCall(
@@ -55,12 +59,12 @@ class ViewerConfigBuilder(
level: LogLevel,
group: LogGroup
) {
val logCall = LogCall(messageString, level, group, fileName)
val context = ParsingContext(fileName, call)
val logCall = LogCall(messageString, level, group, packagePath)
val context = ParsingContext(path, call)
calls.add(logCall to context)
}
}
processor.process(unit, visitor, fileName)
processor.process(unit, visitor, path)
return calls
}