Skip to content

[RFC][Clang] Enable custom type checking for printf #86801

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clang/include/clang/Basic/Builtins.td
Original file line number Diff line number Diff line change
Expand Up @@ -2816,14 +2816,14 @@ def StrLen : LibBuiltin<"string.h"> {
// FIXME: This list is incomplete.
def Printf : LibBuiltin<"stdio.h"> {
let Spellings = ["printf"];
let Attributes = [PrintfFormat<0>];
let Attributes = [PrintfFormat<0>, CustomTypeChecking];
let Prototype = "int(char const*, ...)";
}

// FIXME: The builtin and library function should have the same signature.
def BuiltinPrintf : Builtin {
let Spellings = ["__builtin_printf"];
let Attributes = [NoThrow, PrintfFormat<0>, FunctionWithBuiltinPrefix];
let Attributes = [NoThrow, PrintfFormat<0>, FunctionWithBuiltinPrefix, CustomTypeChecking];
let Prototype = "int(char const* restrict, ...)";
}

Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -2245,6 +2245,7 @@ class Sema final {

bool SemaBuiltinVAStart(unsigned BuiltinID, CallExpr *TheCall);
bool SemaBuiltinVAStartARMMicrosoft(CallExpr *Call);
bool SemaBuiltinPrintf(FunctionDecl *FDecl, CallExpr *TheCall);
bool SemaBuiltinUnorderedCompare(CallExpr *TheCall, unsigned BuiltinID);
bool SemaBuiltinFPClassification(CallExpr *TheCall, unsigned NumArgs,
unsigned BuiltinID);
Expand Down
6 changes: 5 additions & 1 deletion clang/lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3629,8 +3629,12 @@ unsigned FunctionDecl::getBuiltinID(bool ConsiderWrapperFunctions) const {
// OpenCL v1.2 s6.9.f - The library functions defined in
// the C99 standard headers are not available.
if (Context.getLangOpts().OpenCL &&
Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))
Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID)) {
if (Context.getLangOpts().getOpenCLCompatibleVersion() >= 120 &&
(BuiltinID == Builtin::BIprintf))
return BuiltinID;
return 0;
}

// CUDA does not have device-side standard library. printf and malloc are the
// only special cases that are supported by device-side runtime.
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Basic/Builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,8 @@ bool Builtin::Context::performsCallback(unsigned ID,

bool Builtin::Context::canBeRedeclared(unsigned ID) const {
return ID == Builtin::NotBuiltin || ID == Builtin::BI__va_start ||
ID == Builtin::BI__builtin_assume_aligned ||
ID == Builtin::BI__builtin_assume_aligned || ID == Builtin::BIprintf ||
ID == Builtin::BI__builtin_printf ||
(!hasReferenceArgsOrResult(ID) && !hasCustomTypechecking(ID)) ||
isInStdNamespace(ID);
}
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGBuiltin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5825,7 +5825,7 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
getTarget().getTriple().isAMDGCN()) {
if (getLangOpts().OpenMPIsTargetDevice)
return EmitOpenMPDevicePrintfCallExpr(E);
if (getTarget().getTriple().isNVPTX())
if (getTarget().getTriple().isNVPTX() && !getLangOpts().OpenCL)
return EmitNVPTXDevicePrintfCallExpr(E);
if (getTarget().getTriple().isAMDGCN() && getLangOpts().HIP)
return EmitAMDGPUDevicePrintfCallExpr(E);
Expand Down
51 changes: 51 additions & 0 deletions clang/lib/Sema/SemaChecking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2515,6 +2515,11 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
}
break;
}
case Builtin::BIprintf:
case Builtin::BI__builtin_printf:
if (SemaBuiltinPrintf(FDecl, TheCall))
return ExprError();
break;

// The acquire, release, and no fence variants are ARM and AArch64 only.
case Builtin::BI_interlockedbittestandset_acq:
Expand Down Expand Up @@ -9544,6 +9549,52 @@ bool Sema::SemaBuiltinVAStart(unsigned BuiltinID, CallExpr *TheCall) {
return false;
}

bool Sema::SemaBuiltinPrintf(FunctionDecl *FDecl, CallExpr *TheCall) {
auto Proto = FDecl->getType()->getAs<FunctionProtoType>();
auto Args = llvm::ArrayRef(TheCall->getArgs(), TheCall->getNumArgs());

unsigned NumParams = Proto->getNumParams();
unsigned MinArgs = FDecl->getMinRequiredArguments();
if (Args.size() < NumParams) {
if (Args.size() < MinArgs) {
Diag(TheCall->getEndLoc(), diag::err_typecheck_call_too_few_args_at_least)
<< /*function*/ 0 << MinArgs << static_cast<unsigned>(Args.size())
<< /*HasExplicitObjectParameter*/ false << TheCall->getSourceRange();

// Emit the location of the prototype if valid.
if (!FDecl->isImplicit())
Diag(FDecl->getLocation(), diag::note_callee_decl)
<< FDecl << FDecl->getParametersSourceRange();

return true;
}
// We reserve space for the default arguments when we create
// the call expression.
assert((TheCall->getNumArgs() == NumParams) &&
"We should have reserved space for the default arguments before!");
}

SmallVector<Expr *, 8> AllArgs;
VariadicCallType CallType = VariadicCallType::VariadicFunction;

// Convert arguments to paramters types of printf decl/builtin.
bool Invalid = GatherArgumentsForCall(TheCall->getBeginLoc(), FDecl, Proto, 0,
Args, AllArgs, CallType);

if (Invalid)
return true;
unsigned TotalNumArgs = AllArgs.size();
for (unsigned i = 0; i < TotalNumArgs; ++i)
TheCall->setArg(i, AllArgs[i]);

TheCall->setType(Proto->getReturnType());

if (CheckFunctionCall(FDecl, TheCall, Proto))
return true;

return false;
}

bool Sema::SemaBuiltinVAStartARMMicrosoft(CallExpr *Call) {
auto IsSuitablyTypedFormatArgument = [this](const Expr *Arg) -> bool {
const LangOptions &LO = getLangOpts();
Expand Down