mattd created this revision.
mattd added a reviewer: rsmith.
mattd edited the summary of this revision.

This change aims to simplify debugging by annotating the range-for loop 
artificial variables (range, begin, end) with the scope depth.


https://reviews.llvm.org/D42813

Files:
  SemaStmt.cpp


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;


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;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to