Index: SemaStmt.cpp =================================================================== --- SemaStmt.cpp +++ SemaStmt.cpp @@ -2025,7 +2025,7 @@ /// Build a variable declaration for a for-range statement. VarDecl *BuildForRangeVarDecl(Sema &SemaRef, SourceLocation Loc, - QualType Type, const char *Name) { + QualType Type, StringRef Name) { DeclContext *DC = SemaRef.CurContext; IdentifierInfo *II = &SemaRef.PP.getIdentifierTable().get(Name); TypeSourceInfo *TInfo = SemaRef.Context.getTrivialTypeSourceInfo(Type, Loc); @@ -2093,11 +2093,13 @@ return StmtError(); } - // Build auto && __range = range-init + // Build auto && __range = range-init. + // Assume the variables are nested in the inner scope (loop body). + const auto DepthStr = std::to_string(S->getDepth() >> 1); SourceLocation RangeLoc = Range->getLocStart(); VarDecl *RangeVar = BuildForRangeVarDecl(*this, RangeLoc, Context.getAutoRRefDeductType(), - "__range"); + std::string("__range") + DepthStr); if (FinishForRangeVarDecl(*this, RangeVar, Range, RangeLoc, diag::err_for_range_deduction_failure)) { LoopVar->setInvalidDecl(); @@ -2340,10 +2342,12 @@ return StmtError(); // Build auto __begin = begin-expr, __end = end-expr. + // Assume the variables are nested in the inner scope (loop body). + const auto DepthStr = std::to_string(S->getDepth() >> 1); VarDecl *BeginVar = BuildForRangeVarDecl(*this, ColonLoc, AutoType, - "__begin"); + std::string("__begin") + DepthStr); VarDecl *EndVar = BuildForRangeVarDecl(*this, ColonLoc, AutoType, - "__end"); + std::string("__end") + DepthStr); // Build begin-expr and end-expr and attach to __begin and __end variables. ExprResult BeginExpr, EndExpr;