Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit b960232

Browse files
committed
Use llvm::array_lengthof to replace sizeof(array)/sizeof(array[0]).
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@186300 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 0f855a9 commit b960232

File tree

8 files changed

+16
-14
lines changed

8 files changed

+16
-14
lines changed

Diff for: lib/AST/CommentCommandTraits.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ CommandTraits::getTypoCorrectCommandInfo(StringRef Typo) const {
7575

7676
SmallVector<const CommandInfo *, 2> BestCommand;
7777

78-
int NumOfCommands = sizeof(Commands) / sizeof(CommandInfo);
78+
const int NumOfCommands = llvm::array_lengthof(Commands);
7979
for (int i = 0; i < NumOfCommands; i++)
8080
HelperTypoCorrectCommandInfo(BestCommand, Typo, &Commands[i]);
8181

Diff for: lib/Basic/DiagnosticIDs.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "clang/Basic/AllDiagnostics.h"
1616
#include "clang/Basic/DiagnosticCategories.h"
1717
#include "clang/Basic/SourceManager.h"
18+
#include "llvm/ADT/STLExtras.h"
1819
#include "llvm/ADT/SmallVector.h"
1920
#include "llvm/Support/ErrorHandling.h"
2021
#include <map>
@@ -86,7 +87,7 @@ static const StaticDiagInfoRec StaticDiagInfo[] = {
8687
};
8788

8889
static const unsigned StaticDiagInfoSize =
89-
sizeof(StaticDiagInfo)/sizeof(StaticDiagInfo[0])-1;
90+
llvm::array_lengthof(StaticDiagInfo)-1;
9091

9192
/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
9293
/// or null if the ID is invalid.
@@ -224,7 +225,7 @@ static const StaticDiagCategoryRec CategoryNameTable[] = {
224225

225226
/// getNumberOfCategories - Return the number of categories
226227
unsigned DiagnosticIDs::getNumberOfCategories() {
227-
return sizeof(CategoryNameTable) / sizeof(CategoryNameTable[0])-1;
228+
return llvm::array_lengthof(CategoryNameTable) - 1;
228229
}
229230

230231
/// getCategoryNameFromID - Given a category ID, return the name of the
@@ -528,8 +529,7 @@ static const WarningOption OptionTable[] = {
528529
#include "clang/Basic/DiagnosticGroups.inc"
529530
#undef GET_DIAG_TABLE
530531
};
531-
static const size_t OptionTableSize =
532-
sizeof(OptionTable) / sizeof(OptionTable[0]);
532+
static const size_t OptionTableSize = llvm::array_lengthof(OptionTable);
533533

534534
static bool WarningOptionCompare(const WarningOption &LHS,
535535
const WarningOption &RHS) {

Diff for: lib/CodeGen/CGRTTI.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,6 @@ void CodeGenModule::EmitFundamentalRTTIDescriptors() {
972972
Context.UnsignedLongLongTy, Context.FloatTy,
973973
Context.DoubleTy, Context.LongDoubleTy,
974974
Context.Char16Ty, Context.Char32Ty };
975-
for (unsigned i = 0; i < sizeof(FundamentalTypes)/sizeof(QualType); ++i)
975+
for (unsigned i = 0; i < llvm::array_lengthof(FundamentalTypes); ++i)
976976
EmitFundamentalRTTIDescriptor(FundamentalTypes[i]);
977977
}

Diff for: lib/Driver/CC1AsOptions.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//===----------------------------------------------------------------------===//
99

1010
#include "clang/Driver/CC1AsOptions.h"
11+
#include "llvm/ADT/STLExtras.h"
1112
#include "llvm/Option/OptTable.h"
1213
#include "llvm/Option/Option.h"
1314
using namespace clang;
@@ -36,8 +37,7 @@ namespace {
3637
class CC1AsOptTable : public OptTable {
3738
public:
3839
CC1AsOptTable()
39-
: OptTable(CC1AsInfoTable,
40-
sizeof(CC1AsInfoTable) / sizeof(CC1AsInfoTable[0])) {}
40+
: OptTable(CC1AsInfoTable, llvm::array_lengthof(CC1AsInfoTable)) {}
4141
};
4242

4343
}

Diff for: lib/Driver/DriverOptions.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//===----------------------------------------------------------------------===//
99

1010
#include "clang/Driver/Options.h"
11+
#include "llvm/ADT/STLExtras.h"
1112
#include "llvm/Option/OptTable.h"
1213
#include "llvm/Option/Option.h"
1314

@@ -36,7 +37,7 @@ namespace {
3637
class DriverOptTable : public OptTable {
3738
public:
3839
DriverOptTable()
39-
: OptTable(InfoTable, sizeof(InfoTable) / sizeof(InfoTable[0])) {}
40+
: OptTable(InfoTable, llvm::array_lengthof(InfoTable)) {}
4041
};
4142

4243
}

Diff for: lib/Driver/Types.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//===----------------------------------------------------------------------===//
99

1010
#include "clang/Driver/Types.h"
11+
#include "llvm/ADT/STLExtras.h"
1112
#include "llvm/ADT/StringSwitch.h"
1213
#include <cassert>
1314
#include <string.h>
@@ -28,7 +29,7 @@ static const TypeInfo TypeInfos[] = {
2829
#include "clang/Driver/Types.def"
2930
#undef TYPE
3031
};
31-
static const unsigned numTypes = sizeof(TypeInfos) / sizeof(TypeInfos[0]);
32+
static const unsigned numTypes = llvm::array_lengthof(TypeInfos);
3233

3334
static const TypeInfo &getInfo(unsigned id) {
3435
assert(id > 0 && id - 1 < numTypes && "Invalid Type ID.");

Diff for: lib/Sema/SemaLookup.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -3624,7 +3624,7 @@ static void AddKeywordsToConsumer(Sema &SemaRef,
36243624
"extern", "inline", "static", "typedef"
36253625
};
36263626

3627-
const unsigned NumCTypeSpecs = sizeof(CTypeSpecs) / sizeof(CTypeSpecs[0]);
3627+
const unsigned NumCTypeSpecs = llvm::array_lengthof(CTypeSpecs);
36283628
for (unsigned I = 0; I != NumCTypeSpecs; ++I)
36293629
Consumer.addKeywordResult(CTypeSpecs[I]);
36303630

@@ -3671,7 +3671,7 @@ static void AddKeywordsToConsumer(Sema &SemaRef,
36713671
const char *CXXExprs[] = {
36723672
"delete", "new", "operator", "throw", "typeid"
36733673
};
3674-
const unsigned NumCXXExprs = sizeof(CXXExprs) / sizeof(CXXExprs[0]);
3674+
const unsigned NumCXXExprs = llvm::array_lengthof(CXXExprs);
36753675
for (unsigned I = 0; I != NumCXXExprs; ++I)
36763676
Consumer.addKeywordResult(CXXExprs[I]);
36773677

@@ -3697,7 +3697,7 @@ static void AddKeywordsToConsumer(Sema &SemaRef,
36973697
// Statements.
36983698
const char *CStmts[] = {
36993699
"do", "else", "for", "goto", "if", "return", "switch", "while" };
3700-
const unsigned NumCStmts = sizeof(CStmts) / sizeof(CStmts[0]);
3700+
const unsigned NumCStmts = llvm::array_lengthof(CStmts);
37013701
for (unsigned I = 0; I != NumCStmts; ++I)
37023702
Consumer.addKeywordResult(CStmts[I]);
37033703

Diff for: lib/Sema/TreeTransform.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -9126,7 +9126,7 @@ TreeTransform<Derived>::RebuildArrayType(QualType ElementType,
91269126
SemaRef.Context.UnsignedIntTy, SemaRef.Context.UnsignedLongTy,
91279127
SemaRef.Context.UnsignedLongLongTy, SemaRef.Context.UnsignedInt128Ty
91289128
};
9129-
const unsigned NumTypes = sizeof(Types) / sizeof(QualType);
9129+
const unsigned NumTypes = llvm::array_lengthof(Types);
91309130
QualType SizeType;
91319131
for (unsigned I = 0; I != NumTypes; ++I)
91329132
if (Size->getBitWidth() == SemaRef.Context.getIntWidth(Types[I])) {

0 commit comments

Comments
 (0)