[clang] [clang][HIP] Disallow non-constant format string with pointer argument (PR #238287)
Yaxun Liu via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 5 13:00:29 PST 2026
================
@@ -2499,6 +2500,34 @@ checkMathBuiltinElementType(Sema &S, SourceLocation Loc, QualType ArgTy,
return false;
}
+/// The AMDGPU printf lowering can only find %s specifiers in a format string
+/// known at compile time, so with any other format a string argument would be
+/// printed as its address. Any pointer argument may be a string, so reject it.
+static void checkAMDGPUDevicePrintf(Sema &S, CallExpr *TheCall) {
+ const llvm::Triple &TT = S.Context.getTargetInfo().getTriple();
+ if (!S.getLangOpts().HIP ||
+ !(TT.isAMDGCN() || (TT.isSPIRV() && TT.getVendor() == llvm::Triple::AMD)))
+ return;
+ if (TheCall->getNumArgs() < 2 || TheCall->isValueDependent() ||
+ TheCall->isTypeDependent())
+ return;
+
+ auto PtrArg =
+ llvm::find_if(llvm::drop_begin(TheCall->arguments()), [](const Expr *E) {
+ return E->getType()->isPointerType();
+ });
+ if (PtrArg == TheCall->arg_end())
+ return;
+
+ const Expr *Fmt = TheCall->getArg(0);
+ if (Fmt->tryEvaluateString(S.Context))
----------------
yxsamliu wrote:
Could we land #231967 first and add a CodeGen test for local constexpr formats? `constexpr char fmt[] = "%s"; printf(fmt, str);` passes this check, but current lowering can still send the pointer value instead of the string contents.
https://github.com/llvm/llvm-project/pull/238287
More information about the cfe-commits
mailing list