@@ -35,7 +35,16 @@ isCXXSrcFile(
3535{
3636 StringRef ext = llvm::sys::path::extension (filename).drop_front ();
3737 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" ;
3948}
4049
4150template <typename ... Opts>
@@ -247,7 +256,8 @@ adjustCommandLine(
247256 StringRef const workingDir,
248257 std::vector<std::string> const & cmdline,
249258 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)
251261{
252262 if (cmdline.empty ())
253263 {
@@ -359,12 +369,40 @@ adjustCommandLine(
359369 // ------------------------------------------------------
360370 // Language standard
361371 // ------------------------------------------------------
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+
362393 constexpr auto is_std_option = [](std::string_view const opt) {
363394 return opt.starts_with (" -std=" ) || opt.starts_with (" --std=" ) || opt.starts_with (" /std:" );
364395 };
365396 if (std::ranges::find_if (cmdline, is_std_option) == cmdline.end ())
366397 {
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+ }
368406 }
369407
370408 // ------------------------------------------------------
@@ -510,10 +548,11 @@ MrDocsCompilationDatabase(
510548 workingDir,
511549 cmd0.CommandLine ,
512550 config,
513- implicitIncludeDirectories);
551+ implicitIncludeDirectories,
552+ cmd0.Filename );
514553 cmd.Directory = makeAbsoluteAndNative (workingDir, cmd0.Directory );
515554 cmd.Filename = makeAbsoluteAndNative (workingDir, cmd0.Filename );
516- if (isCXXSrcFile (cmd.Filename ))
555+ if (isCXXSrcFile (cmd.Filename ) || isCSrcFile (cmd. Filename ) )
517556 {
518557 const bool emplaced = IndexByFile_.try_emplace (cmd.Filename , AllCommands_.size ()).second ;
519558 if (emplaced)
0 commit comments