diff --git a/src/lib/MrDocsCompilationDatabase.cpp b/src/lib/MrDocsCompilationDatabase.cpp index 17f189878..c97cbda10 100644 --- a/src/lib/MrDocsCompilationDatabase.cpp +++ b/src/lib/MrDocsCompilationDatabase.cpp @@ -201,10 +201,15 @@ isValidMrDocsOption( driver::options::OPT_clang_ignored_gcc_optimization_f_Group, driver::options::OPT_clang_ignored_legacy_options_Group, driver::options::OPT_clang_ignored_m_Group, - driver::options::OPT_flang_ignored_w_Group -#if 0 + driver::options::OPT_flang_ignored_w_Group, + driver::options::OPT__SLASH_O, + driver::options::OPT__SLASH_EH, + driver::options::OPT__SLASH_GR, + driver::options::OPT__SLASH_GR_, + driver::options::OPT__SLASH_M_Group, + // input file options - driver::options::OPT_INPUT, + //driver::options::OPT_INPUT, // output file options driver::options::OPT_o, @@ -219,11 +224,10 @@ isValidMrDocsOption( driver::options::OPT__SLASH_Fr, driver::options::OPT__SLASH_Fm, driver::options::OPT__SLASH_Fx, -#endif - // driver::options::OPT__SLASH_TP - // driver::options::OPT__SLASH_Tp - // driver::options::OPT__SLASH_TC - // driver::options::OPT__SLASH_Tc + driver::options::OPT__SLASH_TP, + driver::options::OPT__SLASH_Tp, + driver::options::OPT__SLASH_TC, + driver::options::OPT__SLASH_Tc )) { return false; @@ -270,7 +274,7 @@ adjustCommandLine( // Copy the compiler path // ------------------------------------------------------ std::string const& progName = cmdline.front(); - std::vector new_cmdline = {progName}; + std::vector new_cmdline = {std::string{"clang"}}; // ------------------------------------------------------ // Convert to InputArgList @@ -282,24 +286,12 @@ adjustCommandLine( cmdLineCStrs.data(), cmdLineCStrs.data() + cmdLineCStrs.size()); - // ------------------------------------------------------ - // Get driver mode - // ------------------------------------------------------ - // The driver mode distinguishes between clang/gcc and msvc - // command line option formats. The value is deduced from - // the `-drive-mode` option or from `progName`. - // Common values are "gcc", "g++", "cpp", "cl" and "flang". - StringRef const driver_mode = driver::getDriverMode(progName, cmdLineCStrs); - // Identify if we should use "msvc/clang-cl" or "clang/gcc" format - // for options. - bool const is_clang_cl = driver::IsClangCL(driver_mode); - // ------------------------------------------------------ // Supress all warnings // ------------------------------------------------------ // Add flags to ignore all warnings. Any options that // affect warnings will be discarded later. - new_cmdline.emplace_back(is_clang_cl ? "/w" : "-w"); + new_cmdline.emplace_back("-w"); new_cmdline.emplace_back("-fsyntax-only"); // ------------------------------------------------------ @@ -393,9 +385,10 @@ adjustCommandLine( isExplicitCCompileCommand || (!isExplicitCppCompileCommand && isImplicitCSourceFile); constexpr auto is_std_option = [](std::string_view const opt) { - return opt.starts_with("-std=") || opt.starts_with("--std=") || opt.starts_with("/std:"); + return opt.starts_with("-std=") || opt.starts_with("--std=") || + opt.starts_with("/std:") || opt.starts_with("-std:"); }; - if (std::ranges::find_if(cmdline, is_std_option) == cmdline.end()) + if (std::ranges::none_of(cmdline, is_std_option)) { if (!isCCompileCommand) { @@ -431,7 +424,7 @@ adjustCommandLine( for (auto const& inc : it->second) { new_cmdline.emplace_back(std::format("-isystem{}", inc)); - } + } } } @@ -474,6 +467,18 @@ adjustCommandLine( new_cmdline.emplace_back(std::format("-I{}", inc)); } + // ------------------------------------------------------ + // Get driver mode + // ------------------------------------------------------ + // The driver mode distinguishes between clang/gcc and msvc + // command line option formats. The value is deduced from + // the `-drive-mode` option or from `progName`. + // Common values are "gcc", "g++", "cpp", "cl" and "flang". + StringRef const driver_mode = driver::getDriverMode(progName, cmdLineCStrs); + // Identify if we should use "msvc/clang-cl" or "clang/gcc" format + // for options. + bool const is_clang_cl = driver::IsClangCL(driver_mode); + // ------------------------------------------------------ // Adjust each argument in the command line // ------------------------------------------------------ @@ -489,18 +494,40 @@ adjustCommandLine( while (idx < cmdline.size()) { // Parse one argument as a Clang option - // ParseOneArg updates Index to the next argument to be parsed. - unsigned const idx0 = idx; std::unique_ptr arg = opts_table.ParseOneArg(args, idx, visibility); if (!isValidMrDocsOption(workingDir, arg)) { continue; } - new_cmdline.insert( - new_cmdline.end(), - cmdline.begin() + idx0, - cmdline.begin() + idx); + + // Translate clang-cl /std: into clang -std= + const llvm::opt::Option opt = + arg->getOption().getUnaliasedOption(); + if (opt.matches(clang::driver::options::OPT__SLASH_std)) + { + MRDOCS_ASSERT(arg->getNumValues() == 1); + + std::string_view v = arg->getValue(); + if (v == "c++latest" || v == "c++23preview") + { + new_cmdline.emplace_back("-std=c++23"); + } + else // c++14,c++17,c++20,c11,c17 + { + new_cmdline.emplace_back(std::format("-std={}", v)); + } + continue; + } + + // Append the translated arguments to the new command line + llvm::opt::ArgStringList output; + arg->render(args, output); + + for (auto const& v : output) + { + new_cmdline.emplace_back(v); + } } return new_cmdline;