@@ -78,36 +78,50 @@ find(
78
78
79
79
// ------------------------------------------------
80
80
81
+ namespace {
82
+ template <class Rep , class Period >
83
+ std::string
84
+ format_duration (
85
+ std::chrono::duration<Rep, Period> delta)
86
+ {
87
+ auto delta_ms = std::chrono::duration_cast<
88
+ std::chrono::milliseconds>(delta).count ();
89
+ if (delta_ms < 1000 )
90
+ {
91
+ return fmt::format (" {} ms" , delta_ms);
92
+ }
93
+ else
94
+ {
95
+ double const delta_s = static_cast <double >(delta_ms) / 1000.0 ;
96
+ return fmt::format (" {:.02f} s" , delta_s);
97
+ }
98
+ }
99
+ }
100
+
81
101
mrdocs::Expected<std::unique_ptr<Corpus>>
82
102
CorpusImpl::
83
103
build (
84
104
report::Level reportLevel,
85
- std::shared_ptr<ConfigImpl const > config,
105
+ std::shared_ptr<ConfigImpl const > const & config,
86
106
tooling::CompilationDatabase const & compilations)
87
107
{
88
108
using clock_type = std::chrono::steady_clock;
89
- const auto format_duration =
90
- [](clock_type::duration delta)
91
- {
92
- auto delta_ms = std::chrono::duration_cast<
93
- std::chrono::milliseconds>(delta).count ();
94
- if (delta_ms < 1000 )
95
- return fmt::format (" {} ms" , delta_ms);
96
- else
97
- return fmt::format (" {:.02f} s" ,
98
- delta_ms / 1000.0 );
99
- };
100
109
auto start_time = clock_type::now ();
101
110
102
- auto corpus = std::make_unique<CorpusImpl>(config);
103
-
104
- // Traverse the AST for all translation units
105
- // and emit serializd bitcode into tool results.
106
- // This operation happens ona thread pool.
107
- report::print (reportLevel, " Extracting declarations" );
111
+ // ------------------------------------------
112
+ // Create empty corpus
113
+ // ------------------------------------------
114
+ // The corpus will keep a reference to Config.
115
+ std::unique_ptr<CorpusImpl> corpus = std::make_unique<CorpusImpl>(config);
108
116
117
+ // ------------------------------------------
118
+ // Execution context
119
+ // ------------------------------------------
120
+ // Create an execution context to store the
121
+ // results of the AST traversal.
122
+ // Any new Info objects will be added to the
123
+ // InfoSet in the execution context.
109
124
#define USE_BITCODE
110
-
111
125
#ifdef USE_BITCODE
112
126
BitcodeExecutionContext context (*config);
113
127
#else
@@ -117,13 +131,9 @@ build(
117
131
makeFrontendActionFactory (context, *config);
118
132
MRDOCS_ASSERT (action);
119
133
120
- // Get a copy of the filename strings
121
- std::vector<std::string> files =
122
- compilations.getAllFiles ();
123
-
124
- if (files.empty ())
125
- return Unexpected (formatError (" Compilations database is empty" ));
126
-
134
+ // ------------------------------------------
135
+ // "Process file" task
136
+ // ------------------------------------------
127
137
auto const processFile =
128
138
[&](std::string path)
129
139
{
@@ -144,15 +154,27 @@ build(
144
154
formatError (" Failed to run action on {}" , path).Throw ();
145
155
};
146
156
147
- // Run the action on all files in the database
157
+ // ------------------------------------------
158
+ // Run the process file task on all files
159
+ // ------------------------------------------
160
+ // Traverse the AST for all translation units
161
+ // and emit serializd bitcode into tool results.
162
+ // This operation happens on a thread pool.
163
+ report::print (reportLevel, " Extracting declarations" );
164
+
165
+ // Get a copy of the filename strings
166
+ std::vector<std::string> files = compilations.getAllFiles ();
167
+ MRDOCS_CHECK (files, " Compilations database is empty" );
148
168
std::vector<Error> errors;
149
- if (files.size () == 1 )
169
+
170
+ // Run the action on all files in the database
171
+ if (files.size () == 1 )
150
172
{
151
173
try
152
174
{
153
175
processFile (std::move (files.front ()));
154
176
}
155
- catch (Exception const & ex)
177
+ catch (Exception const & ex)
156
178
{
157
179
errors.push_back (ex.error ());
158
180
}
@@ -169,16 +191,18 @@ build(
169
191
report::format (reportLevel,
170
192
" [{}/{}] \" {}\" " , idx, files.size (), path);
171
193
172
- processFile (std::move ( path) );
194
+ processFile (path);
173
195
});
174
196
}
175
197
errors = taskGroup.wait ();
176
198
}
177
-
178
- // Report warning and error totals
199
+ // Print diagnostics totals
179
200
context.reportEnd (reportLevel);
180
201
181
- if (! errors.empty ())
202
+ // ------------------------------------------
203
+ // Report warning and error totals
204
+ // ------------------------------------------
205
+ if (!errors.empty ())
182
206
{
183
207
Error err (errors);
184
208
if (! (*config)->ignoreFailures )
@@ -207,6 +231,7 @@ build(
207
231
" Reduced {} symbols in {}" ,
208
232
corpus->info_ .size (),
209
233
format_duration (clock_type::now () - start_time));
234
+ #undef USE_BITCODE
210
235
#else
211
236
auto results = context.results ();
212
237
if (! results)
@@ -219,9 +244,12 @@ build(
219
244
format_duration (clock_type::now () - start_time));
220
245
#endif
221
246
247
+ // ------------------------------------------
248
+ // Finalize corpus
249
+ // ------------------------------------------
222
250
auto lookup = std::make_unique<SymbolLookup>(*corpus);
223
-
224
251
finalize (corpus->info_ , *lookup);
252
+
225
253
return corpus;
226
254
}
227
255
0 commit comments