Tidy up some bool conversions.

For #65.

Change-Id: If27184d647f9f148a8bcbfae06058f9e312a9c6d
Reviewed-on: https://code-review.googlesource.com/3841
Reviewed-by: Paul Wankadia <[email protected]>
diff --git a/re2/compile.cc b/re2/compile.cc
index e5d6088..5037524 100644
--- a/re2/compile.cc
+++ b/re2/compile.cc
@@ -762,16 +762,16 @@
     }
 
     case kRegexpStar:
-      return Star(child_frags[0], re->parse_flags()&Regexp::NonGreedy);
+      return Star(child_frags[0], (re->parse_flags()&Regexp::NonGreedy) != 0);
 
     case kRegexpPlus:
-      return Plus(child_frags[0], re->parse_flags()&Regexp::NonGreedy);
+      return Plus(child_frags[0], (re->parse_flags()&Regexp::NonGreedy) != 0);
 
     case kRegexpQuest:
-      return Quest(child_frags[0], re->parse_flags()&Regexp::NonGreedy);
+      return Quest(child_frags[0], (re->parse_flags()&Regexp::NonGreedy) != 0);
 
     case kRegexpLiteral:
-      return Literal(re->rune(), re->parse_flags()&Regexp::FoldCase);
+      return Literal(re->rune(), (re->parse_flags()&Regexp::FoldCase) != 0);
 
     case kRegexpLiteralString: {
       // Concatenation of literals.
@@ -779,7 +779,8 @@
         return Nop();
       Frag f;
       for (int i = 0; i < re->nrunes(); i++) {
-        Frag f1 = Literal(re->runes()[i], re->parse_flags()&Regexp::FoldCase);
+        Frag f1 = Literal(re->runes()[i],
+                          (re->parse_flags()&Regexp::FoldCase) != 0);
         if (i == 0)
           f = f1;
         else
diff --git a/re2/dfa.cc b/re2/dfa.cc
index 7801e07..1f54b9f 100644
--- a/re2/dfa.cc
+++ b/re2/dfa.cc
@@ -94,7 +94,7 @@
   // States, linked by the next_ pointers.  If in state s and reading
   // byte c, the next state should be s->next_[c].
   struct State {
-    inline bool IsMatch() const { return flag_ & kFlagMatch; }
+    inline bool IsMatch() const { return (flag_ & kFlagMatch) != 0; }
     void SaveMatch(vector<int>* v);
 
     int* inst_;         // Instruction pointers in the state.
@@ -1015,7 +1015,7 @@
   // The state flag kFlagLastWord says whether the last
   // byte processed was a word character.  Use that info to
   // insert empty-width (non-)word boundaries.
-  bool islastword = state->flag_ & kFlagLastWord;
+  bool islastword = (state->flag_ & kFlagLastWord) != 0;
   bool isword = (c != kByteEndText && Prog::IsWordChar(static_cast<uint8>(c)));
   if (isword == islastword)
     beforeflag |= kEmptyNonWordBoundary;
diff --git a/re2/prefilter.cc b/re2/prefilter.cc
index 4a25a43..45e43c9 100644
--- a/re2/prefilter.cc
+++ b/re2/prefilter.cc
@@ -503,7 +503,7 @@
     LOG(INFO) << "BuildPrefilter::Info: " << re->ToString();
   }
 
-  bool latin1 = re->parse_flags() & Regexp::Latin1;
+  bool latin1 = (re->parse_flags() & Regexp::Latin1) != 0;
   Prefilter::Info::Walker w(latin1);
   Prefilter::Info* info = w.WalkExponential(re, NULL, 100000);
 
diff --git a/re2/regexp.cc b/re2/regexp.cc
index 7bb8390..99e72e5 100644
--- a/re2/regexp.cc
+++ b/re2/regexp.cc
@@ -678,7 +678,7 @@
       }
       break;
   }
-  *foldcase = (sub[i]->parse_flags() & FoldCase);
+  *foldcase = (sub[i]->parse_flags() & FoldCase) != 0;
   i++;
 
   // The rest.
diff --git a/re2/regexp.h b/re2/regexp.h
index b49ce0d..5f222b7 100644
--- a/re2/regexp.h
+++ b/re2/regexp.h
@@ -313,7 +313,7 @@
   // Get.  No set, Regexps are logically immutable once created.
   RegexpOp op() { return static_cast<RegexpOp>(op_); }
   int nsub() { return nsub_; }
-  bool simple() { return simple_; }
+  bool simple() { return simple_ != 0; }
   enum ParseFlags parse_flags() { return static_cast<ParseFlags>(parse_flags_); }
   int Ref();  // For testing.
 
diff --git a/re2/simplify.cc b/re2/simplify.cc
index d14483f..ecc60e7 100644
--- a/re2/simplify.cc
+++ b/re2/simplify.cc
@@ -61,7 +61,7 @@
       // These are simple as long as the subpieces are simple.
       subs = sub();
       for (int i = 0; i < nsub_; i++)
-        if (!subs[i]->simple_)
+        if (!subs[i]->simple())
           return false;
       return true;
     case kRegexpCharClass:
@@ -71,12 +71,12 @@
       return !cc_->empty() && !cc_->full();
     case kRegexpCapture:
       subs = sub();
-      return subs[0]->simple_;
+      return subs[0]->simple();
     case kRegexpStar:
     case kRegexpPlus:
     case kRegexpQuest:
       subs = sub();
-      if (!subs[0]->simple_)
+      if (!subs[0]->simple())
         return false;
       switch (subs[0]->op_) {
         case kRegexpStar:
@@ -438,7 +438,7 @@
 }
 
 Regexp* SimplifyWalker::PreVisit(Regexp* re, Regexp* parent_arg, bool* stop) {
-  if (re->simple_) {
+  if (re->simple()) {
     *stop = true;
     return re->Incref();
   }
diff --git a/re2/tostring.cc b/re2/tostring.cc
index c59d4d9..0230c8c 100644
--- a/re2/tostring.cc
+++ b/re2/tostring.cc
@@ -156,12 +156,14 @@
       break;
 
     case kRegexpLiteral:
-      AppendLiteral(t_, re->rune(), re->parse_flags() & Regexp::FoldCase);
+      AppendLiteral(t_, re->rune(),
+                    (re->parse_flags() & Regexp::FoldCase) != 0);
       break;
 
     case kRegexpLiteralString:
       for (int i = 0; i < re->nrunes(); i++)
-        AppendLiteral(t_, re->runes()[i], re->parse_flags() & Regexp::FoldCase);
+        AppendLiteral(t_, re->runes()[i],
+                      (re->parse_flags() & Regexp::FoldCase) != 0);
       if (prec < PrecConcat)
         t_->append(")");
       break;
diff --git a/util/pcre.cc b/util/pcre.cc
index 07d4039..9a3f32d 100644
--- a/util/pcre.cc
+++ b/util/pcre.cc
@@ -485,7 +485,7 @@
 /***** Actual matching and rewriting code *****/
 
 bool PCRE::HitLimit() {
-  return hit_limit_;
+  return hit_limit_ != 0;
 }
 
 void PCRE::ClearHitLimit() {
diff --git a/util/sparse_array.h b/util/sparse_array.h
index 8bc243b..d1e98f5 100644
--- a/util/sparse_array.h
+++ b/util/sparse_array.h
@@ -220,18 +220,25 @@
   // and at the beginning and end of all public non-const member functions.
   inline void DebugCheckInvariants() const;
 
+  static bool InitMemory() {
+#ifdef MEMORY_SANITIZER
+    return true;
+#else
+    return RunningOnValgrind();
+#endif
+  }
+
   int size_;
   int max_size_;
   int* sparse_to_dense_;
   vector<IndexValue> dense_;
-  bool valgrind_;
 
   DISALLOW_COPY_AND_ASSIGN(SparseArray);
 };
 
 template<typename Value>
 SparseArray<Value>::SparseArray()
-    : size_(0), max_size_(0), sparse_to_dense_(NULL), dense_(), valgrind_(RunningOnValgrind()) {}
+    : size_(0), max_size_(0), sparse_to_dense_(NULL), dense_() {}
 
 // IndexValue pairs: exposed in SparseArray::iterator.
 template<typename Value>
@@ -273,7 +280,7 @@
     if (sparse_to_dense_) {
       memmove(a, sparse_to_dense_, max_size_*sizeof a[0]);
       // Don't need to zero the memory but appease Valgrind.
-      if (valgrind_) {
+      if (InitMemory()) {
         for (int i = max_size_; i < new_max_size; i++)
           a[i] = 0xababababU;
       }
@@ -418,10 +425,9 @@
 template<typename Value> SparseArray<Value>::SparseArray(int max_size) {
   max_size_ = max_size;
   sparse_to_dense_ = new int[max_size];
-  valgrind_ = RunningOnValgrind();
   dense_.resize(max_size);
   // Don't need to zero the new memory, but appease Valgrind.
-  if (valgrind_) {
+  if (InitMemory()) {
     for (int i = 0; i < max_size; i++) {
       sparse_to_dense_[i] = 0xababababU;
       dense_[i].index_ = 0xababababU;
diff --git a/util/sparse_set.h b/util/sparse_set.h
index ff592a8..9dd41ee 100644
--- a/util/sparse_set.h
+++ b/util/sparse_set.h
@@ -51,28 +51,18 @@
 
 namespace re2 {
 
-static bool InitMemory() {
-#ifdef MEMORY_SANITIZER
-  return true;
-#else
-  return RunningOnValgrind();
-#endif
-}
-
 class SparseSet {
  public:
   SparseSet()
-    : size_(0), max_size_(0), sparse_to_dense_(NULL), dense_(NULL),
-      init_memory_(InitMemory()) {}
+    : size_(0), max_size_(0), sparse_to_dense_(NULL), dense_(NULL) {}
 
   SparseSet(int max_size) {
     max_size_ = max_size;
     sparse_to_dense_ = new int[max_size];
     dense_ = new int[max_size];
-    init_memory_ = InitMemory();
     // Don't need to zero the memory, but do so anyway
     // to appease Valgrind.
-    if (init_memory_) {
+    if (InitMemory()) {
       for (int i = 0; i < max_size; i++) {
         dense_[i] = 0xababababU;
         sparse_to_dense_[i] = 0xababababU;
@@ -104,7 +94,7 @@
       int* a = new int[new_max_size];
       if (sparse_to_dense_) {
         memmove(a, sparse_to_dense_, max_size_*sizeof a[0]);
-        if (init_memory_) {
+        if (InitMemory()) {
           for (int i = max_size_; i < new_max_size; i++)
             a[i] = 0xababababU;
         }
@@ -115,7 +105,7 @@
       a = new int[new_max_size];
       if (dense_) {
         memmove(a, dense_, size_*sizeof a[0]);
-        if (init_memory_) {
+        if (InitMemory()) {
           for (int i = size_; i < new_max_size; i++)
             a[i] = 0xababababU;
         }
@@ -174,11 +164,18 @@
   static bool less(int a, int b) { return a < b; }
 
  private:
+  static bool InitMemory() {
+#ifdef MEMORY_SANITIZER
+    return true;
+#else
+    return RunningOnValgrind();
+#endif
+  }
+
   int size_;
   int max_size_;
   int* sparse_to_dense_;
   int* dense_;
-  bool init_memory_;
 
   DISALLOW_COPY_AND_ASSIGN(SparseSet);
 };
diff --git a/util/util.h b/util/util.h
index d4b072d..c59d91f 100644
--- a/util/util.h
+++ b/util/util.h
@@ -70,9 +70,6 @@
 #define strtoull _strtoui64
 #define vsnprintf vsnprintf_s
 
-#pragma warning(disable: 4018) // signed/unsigned mismatch
-#pragma warning(disable: 4800) // conversion from int to bool
-
 #endif
 
 namespace re2 {
@@ -141,7 +138,7 @@
   return ((uint64)x << 32) | y;
 }
 
-int RunningOnValgrind();
+bool RunningOnValgrind();
 
 }  // namespace re2
 
diff --git a/util/valgrind.cc b/util/valgrind.cc
index 82f9a4c..19ec22e 100644
--- a/util/valgrind.cc
+++ b/util/valgrind.cc
@@ -9,17 +9,11 @@
 
 namespace re2 {
 
-#ifndef __has_feature
-#define __has_feature(x) 0
-#endif
-
-int RunningOnValgrind() {
-#if __has_feature(memory_sanitizer)
-	return true;
-#elif defined(RUNNING_ON_VALGRIND)
-	return RUNNING_ON_VALGRIND;
+bool RunningOnValgrind() {
+#ifdef RUNNING_ON_VALGRIND
+  return RUNNING_ON_VALGRIND != 0;
 #else
-	return 0;
+  return false;
 #endif
 }