[clang] [Clang][Driver] Ensure clang can construct toolchain target names (PR #115798)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 11 17:28:43 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-driver
Author: Matthew Mirvish (mincrmatt12)
<details>
<summary>Changes</summary>
This adds an extra validation step to `ToolChain::getTargetAndModeFromProgramName` to actually try constructing the target info for the triple it deduces, instead of just relying on the LLVM target registry.
Some targets, like `xtensa`, are supported by LLVM but are not supported by clang, so just checking the LLVM registry causes inconsistency.
This is based on the discussion from clangd/clangd#<!-- -->2184 (and indeed would solve that issue) - the approach here of "construct the target info and check for null" is taken from [clangd](https://github.com/llvm/llvm-project/blob/5716f836d25e93bf8f664a14fe55c70e07a369be/clang-tools-extra/clangd/SystemIncludeExtractor.cpp#L253).
---
Full diff: https://github.com/llvm/llvm-project/pull/115798.diff
1 Files Affected:
- (modified) clang/lib/Driver/ToolChain.cpp (+15)
``````````diff
diff --git a/clang/lib/Driver/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp
index 646dbc0faf5a9f..e094a5fd0267f3 100644
--- a/clang/lib/Driver/ToolChain.cpp
+++ b/clang/lib/Driver/ToolChain.cpp
@@ -16,6 +16,8 @@
#include "ToolChains/InterfaceStubs.h"
#include "clang/Basic/ObjCRuntime.h"
#include "clang/Basic/Sanitizers.h"
+#include "clang/Basic/TargetInfo.h"
+#include "clang/Basic/TargetOptions.h"
#include "clang/Config/config.h"
#include "clang/Driver/Action.h"
#include "clang/Driver/Driver.h"
@@ -496,8 +498,21 @@ ToolChain::getTargetAndModeFromProgramName(StringRef PN) {
StringRef Prefix(ProgName);
Prefix = Prefix.slice(0, LastComponent);
std::string IgnoredError;
+
+ // First, check if the target is a supported LLVM target.
bool IsRegistered =
llvm::TargetRegistry::lookupTarget(std::string(Prefix), IgnoredError);
+ // If so, further check that clang is able to use it as a target.
+ if (IsRegistered) {
+ std::shared_ptr<TargetOptions> TargetOpts(new TargetOptions);
+ TargetOpts->Triple = Prefix.str();
+ DiagnosticsEngine Diags(new DiagnosticIDs, new DiagnosticOptions,
+ new IgnoringDiagConsumer);
+ llvm::IntrusiveRefCntPtr<TargetInfo> Target =
+ clang::TargetInfo::CreateTargetInfo(Diags, TargetOpts);
+ if (!Target)
+ IsRegistered = false;
+ }
return ParsedClangName{std::string(Prefix), ModeSuffix, DS->ModeFlag,
IsRegistered};
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/115798
More information about the cfe-commits
mailing list