compiler: adjust code to avoid shadowing local variables

Also add a couple of missing calls to free after mpz_get_str.

This should make the code clean with respect to -Wshadow=local.

Based on patch by Bernd Edlinger.

Change-Id: Iec12a8b75ccf51ba4c6593c25d1a5dc9647d3afd
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/198837
Reviewed-by: Than McIntosh <thanm@google.com>
diff --git a/go/ast-dump.cc b/go/ast-dump.cc
index b20f7e4..a3cbda9 100644
--- a/go/ast-dump.cc
+++ b/go/ast-dump.cc
@@ -135,11 +135,11 @@
         {
           if (it != res->begin())
             this->ast_dump_context_->ostream() << ",";
-          Named_object* no = (*it);
+          Named_object* rno = (*it);
 
-          this->ast_dump_context_->ostream() << no->name() << " ";
-          go_assert(no->is_result_variable());
-          Result_variable* resvar = no->result_var_value();
+          this->ast_dump_context_->ostream() << rno->name() << " ";
+          go_assert(rno->is_result_variable());
+          Result_variable* resvar = rno->result_var_value();
 
           this->ast_dump_context_->dump_type(resvar->type());
 
diff --git a/go/escape.cc b/go/escape.cc
index bfd1a39..f8e07f7 100644
--- a/go/escape.cc
+++ b/go/escape.cc
@@ -1541,7 +1541,6 @@
 
   if (debug_level > 1)
     {
-      Node* n = Node::make_node(*pexpr);
       std::string fn_name = this->context_->current_function_name();
       go_debug((*pexpr)->location(), "[%d] %s esc: %s",
 	       this->context_->loop_depth(), fn_name.c_str(),
diff --git a/go/expressions.cc b/go/expressions.cc
index a72ba24..9babc34 100644
--- a/go/expressions.cc
+++ b/go/expressions.cc
@@ -4104,9 +4104,11 @@
             x = mpz_get_ui(intval);
           else
             {
-              char* s = mpz_get_str(NULL, 16, intval);
+              char* ms = mpz_get_str(NULL, 16, intval);
               go_warning_at(loc, 0,
-                            "unicode code point 0x%s out of range in string", s);
+                            "unicode code point 0x%s out of range in string",
+                            ms);
+              free(ms);
               x = 0xfffd;
             }
 	  Lex::append_char(x, true, &s, loc);
@@ -8016,14 +8018,14 @@
   Expression* ret = Expression::make_struct_composite_literal(st, vals, loc);
   ret = Expression::make_heap_expression(ret, loc);
 
-  Node* n = Node::make_node(this);
-  if ((n->encoding() & ESCAPE_MASK) == Node::ESCAPE_NONE)
+  Node* node = Node::make_node(this);
+  if ((node->encoding() & ESCAPE_MASK) == Node::ESCAPE_NONE)
     ret->heap_expression()->set_allocate_on_stack();
   else if (gogo->compiling_runtime()
 	   && gogo->package_name() == "runtime"
 	   && !saw_errors())
     go_error_at(loc, "%s escapes to heap, not allowed in runtime",
-                n->ast_format(gogo).c_str());
+                node->ast_format(gogo).c_str());
 
   // If necessary, check whether the expression or any embedded
   // pointers are nil.
@@ -8741,8 +8743,6 @@
 				  Expression::make_nil(loc));
       else
 	{
-	  Numeric_constant nclen;
-	  unsigned long vlen;
 	  if (len_arg->numeric_constant_value(&nclen)
 	      && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID
 	      && vlen <= Map_type::bucket_size)
@@ -9053,8 +9053,7 @@
           else
             {
               Type* int32_type = Type::lookup_integer_type("int32");
-              Expression* zero =
-                Expression::make_integer_ul(0, int32_type, loc);
+              zero = Expression::make_integer_ul(0, int32_type, loc);
               call = Runtime::make_call(Runtime::BUILTIN_MEMSET, loc, 3, a1,
                                         zero, a2);
             }
@@ -9064,15 +9063,12 @@
               // For a slice containing pointers, growslice already zeroed
               // the memory. We only need to zero in non-growing case.
               // Note: growslice does not zero the memory in non-pointer case.
-              Expression* left =
-                Expression::make_temporary_reference(ntmp, loc);
-              left = Expression::make_cast(uint_type, left, loc);
-              Expression* right =
-                Expression::make_temporary_reference(c1tmp, loc);
-              right = Expression::make_cast(uint_type, right, loc);
-              Expression* cond =
-                Expression::make_binary(OPERATOR_GT, left, right, loc);
-              Expression* zero = Expression::make_integer_ul(0, int_type, loc);
+              ref = Expression::make_temporary_reference(ntmp, loc);
+              ref = Expression::make_cast(uint_type, ref, loc);
+              ref2 = Expression::make_temporary_reference(c1tmp, loc);
+              ref2 = Expression::make_cast(uint_type, ref2, loc);
+              cond = Expression::make_binary(OPERATOR_GT, ref, ref2, loc);
+              zero = Expression::make_integer_ul(0, int_type, loc);
               call = Expression::make_conditional(cond, call, zero, loc);
             }
         }
@@ -10877,9 +10873,7 @@
   if (this->result_count() > 1 && this->call_temp_ == NULL)
     {
       Struct_field_list* sfl = new Struct_field_list();
-      Function_type* fntype = this->get_function_type();
       const Typed_identifier_list* results = fntype->results();
-      Location loc = this->location();
 
       int i = 0;
       char buf[20];
@@ -12295,10 +12289,10 @@
     }
   else
     {
-      Expression* first_arg;
-      fn = this->interface_method_function(interface_method, &first_arg,
+      Expression* arg0;
+      fn = this->interface_method_function(interface_method, &arg0,
                                            location);
-      fn_args[0] = first_arg->get_backend(context);
+      fn_args[0] = arg0->get_backend(context);
     }
 
   Bexpression* bclosure = NULL;
@@ -16453,11 +16447,11 @@
       traverse_order = new std::vector<unsigned long>();
       traverse_order->reserve(v.size());
 
-      for (V::const_iterator p = v.begin(); p != v.end(); ++p)
+      for (V::const_iterator pv = v.begin(); pv != v.end(); ++pv)
 	{
-	  indexes->push_back(p->index);
-	  vals->push_back(p->expr);
-	  traverse_order->push_back(p->traversal_order);
+	  indexes->push_back(pv->index);
+	  vals->push_back(pv->expr);
+	  traverse_order->push_back(pv->traversal_order);
 	}
     }
 
@@ -17771,9 +17765,9 @@
 
         Interface_type* itype = this->iface_->type()->interface_type();
 
-        Hashtable::const_iterator p = result_types.find(itype);
-        if (p != result_types.end())
-          return p->second;
+        Hashtable::const_iterator pr = result_types.find(itype);
+        if (pr != result_types.end())
+          return pr->second;
 
         Type* pdt = Type::make_type_descriptor_ptr_type();
         if (itype->is_empty())
diff --git a/go/gogo.cc b/go/gogo.cc
index e7af673..a79cfc3 100644
--- a/go/gogo.cc
+++ b/go/gogo.cc
@@ -518,11 +518,11 @@
       else if (ln == ".")
 	{
 	  Bindings* bindings = package->bindings();
-	  for (Bindings::const_declarations_iterator p =
+	  for (Bindings::const_declarations_iterator pd =
 		 bindings->begin_declarations();
-	       p != bindings->end_declarations();
-	       ++p)
-	    this->add_dot_import_object(p->second);
+	       pd != bindings->end_declarations();
+	       ++pd)
+	    this->add_dot_import_object(pd->second);
           std::string dot_alias = "." + package->package_name();
           package->add_alias(dot_alias, location);
 	}
@@ -678,8 +678,8 @@
            pci != ii->precursors().end();
            ++pci)
         {
-          Import_init* ii = this->lookup_init(*pci);
-          nonroots.insert(ii);
+          Import_init* ii_init = this->lookup_init(*pci);
+          nonroots.insert(ii_init);
         }
     }
 
@@ -2613,11 +2613,11 @@
 	    {
 	      if (no->type_declaration_value()->has_methods())
 		{
-		  for (std::vector<Named_object*>::const_iterator p =
+		  for (std::vector<Named_object*>::const_iterator pm =
 			 no->type_declaration_value()->methods()->begin();
-		       p != no->type_declaration_value()->methods()->end();
-		       p++)
-		    go_error_at((*p)->location(),
+		       pm != no->type_declaration_value()->methods()->end();
+		       pm++)
+		    go_error_at((*pm)->location(),
 				"may not define methods on non-local type");
 		}
 	      no->set_type_value(global_no->type_value());
@@ -6550,8 +6550,8 @@
 
       // Build the backend representation for all the statements in the
       // function.
-      Translate_context context(gogo, named_function, NULL, NULL);
-      Bblock* code_block = this->block_->get_backend(&context);
+      Translate_context bcontext(gogo, named_function, NULL, NULL);
+      Bblock* code_block = this->block_->get_backend(&bcontext);
 
       // Initialize variables if necessary.
       Translate_context icontext(gogo, named_function, this->block_,
@@ -6608,8 +6608,8 @@
   // If we created a descriptor for the function, make sure we emit it.
   if (this->descriptor_ != NULL)
     {
-      Translate_context context(gogo, NULL, NULL, NULL);
-      this->descriptor_->get_backend(&context);
+      Translate_context dcontext(gogo, NULL, NULL, NULL);
+      this->descriptor_->get_backend(&dcontext);
     }
 }
 
diff --git a/go/parse.cc b/go/parse.cc
index 52371b2..e50af61 100644
--- a/go/parse.cc
+++ b/go/parse.cc
@@ -836,7 +836,7 @@
     {
       std::string name = token->identifier();
       bool is_exported = token->is_identifier_exported();
-      Location location = token->location();
+      Location id_location = token->location();
       token = this->advance_token();
       if (!token->is_op(OPERATOR_COMMA))
 	{
@@ -861,7 +861,7 @@
 	    }
 
 	  this->unget_token(Token::make_identifier_token(name, is_exported,
-							 location));
+							 id_location));
 	}
       else
 	{
@@ -872,15 +872,15 @@
 	  // commas as we can.
 	  std::string id_name = this->gogo_->pack_hidden_name(name,
 							      is_exported);
-	  ret->push_back(Typed_identifier(id_name, NULL, location));
+	  ret->push_back(Typed_identifier(id_name, NULL, id_location));
 	  bool just_saw_comma = true;
 	  while (this->advance_token()->is_identifier())
 	    {
 	      name = this->peek_token()->identifier();
 	      is_exported = this->peek_token()->is_identifier_exported();
-	      location = this->peek_token()->location();
+	      id_location = this->peek_token()->location();
 	      id_name = this->gogo_->pack_hidden_name(name, is_exported);
-	      ret->push_back(Typed_identifier(id_name, NULL, location));
+	      ret->push_back(Typed_identifier(id_name, NULL, id_location));
 	      if (!this->advance_token()->is_op(OPERATOR_COMMA))
 		{
 		  just_saw_comma = false;
@@ -909,7 +909,7 @@
 	      // names.
 	      parameters_have_names = false;
 	      this->unget_token(Token::make_identifier_token(name, is_exported,
-							     location));
+							     id_location));
 	      ret->pop_back();
 	      just_saw_comma = true;
 	    }
@@ -2808,7 +2808,7 @@
 	{
 	  std::string identifier = token->identifier();
 	  bool is_exported = token->is_identifier_exported();
-	  Location location = token->location();
+	  Location id_location = token->location();
 
 	  if (this->advance_token()->is_op(OPERATOR_COLON))
 	    {
@@ -2820,14 +2820,14 @@
 	      Gogo* gogo = this->gogo_;
 	      val = this->id_to_expression(gogo->pack_hidden_name(identifier,
 								  is_exported),
-					   location, false);
+					   id_location, false);
 	      is_name = true;
 	    }
 	  else
 	    {
 	      this->unget_token(Token::make_identifier_token(identifier,
 							     is_exported,
-							     location));
+							     id_location));
 	      val = this->expression(PRECEDENCE_NORMAL, false, true, NULL,
 				     NULL);
 	    }
@@ -2923,14 +2923,14 @@
 	    go_error_at(this->location(), "expected %<,%> or %<}%>");
 
 	  this->gogo_->mark_locals_used();
-	  int depth = 0;
+	  int edepth = 0;
 	  while (!token->is_eof()
-		 && (depth > 0 || !token->is_op(OPERATOR_RCURLY)))
+		 && (edepth > 0 || !token->is_op(OPERATOR_RCURLY)))
 	    {
 	      if (token->is_op(OPERATOR_LCURLY))
-		++depth;
+		++edepth;
 	      else if (token->is_op(OPERATOR_RCURLY))
-		--depth;
+		--edepth;
 	      token = this->advance_token();
 	    }
 	  if (token->is_op(OPERATOR_RCURLY))
diff --git a/go/statements.cc b/go/statements.cc
index 3dc394a..f52b33d 100644
--- a/go/statements.cc
+++ b/go/statements.cc
@@ -2938,7 +2938,7 @@
       go_assert(call_statement->classification() == STATEMENT_EXPRESSION);
       Expression_statement* es =
 	static_cast<Expression_statement*>(call_statement);
-      Call_expression* ce = es->expr()->call_expression();
+      ce = es->expr()->call_expression();
       if (ce == NULL)
 	go_assert(saw_errors());
       else
@@ -5972,10 +5972,11 @@
           // if selectnbrecv2(&lhs, &ok, chan) { body } else { default body }
 
           Type* booltype = Type::make_boolean_type();
-          Temporary_statement* ts = Statement::make_temporary(booltype, NULL, loc);
-          b->add_statement(ts);
+          Temporary_statement* okts = Statement::make_temporary(booltype, NULL,
+                                                                loc);
+          b->add_statement(okts);
 
-          okref = Expression::make_temporary_reference(ts, loc);
+          okref = Expression::make_temporary_reference(okts, loc);
           Expression* okaddr = Expression::make_unary(OPERATOR_AND, okref, loc);
           call = Runtime::make_call(Runtime::SELECTNBRECV2, loc, 3, addr, okaddr,
                                     chanref);
@@ -6595,7 +6596,7 @@
       iter_init = new Block(body_block, loc);
 
       ref = Expression::make_temporary_reference(range_temp, loc);
-      Expression* ref2 = Expression::make_temporary_reference(index_temp, loc);
+      ref2 = Expression::make_temporary_reference(index_temp, loc);
       Expression* index = Expression::make_index(ref, ref2, NULL, NULL, loc);
 
       tref = Expression::make_temporary_reference(value_temp, loc);
@@ -6693,7 +6694,7 @@
       iter_init = new Block(body_block, loc);
 
       ref = Expression::make_temporary_reference(for_temp, loc);
-      Expression* ref2 = Expression::make_temporary_reference(index_temp, loc);
+      ref2 = Expression::make_temporary_reference(index_temp, loc);
       Expression* index = Expression::make_index(ref, ref2, NULL, NULL, loc);
 
       tref = Expression::make_temporary_reference(value_temp, loc);
@@ -7179,9 +7180,9 @@
   else
     {
       Type* int32_type = Type::lookup_integer_type("int32");
-      Expression* zero = Expression::make_integer_ul(0, int32_type, loc);
+      Expression* zero32 = Expression::make_integer_ul(0, int32_type, loc);
       call = Runtime::make_call(Runtime::BUILTIN_MEMSET, loc, 3, ptr_arg,
-                                zero, sz_arg);
+                                zero32, sz_arg);
     }
   Statement* cs3 = Statement::make_statement(call, true);
   b->add_statement(cs3);
diff --git a/go/types.cc b/go/types.cc
index eeae9fa..e02b832 100644
--- a/go/types.cc
+++ b/go/types.cc
@@ -6410,12 +6410,11 @@
         fvals->push_back(Expression::make_nil(bloc));
       else
 	{
-	  std::string n;
           if (is_embedded_builtin)
             n = gogo->package_name();
           else
             n = Gogo::hidden_name_pkgpath(pf->field_name());
-	  Expression* s = Expression::make_string(n, bloc);
+	  s = Expression::make_string(n, bloc);
 	  fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
 	}
 
@@ -6429,7 +6428,7 @@
 	fvals->push_back(Expression::make_nil(bloc));
       else
 	{
-	  Expression* s = Expression::make_string(pf->tag(), bloc);
+	  s = Expression::make_string(pf->tag(), bloc);
 	  fvals->push_back(Expression::make_unary(OPERATOR_AND, s, bloc));
 	}
 
@@ -6635,22 +6634,22 @@
 	{
 	  const std::string& tag(p->tag());
 	  ret->append(" \"");
-	  for (std::string::const_iterator p = tag.begin();
-	       p != tag.end();
-	       ++p)
+	  for (std::string::const_iterator pt = tag.begin();
+	       pt != tag.end();
+	       ++pt)
 	    {
-	      if (*p == '\0')
+	      if (*pt == '\0')
 		ret->append("\\x00");
-	      else if (*p == '\n')
+	      else if (*pt == '\n')
 		ret->append("\\n");
-	      else if (*p == '\t')
+	      else if (*pt == '\t')
 		ret->append("\\t");
-	      else if (*p == '"')
+	      else if (*pt == '"')
 		ret->append("\\\"");
-	      else if (*p == '\\')
+	      else if (*pt == '\\')
 		ret->append("\\\\");
 	      else
-		ret->push_back(*p);
+		ret->push_back(*pt);
 	    }
 	  ret->push_back('"');
 	}
@@ -7197,11 +7196,11 @@
       return false;
     case Numeric_constant::NC_UL_BIG:
       {
-	mpz_t val;
-	if (!nc.to_int(&val))
+	mpz_t mval;
+	if (!nc.to_int(&mval))
 	  go_unreachable();
-	unsigned int bits = mpz_sizeinbase(val, 2);
-	mpz_clear(val);
+	unsigned int bits = mpz_sizeinbase(mval, 2);
+	mpz_clear(mval);
 	if (bits >= tbits)
 	  {
 	    go_error_at(this->length_->location(), "array bound overflows");
@@ -7704,6 +7703,7 @@
         }
       char* s = mpz_get_str(NULL, 10, val);
       exp->write_string(s);
+      free(s);
       exp->write_string(" ");
       mpz_clear(val);
     }
@@ -9752,7 +9752,7 @@
 	  parameters = new Typed_identifier_list;
 	  while (true)
 	    {
-	      std::string name = imp->read_name();
+	      std::string pname = imp->read_name();
 	      imp->require_c_string(" ");
 
 	      if (imp->match_c_string("..."))
@@ -9764,7 +9764,7 @@
 	      Type* ptype = imp->read_type();
 	      if (is_varargs)
 		ptype = Type::make_array_type(ptype, NULL);
-	      parameters->push_back(Typed_identifier(name, ptype,
+	      parameters->push_back(Typed_identifier(pname, ptype,
 						     imp->location()));
 	      if (imp->peek_char() != ',')
 		break;
@@ -9791,10 +9791,10 @@
 	      imp->advance(1);
 	      while (true)
 		{
-		  std::string name = imp->read_name();
+		  std::string rname = imp->read_name();
 		  imp->require_c_string(" ");
 		  Type* rtype = imp->read_type();
-		  results->push_back(Typed_identifier(name, rtype,
+		  results->push_back(Typed_identifier(rname, rtype,
 						      imp->location()));
 		  if (imp->peek_char() != ',')
 		    break;