@@ -30,17 +30,15 @@ void CoverageInstru::innerAnalysis(BasicBlockAnalysis &basicBlockAnalysis) const
30
30
31
31
InstrumentationResponse CoverageInstru::instrument () const noexcept {
32
32
if (config->fileName .empty () || config->reportFunction .empty () || config->sourceMap .empty () ||
33
- config->targetName .empty () || config->expectInfoOutputFilePath .empty () ||
34
- config-> debugInfoOutputFilePath . empty () ) {
33
+ config->targetName .empty () || config->expectInfoOutputFilePath .empty ()
34
+ ) {
35
35
std::cout << *config << std::endl;
36
36
return InstrumentationResponse::CONFIG_ERROR; // config error
37
37
}
38
38
std::filesystem::path filePath (config->fileName );
39
39
std::filesystem::path targetFilePath (config->targetName );
40
- std::filesystem::path debugInfoPath (config->debugInfoOutputFilePath );
41
40
std::filesystem::path sourceMapPath (config->sourceMap );
42
41
if ((!std::filesystem::exists (filePath)) ||
43
- (!std::filesystem::exists (debugInfoPath.parent_path ())) ||
44
42
(!std::filesystem::exists (sourceMapPath)) ||
45
43
(!std::filesystem::exists (targetFilePath.parent_path ()))) {
46
44
std::cout << *config << std::endl;
@@ -49,69 +47,82 @@ InstrumentationResponse CoverageInstru::instrument() const noexcept {
49
47
50
48
wasm::Module module ;
51
49
wasm::ModuleReader reader;
52
-
50
+ Json::StreamWriterBuilder jsonBuilder;
51
+ jsonBuilder[" indentation" ] = " " ;
53
52
reader.read (std::string (config->fileName ), module , std::string (config->sourceMap ));
54
53
BasicBlockAnalysis basicBlockAnalysis = BasicBlockAnalysis ();
55
54
innerAnalysis (basicBlockAnalysis);
56
- BasicBlockWalker basicBlockWalker = BasicBlockWalker (&module , basicBlockAnalysis);
57
- basicBlockWalker.basicBlockWalk ();
58
- const std::unordered_map<std::string_view, FunctionAnalysisResult> &results =
59
- basicBlockWalker.getResults ();
60
- Json::Value json;
61
- Json::Value debugInfoJson;
62
- Json::Value debugFileJson;
63
- for (auto &[function, result] : results) {
64
- Json::Value innerJson;
65
- innerJson[" index" ] = result.functionIndex ;
66
- Json::Value branchInfoArray (Json::ValueType::arrayValue);
67
- for (const auto &branchInfo : result.branchInfo ) {
68
- Json::Value inner_array;
69
- inner_array.append (branchInfo.first );
70
- inner_array.append (branchInfo.second );
71
- branchInfoArray.append (std::move (inner_array));
55
+
56
+ if (config->collectCoverage ) {
57
+ if (config->debugInfoOutputFilePath .empty ()) {
58
+ std::cout << *config << std::endl;
59
+ return InstrumentationResponse::CONFIG_ERROR; // config error
60
+ }
61
+ std::filesystem::path debugInfoPath (config->debugInfoOutputFilePath );
62
+ if ((!std::filesystem::exists (debugInfoPath.parent_path ()))) {
63
+ std::cout << *config << std::endl;
64
+ return InstrumentationResponse::CONFIG_FILEPATH_ERROR; // config file path error
72
65
}
73
- innerJson[" branchInfo" ] = branchInfoArray;
74
- Json::Value debugLineJson;
75
- for (const auto &basicBlock : result.basicBlocks ) {
76
- if (basicBlock.basicBlockIndex != static_cast <wasm::Index>(-1 )) {
77
- Json::Value debugLineItemJsonArray (Json::ValueType::arrayValue);
78
- for (const auto &debugLine : basicBlock.debugLocations ) {
79
- Json::Value debugInfo;
80
- debugInfo.append (debugLine.fileIndex );
81
- debugInfo.append (debugLine.lineNumber );
82
- debugInfo.append (debugLine.columnNumber );
83
- debugLineItemJsonArray.append (std::move (debugInfo));
66
+
67
+ BasicBlockWalker basicBlockWalker = BasicBlockWalker (&module , basicBlockAnalysis);
68
+ basicBlockWalker.basicBlockWalk ();
69
+ const std::unordered_map<std::string_view, FunctionAnalysisResult> &results =
70
+ basicBlockWalker.getResults ();
71
+ Json::Value json;
72
+ Json::Value debugInfoJson;
73
+ Json::Value debugFileJson;
74
+ for (auto &[function, result] : results) {
75
+ Json::Value innerJson;
76
+ innerJson[" index" ] = result.functionIndex ;
77
+ Json::Value branchInfoArray (Json::ValueType::arrayValue);
78
+ for (const auto &branchInfo : result.branchInfo ) {
79
+ Json::Value inner_array;
80
+ inner_array.append (branchInfo.first );
81
+ inner_array.append (branchInfo.second );
82
+ branchInfoArray.append (std::move (inner_array));
83
+ }
84
+ innerJson[" branchInfo" ] = branchInfoArray;
85
+ Json::Value debugLineJson;
86
+ for (const auto &basicBlock : result.basicBlocks ) {
87
+ if (basicBlock.basicBlockIndex != static_cast <wasm::Index>(-1 )) {
88
+ Json::Value debugLineItemJsonArray (Json::ValueType::arrayValue);
89
+ for (const auto &debugLine : basicBlock.debugLocations ) {
90
+ Json::Value debugInfo;
91
+ debugInfo.append (debugLine.fileIndex );
92
+ debugInfo.append (debugLine.lineNumber );
93
+ debugInfo.append (debugLine.columnNumber );
94
+ debugLineItemJsonArray.append (std::move (debugInfo));
95
+ }
96
+ debugLineJson[basicBlock.basicBlockIndex ] = debugLineItemJsonArray;
84
97
}
85
- debugLineJson[basicBlock.basicBlockIndex ] = debugLineItemJsonArray;
86
98
}
99
+ innerJson[" lineInfo" ] = debugLineJson;
100
+ debugInfoJson[function.data ()] = innerJson;
87
101
}
88
- innerJson[" lineInfo" ] = debugLineJson;
89
- debugInfoJson[function.data ()] = innerJson;
90
- }
91
- for (const std::string &debugInfoFileName : module .debugInfoFileNames ) {
92
- debugFileJson.append (debugInfoFileName);
93
- }
94
- json[" debugInfos" ] = debugInfoJson;
95
- json[" debugFiles" ] = debugFileJson;
96
- std::ofstream jsonWriteStream (config->debugInfoOutputFilePath .data (), std::ios::trunc);
97
- Json::StreamWriterBuilder jsonBuilder;
98
- jsonBuilder[" indentation" ] = " " ;
99
- std::unique_ptr<Json::StreamWriter> jsonWriter (jsonBuilder.newStreamWriter ());
100
- if (jsonWriter->write (json, &jsonWriteStream) != 0 ) {
101
- // Hard to control IO error
102
- // LCOV_EXCL_START
103
- return InstrumentationResponse::DEBUG_INFO_GENERATION_ERROR; // debug info json write failed
104
- // LCOV_EXCL_STOP
105
- }
106
- jsonWriteStream.close ();
107
- if (jsonWriteStream.fail () || jsonWriteStream.bad ()) {
108
- // Hard to control IO error
109
- // LCOV_EXCL_START
110
- return InstrumentationResponse::DEBUG_INFO_GENERATION_ERROR; // debug info json write failed
111
- // LCOV_EXCL_STOP
102
+ for (const std::string &debugInfoFileName : module .debugInfoFileNames ) {
103
+ debugFileJson.append (debugInfoFileName);
104
+ }
105
+ json[" debugInfos" ] = debugInfoJson;
106
+ json[" debugFiles" ] = debugFileJson;
107
+ std::ofstream jsonWriteStream (config->debugInfoOutputFilePath .data (), std::ios::trunc);
108
+
109
+ std::unique_ptr<Json::StreamWriter> jsonWriter (jsonBuilder.newStreamWriter ());
110
+ if (jsonWriter->write (json, &jsonWriteStream) != 0 ) {
111
+ // Hard to control IO error
112
+ // LCOV_EXCL_START
113
+ return InstrumentationResponse::DEBUG_INFO_GENERATION_ERROR; // debug info json write failed
114
+ // LCOV_EXCL_STOP
115
+ }
116
+ jsonWriteStream.close ();
117
+ if (jsonWriteStream.fail () || jsonWriteStream.bad ()) {
118
+ // Hard to control IO error
119
+ // LCOV_EXCL_START
120
+ return InstrumentationResponse::DEBUG_INFO_GENERATION_ERROR; // debug info json write failed
121
+ // LCOV_EXCL_STOP
122
+ }
123
+ CovInstrumentationWalker covWalker (&module , config->reportFunction .data (), basicBlockWalker);
124
+ covWalker.covWalk ();
112
125
}
113
- CovInstrumentationWalker covWalker (&module , config->reportFunction .data (), basicBlockWalker);
114
- covWalker.covWalk ();
115
126
116
127
MockInstrumentationWalker mockWalker (&module );
117
128
mockWalker.mockWalk ();
@@ -167,7 +178,7 @@ wasm_instrument(char const *const fileName, char const *const targetName,
167
178
char const *const reportFunction, char const *const sourceMap,
168
179
char const *const expectInfoOutputFilePath,
169
180
char const *const debugInfoOutputFilePath, char const *const includes,
170
- char const *const excludes, bool skipLib) noexcept {
181
+ char const *const excludes, bool skipLib, bool collectCoverage ) noexcept {
171
182
172
183
wasmInstrumentation::InstrumentationConfig config;
173
184
config.fileName = fileName;
@@ -179,6 +190,7 @@ wasm_instrument(char const *const fileName, char const *const targetName,
179
190
config.includes = includes;
180
191
config.excludes = excludes;
181
192
config.skipLib = skipLib;
193
+ config.collectCoverage = collectCoverage;
182
194
wasmInstrumentation::CoverageInstru instrumentor (&config);
183
195
return instrumentor.instrument ();
184
196
}
0 commit comments