Index: include/clang/Basic/Diagnostic.h =================================================================== --- include/clang/Basic/Diagnostic.h +++ include/clang/Basic/Diagnostic.h @@ -390,6 +390,9 @@ /// and '='. std::string FlagValue; + /// \brief Command line options user entered. + std::string CmdLineOpts; + public: explicit DiagnosticsEngine(IntrusiveRefCntPtr Diags, DiagnosticOptions *DiagOpts, @@ -766,6 +769,11 @@ /// \brief Return the value associated with this diagnostic flag. StringRef getFlagValue() const { return FlagValue; } + /// \brief Set and get command line options as a string. + void setCmdLineOpts(std::string &opts) { CmdLineOpts = opts; } + + std::string getCmdLineOpts() const { return CmdLineOpts; } + private: /// \brief Report the delayed diagnostic. void ReportDelayed(); Index: lib/CodeGen/CGDebugInfo.cpp =================================================================== --- lib/CodeGen/CGDebugInfo.cpp +++ lib/CodeGen/CGDebugInfo.cpp @@ -504,6 +504,7 @@ } std::string Producer = getClangFullVersion(); + Producer += CGM.getDiags().getCmdLineOpts(); // Figure out which version of the ObjC runtime we have. unsigned RuntimeVers = 0; Index: lib/Driver/Tools.cpp =================================================================== --- lib/Driver/Tools.cpp +++ lib/Driver/Tools.cpp @@ -5391,6 +5391,11 @@ CmdArgs.push_back("-fwhole-program-vtables"); } + if (D.getDiags().getCmdLineOpts() != "") { + CmdArgs.push_back("-record-cmd-opts"); + CmdArgs.push_back(D.getDiags().getCmdLineOpts().c_str()); + } + // Finally add the compile command to the compilation. if (Args.hasArg(options::OPT__SLASH_fallback) && Output.getType() == types::TY_Object && Index: tools/driver/cc1_main.cpp =================================================================== --- tools/driver/cc1_main.cpp +++ tools/driver/cc1_main.cpp @@ -168,6 +168,16 @@ #endif int cc1_main(ArrayRef Argv, const char *Argv0, void *MainAddr) { + // Pop out -record-cmd-opts from Argv. + // If the Argv is generated from driver, it will contain command line options + // as the last two arguments, record and remove them from current Argv. + std::string CmdLineOpts = ""; + if (Argv.size() > 2 && + strcmp(Argv[Argv.size() - 2], "-record-cmd-opts") == 0) { + CmdLineOpts = Argv[Argv.size() - 1]; + Argv = Argv.drop_back(2); + } + ensureSufficientStack(); std::unique_ptr Clang(new CompilerInstance()); @@ -217,6 +227,11 @@ if (!Success) return 1; + // Set CmdLineOpts in Diagnostics Enginer, so that it can be used by + // DW_AT_producer in CGDebugInfo. + if (CmdLineOpts != "") + Clang->getDiagnostics().setCmdLineOpts(CmdLineOpts); + // Execute the frontend actions. Success = ExecuteCompilerInvocation(Clang.get()); Index: tools/driver/driver.cpp =================================================================== --- tools/driver/driver.cpp +++ tools/driver/driver.cpp @@ -445,6 +445,13 @@ ProcessWarningOptions(Diags, *DiagOpts, /*ReportDiags=*/false); + // Set command line options before building the Driver. + std::string CmdLineOpts = ""; + for (const auto arg : argv) { + CmdLineOpts += " " + std::string(arg); + } + Diags.setCmdLineOpts(CmdLineOpts); + Driver TheDriver(Path, llvm::sys::getDefaultTargetTriple(), Diags); SetInstallDir(argv, TheDriver, CanonicalPrefixes);