diff --git a/clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.cpp b/clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.cpp --- a/clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.cpp +++ b/clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.cpp @@ -9,6 +9,7 @@ #include "FunctionSizeCheck.h" #include "clang/AST/RecursiveASTVisitor.h" #include "clang/ASTMatchers/ASTMatchFinder.h" +#include "llvm/ADT/BitVector.h" using namespace clang::ast_matchers; @@ -118,7 +119,7 @@ std::vector NestingThresholders; }; FunctionInfo Info; - std::vector TrackedParent; + llvm::BitVector TrackedParent; unsigned StructNesting = 0; unsigned CurrentNestingLevel = 0; }; diff --git a/clang-tools-extra/clangd/SourceCode.cpp b/clang-tools-extra/clangd/SourceCode.cpp --- a/clang-tools-extra/clangd/SourceCode.cpp +++ b/clang-tools-extra/clangd/SourceCode.cpp @@ -27,6 +27,7 @@ #include "clang/Tooling/Core/Replacement.h" #include "clang/Tooling/Syntax/Tokens.h" #include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/BitVector.h" #include "llvm/ADT/None.h" #include "llvm/ADT/STLExtras.h" #include "llvm/ADT/StringExtras.h" @@ -663,7 +664,7 @@ // Stack of enclosing namespaces, e.g. {"clang", "clangd"} std::vector Enclosing; // Contains e.g. "clang", "clangd" // Stack counts open braces. true if the brace opened a namespace. - std::vector BraceStack; + llvm::BitVector BraceStack; enum { Default, diff --git a/clang/lib/Format/UnwrappedLineParser.h b/clang/lib/Format/UnwrappedLineParser.h --- a/clang/lib/Format/UnwrappedLineParser.h +++ b/clang/lib/Format/UnwrappedLineParser.h @@ -18,6 +18,7 @@ #include "FormatToken.h" #include "clang/Basic/IdentifierTable.h" #include "clang/Format/Format.h" +#include "llvm/ADT/BitVector.h" #include "llvm/Support/Regex.h" #include #include @@ -231,7 +232,7 @@ // We store for each line whether it must be a declaration depending on // whether we are in a compound statement or not. - std::vector DeclarationScopeStack; + llvm::BitVector DeclarationScopeStack; const FormatStyle &Style; const AdditionalKeywords &Keywords; diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -58,7 +58,7 @@ class ScopedDeclarationState { public: - ScopedDeclarationState(UnwrappedLine &Line, std::vector &Stack, + ScopedDeclarationState(UnwrappedLine &Line, llvm::BitVector &Stack, bool MustBeDeclaration) : Line(Line), Stack(Stack) { Line.MustBeDeclaration = MustBeDeclaration; @@ -74,7 +74,7 @@ private: UnwrappedLine &Line; - std::vector &Stack; + llvm::BitVector &Stack; }; static bool isLineComment(const FormatToken &FormatTok) { diff --git a/llvm/include/llvm/ADT/BitVector.h b/llvm/include/llvm/ADT/BitVector.h --- a/llvm/include/llvm/ADT/BitVector.h +++ b/llvm/include/llvm/ADT/BitVector.h @@ -444,6 +444,12 @@ return (Bits[Idx / BITWORD_SIZE] & Mask) != 0; } + /// Return the last element in the vector. + bool back() const { + assert(!empty() && "Getting last element of empty vector."); + return (*this)[size() - 1]; + } + bool test(unsigned Idx) const { return (*this)[Idx]; } @@ -465,6 +471,12 @@ set(OldSize); } + /// Pop one bit from the end of the vector. + void pop_back() { + assert(!empty() && "Empty vector has no element to pop."); + resize(size() - 1); + } + /// Test if any common bits are set. bool anyCommon(const BitVector &RHS) const { unsigned ThisWords = Bits.size(); diff --git a/llvm/include/llvm/ADT/SmallBitVector.h b/llvm/include/llvm/ADT/SmallBitVector.h --- a/llvm/include/llvm/ADT/SmallBitVector.h +++ b/llvm/include/llvm/ADT/SmallBitVector.h @@ -462,6 +462,12 @@ return getPointer()->operator[](Idx); } + /// Return the last element in the vector. + bool back() const { + assert(!empty() && "Getting last element of empty vector."); + return (*this)[size() - 1]; + } + bool test(unsigned Idx) const { return (*this)[Idx]; } @@ -471,6 +477,12 @@ resize(size() + 1, Val); } + /// Pop one bit from the end of the vector. + void pop_back() { + assert(!empty() && "Empty vector has no element to pop."); + resize(size() - 1); + } + /// Test if any common bits are set. bool anyCommon(const SmallBitVector &RHS) const { if (isSmall() && RHS.isSmall()) diff --git a/llvm/lib/MC/MCParser/MasmParser.cpp b/llvm/lib/MC/MCParser/MasmParser.cpp --- a/llvm/lib/MC/MCParser/MasmParser.cpp +++ b/llvm/lib/MC/MCParser/MasmParser.cpp @@ -13,6 +13,7 @@ #include "llvm/ADT/APFloat.h" #include "llvm/ADT/APInt.h" #include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/BitVector.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/None.h" #include "llvm/ADT/Optional.h" @@ -379,7 +380,7 @@ /// time of assembly struct tm TM; - std::vector EndStatementAtEOFStack; + BitVector EndStatementAtEOFStack; AsmCond TheCondState; std::vector TheCondStack; diff --git a/llvm/unittests/ADT/BitVectorTest.cpp b/llvm/unittests/ADT/BitVectorTest.cpp --- a/llvm/unittests/ADT/BitVectorTest.cpp +++ b/llvm/unittests/ADT/BitVectorTest.cpp @@ -1171,21 +1171,25 @@ EXPECT_EQ(-1, Vec.find_first()); EXPECT_EQ(10U, Vec.size()); EXPECT_EQ(0U, Vec.count()); + EXPECT_EQ(false, Vec.back()); Vec.push_back(true); EXPECT_EQ(10, Vec.find_first()); EXPECT_EQ(11U, Vec.size()); EXPECT_EQ(1U, Vec.count()); + EXPECT_EQ(true, Vec.back()); Vec.push_back(false); EXPECT_EQ(10, Vec.find_first()); EXPECT_EQ(12U, Vec.size()); EXPECT_EQ(1U, Vec.count()); + EXPECT_EQ(false, Vec.back()); Vec.push_back(true); EXPECT_EQ(10, Vec.find_first()); EXPECT_EQ(13U, Vec.size()); EXPECT_EQ(2U, Vec.count()); + EXPECT_EQ(true, Vec.back()); // Add a lot of values to cause reallocation. for (int i = 0; i != 100; ++i) { @@ -1197,6 +1201,28 @@ EXPECT_EQ(102U, Vec.count()); } +TYPED_TEST(BitVectorTest, PopBack) { + TypeParam Vec(10, true); + EXPECT_EQ(10U, Vec.size()); + EXPECT_EQ(10U, Vec.count()); + EXPECT_EQ(true, Vec.back()); + + Vec.pop_back(); + EXPECT_EQ(9U, Vec.size()); + EXPECT_EQ(9U, Vec.count()); + EXPECT_EQ(true, Vec.back()); + + Vec.push_back(false); + EXPECT_EQ(10U, Vec.size()); + EXPECT_EQ(9U, Vec.count()); + EXPECT_EQ(false, Vec.back()); + + Vec.pop_back(); + EXPECT_EQ(9U, Vec.size()); + EXPECT_EQ(9U, Vec.count()); + EXPECT_EQ(true, Vec.back()); +} + TYPED_TEST(BitVectorTest, DenseSet) { DenseSet Set; TypeParam A(10, true);