diff --git a/clang-tools-extra/clangd/Compiler.cpp b/clang-tools-extra/clangd/Compiler.cpp --- a/clang-tools-extra/clangd/Compiler.cpp +++ b/clang-tools-extra/clangd/Compiler.cpp @@ -123,6 +123,33 @@ if (!Clang->hasTarget()) return nullptr; + // Correctly set AuxTarget. The code is borrowed from + // `CompilerInstance::ExecuteAction(FrontendAction &)` inside + // "clang/lib/Frontend/CompilerInstance.cpp" + if ((Clang->getLangOpts().CUDA || Clang->getLangOpts().OpenMPIsDevice || + Clang->getLangOpts().SYCLIsDevice) && + !Clang->getFrontendOpts().AuxTriple.empty()) { + auto TO = std::make_shared(); + TO->Triple = llvm::Triple::normalize(Clang->getFrontendOpts().AuxTriple); + if (Clang->getFrontendOpts().AuxTargetCPU) + TO->CPU = Clang->getFrontendOpts().AuxTargetCPU.getValue(); + if (Clang->getFrontendOpts().AuxTargetFeatures) + TO->FeaturesAsWritten = + Clang->getFrontendOpts().AuxTargetFeatures.getValue(); + TO->HostTriple = Clang->getTarget().getTriple().str(); + Clang->setAuxTarget( + TargetInfo::CreateTargetInfo(Clang->getDiagnostics(), TO)); + } + + // Inform the target of the language options. + // + // FIXME: We shouldn't need to do this, the target should be immutable once + // created. This complexity should be lifted elsewhere. + Clang->getTarget().adjust(Clang->getLangOpts()); + + if (auto *Aux = Clang->getAuxTarget()) + Clang->getTarget().setAuxTarget(Aux); + // RemappedFileBuffers will handle the lifetime of the Buffer pointer, // release it. Buffer.release(); diff --git a/clang/lib/Frontend/PrecompiledPreamble.cpp b/clang/lib/Frontend/PrecompiledPreamble.cpp --- a/clang/lib/Frontend/PrecompiledPreamble.cpp +++ b/clang/lib/Frontend/PrecompiledPreamble.cpp @@ -370,12 +370,33 @@ if (!Clang->hasTarget()) return BuildPreambleError::CouldntCreateTargetInfo; + // Correctly set AuxTarget. The code is borrowed from + // `CompilerInstance::ExecuteAction(FrontendAction &)` inside + // "clang/lib/Frontend/CompilerInstance.cpp" + if ((Clang->getLangOpts().CUDA || Clang->getLangOpts().OpenMPIsDevice || + Clang->getLangOpts().SYCLIsDevice) && + !Clang->getFrontendOpts().AuxTriple.empty()) { + auto TO = std::make_shared(); + TO->Triple = llvm::Triple::normalize(Clang->getFrontendOpts().AuxTriple); + if (Clang->getFrontendOpts().AuxTargetCPU) + TO->CPU = Clang->getFrontendOpts().AuxTargetCPU.getValue(); + if (Clang->getFrontendOpts().AuxTargetFeatures) + TO->FeaturesAsWritten = + Clang->getFrontendOpts().AuxTargetFeatures.getValue(); + TO->HostTriple = Clang->getTarget().getTriple().str(); + Clang->setAuxTarget( + TargetInfo::CreateTargetInfo(Clang->getDiagnostics(), TO)); + } + // Inform the target of the language options. // // FIXME: We shouldn't need to do this, the target should be immutable once // created. This complexity should be lifted elsewhere. Clang->getTarget().adjust(Clang->getLangOpts()); + if (auto *Aux = Clang->getAuxTarget()) + Clang->getTarget().setAuxTarget(Aux); + if (Clang->getFrontendOpts().Inputs.size() != 1 || Clang->getFrontendOpts().Inputs[0].getKind().getFormat() != InputKind::Source ||