@@ -195,49 +195,34 @@ parseCmakeArgs(std::string const& cmakeArgsStr) {
195
195
return args;
196
196
}
197
197
198
- } // anonymous namespace
199
-
200
- Expected<std::string>
201
- executeCmakeExportCompileCommands (llvm::StringRef projectPath, llvm::StringRef cmakeArgs)
202
- {
203
- MRDOCS_CHECK (llvm::sys::fs::exists (projectPath), " Project path does not exist" );
204
- MRDOCS_TRY (auto const cmakePath, getCmakePath ());
205
-
206
- llvm::SmallString<128 > tempDir;
207
- MRDOCS_CHECK (!llvm::sys::fs::createUniqueDirectory (" compile_commands" , tempDir), " Failed to create temporary directory" );
208
-
209
- llvm::SmallString<128 > errorPath;
210
- MRDOCS_CHECK (!llvm::sys::fs::createTemporaryFile (" cmake-error" , " txt" , errorPath),
211
- " Failed to create temporary file" );
212
-
213
- std::optional<llvm::StringRef> const redirects[] = {llvm::StringRef (), llvm::StringRef (), errorPath.str ()};
214
- std::vector<llvm::StringRef> args = {cmakePath, " -S" , projectPath, " -B" , tempDir.str (), " -DCMAKE_EXPORT_COMPILE_COMMANDS=ON" };
215
-
216
- auto const additionalArgs = parseCmakeArgs (cmakeArgs.str ());
217
-
198
+ Expected<void >
199
+ pushCMakeArgs (
200
+ std::string const & cmakePath,
201
+ std::vector<llvm::StringRef> &args,
202
+ std::vector<std::string> const & additionalArgs) {
218
203
bool visualStudioFound = false ;
219
- for (size_t i = 0 ; i < additionalArgs.size (); ++i)
204
+ for (size_t i = 0 ; i < additionalArgs.size (); ++i)
220
205
{
221
206
auto const & arg = additionalArgs[i];
222
207
if (arg.starts_with (" -G" ))
223
208
{
224
- if (arg.size () == 2 )
209
+ if (arg.size () == 2 )
225
210
{
226
- if (i + 1 < additionalArgs.size ())
211
+ if (i + 1 < additionalArgs.size ())
227
212
{
228
213
auto const & generatorName = additionalArgs[i + 1 ];
229
- if (generatorName.starts_with (" Visual Studio" ))
214
+ if (generatorName.starts_with (" Visual Studio" ))
230
215
{
231
- args.push_back (" -GNinja" );
216
+ args.emplace_back (" -GNinja" );
232
217
visualStudioFound = true ;
233
218
++i;
234
219
continue ;
235
220
}
236
221
}
237
222
} else {
238
- if (arg.find (" Visual Studio" , 2 ) != std::string::npos)
223
+ if (arg.find (" Visual Studio" , 2 ) != std::string::npos)
239
224
{
240
- args.push_back (" -GNinja" );
225
+ args.emplace_back (" -GNinja" );
241
226
visualStudioFound = true ;
242
227
continue ;
243
228
}
@@ -246,39 +231,63 @@ executeCmakeExportCompileCommands(llvm::StringRef projectPath, llvm::StringRef c
246
231
247
232
if (arg.starts_with (" -D" ))
248
233
{
249
- if (arg.size () == 2 )
234
+ if (arg.size () == 2 )
250
235
{
251
- if (i + 1 < additionalArgs.size ())
236
+ if (i + 1 < additionalArgs.size ())
252
237
{
253
238
auto const & optionName = additionalArgs[i + 1 ];
254
- if (optionName.starts_with (" CMAKE_EXPORT_COMPILE_COMMANDS" ))
239
+ if (optionName.starts_with (" CMAKE_EXPORT_COMPILE_COMMANDS" ))
255
240
{
256
241
++i;
257
242
continue ;
258
243
}
259
244
}
260
245
} else {
261
- if (arg.find (" CMAKE_EXPORT_COMPILE_COMMANDS" , 2 ) != std::string::npos)
246
+ if (arg.find (" CMAKE_EXPORT_COMPILE_COMMANDS" , 2 ) != std::string::npos)
262
247
{
263
248
continue ;
264
249
}
265
250
}
266
- }
267
- args.push_back (arg);
251
+ }
252
+ args.emplace_back (arg);
268
253
}
269
-
270
- if ( ! visualStudioFound)
254
+
255
+ if (! visualStudioFound)
271
256
{
272
- MRDOCS_TRY (auto const cmakeDefaultGeneratorIsVisualStudio, cmakeDefaultGeneratorIsVisualStudio (cmakePath));
273
- if (cmakeDefaultGeneratorIsVisualStudio)
257
+ MRDOCS_TRY (
258
+ bool const cmakeDefaultGeneratorIsVisualStudio,
259
+ cmakeDefaultGeneratorIsVisualStudio (cmakePath));
260
+ if (cmakeDefaultGeneratorIsVisualStudio)
274
261
{
275
- args.push_back (" -GNinja" );
262
+ args.emplace_back (" -GNinja" );
276
263
}
277
264
}
265
+ return {};
266
+ }
267
+
268
+ } // anonymous namespace
269
+
270
+ Expected<std::string>
271
+ executeCmakeExportCompileCommands (llvm::StringRef projectPath, llvm::StringRef cmakeArgs)
272
+ {
273
+ MRDOCS_CHECK (llvm::sys::fs::exists (projectPath), " Project path does not exist" );
274
+ MRDOCS_TRY (auto const cmakePath, getCmakePath ());
275
+
276
+ llvm::SmallString<128 > tempDir;
277
+ MRDOCS_CHECK (!llvm::sys::fs::createUniqueDirectory (" compile_commands" , tempDir), " Failed to create temporary directory" );
278
+
279
+ ScopedTempFile const errorPath (" cmake-error" , " txt" );
280
+ MRDOCS_CHECK (errorPath, " Failed to create temporary file" );
281
+
282
+ std::optional<llvm::StringRef> const redirects[] = {llvm::StringRef (), llvm::StringRef (), errorPath.path ()};
283
+ std::vector<llvm::StringRef> args = {cmakePath, " -S" , projectPath, " -B" , tempDir.str (), " -DCMAKE_EXPORT_COMPILE_COMMANDS=ON" };
284
+
285
+ auto const additionalArgs = parseCmakeArgs (cmakeArgs.str ());
286
+ MRDOCS_TRY (pushCMakeArgs (cmakePath, args, additionalArgs));
278
287
279
288
int const result = llvm::sys::ExecuteAndWait (cmakePath, args, std::nullopt, redirects);
280
289
if (result != 0 ) {
281
- auto bufferOrError = llvm::MemoryBuffer::getFile (errorPath);
290
+ auto bufferOrError = llvm::MemoryBuffer::getFile (errorPath. path () );
282
291
MRDOCS_CHECK (bufferOrError, " CMake execution failed (no error output available)" );
283
292
return Unexpected (Error (" CMake execution failed: \n " + bufferOrError.get ()->getBuffer ().str ()));
284
293
}
@@ -289,5 +298,6 @@ executeCmakeExportCompileCommands(llvm::StringRef projectPath, llvm::StringRef c
289
298
return compileCommandsPath.str ().str ();
290
299
}
291
300
301
+
292
302
} // mrdocs
293
303
} // clang
0 commit comments