@@ -35,7 +35,16 @@ isCXXSrcFile(
35
35
{
36
36
StringRef ext = llvm::sys::path::extension (filename).drop_front ();
37
37
driver::types::ID extensionId = driver::types::lookupTypeForExtension (ext);
38
- return driver::types::isCXX (extensionId) || ext == " c" ;
38
+ return driver::types::isCXX (extensionId);
39
+ }
40
+
41
+ static
42
+ bool
43
+ isCSrcFile (
44
+ std::string_view filename)
45
+ {
46
+ StringRef ext = llvm::sys::path::extension (filename).drop_front ();
47
+ return ext == " c" ;
39
48
}
40
49
41
50
template <typename ... Opts>
@@ -247,7 +256,8 @@ adjustCommandLine(
247
256
StringRef const workingDir,
248
257
std::vector<std::string> const & cmdline,
249
258
std::shared_ptr<Config const > const & config,
250
- std::unordered_map<std::string, std::vector<std::string>> const & implicitIncludeDirectories)
259
+ std::unordered_map<std::string, std::vector<std::string>> const & implicitIncludeDirectories,
260
+ std::string_view filename)
251
261
{
252
262
if (cmdline.empty ())
253
263
{
@@ -359,12 +369,40 @@ adjustCommandLine(
359
369
// ------------------------------------------------------
360
370
// Language standard
361
371
// ------------------------------------------------------
372
+ // If cmdline contains `-x c` or `-x c++`, then the
373
+ // language is explicitly set.
374
+ bool isExplicitCppCompileCommand = false ;
375
+ bool isExplicitCCompileCommand = false ;
376
+ constexpr auto is_x_option = [](std::string_view const opt) {
377
+ return opt == " -x" || opt == " --language" ;
378
+ };
379
+ if (auto const it = std::ranges::find_if (cmdline, is_x_option);
380
+ it != cmdline.end ())
381
+ {
382
+ if (auto const next = std::next (it);
383
+ next != cmdline.end ())
384
+ {
385
+ isExplicitCppCompileCommand = *next == " c++" ;
386
+ isExplicitCCompileCommand = *next == " c" ;
387
+ }
388
+ }
389
+ bool const isImplicitCSourceFile = isCSrcFile (filename);
390
+ bool const isCCompileCommand =
391
+ isExplicitCCompileCommand || (!isExplicitCppCompileCommand && isImplicitCSourceFile);
392
+
362
393
constexpr auto is_std_option = [](std::string_view const opt) {
363
394
return opt.starts_with (" -std=" ) || opt.starts_with (" --std=" ) || opt.starts_with (" /std:" );
364
395
};
365
396
if (std::ranges::find_if (cmdline, is_std_option) == cmdline.end ())
366
397
{
367
- new_cmdline.emplace_back (" -std=c++23" );
398
+ if (!isCCompileCommand)
399
+ {
400
+ new_cmdline.emplace_back (" -std=c++23" );
401
+ }
402
+ else
403
+ {
404
+ new_cmdline.emplace_back (" -std=c23" );
405
+ }
368
406
}
369
407
370
408
// ------------------------------------------------------
@@ -510,10 +548,11 @@ MrDocsCompilationDatabase(
510
548
workingDir,
511
549
cmd0.CommandLine ,
512
550
config,
513
- implicitIncludeDirectories);
551
+ implicitIncludeDirectories,
552
+ cmd0.Filename );
514
553
cmd.Directory = makeAbsoluteAndNative (workingDir, cmd0.Directory );
515
554
cmd.Filename = makeAbsoluteAndNative (workingDir, cmd0.Filename );
516
- if (isCXXSrcFile (cmd.Filename ))
555
+ if (isCXXSrcFile (cmd.Filename ) || isCSrcFile (cmd. Filename ) )
517
556
{
518
557
const bool emplaced = IndexByFile_.try_emplace (cmd.Filename , AllCommands_.size ()).second ;
519
558
if (emplaced)
0 commit comments