[llvm] r232366 - [llvm] Replacing asserts with static_asserts where appropriate

Duncan P. N. Exon Smith dexonsmith at apple.com
Tue Mar 17 10:27:25 PDT 2015


+kcc

> On 2015-Mar-16, at 02:53, Gabor Horvath <xazax.hun at gmail.com> wrote:
> 
> Author: xazax
> Date: Mon Mar 16 04:53:42 2015
> New Revision: 232366
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=232366&view=rev
> Log:
> [llvm] Replacing asserts with static_asserts where appropriate
> 
> Summary:
> This patch consists of the suggestions of clang-tidy/misc-static-assert check.
> 
> 
> Reviewers: alexfh
> 
> Reviewed By: alexfh
> 
> Subscribers: xazax.hun, llvm-commits
> 
> Differential Revision: http://reviews.llvm.org/D8343
> 
> Modified:
>    llvm/trunk/lib/CodeGen/MachineVerifier.cpp
>    llvm/trunk/lib/CodeGen/SlotIndexes.cpp
>    llvm/trunk/lib/MC/MCDwarf.cpp
>    llvm/trunk/lib/Support/FoldingSet.cpp
>    llvm/trunk/lib/Target/X86/X86FloatingPoint.cpp
>    llvm/trunk/lib/Target/X86/X86ISelLowering.cpp
>    llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp
>    llvm/trunk/unittests/ADT/SCCIteratorTest.cpp
> 
> Modified: llvm/trunk/lib/CodeGen/MachineVerifier.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/MachineVerifier.cpp?rev=232366&r1=232365&r2=232366&view=diff
> ==============================================================================
> --- llvm/trunk/lib/CodeGen/MachineVerifier.cpp (original)
> +++ llvm/trunk/lib/CodeGen/MachineVerifier.cpp Mon Mar 16 04:53:42 2015
> @@ -739,7 +739,7 @@ void MachineVerifier::verifyInlineAsm(co
>   if (!isUInt<5>(MI->getOperand(1).getImm()))
>     report("Unknown asm flags", &MI->getOperand(1), 1);
> 
> -  assert(InlineAsm::MIOp_FirstOperand == 2 && "Asm format changed");
> +  static_assert(InlineAsm::MIOp_FirstOperand == 2, "Asm format changed");
> 
>   unsigned OpNo = InlineAsm::MIOp_FirstOperand;
>   unsigned NumOps;
> 
> Modified: llvm/trunk/lib/CodeGen/SlotIndexes.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SlotIndexes.cpp?rev=232366&r1=232365&r2=232366&view=diff
> ==============================================================================
> --- llvm/trunk/lib/CodeGen/SlotIndexes.cpp (original)
> +++ llvm/trunk/lib/CodeGen/SlotIndexes.cpp Mon Mar 16 04:53:42 2015
> @@ -127,7 +127,7 @@ void SlotIndexes::renumberIndexes() {
> void SlotIndexes::renumberIndexes(IndexList::iterator curItr) {
>   // Number indexes with half the default spacing so we can catch up quickly.
>   const unsigned Space = SlotIndex::InstrDist/2;
> -  assert((Space & 3) == 0 && "InstrDist must be a multiple of 2*NUM");
> +  static_assert((Space & 3) == 0, "InstrDist must be a multiple of 2*NUM");
> 
>   IndexList::iterator startItr = std::prev(curItr);
>   unsigned index = startItr->getIndex();
> 
> Modified: llvm/trunk/lib/MC/MCDwarf.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/MCDwarf.cpp?rev=232366&r1=232365&r2=232366&view=diff
> ==============================================================================
> --- llvm/trunk/lib/MC/MCDwarf.cpp (original)
> +++ llvm/trunk/lib/MC/MCDwarf.cpp Mon Mar 16 04:53:42 2015
> @@ -243,7 +243,9 @@ std::pair<MCSymbol *, MCSymbol *> MCDwar
>       0, // length of DW_LNS_set_epilogue_begin
>       1  // DW_LNS_set_isa
>   };
> -  assert(array_lengthof(StandardOpcodeLengths) == (DWARF2_LINE_OPCODE_BASE - 1));
> +  static_assert(array_lengthof(StandardOpcodeLengths) ==
> +                    (DWARF2_LINE_OPCODE_BASE - 1),
> +                "");
>   return Emit(MCOS, StandardOpcodeLengths);
> }
> 
> 
> Modified: llvm/trunk/lib/Support/FoldingSet.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/FoldingSet.cpp?rev=232366&r1=232365&r2=232366&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Support/FoldingSet.cpp (original)
> +++ llvm/trunk/lib/Support/FoldingSet.cpp Mon Mar 16 04:53:42 2015
> @@ -101,6 +101,8 @@ void FoldingSetNodeID::AddString(StringR
>     // Otherwise do it the hard way.
>     // To be compatible with above bulk transfer, we need to take endianness
>     // into account.
> +    static_assert(sys::IsBigEndianHost || sys::IsLittleEndianHost,
> +                  "Unexpected host endianness");
>     if (sys::IsBigEndianHost) {
>       for (Pos += 4; Pos <= Size; Pos += 4) {
>         unsigned V = ((unsigned char)String[Pos - 4] << 24) |
> @@ -109,8 +111,7 @@ void FoldingSetNodeID::AddString(StringR
>                       (unsigned char)String[Pos - 1];
>         Bits.push_back(V);
>       }
> -    } else {
> -      assert(sys::IsLittleEndianHost && "Unexpected host endianness");
> +    } else {  // Little-endian host
>       for (Pos += 4; Pos <= Size; Pos += 4) {
>         unsigned V = ((unsigned char)String[Pos - 1] << 24) |
>                      ((unsigned char)String[Pos - 2] << 16) |
> 
> Modified: llvm/trunk/lib/Target/X86/X86FloatingPoint.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86FloatingPoint.cpp?rev=232366&r1=232365&r2=232366&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Target/X86/X86FloatingPoint.cpp (original)
> +++ llvm/trunk/lib/Target/X86/X86FloatingPoint.cpp Mon Mar 16 04:53:42 2015
> @@ -300,7 +300,7 @@ bool FPS::runOnMachineFunction(MachineFu
>   // function.  If it is all integer, there is nothing for us to do!
>   bool FPIsUsed = false;
> 
> -  assert(X86::FP6 == X86::FP0+6 && "Register enums aren't sorted right!");
> +  static_assert(X86::FP6 == X86::FP0+6, "Register enums aren't sorted right!");
>   for (unsigned i = 0; i <= 6; ++i)
>     if (MF.getRegInfo().isPhysRegUsed(X86::FP0+i)) {
>       FPIsUsed = true;
> 
> Modified: llvm/trunk/lib/Target/X86/X86ISelLowering.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/X86/X86ISelLowering.cpp?rev=232366&r1=232365&r2=232366&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Target/X86/X86ISelLowering.cpp (original)
> +++ llvm/trunk/lib/Target/X86/X86ISelLowering.cpp Mon Mar 16 04:53:42 2015
> @@ -17841,7 +17841,8 @@ X86TargetLowering::EmitVAARG64WithCustom
>   // 9  ) EFLAGS (implicit-def)
> 
>   assert(MI->getNumOperands() == 10 && "VAARG_64 should have 10 operands!");
> -  assert(X86::AddrNumOperands == 5 && "VAARG_64 assumes 5 address operands");
> +  static_assert(X86::AddrNumOperands == 5,
> +                "VAARG_64 assumes 5 address operands");
> 
>   unsigned DestReg = MI->getOperand(0).getReg();
>   MachineOperand &Base = MI->getOperand(1);
> 
> Modified: llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp?rev=232366&r1=232365&r2=232366&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp (original)
> +++ llvm/trunk/lib/Transforms/Instrumentation/AddressSanitizer.cpp Mon Mar 16 04:53:42 2015
> @@ -1621,7 +1621,11 @@ static int StackMallocSizeClass(uint64_t
> void FunctionStackPoisoner::SetShadowToStackAfterReturnInlined(
>     IRBuilder<> &IRB, Value *ShadowBase, int Size) {
>   assert(!(Size % 8));
> -  assert(kAsanStackAfterReturnMagic == 0xf5);
> +
> +  #ifndef NDEBUG
> +  static_assert(kAsanStackAfterReturnMagic == 0xf5, "");
> +  #endif
> +

Is this really necessary?  I see that:

    #ifndef NDEBUG
    static const int kAsanStackAfterReturnMagic = 0xf5;
    #endif

Why not remove the `NDEBUG` from there?

Moreover, why do we define a constant just to assert what its value is?
Looking at the original commit (r190863), there was some commented out
code using the variable, but it has since been removed.

@kcc, should we remove this constant now?

>   for (int i = 0; i < Size; i += 8) {
>     Value *p = IRB.CreateAdd(ShadowBase, ConstantInt::get(IntptrTy, i));
>     IRB.CreateStore(ConstantInt::get(IRB.getInt64Ty(), 0xf5f5f5f5f5f5f5f5ULL),
> 
> Modified: llvm/trunk/unittests/ADT/SCCIteratorTest.cpp
> URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/SCCIteratorTest.cpp?rev=232366&r1=232365&r2=232366&view=diff
> ==============================================================================
> --- llvm/trunk/unittests/ADT/SCCIteratorTest.cpp (original)
> +++ llvm/trunk/unittests/ADT/SCCIteratorTest.cpp Mon Mar 16 04:53:42 2015
> @@ -250,7 +250,7 @@ TEST(SCCIteratorTest, AllSmallGraphs) {
>   typedef Graph<NUM_NODES> GT;
> 
>   /// Enumerate all graphs using NUM_GRAPHS bits.
> -  assert(NUM_GRAPHS < sizeof(unsigned) * CHAR_BIT && "Too many graphs!");
> +  static_assert(NUM_GRAPHS < sizeof(unsigned) * CHAR_BIT, "Too many graphs!");
>   for (unsigned GraphDescriptor = 0; GraphDescriptor < (1U << NUM_GRAPHS);
>        ++GraphDescriptor) {
>     GT G;
> 
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits





More information about the llvm-commits mailing list