Skip to content

Commit e38003f

Browse files
Suppress all uses of LLVM_END_WITH_NULL. NFC.
Use variadic templates instead of relying on <cstdarg> + sentinel. This enforces better type checking and makes code more readable. Differential Revision: https://reviews.llvm.org/D32541 llvm-svn: 302571
1 parent f570c76 commit e38003f

22 files changed

+122
-157
lines changed

Diff for: llvm/include/llvm/ADT/STLExtras.h

+12
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,18 @@ struct is_one_of<T, U, Ts...> {
706706
std::is_same<T, U>::value || is_one_of<T, Ts...>::value;
707707
};
708708

709+
/// \brief traits class for checking whether type T is a base class for all
710+
/// the given types in the variadic list.
711+
template <typename T, typename... Ts> struct are_base_of {
712+
static const bool value = true;
713+
};
714+
715+
template <typename T, typename U, typename... Ts>
716+
struct are_base_of<T, U, Ts...> {
717+
static const bool value =
718+
std::is_base_of<T, U>::value && are_base_of<T, Ts...>::value;
719+
};
720+
709721
//===----------------------------------------------------------------------===//
710722
// Extra additions for arrays
711723
//===----------------------------------------------------------------------===//

Diff for: llvm/include/llvm/IR/Constants.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm/ADT/ArrayRef.h"
2727
#include "llvm/ADT/None.h"
2828
#include "llvm/ADT/Optional.h"
29+
#include "llvm/ADT/STLExtras.h"
2930
#include "llvm/ADT/StringRef.h"
3031
#include "llvm/IR/Constant.h"
3132
#include "llvm/IR/DerivedTypes.h"
@@ -452,7 +453,14 @@ class ConstantStruct final : public ConstantAggregate {
452453
public:
453454
// ConstantStruct accessors
454455
static Constant *get(StructType *T, ArrayRef<Constant*> V);
455-
static Constant *get(StructType *T, ...) LLVM_END_WITH_NULL;
456+
457+
template <typename... Csts>
458+
static typename std::enable_if<are_base_of<Constant, Csts...>::value,
459+
Constant *>::type
460+
get(StructType *T, Csts *... Vs) {
461+
SmallVector<Constant *, 8> Values{{Vs...}};
462+
return get(T, Values);
463+
}
456464

457465
/// Return an anonymous struct that has the specified elements.
458466
/// If the struct is possibly empty, then you must specify a context.

Diff for: llvm/include/llvm/IR/DerivedTypes.h

+26-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define LLVM_IR_DERIVEDTYPES_H
2020

2121
#include "llvm/ADT/ArrayRef.h"
22+
#include "llvm/ADT/STLExtras.h"
2223
#include "llvm/ADT/StringRef.h"
2324
#include "llvm/IR/Type.h"
2425
#include "llvm/Support/Casting.h"
@@ -228,7 +229,14 @@ class StructType : public CompositeType {
228229
static StructType *create(LLVMContext &Context, ArrayRef<Type *> Elements,
229230
StringRef Name, bool isPacked = false);
230231
static StructType *create(LLVMContext &Context, ArrayRef<Type *> Elements);
231-
static StructType *create(StringRef Name, Type *elt1, ...) LLVM_END_WITH_NULL;
232+
template <class... Tys>
233+
static typename std::enable_if<are_base_of<Type, Tys...>::value,
234+
StructType *>::type
235+
create(StringRef Name, Type *elt1, Tys *... elts) {
236+
assert(elt1 && "Cannot create a struct type with no elements with this");
237+
SmallVector<llvm::Type *, 8> StructFields{{elt1, elts...}};
238+
return create(StructFields, Name);
239+
}
232240

233241
/// This static method is the primary way to create a literal StructType.
234242
static StructType *get(LLVMContext &Context, ArrayRef<Type*> Elements,
@@ -240,7 +248,15 @@ class StructType : public CompositeType {
240248
/// This static method is a convenience method for creating structure types by
241249
/// specifying the elements as arguments. Note that this method always returns
242250
/// a non-packed struct, and requires at least one element type.
243-
static StructType *get(Type *elt1, ...) LLVM_END_WITH_NULL;
251+
template <class... Tys>
252+
static typename std::enable_if<are_base_of<Type, Tys...>::value,
253+
StructType *>::type
254+
get(Type *elt1, Tys *... elts) {
255+
assert(elt1 && "Cannot create a struct type with no elements with this");
256+
LLVMContext &Ctx = elt1->getContext();
257+
SmallVector<llvm::Type *, 8> StructFields{{elt1, elts...}};
258+
return llvm::StructType::get(Ctx, StructFields);
259+
}
244260

245261
bool isPacked() const { return (getSubclassData() & SCDB_Packed) != 0; }
246262

@@ -269,7 +285,14 @@ class StructType : public CompositeType {
269285

270286
/// Specify a body for an opaque identified type.
271287
void setBody(ArrayRef<Type*> Elements, bool isPacked = false);
272-
void setBody(Type *elt1, ...) LLVM_END_WITH_NULL;
288+
289+
template <typename... Tys>
290+
typename std::enable_if<are_base_of<Type, Tys...>::value, void>::type
291+
setBody(Type *elt1, Tys *... elts) {
292+
assert(elt1 && "Cannot create a struct type with no elements with this");
293+
SmallVector<llvm::Type *, 8> StructFields{{elt1, elts...}};
294+
setBody(StructFields);
295+
}
273296

274297
/// Return true if the specified type is valid as a element type.
275298
static bool isValidElementType(Type *ElemTy);

Diff for: llvm/include/llvm/Support/Compiler.h

-6
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,6 @@
111111
#define LLVM_PREFETCH(addr, rw, locality)
112112
#endif
113113

114-
#if __has_attribute(sentinel) || LLVM_GNUC_PREREQ(3, 0, 0)
115-
#define LLVM_END_WITH_NULL __attribute__((sentinel))
116-
#else
117-
#define LLVM_END_WITH_NULL
118-
#endif
119-
120114
#if __has_attribute(used) || LLVM_GNUC_PREREQ(3, 1, 0)
121115
#define LLVM_ATTRIBUTE_USED __attribute__((__used__))
122116
#else

Diff for: llvm/lib/CodeGen/SjLjEHPrepare.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ bool SjLjEHPrepare::doInitialization(Module &M) {
9393
doubleUnderDataTy, // __data
9494
VoidPtrTy, // __personality
9595
VoidPtrTy, // __lsda
96-
doubleUnderJBufTy, // __jbuf
97-
nullptr);
96+
doubleUnderJBufTy // __jbuf
97+
);
9898

9999
return true;
100100
}

Diff for: llvm/lib/IR/Constants.cpp

+2-13
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include "llvm/Support/MathExtras.h"
3131
#include "llvm/Support/raw_ostream.h"
3232
#include <algorithm>
33-
#include <cstdarg>
33+
3434
using namespace llvm;
3535

3636
//===----------------------------------------------------------------------===//
@@ -966,16 +966,6 @@ Constant *ConstantStruct::get(StructType *ST, ArrayRef<Constant*> V) {
966966
return ST->getContext().pImpl->StructConstants.getOrCreate(ST, V);
967967
}
968968

969-
Constant *ConstantStruct::get(StructType *T, ...) {
970-
va_list ap;
971-
SmallVector<Constant*, 8> Values;
972-
va_start(ap, T);
973-
while (Constant *Val = va_arg(ap, llvm::Constant*))
974-
Values.push_back(Val);
975-
va_end(ap);
976-
return get(T, Values);
977-
}
978-
979969
ConstantVector::ConstantVector(VectorType *T, ArrayRef<Constant *> V)
980970
: ConstantAggregate(T, ConstantVectorVal, V) {
981971
assert(V.size() == T->getNumElements() &&
@@ -1810,8 +1800,7 @@ Constant *ConstantExpr::getSizeOf(Type* Ty) {
18101800
Constant *ConstantExpr::getAlignOf(Type* Ty) {
18111801
// alignof is implemented as: (i64) gep ({i1,Ty}*)null, 0, 1
18121802
// Note that a non-inbounds gep is used, as null isn't within any object.
1813-
Type *AligningTy =
1814-
StructType::get(Type::getInt1Ty(Ty->getContext()), Ty, nullptr);
1803+
Type *AligningTy = StructType::get(Type::getInt1Ty(Ty->getContext()), Ty);
18151804
Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo(0));
18161805
Constant *Zero = ConstantInt::get(Type::getInt64Ty(Ty->getContext()), 0);
18171806
Constant *One = ConstantInt::get(Type::getInt32Ty(Ty->getContext()), 1);

Diff for: llvm/lib/IR/Instructions.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -1543,8 +1543,7 @@ AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
15431543
SynchronizationScope SynchScope,
15441544
Instruction *InsertBefore)
15451545
: Instruction(
1546-
StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1547-
nullptr),
1546+
StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
15481547
AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
15491548
OperandTraits<AtomicCmpXchgInst>::operands(this), InsertBefore) {
15501549
Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);
@@ -1556,8 +1555,7 @@ AtomicCmpXchgInst::AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
15561555
SynchronizationScope SynchScope,
15571556
BasicBlock *InsertAtEnd)
15581557
: Instruction(
1559-
StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext()),
1560-
nullptr),
1558+
StructType::get(Cmp->getType(), Type::getInt1Ty(Cmp->getContext())),
15611559
AtomicCmpXchg, OperandTraits<AtomicCmpXchgInst>::op_begin(this),
15621560
OperandTraits<AtomicCmpXchgInst>::operands(this), InsertAtEnd) {
15631561
Init(Ptr, Cmp, NewVal, SuccessOrdering, FailureOrdering, SynchScope);

Diff for: llvm/lib/IR/Module.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include "llvm/Support/Path.h"
3131
#include "llvm/Support/RandomNumberGenerator.h"
3232
#include <algorithm>
33-
#include <cstdarg>
3433
#include <cstdlib>
3534

3635
using namespace llvm;

Diff for: llvm/lib/IR/Type.cpp

-44
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "llvm/ADT/SmallString.h"
1717
#include "llvm/IR/Module.h"
1818
#include <algorithm>
19-
#include <cstdarg>
2019
using namespace llvm;
2120

2221
//===----------------------------------------------------------------------===//
@@ -419,21 +418,6 @@ StructType *StructType::get(LLVMContext &Context, bool isPacked) {
419418
return get(Context, None, isPacked);
420419
}
421420

422-
StructType *StructType::get(Type *type, ...) {
423-
assert(type && "Cannot create a struct type with no elements with this");
424-
LLVMContext &Ctx = type->getContext();
425-
va_list ap;
426-
SmallVector<llvm::Type*, 8> StructFields;
427-
va_start(ap, type);
428-
while (type) {
429-
StructFields.push_back(type);
430-
type = va_arg(ap, llvm::Type*);
431-
}
432-
auto *Ret = llvm::StructType::get(Ctx, StructFields);
433-
va_end(ap);
434-
return Ret;
435-
}
436-
437421
StructType *StructType::create(LLVMContext &Context, ArrayRef<Type*> Elements,
438422
StringRef Name, bool isPacked) {
439423
StructType *ST = create(Context, Name);
@@ -462,21 +446,6 @@ StructType *StructType::create(ArrayRef<Type*> Elements) {
462446
return create(Elements[0]->getContext(), Elements, StringRef());
463447
}
464448

465-
StructType *StructType::create(StringRef Name, Type *type, ...) {
466-
assert(type && "Cannot create a struct type with no elements with this");
467-
LLVMContext &Ctx = type->getContext();
468-
va_list ap;
469-
SmallVector<llvm::Type*, 8> StructFields;
470-
va_start(ap, type);
471-
while (type) {
472-
StructFields.push_back(type);
473-
type = va_arg(ap, llvm::Type*);
474-
}
475-
auto *Ret = llvm::StructType::create(Ctx, StructFields, Name);
476-
va_end(ap);
477-
return Ret;
478-
}
479-
480449
bool StructType::isSized(SmallPtrSetImpl<Type*> *Visited) const {
481450
if ((getSubclassData() & SCDB_IsSized) != 0)
482451
return true;
@@ -508,19 +477,6 @@ StringRef StructType::getName() const {
508477
return ((StringMapEntry<StructType*> *)SymbolTableEntry)->getKey();
509478
}
510479

511-
void StructType::setBody(Type *type, ...) {
512-
assert(type && "Cannot create a struct type with no elements with this");
513-
va_list ap;
514-
SmallVector<llvm::Type*, 8> StructFields;
515-
va_start(ap, type);
516-
while (type) {
517-
StructFields.push_back(type);
518-
type = va_arg(ap, llvm::Type*);
519-
}
520-
setBody(StructFields);
521-
va_end(ap);
522-
}
523-
524480
bool StructType::isValidElementType(Type *ElemTy) {
525481
return !ElemTy->isVoidTy() && !ElemTy->isLabelTy() &&
526482
!ElemTy->isMetadataTy() && !ElemTy->isFunctionTy() &&

Diff for: llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2265,7 +2265,7 @@ SDValue AArch64TargetLowering::LowerFSINCOS(SDValue Op,
22652265
SDValue Callee =
22662266
DAG.getExternalSymbol(LibcallName, getPointerTy(DAG.getDataLayout()));
22672267

2268-
StructType *RetTy = StructType::get(ArgTy, ArgTy, nullptr);
2268+
StructType *RetTy = StructType::get(ArgTy, ArgTy);
22692269
TargetLowering::CallLoweringInfo CLI(DAG);
22702270
CLI.setDebugLoc(dl)
22712271
.setChain(DAG.getEntryNode())

Diff for: llvm/lib/Target/ARM/ARMISelLowering.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -7364,7 +7364,7 @@ SDValue ARMTargetLowering::LowerFSINCOS(SDValue Op, SelectionDAG &DAG) const {
73647364
const TargetLowering &TLI = DAG.getTargetLoweringInfo();
73657365

73667366
// Pair of floats / doubles used to pass the result.
7367-
Type *RetTy = StructType::get(ArgTy, ArgTy, nullptr);
7367+
Type *RetTy = StructType::get(ArgTy, ArgTy);
73687368
auto &DL = DAG.getDataLayout();
73697369

73707370
ArgListTy Args;
@@ -13114,7 +13114,7 @@ SDValue ARMTargetLowering::LowerDivRem(SDValue Op, SelectionDAG &DAG) const {
1311413114
SDValue Callee = DAG.getExternalSymbol(getLibcallName(LC),
1311513115
getPointerTy(DAG.getDataLayout()));
1311613116

13117-
Type *RetTy = (Type*)StructType::get(Ty, Ty, nullptr);
13117+
Type *RetTy = StructType::get(Ty, Ty);
1311813118

1311913119
if (Subtarget->isTargetWindows())
1312013120
InChain = WinDBZCheckDenominator(DAG, Op.getNode(), InChain);

Diff for: llvm/lib/Target/X86/X86ISelLowering.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -23306,9 +23306,8 @@ static SDValue LowerFSINCOS(SDValue Op, const X86Subtarget &Subtarget,
2330623306
SDValue Callee =
2330723307
DAG.getExternalSymbol(LibcallName, TLI.getPointerTy(DAG.getDataLayout()));
2330823308

23309-
Type *RetTy = isF64
23310-
? (Type*)StructType::get(ArgTy, ArgTy, nullptr)
23311-
: (Type*)VectorType::get(ArgTy, 4);
23309+
Type *RetTy = isF64 ? (Type *)StructType::get(ArgTy, ArgTy)
23310+
: (Type *)VectorType::get(ArgTy, 4);
2331223311

2331323312
TargetLowering::CallLoweringInfo CLI(DAG);
2331423313
CLI.setDebugLoc(dl)

Diff for: llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

+9-10
Original file line numberDiff line numberDiff line change
@@ -1782,7 +1782,7 @@ void AddressSanitizerModule::InstrumentGlobalsMachO(
17821782
// On recent Mach-O platforms, use a structure which binds the liveness of
17831783
// the global variable to the metadata struct. Keep the list of "Liveness" GV
17841784
// created to be added to llvm.compiler.used
1785-
StructType *LivenessTy = StructType::get(IntptrTy, IntptrTy, nullptr);
1785+
StructType *LivenessTy = StructType::get(IntptrTy, IntptrTy);
17861786
SmallVector<GlobalValue *, 16> LivenessGlobals(ExtendedGlobals.size());
17871787

17881788
for (size_t i = 0; i < ExtendedGlobals.size(); i++) {
@@ -1793,9 +1793,9 @@ void AddressSanitizerModule::InstrumentGlobalsMachO(
17931793

17941794
// On recent Mach-O platforms, we emit the global metadata in a way that
17951795
// allows the linker to properly strip dead globals.
1796-
auto LivenessBinder = ConstantStruct::get(
1797-
LivenessTy, Initializer->getAggregateElement(0u),
1798-
ConstantExpr::getPointerCast(Metadata, IntptrTy), nullptr);
1796+
auto LivenessBinder =
1797+
ConstantStruct::get(LivenessTy, Initializer->getAggregateElement(0u),
1798+
ConstantExpr::getPointerCast(Metadata, IntptrTy));
17991799
GlobalVariable *Liveness = new GlobalVariable(
18001800
M, LivenessTy, false, GlobalVariable::InternalLinkage, LivenessBinder,
18011801
Twine("__asan_binder_") + G->getName());
@@ -1893,7 +1893,7 @@ bool AddressSanitizerModule::InstrumentGlobals(IRBuilder<> &IRB, Module &M, bool
18931893
// We initialize an array of such structures and pass it to a run-time call.
18941894
StructType *GlobalStructTy =
18951895
StructType::get(IntptrTy, IntptrTy, IntptrTy, IntptrTy, IntptrTy,
1896-
IntptrTy, IntptrTy, IntptrTy, nullptr);
1896+
IntptrTy, IntptrTy, IntptrTy);
18971897
SmallVector<GlobalVariable *, 16> NewGlobals(n);
18981898
SmallVector<Constant *, 16> Initializers(n);
18991899

@@ -1929,10 +1929,9 @@ bool AddressSanitizerModule::InstrumentGlobals(IRBuilder<> &IRB, Module &M, bool
19291929
assert(((RightRedzoneSize + SizeInBytes) % MinRZ) == 0);
19301930
Type *RightRedZoneTy = ArrayType::get(IRB.getInt8Ty(), RightRedzoneSize);
19311931

1932-
StructType *NewTy = StructType::get(Ty, RightRedZoneTy, nullptr);
1933-
Constant *NewInitializer =
1934-
ConstantStruct::get(NewTy, G->getInitializer(),
1935-
Constant::getNullValue(RightRedZoneTy), nullptr);
1932+
StructType *NewTy = StructType::get(Ty, RightRedZoneTy);
1933+
Constant *NewInitializer = ConstantStruct::get(
1934+
NewTy, G->getInitializer(), Constant::getNullValue(RightRedZoneTy));
19361935

19371936
// Create a new global variable with enough space for a redzone.
19381937
GlobalValue::LinkageTypes Linkage = G->getLinkage();
@@ -2013,7 +2012,7 @@ bool AddressSanitizerModule::InstrumentGlobals(IRBuilder<> &IRB, Module &M, bool
20132012
ConstantExpr::getPointerCast(Name, IntptrTy),
20142013
ConstantExpr::getPointerCast(ModuleName, IntptrTy),
20152014
ConstantInt::get(IntptrTy, MD.IsDynInit), SourceLoc,
2016-
ConstantExpr::getPointerCast(ODRIndicator, IntptrTy), nullptr);
2015+
ConstantExpr::getPointerCast(ODRIndicator, IntptrTy));
20172016

20182017
if (ClInitializers && MD.IsDynInit) HasDynamicallyInitializedGlobals = true;
20192018

Diff for: llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ FunctionType *DataFlowSanitizer::getArgsFunctionType(FunctionType *T) {
388388
ArgTypes.push_back(ShadowPtrTy);
389389
Type *RetType = T->getReturnType();
390390
if (!RetType->isVoidTy())
391-
RetType = StructType::get(RetType, ShadowTy, (Type *)nullptr);
391+
RetType = StructType::get(RetType, ShadowTy);
392392
return FunctionType::get(RetType, ArgTypes, T->isVarArg());
393393
}
394394

@@ -476,16 +476,14 @@ bool DataFlowSanitizer::doInitialization(Module &M) {
476476
GetArgTLS = ConstantExpr::getIntToPtr(
477477
ConstantInt::get(IntptrTy, uintptr_t(GetArgTLSPtr)),
478478
PointerType::getUnqual(
479-
FunctionType::get(PointerType::getUnqual(ArgTLSTy),
480-
(Type *)nullptr)));
479+
FunctionType::get(PointerType::getUnqual(ArgTLSTy), (Type*)nullptr)));
481480
}
482481
if (GetRetvalTLSPtr) {
483482
RetvalTLS = nullptr;
484483
GetRetvalTLS = ConstantExpr::getIntToPtr(
485484
ConstantInt::get(IntptrTy, uintptr_t(GetRetvalTLSPtr)),
486485
PointerType::getUnqual(
487-
FunctionType::get(PointerType::getUnqual(ShadowTy),
488-
(Type *)nullptr)));
486+
FunctionType::get(PointerType::getUnqual(ShadowTy), (Type*)nullptr)));
489487
}
490488

491489
ColdCallWeights = MDBuilder(*Ctx).createBranchWeights(1, 1000);

0 commit comments

Comments
 (0)