Skip to content

Commit 4034d79

Browse files
committed
feat: 转化cpp编译错误
1 parent 72b3da8 commit 4034d79

File tree

1 file changed

+51
-3
lines changed
  • packages/uni-uts-v1/src/stacktrace

1 file changed

+51
-3
lines changed
Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,62 @@
11
import type { CompileStacktraceOptions } from './utils'
2+
import {
3+
resolveSourceMapDirByCacheDir,
4+
resolveSourceMapFileBySourceFile,
5+
} from './utils'
6+
import { originalPositionFor } from '../sourceMap'
27

38
export interface ParseCppStacktraceOptions extends CompileStacktraceOptions {
49
platform: 'app-harmony' | 'app-ios' | 'app-android'
510
language: 'cpp'
611
stacktrace: string
7-
sourceRoot: string
8-
sourceMapFile: string
12+
cacheDir: string
913
}
1014

15+
// TODO 不同平台cpp堆栈是否一致
16+
export const CPP_RUNTIME_ERROR_RE =
17+
/src\/main\/cpp(.*\.cpp):(\d+):(\d+):\serror:\s(.*)/
18+
19+
/**
20+
* cpp暂时只处理编译错误
21+
* cpp编译错误信息示例:
22+
* /xxx/unpackage/dist/dev/app-harmony/entry/src/main/cpp/GenPagesIndexIndexSharedData.cpp:17:15: error: no member named 'backgroundColor123' in 'uniappx::NativeViewImpl'
23+
* e0->backgroundColor123(0xffff0000);
24+
* ~~ ^
25+
* 1 error generated.
26+
*/
1127
export async function parseCppStacktrace(
1228
stacktrace: string,
1329
options: Omit<ParseCppStacktraceOptions, 'language'>
14-
) {}
30+
): Promise<string> {
31+
const matched = stacktrace.match(CPP_RUNTIME_ERROR_RE)
32+
if (!matched) {
33+
return stacktrace
34+
}
35+
36+
const sourceMapDir = resolveSourceMapDirByCacheDir(options.cacheDir)
37+
const [, filePath, line, column, message] = matched
38+
const lines: string[] = []
39+
lines.push(message)
40+
const sourceMapFile = resolveSourceMapFileBySourceFile(
41+
'cpp/' + filePath,
42+
sourceMapDir
43+
)
44+
if (!sourceMapFile) {
45+
return stacktrace
46+
}
47+
const originalPosition = await originalPositionFor({
48+
sourceMapFile,
49+
line: parseInt(line),
50+
column: parseInt(column),
51+
withSourceContent: true,
52+
})
53+
if (!originalPosition.source) {
54+
return stacktrace
55+
}
56+
lines.push(
57+
`at ${originalPosition.source.split('?')[0]}:${originalPosition.line}:${
58+
originalPosition.column
59+
}`
60+
)
61+
return lines.join('\n')
62+
}

0 commit comments

Comments
 (0)