Skip to content

[Clang][Driver][NFC] Capture -foffload-lto Arg before dereferencing#181752

Open
srividya-sundaram wants to merge 1 commit intollvm:mainfrom
srividya-sundaram:capture-offloadlto
Open

[Clang][Driver][NFC] Capture -foffload-lto Arg before dereferencing#181752
srividya-sundaram wants to merge 1 commit intollvm:mainfrom
srividya-sundaram:capture-offloadlto

Conversation

@srividya-sundaram
Copy link
Contributor

@srividya-sundaram srividya-sundaram commented Feb 16, 2026

Capture the pointer returned by getLastArg first to ensure the argument exists before dereferencing.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Feb 16, 2026
@llvmbot
Copy link
Member

llvmbot commented Feb 16, 2026

@llvm/pr-subscribers-clang-driver

@llvm/pr-subscribers-clang

Author: Srividya Sundaram (srividya-sundaram)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/181752.diff

1 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/Clang.cpp (+6-10)
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index c16aa33f29ebb..efa8ab7d659f5 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -5233,23 +5233,19 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
       CmdArgs.push_back("-emit-llvm-uselists");
 
     if (IsUsingLTO) {
+      Arg *OffloadLTO = Args.getLastArg(options::OPT_foffload_lto,
+                                        options::OPT_foffload_lto_EQ);
       if (IsDeviceOffloadAction && !JA.isDeviceOffloading(Action::OFK_OpenMP) &&
           !Args.hasFlag(options::OPT_offload_new_driver,
                         options::OPT_no_offload_new_driver,
                         C.isOffloadingHostKind(Action::OFK_Cuda)) &&
-          !Triple.isAMDGPU()) {
+          !Triple.isAMDGPU() && OffloadLTO) {
         D.Diag(diag::err_drv_unsupported_opt_for_target)
-            << Args.getLastArg(options::OPT_foffload_lto,
-                               options::OPT_foffload_lto_EQ)
-                   ->getAsString(Args)
-            << Triple.getTriple();
+            << OffloadLTO->getAsString(Args) << Triple.getTriple();
       } else if (Triple.isNVPTX() && !IsRDCMode &&
-                 JA.isDeviceOffloading(Action::OFK_Cuda)) {
+                 JA.isDeviceOffloading(Action::OFK_Cuda) && OffloadLTO) {
         D.Diag(diag::err_drv_unsupported_opt_for_language_mode)
-            << Args.getLastArg(options::OPT_foffload_lto,
-                               options::OPT_foffload_lto_EQ)
-                   ->getAsString(Args)
-            << "-fno-gpu-rdc";
+            << OffloadLTO->getAsString(Args) << "-fno-gpu-rdc";
       } else {
         assert(LTOMode == LTOK_Full || LTOMode == LTOK_Thin);
         CmdArgs.push_back(Args.MakeArgString(

Copy link
Contributor

@bader bader left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but I'd like @jhuber6 to take a look as well.

Copy link
Contributor

@jhuber6 jhuber6 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsUsingLTO is fed from the Driver, which should always have these arguments defined if true. But it's nice to be careful.

CmdArgs.push_back("-emit-llvm-uselists");

if (IsUsingLTO) {
Arg *OffloadLTO = Args.getLastArg(options::OPT_foffload_lto,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this just for error handling? Do we have a test for this? Could just use getLastArgValue? That takes a default I believe so it would be sound if this was not passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants