jansvoboda11 created this revision.
jansvoboda11 added a reviewer: dexonsmith.
Herald added subscribers: carlosgalvezp, usaxena95, kadircet, arphaman.
jansvoboda11 requested review of this revision.
Herald added projects: clang, clang-tools-extra.
Herald added a subscriber: cfe-commits.

LLVM Programmer’s Manual strongly discourages the use of `std::vector<bool>` 
and suggests `llvm::BitVector` as a possible replacement.

This patch does just that for clang and clang-tools-extra.

Depends on D117116 <https://reviews.llvm.org/D117116>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D117119

Files:
  clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
  clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.cpp
  clang-tools-extra/clangd/Selection.cpp
  clang-tools-extra/clangd/SourceCode.cpp
  clang/include/clang/Lex/HeaderSearch.h
  clang/lib/Format/UnwrappedLineParser.cpp
  clang/lib/Format/UnwrappedLineParser.h
  clang/lib/Lex/HeaderSearch.cpp
  clang/lib/Serialization/ASTWriter.cpp
  clang/lib/Tooling/Syntax/Tree.cpp
  clang/unittests/Lex/HeaderSearchTest.cpp

Index: clang/unittests/Lex/HeaderSearchTest.cpp
===================================================================
--- clang/unittests/Lex/HeaderSearchTest.cpp
+++ clang/unittests/Lex/HeaderSearchTest.cpp
@@ -302,8 +302,8 @@
   {
     Module *M2 = Search.lookupModule("M2");
     EXPECT_NE(M2, nullptr);
-    EXPECT_EQ(Search.getSearchDirUsage(), (std::vector<bool>{0, 1, 0}));
-    EXPECT_EQ(Search.computeUserEntryUsage(), (std::vector<bool>{0, 1, 0}));
+    EXPECT_EQ(Search.getSearchDirUsage(), (llvm::BitVector{0, 1, 0}));
+    EXPECT_EQ(Search.computeUserEntryUsage(), (llvm::BitVector{0, 1, 0}));
   }
 
   addSearchDir("/M1");
@@ -312,8 +312,8 @@
   {
     Module *M1 = Search.lookupModule("M1");
     EXPECT_NE(M1, nullptr);
-    EXPECT_EQ(Search.getSearchDirUsage(), (std::vector<bool>{0, 1, 1, 0}));
-    EXPECT_EQ(Search.computeUserEntryUsage(), (std::vector<bool>{0, 1, 0}));
+    EXPECT_EQ(Search.getSearchDirUsage(), (llvm::BitVector{0, 1, 1, 0}));
+    EXPECT_EQ(Search.computeUserEntryUsage(), (llvm::BitVector{0, 1, 0}));
   }
 }
 
Index: clang/lib/Tooling/Syntax/Tree.cpp
===================================================================
--- clang/lib/Tooling/Syntax/Tree.cpp
+++ clang/lib/Tooling/Syntax/Tree.cpp
@@ -9,6 +9,7 @@
 #include "clang/Basic/TokenKinds.h"
 #include "clang/Tooling/Syntax/Nodes.h"
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/BitVector.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/Casting.h"
 #include <cassert>
@@ -202,7 +203,7 @@
 }
 
 static void dumpNode(raw_ostream &OS, const syntax::Node *N,
-                     const SourceManager &SM, std::vector<bool> IndentMask) {
+                     const SourceManager &SM, llvm::BitVector IndentMask) {
   auto DumpExtraInfo = [&OS](const syntax::Node *N) {
     if (N->getRole() != syntax::NodeRole::Unknown)
       OS << " " << N->getRole();
Index: clang/lib/Serialization/ASTWriter.cpp
===================================================================
--- clang/lib/Serialization/ASTWriter.cpp
+++ clang/lib/Serialization/ASTWriter.cpp
@@ -132,7 +132,7 @@
                          sizeof(T) * v.size());
 }
 
-static std::string bytes(const std::vector<bool> &V) {
+static std::string bytes(const llvm::BitVector &V) {
   std::string Str;
   Str.reserve(V.size() / 8);
   for (unsigned I = 0, E = V.size(); I < E;) {
Index: clang/lib/Lex/HeaderSearch.cpp
===================================================================
--- clang/lib/Lex/HeaderSearch.cpp
+++ clang/lib/Lex/HeaderSearch.cpp
@@ -138,8 +138,8 @@
   SystemDirIdx++;
 }
 
-std::vector<bool> HeaderSearch::computeUserEntryUsage() const {
-  std::vector<bool> UserEntryUsage(HSOpts->UserEntries.size());
+llvm::BitVector HeaderSearch::computeUserEntryUsage() const {
+  llvm::BitVector UserEntryUsage(HSOpts->UserEntries.size());
   for (const DirectoryLookup *SearchDir : UsedSearchDirs) {
     if (UsedSearchDirs.contains(SearchDir)) {
       auto UserEntryIdxIt = SearchDirToHSEntry.find(SearchDir);
@@ -151,8 +151,8 @@
   return UserEntryUsage;
 }
 
-std::vector<bool> HeaderSearch::getSearchDirUsage() const {
-  std::vector<bool> SearchDirUsage(SearchDirs.size());
+llvm::BitVector HeaderSearch::getSearchDirUsage() const {
+  llvm::BitVector SearchDirUsage(SearchDirs.size());
   for (unsigned I = 0, E = SearchDirs.size(); I < E; ++I)
     if (UsedSearchDirs.contains(SearchDirs[I]))
       SearchDirUsage[I] = true;
Index: clang/lib/Format/UnwrappedLineParser.h
===================================================================
--- clang/lib/Format/UnwrappedLineParser.h
+++ 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 <stack>
 #include <vector>
@@ -220,7 +221,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<bool> DeclarationScopeStack;
+  llvm::BitVector DeclarationScopeStack;
 
   const FormatStyle &Style;
   const AdditionalKeywords &Keywords;
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -57,7 +57,7 @@
 
 class ScopedDeclarationState {
 public:
-  ScopedDeclarationState(UnwrappedLine &Line, std::vector<bool> &Stack,
+  ScopedDeclarationState(UnwrappedLine &Line, llvm::BitVector &Stack,
                          bool MustBeDeclaration)
       : Line(Line), Stack(Stack) {
     Line.MustBeDeclaration = MustBeDeclaration;
@@ -73,7 +73,7 @@
 
 private:
   UnwrappedLine &Line;
-  std::vector<bool> &Stack;
+  llvm::BitVector &Stack;
 };
 
 static bool isLineComment(const FormatToken &FormatTok) {
Index: clang/include/clang/Lex/HeaderSearch.h
===================================================================
--- clang/include/clang/Lex/HeaderSearch.h
+++ clang/include/clang/Lex/HeaderSearch.h
@@ -19,6 +19,7 @@
 #include "clang/Lex/HeaderMap.h"
 #include "clang/Lex/ModuleMap.h"
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/BitVector.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SetVector.h"
 #include "llvm/ADT/SmallSet.h"
@@ -499,11 +500,10 @@
 
   /// Determine which HeaderSearchOptions::UserEntries have been successfully
   /// used so far and mark their index with 'true' in the resulting bit vector.
-  // TODO: Use llvm::BitVector instead.
-  std::vector<bool> computeUserEntryUsage() const;
+  llvm::BitVector computeUserEntryUsage() const;
   /// Return a bit vector of length \c SearchDirs.size() that indicates for each
   /// search directory whether it was used.
-  std::vector<bool> getSearchDirUsage() const;
+  llvm::BitVector getSearchDirUsage() const;
 
   /// This method returns a HeaderMap for the specified
   /// FileEntry, uniquing them through the 'HeaderMaps' datastructure.
Index: clang-tools-extra/clangd/SourceCode.cpp
===================================================================
--- clang-tools-extra/clangd/SourceCode.cpp
+++ 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<std::string> Enclosing; // Contains e.g. "clang", "clangd"
   // Stack counts open braces. true if the brace opened a namespace.
-  std::vector<bool> BraceStack;
+  llvm::BitVector BraceStack;
 
   enum {
     Default,
Index: clang-tools-extra/clangd/Selection.cpp
===================================================================
--- clang-tools-extra/clangd/Selection.cpp
+++ clang-tools-extra/clangd/Selection.cpp
@@ -25,6 +25,7 @@
 #include "clang/Basic/TokenKinds.h"
 #include "clang/Lex/Lexer.h"
 #include "clang/Tooling/Syntax/Tokens.h"
+#include "llvm/ADT/BitVector.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/Casting.h"
@@ -249,7 +250,7 @@
         });
     auto Sel = llvm::makeArrayRef(SelFirst, SelLimit);
     // Find which of these are preprocessed to nothing and should be ignored.
-    std::vector<bool> PPIgnored(Sel.size(), false);
+    llvm::BitVector PPIgnored(Sel.size(), false);
     for (const syntax::TokenBuffer::Expansion &X :
          Buf.expansionsOverlapping(Sel)) {
       if (X.Expanded.empty()) {
Index: clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/readability/FunctionSizeCheck.cpp
+++ 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<SourceLocation> NestingThresholders;
   };
   FunctionInfo Info;
-  std::vector<bool> TrackedParent;
+  llvm::BitVector TrackedParent;
   unsigned StructNesting = 0;
   unsigned CurrentNestingLevel = 0;
 };
Index: clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
===================================================================
--- clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
+++ clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
@@ -29,6 +29,7 @@
 #include "clang/Lex/Lexer.h"
 #include "clang/Tooling/Core/Diagnostic.h"
 #include "clang/Tooling/Core/Replacement.h"
+#include "llvm/ADT/BitVector.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringMap.h"
@@ -863,7 +864,7 @@
     }
   }
 
-  std::vector<bool> Apply(ErrorFixes.size(), true);
+  llvm::BitVector Apply(ErrorFixes.size(), true);
   for (auto &FileAndEvents : FileEvents) {
     std::vector<Event> &Events = FileAndEvents.second;
     // Sweep.
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
  • [PATCH] D117119: [clang] Remov... Jan Svoboda via Phabricator via cfe-commits

Reply via email to