[PATCH] Prevent LoopConvert from shadowing already defined variables with the same name as the loop variable.

Ariel Bernal ariel.j.bernal at intel.com
Mon Jun 17 12:58:30 PDT 2013


  Another attempt to find shadowed variables, function and template parameters with the same name as the newly generated loop variable by using ASTContext.getParents().

Hi revane, klimek, doug.gregor,

http://llvm-reviews.chandlerc.com/D950

CHANGE SINCE LAST DIFF
  http://llvm-reviews.chandlerc.com/D950?vs=2420&id=2452#toc

Files:
  cpp11-migrate/LoopConvert/VariableNaming.cpp

Index: cpp11-migrate/LoopConvert/VariableNaming.cpp
===================================================================
--- cpp11-migrate/LoopConvert/VariableNaming.cpp
+++ cpp11-migrate/LoopConvert/VariableNaming.cpp
@@ -82,6 +82,75 @@
       return true;
   }
 
+  const Stmt *PrevStmt = SourceStmt;
+  const Decl *PrevDecl = OldIndex;
+  // Reverse AST from the loop statement.
+  ast_type_traits::DynTypedNode Start =
+      clang::ast_type_traits::DynTypedNode::create(*SourceStmt);
+  do {
+    const ASTContext::ParentVector Parents =
+        const_cast<ASTContext *>(Context)->getParents(Start);
+    if (Parents.empty())
+      break;
+    ASTContext::ParentVector::const_iterator Parent = Parents.begin();
+
+    if (const Stmt *S = Parent->get<Stmt>()) {
+      // Check for VarDecls in a parent statement.
+      for (Stmt::const_child_iterator I = S->child_begin(), E = S->child_end();
+           I != E; ++I) {
+        // Skip statements after the current statement.
+        if (*I == PrevStmt)
+          break;
+        if (const DeclStmt *D = dyn_cast<DeclStmt>(*I)) {
+          for (DeclStmt::const_decl_iterator ID = D->decl_begin(),
+                                             IE = D->decl_end();
+               ID != IE; ++ID)
+            if (const VarDecl *V = dyn_cast<VarDecl>(*ID))
+              if (V->getName() == Symbol)
+                return true;
+        }
+      }
+      PrevStmt = S;
+    } else if (const Decl *D = Parent->get<Decl>()) {
+      if (const TemplateDecl *T = dyn_cast<TemplateDecl>(D)) {
+        // Check that the symbol is not a template parameter of the current
+        // function or class.
+        const TemplateParameterList *TP = T->getTemplateParameters();
+        for (TemplateParameterList::const_iterator I = TP->begin(),
+                                                   E = TP->end();
+             I != E; ++I) {
+          if ((*I)->getName() == Symbol)
+            return true;
+        }
+      } else if (const FunctionDecl *F = dyn_cast<FunctionDecl>(D)) {
+        // Check that the symbol is not a function or CXXMethodDecl parameter.
+        for (FunctionDecl::param_const_iterator I = F->param_begin(),
+                                                E = F->param_end();
+             I != E; ++I) {
+          if ((*I)->getName() == Symbol)
+            return true;
+        }
+      } else if (const DeclContext *DC = dyn_cast<DeclContext>(D)) {
+        // Check for variable or field declarations with the same name in a
+        // parent context.
+        for (clang::DeclContext::decl_iterator I = DC->decls_begin(),
+                                               E = DC->decls_end();
+             I != E; ++I) {
+          if (const NamedDecl *V = dyn_cast<NamedDecl>(*I)) {
+            // Skip declarations after current Decl.
+            if (V == PrevDecl)
+              break;
+            if (isa<VarDecl>(*I) || isa<FieldDecl>(*I))
+              if (dyn_cast<NamedDecl>(*I)->getName() == Symbol)
+                return true;
+          }
+        }
+      }
+      PrevDecl = D;
+    }
+    Start = *Parent;
+  } while (1);
+
   // FIXME: Rather than detecting conflicts at their usages, we should check the
   // parent context.
   // For some reason, lookup() always returns the pair (NULL, NULL) because its
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D950.3.patch
Type: text/x-patch
Size: 3324 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130617/0458d2e0/attachment.bin>


More information about the cfe-commits mailing list