Index: lib/Sema/SemaExpr.cpp =================================================================== --- lib/Sema/SemaExpr.cpp +++ lib/Sema/SemaExpr.cpp @@ -905,6 +905,16 @@ diag::err_call_incomplete_argument)) return ExprError(); + // If this is a literal zero that is narrower than a pointer, widen it to a + // pointer-sized integer. This allows passing NULL through variadic functions + // in pre-C++11 codebases where NULL is defined to 0 or 0L on LLP64 platforms. + if (E->getType()->isIntegerType() && + Context.getTypeSize(E->getType()) < + Context.getTargetInfo().getPointerWidth(0) && + E->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) { + return ImpCastExprToType(E, Context.getIntPtrType(), CK_IntegralCast); + } + return E; } Index: test/CodeGen/variadic-null-llp64.c =================================================================== --- /dev/null +++ test/CodeGen/variadic-null-llp64.c @@ -0,0 +1,14 @@ +// RUN: %clang_cc1 %s -emit-llvm -o - -triple x86_64-windows-msvc | FileCheck %s + +// Make it possible to pass NULL through variadic functions on platforms where +// NULL has an integer type that is more narrow than a pointer. On such +// platforms we widen null pointer constants to a pointer-sized integer. + +#define NULL 0 + +void v(const char *f, ...); +void f(const char *f) { + v(f, 1, 2, 3, NULL); +} +// CHECK: define void @f(i8* %f) +// CHECK: call void (i8*, ...)* @v(i8* {{.*}}, i32 1, i32 2, i32 3, i64 0)