Addressed all review comments. Thanks. Plus a few minor improvements.
- Added a few comments in the naming-alias test. The test case was copied
from the original naming test, but I've added a few comments to explain what
loop convert is trying to do.
- Readded the hardcoding of variable names to all loop convert tests.
- Changed DeclFinderASTVisitor::VisitTypeLoc(). Turns out in the previous
version, it wasn't handling for typedefs properly and was actually failing one
of the typedef tests.
- Added a namespace test .
Hi klimek,
http://llvm-reviews.chandlerc.com/D484
CHANGE SINCE LAST DIFF
http://llvm-reviews.chandlerc.com/D484?vs=1201&id=1207#toc
Files:
cpp11-migrate/LoopConvert/LoopActions.cpp
cpp11-migrate/LoopConvert/StmtAncestor.cpp
cpp11-migrate/LoopConvert/StmtAncestor.h
cpp11-migrate/LoopConvert/VariableNaming.cpp
cpp11-migrate/LoopConvert/VariableNaming.h
test/cpp11-migrate/Combined/combined.cpp
test/cpp11-migrate/LoopConvert/array.cpp
test/cpp11-migrate/LoopConvert/confidence.cpp
test/cpp11-migrate/LoopConvert/dependency.cpp
test/cpp11-migrate/LoopConvert/iterator.cpp
test/cpp11-migrate/LoopConvert/naming-alias.cpp
test/cpp11-migrate/LoopConvert/naming-conflict.cpp
test/cpp11-migrate/LoopConvert/naming.cpp
test/cpp11-migrate/LoopConvert/nesting.cpp
test/cpp11-migrate/LoopConvert/pseudoarray.cpp
test/cpp11-migrate/LoopConvert/single-iterator.cpp
Index: cpp11-migrate/LoopConvert/LoopActions.cpp
===================================================================
--- cpp11-migrate/LoopConvert/LoopActions.cpp
+++ cpp11-migrate/LoopConvert/LoopActions.cpp
@@ -749,7 +749,7 @@
// was used exactly once - in the initialization of AliasVar.
} else {
VariableNamer Namer(GeneratedDecls, &ParentFinder->getStmtToParentStmtMap(),
- TheLoop, IndexVar, MaybeContainer);
+ TheLoop, IndexVar, MaybeContainer, Context);
VarName = Namer.createIndexName();
// First, replace all usages of the array subscript expression with our new
// variable.
Index: cpp11-migrate/LoopConvert/StmtAncestor.cpp
===================================================================
--- cpp11-migrate/LoopConvert/StmtAncestor.cpp
+++ cpp11-migrate/LoopConvert/StmtAncestor.cpp
@@ -115,3 +115,25 @@
return VisitNamedDecl(D);
return true;
}
+
+/// \brief If the new variable name conflicts with any type used in the loop,
+/// then we mark that variable name as taken.
+bool DeclFinderASTVisitor::VisitTypeLoc(TypeLoc TL) {
+ QualType QType = TL.getType();
+
+ // Check if our name conflicts with a type, to handle for typedefs.
+ if (QType.getAsString() == Name) {
+ Found = true;
+ return false;
+ }
+ // Check for base type conflicts. For example, when a struct is being
+ // referenced in the body of the loop, the above getAsString() will return the
+ // whole type (ex. "struct s"), but will be caught here.
+ if (const IdentifierInfo *Ident = QType.getBaseTypeIdentifier()) {
+ if (Ident->getName() == Name) {
+ Found = true;
+ return false;
+ }
+ }
+ return true;
+}
Index: cpp11-migrate/LoopConvert/StmtAncestor.h
===================================================================
--- cpp11-migrate/LoopConvert/StmtAncestor.h
+++ cpp11-migrate/LoopConvert/StmtAncestor.h
@@ -194,6 +194,7 @@
bool VisitForStmt(clang::ForStmt *F);
bool VisitNamedDecl(clang::NamedDecl *D);
bool VisitDeclRefExpr(clang::DeclRefExpr *D);
+ bool VisitTypeLoc(clang::TypeLoc TL);
};
#endif // LLVM_TOOLS_CLANG_TOOLS_EXTRA_CPP11_MIGRATE_STMT_ANCESTOR_H
Index: cpp11-migrate/LoopConvert/VariableNaming.cpp
===================================================================
--- cpp11-migrate/LoopConvert/VariableNaming.cpp
+++ cpp11-migrate/LoopConvert/VariableNaming.cpp
@@ -57,12 +57,24 @@
return IteratorName;
}
-/// \brief Determines whether or not the the name Symbol exists in LoopContext,
-/// any of its parent contexts, or any of its child statements.
+/// \brief Determines whether or not the the name \a Symbol conflicts with
+/// language keywords or defined macros. Also checks if the name exists in
+/// LoopContext, any of its parent contexts, or any of its child statements.
///
/// We also check to see if the same identifier was generated by this loop
/// converter in a loop nested within SourceStmt.
-bool VariableNamer::declarationExists(const StringRef Symbol) {
+bool VariableNamer::declarationExists(StringRef Symbol) {
+ assert(Context != 0 && "Expected an ASTContext");
+ IdentifierInfo &Ident = Context->Idents.get(Symbol);
+
+ // Check if the symbol is not an identifier (ie. is a keyword or alias).
+ if (!isAnyIdentifier(Ident.getTokenID()))
+ return true;
+
+ // Check for conflicting macro definitions.
+ if (Ident.hasMacroDefinition())
+ return true;
+
// Determine if the symbol was generated in a parent context.
for (const Stmt *S = SourceStmt; S != NULL; S = ReverseAST->lookup(S)) {
StmtGeneratedVarNameMap::const_iterator I = GeneratedDecls->find(S);
Index: cpp11-migrate/LoopConvert/VariableNaming.h
===================================================================
--- cpp11-migrate/LoopConvert/VariableNaming.h
+++ cpp11-migrate/LoopConvert/VariableNaming.h
@@ -27,12 +27,13 @@
/// index, if they exist.
class VariableNamer {
public:
- VariableNamer(StmtGeneratedVarNameMap *GeneratedDecls,
- const StmtParentMap *ReverseAST, const clang::Stmt *SourceStmt,
- const clang::VarDecl *OldIndex,
- const clang::VarDecl *TheContainer) :
- GeneratedDecls(GeneratedDecls), ReverseAST(ReverseAST),
- SourceStmt(SourceStmt), OldIndex(OldIndex), TheContainer(TheContainer) { }
+ VariableNamer(
+ StmtGeneratedVarNameMap *GeneratedDecls, const StmtParentMap *ReverseAST,
+ const clang::Stmt *SourceStmt, const clang::VarDecl *OldIndex,
+ const clang::VarDecl *TheContainer, const clang::ASTContext *Context)
+ : GeneratedDecls(GeneratedDecls), ReverseAST(ReverseAST),
+ SourceStmt(SourceStmt), OldIndex(OldIndex), TheContainer(TheContainer),
+ Context(Context) {}
/// \brief Generate a new index name.
///
@@ -47,10 +48,11 @@
const clang::Stmt *SourceStmt;
const clang::VarDecl *OldIndex;
const clang::VarDecl *TheContainer;
+ const clang::ASTContext *Context;
// Determine whether or not a declaration that would conflict with Symbol
// exists in an outer context or in any statement contained in SourceStmt.
- bool declarationExists(const llvm::StringRef Symbol);
+ bool declarationExists(llvm::StringRef Symbol);
};
#endif // LLVM_TOOLS_CLANG_TOOLS_EXTRA_CPP11_MIGRATE_VARIABLE_NAMING_H
Index: test/cpp11-migrate/Combined/combined.cpp
===================================================================
--- test/cpp11-migrate/Combined/combined.cpp
+++ test/cpp11-migrate/Combined/combined.cpp
@@ -28,8 +28,8 @@
*it = NULL;
}
- // CHECK: for ({{[a-zA-Z_ ]+&? ?}}[[VAR:[a-z_]+]] : t)
- // CHECK-NEXT: [[VAR]] = nullptr;
+ // CHECK: for (auto & elem : t)
+ // CHECK-NEXT: elem = nullptr;
}
void test_loopconvert_and_nullptr_risky() {
@@ -40,6 +40,6 @@
(*pArr)[i] = NULL;
}
- // RISKY: for (auto & [[VAR:[a-z_]+]] : *pArr)
- // RISKY-NEXT: [[VAR:[a-z_]+]] = nullptr;
+ // RISKY: for (auto & elem : *pArr)
+ // RISKY-NEXT: elem = nullptr;
}
Index: test/cpp11-migrate/LoopConvert/array.cpp
===================================================================
--- test/cpp11-migrate/LoopConvert/array.cpp
+++ test/cpp11-migrate/LoopConvert/array.cpp
@@ -25,77 +25,77 @@
sum += arr[i];
int k;
}
- // CHECK: for (auto & [[VAR:[a-z_]+]] : arr) {
- // CHECK-NEXT: sum += [[VAR]];
+ // CHECK: for (auto & elem : arr) {
+ // CHECK-NEXT: sum += elem;
// CHECK-NEXT: int k;
// CHECK-NEXT: }
for (int i = 0; i < N; ++i) {
printf("Fibonacci number is %d\n", arr[i]);
sum += arr[i] + 2;
}
- // CHECK: for (auto & [[VAR:[a-z_]+]] : arr)
- // CHECK-NEXT: printf("Fibonacci number is %d\n", [[VAR]]);
- // CHECK-NEXT: sum += [[VAR]] + 2;
+ // CHECK: for (auto & elem : arr)
+ // CHECK-NEXT: printf("Fibonacci number is %d\n", elem);
+ // CHECK-NEXT: sum += elem + 2;
for (int i = 0; i < N; ++i) {
int x = arr[i];
int y = arr[i] + 2;
}
- // CHECK: for (auto & [[VAR:[a-z_]+]] : arr)
- // CHECK-NEXT: int x = [[VAR]];
- // CHECK-NEXT: int y = [[VAR]] + 2;
+ // CHECK: for (auto & elem : arr)
+ // CHECK-NEXT: int x = elem;
+ // CHECK-NEXT: int y = elem + 2;
for (int i = 0; i < N; ++i) {
int x = N;
x = arr[i];
}
- // CHECK: for (auto & [[VAR:[a-z_]+]] : arr)
+ // CHECK: for (auto & elem : arr)
// CHECK-NEXT: int x = N;
- // CHECK-NEXT: x = [[VAR]];
+ // CHECK-NEXT: x = elem;
for (int i = 0; i < N; ++i) {
arr[i] += 1;
}
- // CHECK: for (auto & [[VAR:[a-z_]+]] : arr) {
- // CHECK-NEXT: [[VAR]] += 1;
+ // CHECK: for (auto & elem : arr) {
+ // CHECK-NEXT: elem += 1;
// CHECK-NEXT: }
for (int i = 0; i < N; ++i) {
int x = arr[i] + 2;
arr[i] ++;
}
- // CHECK: for (auto & [[VAR:[a-z_]+]] : arr)
- // CHECK-NEXT: int x = [[VAR]] + 2;
- // CHECK-NEXT: [[VAR]] ++;
+ // CHECK: for (auto & elem : arr)
+ // CHECK-NEXT: int x = elem + 2;
+ // CHECK-NEXT: elem ++;
for (int i = 0; i < N; ++i) {
arr[i] = 4 + arr[i];
}
- // CHECK: for (auto & [[VAR:[a-z_]+]] : arr)
- // CHECK-NEXT: [[VAR]] = 4 + [[VAR]];
+ // CHECK: for (auto & elem : arr)
+ // CHECK-NEXT: elem = 4 + elem;
for (int i = 0; i < NMinusOne + 1; ++i) {
sum += arr[i];
}
- // CHECK: for (auto & [[VAR:[a-z_]+]] : arr) {
- // CHECK-NEXT: sum += [[VAR]];
+ // CHECK: for (auto & elem : arr) {
+ // CHECK-NEXT: sum += elem;
// CHECK-NEXT: }
for (int i = 0; i < N; ++i) {
printf("Fibonacci number %d has address %p\n", arr[i], &arr[i]);
sum += arr[i] + 2;
}
- // CHECK: for (auto & [[VAR:[a-z_]+]] : arr)
- // CHECK-NEXT: printf("Fibonacci number %d has address %p\n", [[VAR]], &[[VAR]]);
- // CHECK-NEXT: sum += [[VAR]] + 2;
+ // CHECK: for (auto & elem : arr)
+ // CHECK-NEXT: printf("Fibonacci number %d has address %p\n", elem, &elem);
+ // CHECK-NEXT: sum += elem + 2;
Val teas[N];
for (int i = 0; i < N; ++i) {
teas[i].g();
}
- // CHECK: for (auto & [[VAR:[a-z_]+]] : teas) {
- // CHECK-NEXT: [[VAR]].g();
+ // CHECK: for (auto & tea : teas) {
+ // CHECK-NEXT: tea.g();
// CHECK-NEXT: }
}
@@ -106,31 +106,31 @@
for (int i = 0; i < N; ++i) {
printf("%d", Arr[i]);
}
- // CHECK: for (auto & [[VAR:[a-z_]+]] : Arr) {
- // CHECK-NEXT: printf("%d", [[VAR]]);
+ // CHECK: for (auto & elem : Arr) {
+ // CHECK-NEXT: printf("%d", elem);
// CHECK-NEXT: }
for (int i = 0; i < N; ++i) {
printf("%d", ValArr[i].x);
}
- // CHECK: for (auto & [[VAR:[a-z_]+]] : ValArr) {
- // CHECK-NEXT: printf("%d", [[VAR]].x);
+ // CHECK: for (auto & elem : ValArr) {
+ // CHECK-NEXT: printf("%d", elem.x);
// CHECK-NEXT: }
}
void explicitThis() {
for (int i = 0; i < N; ++i) {
printf("%d", this->Arr[i]);
}
- // CHECK: for (auto & [[VAR:[a-z_]+]] : this->Arr) {
- // CHECK-NEXT: printf("%d", [[VAR]]);
+ // CHECK: for (auto & elem : this->Arr) {
+ // CHECK-NEXT: printf("%d", elem);
// CHECK-NEXT: }
for (int i = 0; i < N; ++i) {
printf("%d", this->ValArr[i].x);
}
- // CHECK: for (auto & [[VAR:[a-z_]+]] : this->ValArr) {
- // CHECK-NEXT: printf("%d", [[VAR]].x);
+ // CHECK: for (auto & elem : this->ValArr) {
+ // CHECK-NEXT: printf("%d", elem.x);
// CHECK-NEXT: }
}
};
@@ -150,6 +150,6 @@
void (Val::*mfpArr[N])(void) = { &Val::g };
for (int i = 0; i < N; ++i)
(v.*mfpArr[i])();
- // CHECK: for (auto & [[VAR:[a-z_]+]] : mfpArr)
- // CHECK-NEXT: (v.*[[VAR]])();
+ // CHECK: for (auto & elem : mfpArr)
+ // CHECK-NEXT: (v.*elem)();
}
Index: test/cpp11-migrate/LoopConvert/confidence.cpp
===================================================================
--- test/cpp11-migrate/LoopConvert/confidence.cpp
+++ test/cpp11-migrate/LoopConvert/confidence.cpp
@@ -19,15 +19,15 @@
// CHECK: for (int i = 0; i < M; ++i) {
// CHECK-NEXT: sum += Arr[0][i];
// CHECK-NEXT: }
- // RISKY: for (auto & [[VAR:[a-z_]+]] : Arr[0]) {
- // RISKY-NEXT: sum += [[VAR]];
+ // RISKY: for (auto & elem : Arr[0]) {
+ // RISKY-NEXT: sum += elem;
// RISKY-NEXT: }
for (int i = 0; i < N; ++i) {
sum += (*pArr)[i];
}
- // RISKY: for (auto & [[VAR:[a-z_]+]] : *pArr) {
- // RISKY-NEXT: sum += [[VAR]];
+ // RISKY: for (auto & elem : *pArr) {
+ // RISKY-NEXT: sum += elem;
// RISKY-NEXT: }
// CHECK: for (int i = 0; i < N; ++i) {
// CHECK-NEXT: sum += (*pArr)[i];
Index: test/cpp11-migrate/LoopConvert/dependency.cpp
===================================================================
--- test/cpp11-migrate/LoopConvert/dependency.cpp
+++ test/cpp11-migrate/LoopConvert/dependency.cpp
@@ -10,9 +10,9 @@
int a = 0;
int b = arr[i][a];
}
- // CHECK: for (auto & [[VAR:[a-z_]+]] : arr) {
+ // CHECK: for (auto & elem : arr) {
// CHECK-NEXT: int a = 0;
- // CHECK-NEXT: int b = [[VAR]][a];
+ // CHECK-NEXT: int b = elem[a];
// CHECK-NEXT: }
for (int j = 0; j < M; ++j) {
Index: test/cpp11-migrate/LoopConvert/iterator.cpp
===================================================================
--- test/cpp11-migrate/LoopConvert/iterator.cpp
+++ test/cpp11-migrate/LoopConvert/iterator.cpp
@@ -12,95 +12,95 @@
for (T::iterator it = t.begin(), e = t.end(); it != e; ++it) {
printf("I found %d\n", *it);
}
- // CHECK: for ({{[a-zA-Z_ ]+&? ?}}[[VAR:[a-z_]+]] : t)
- // CHECK-NEXT: printf("I found %d\n", [[VAR]]);
+ // CHECK: for (auto & elem : t)
+ // CHECK-NEXT: printf("I found %d\n", elem);
T *pt;
for (T::iterator it = pt->begin(), e = pt->end(); it != e; ++it) {
printf("I found %d\n", *it);
}
- // CHECK: for ({{[a-zA-Z_ ]+&? ?}}[[VAR:[a-z_]+]] : *pt)
- // CHECK-NEXT: printf("I found %d\n", [[VAR]]);
+ // CHECK: for (auto & elem : *pt)
+ // CHECK-NEXT: printf("I found %d\n", elem);
S s;
for (S::const_iterator it = s.begin(), e = s.end(); it != e; ++it) {
printf("s has value %d\n", (*it).x);
}
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : s)
- // CHECK-NEXT: printf("s has value %d\n", ([[VAR]]).x);
+ // CHECK: for (auto & elem : s)
+ // CHECK-NEXT: printf("s has value %d\n", (elem).x);
S *ps;
for (S::const_iterator it = ps->begin(), e = ps->end(); it != e; ++it) {
printf("s has value %d\n", (*it).x);
}
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : *ps)
- // CHECK-NEXT: printf("s has value %d\n", ([[VAR]]).x);
+ // CHECK: for (auto & p : *ps)
+ // CHECK-NEXT: printf("s has value %d\n", (p).x);
for (S::const_iterator it = s.begin(), e = s.end(); it != e; ++it) {
printf("s has value %d\n", it->x);
}
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : s)
- // CHECK-NEXT: printf("s has value %d\n", [[VAR]].x);
+ // CHECK: for (auto & elem : s)
+ // CHECK-NEXT: printf("s has value %d\n", elem.x);
for (S::iterator it = s.begin(), e = s.end(); it != e; ++it) {
it->x = 3;
}
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : s)
- // CHECK-NEXT: [[VAR]].x = 3;
+ // CHECK: for (auto & elem : s)
+ // CHECK-NEXT: elem.x = 3;
for (S::iterator it = s.begin(), e = s.end(); it != e; ++it) {
(*it).x = 3;
}
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : s)
- // CHECK-NEXT: ([[VAR]]).x = 3;
+ // CHECK: for (auto & elem : s)
+ // CHECK-NEXT: (elem).x = 3;
for (S::iterator it = s.begin(), e = s.end(); it != e; ++it) {
it->nonConstFun(4, 5);
}
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : s)
- // CHECK-NEXT: [[VAR]].nonConstFun(4, 5);
+ // CHECK: for (auto & elem : s)
+ // CHECK-NEXT: elem.nonConstFun(4, 5);
U u;
for (U::iterator it = u.begin(), e = u.end(); it != e; ++it) {
printf("s has value %d\n", it->x);
}
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : u)
- // CHECK-NEXT: printf("s has value %d\n", [[VAR]].x);
+ // CHECK: for (auto & elem : u)
+ // CHECK-NEXT: printf("s has value %d\n", elem.x);
for (U::iterator it = u.begin(), e = u.end(); it != e; ++it) {
printf("s has value %d\n", (*it).x);
}
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : u)
- // CHECK-NEXT: printf("s has value %d\n", ([[VAR]]).x);
+ // CHECK: for (auto & elem : u)
+ // CHECK-NEXT: printf("s has value %d\n", (elem).x);
U::iterator A;
for (U::iterator i = u.begin(), e = u.end(); i != e; ++i)
int k = A->x + i->x;
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : u)
- // CHECK-NEXT: int k = A->x + [[VAR]].x;
+ // CHECK: for (auto & elem : u)
+ // CHECK-NEXT: int k = A->x + elem.x;
dependent<int> v;
for (dependent<int>::const_iterator it = v.begin(), e = v.end();
it != e; ++it) {
printf("Fibonacci number is %d\n", *it);
}
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : v)
- // CHECK-NEXT: printf("Fibonacci number is %d\n", [[VAR]]);
+ // CHECK: for (auto & elem : v)
+ // CHECK-NEXT: printf("Fibonacci number is %d\n", elem);
for (dependent<int>::const_iterator it(v.begin()), e = v.end();
it != e; ++it) {
printf("Fibonacci number is %d\n", *it);
}
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : v)
- // CHECK-NEXT: printf("Fibonacci number is %d\n", [[VAR]]);
+ // CHECK: for (auto & elem : v)
+ // CHECK-NEXT: printf("Fibonacci number is %d\n", elem);
doublyDependent<int,int> intmap;
for (doublyDependent<int,int>::iterator it = intmap.begin(), e = intmap.end();
it != e; ++it) {
printf("intmap[%d] = %d", it->first, it->second);
}
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : intmap)
- // CHECK-NEXT: printf("intmap[%d] = %d", [[VAR]].first, [[VAR]].second);
+ // CHECK: for (auto & elem : intmap)
+ // CHECK-NEXT: printf("intmap[%d] = %d", elem.first, elem.second);
}
// Tests to ensure that an implicit 'this' is picked up as the container.
Index: test/cpp11-migrate/LoopConvert/naming-alias.cpp
===================================================================
--- /dev/null
+++ test/cpp11-migrate/LoopConvert/naming-alias.cpp
@@ -0,0 +1,59 @@
+// RUN: grep -Ev "// *[A-Z-]+:" %s > %t.cpp
+// RUN: cpp11-migrate -loop-convert %t.cpp -- -I %S/Inputs
+// RUN: FileCheck -input-file=%t.cpp %s
+
+#include "structures.h"
+
+const int N = 10;
+
+Val Arr[N];
+Val &func(Val &);
+
+void aliasing() {
+ // If the loop container is only used for a declaration of a temporary
+ // variable to hold each element, we can name the new variable for the
+ // converted range-based loop as the temporary variable's name.
+
+ // In the following case, "t" is used as a temporary variable to hold each
+ // element, and thus we consider the name "t" aliased to the loop.
+ // The extra blank braces are left as a placeholder for after the variable
+ // declaration is deleted.
+ for (int i = 0; i < N; ++i) {
+ Val &t = Arr[i]; { }
+ int y = t.x;
+ }
+ // CHECK: for (auto & t : Arr)
+ // CHECK-NOT: Val &{{[a-z_]+}} =
+ // CHECK-NEXT: { }
+ // CHECK-NEXT: int y = t.x;
+
+ // The container was not only used to initialize a temporary loop variable for
+ // the container's elements, so we do not alias the new loop variable.
+ for (int i = 0; i < N; ++i) {
+ Val &t = Arr[i];
+ int y = t.x;
+ int z = Arr[i].x + t.x;
+ }
+ // CHECK: for (auto & elem : Arr)
+ // CHECK-NEXT: Val &t = elem;
+ // CHECK-NEXT: int y = t.x;
+ // CHECK-NEXT: int z = elem.x + t.x;
+
+ for (int i = 0; i < N; ++i) {
+ Val t = Arr[i];
+ int y = t.x;
+ int z = Arr[i].x + t.x;
+ }
+ // CHECK: for (auto & elem : Arr)
+ // CHECK-NEXT: Val t = elem;
+ // CHECK-NEXT: int y = t.x;
+ // CHECK-NEXT: int z = elem.x + t.x;
+
+ for (int i = 0; i < N; ++i) {
+ Val &t = func(Arr[i]);
+ int y = t.x;
+ }
+ // CHECK: for (auto & elem : Arr)
+ // CHECK-NEXT: Val &t = func(elem);
+ // CHECK-NEXT: int y = t.x;
+}
Index: test/cpp11-migrate/LoopConvert/naming-conflict.cpp
===================================================================
--- /dev/null
+++ test/cpp11-migrate/LoopConvert/naming-conflict.cpp
@@ -0,0 +1,112 @@
+// RUN: grep -Ev "// *[A-Z-]+:" %s > %t.cpp
+// RUN: cpp11-migrate -loop-convert %t.cpp -- -I %S/Inputs
+// RUN: FileCheck -input-file=%t.cpp %s
+
+#include "structures.h"
+
+#define MAX(a,b) (a > b) ? a : b
+#define DEF 5
+
+const int N = 10;
+int nums[N];
+int sum = 0;
+
+namespace ns {
+ struct st {
+ int x;
+ };
+}
+
+void sameNames() {
+ int num = 0;
+ for (int i = 0; i < N; ++i) {
+ printf("Fibonacci number is %d\n", nums[i]);
+ sum += nums[i] + 2 + num;
+ (void) nums[i];
+ }
+ // CHECK: for (auto & nums_i : nums)
+ // CHECK-NEXT: printf("Fibonacci number is %d\n", nums_i);
+ // CHECK-NEXT: sum += nums_i + 2 + num;
+ // CHECK-NOT: (void) num;
+ // CHECK-NEXT: }
+}
+
+void macroConflict() {
+ S MAXs;
+ for (S::const_iterator it = MAXs.begin(), e = MAXs.end(); it != e; ++it) {
+ printf("s has value %d\n", (*it).x);
+ printf("Max of 3 and 5: %d\n", MAX(3,5));
+ }
+ // CHECK: for (auto & MAXs_it : MAXs)
+ // CHECK-NEXT: printf("s has value %d\n", (MAXs_it).x);
+ // CHECK-NEXT: printf("Max of 3 and 5: %d\n", MAX(3,5));
+
+ T DEFs;
+ for (T::iterator it = DEFs.begin(), e = DEFs.end(); it != e; ++it) {
+ if (*it == DEF) {
+ printf("I found %d\n", *it);
+ }
+ }
+ // CHECK: for (auto & DEFs_it : DEFs)
+ // CHECK-NEXT: if (DEFs_it == DEF) {
+ // CHECK-NEXT: printf("I found %d\n", DEFs_it);
+}
+
+void keywordConflict() {
+ T ints;
+ for (T::iterator it = ints.begin(), e = ints.end(); it != e; ++it) {
+ *it = 5;
+ }
+ // CHECK: for (auto & ints_it : ints)
+ // CHECK-NEXT: ints_it = 5;
+
+ U __FUNCTION__s;
+ for (U::iterator it = __FUNCTION__s.begin(), e = __FUNCTION__s.end();
+ it != e; ++it) {
+ int __FUNCTION__s_it = (*it).x + 2;
+ }
+ // CHECK: for (auto & __FUNCTION__s_elem : __FUNCTION__s)
+ // CHECK-NEXT: int __FUNCTION__s_it = (__FUNCTION__s_elem).x + 2;
+}
+
+void typeConflict() {
+ T Vals;
+ // Using the name "Val", although it is the name of an existing struct, is
+ // safe in this loop since it will only exist within this scope.
+ for (T::iterator it = Vals.begin(), e = Vals.end(); it != e; ++it) {
+ }
+ // CHECK: for (auto & Val : Vals)
+
+ // We cannot use the name "Val" in this loop since there is a reference to
+ // it in the body of the loop.
+ for (T::iterator it = Vals.begin(), e = Vals.end(); it != e; ++it) {
+ *it = sizeof(Val);
+ }
+ // CHECK: for (auto & Vals_it : Vals)
+ // CHECK-NEXT: Vals_it = sizeof(Val);
+
+ typedef struct Val TD;
+ U TDs;
+ // Naming the variable "TD" within this loop is safe because the typedef
+ // was never used within the loop.
+ for (U::iterator it = TDs.begin(), e = TDs.end(); it != e; ++it) {
+ }
+ // CHECK: for (auto & TD : TDs)
+
+ // "TD" cannot be used in this loop since the typedef is being used.
+ for (U::iterator it = TDs.begin(), e = TDs.end(); it != e; ++it) {
+ TD V;
+ V.x = 5;
+ }
+ // CHECK: for (auto & TDs_it : TDs)
+ // CHECK-NEXT: TD V;
+ // CHECK-NEXT: V.x = 5;
+
+ using ns::st;
+ T sts;
+ for (T::iterator it = sts.begin(), e = sts.end(); it != e; ++it) {
+ *it = sizeof(st);
+ }
+ // CHECK: for (auto & sts_it : sts)
+ // CHECK-NEXT: sts_it = sizeof(st);
+}
Index: test/cpp11-migrate/LoopConvert/naming.cpp
===================================================================
--- test/cpp11-migrate/LoopConvert/naming.cpp
+++ /dev/null
@@ -1,67 +0,0 @@
-// RUN: grep -Ev "// *[A-Z-]+:" %s > %t.cpp
-// RUN: cpp11-migrate -loop-convert %t.cpp -- -I %S/Inputs
-// RUN: FileCheck -input-file=%t.cpp %s
-
-#include "structures.h"
-
-const int N = 10;
-int nums[N];
-int sum = 0;
-
-Val Arr[N];
-Val &func(Val &);
-
-void aliasing() {
- // The extra blank braces are left as a placeholder for after the variable
- // declaration is deleted.
- for (int i = 0; i < N; ++i) {
- Val &t = Arr[i]; { }
- int y = t.x;
- }
- // CHECK: for (auto & t : Arr)
- // CHECK-NEXT: { }
- // CHECK-NEXT: int y = t.x;
-
- for (int i = 0; i < N; ++i) {
- Val &t = Arr[i];
- int y = t.x;
- int z = Arr[i].x + t.x;
- }
- // CHECK: for (auto & [[VAR:[a-z_]+]] : Arr)
- // CHECK-NEXT: Val &t = [[VAR]];
- // CHECK-NEXT: int y = t.x;
- // CHECK-NEXT: int z = [[VAR]].x + t.x;
-
- for (int i = 0; i < N; ++i) {
- Val t = Arr[i];
- int y = t.x;
- int z = Arr[i].x + t.x;
- }
- // CHECK: for (auto & [[VAR:[a-z_]+]] : Arr)
- // CHECK-NEXT: Val t = [[VAR]];
- // CHECK-NEXT: int y = t.x;
- // CHECK-NEXT: int z = [[VAR]].x + t.x;
-
- for (int i = 0; i < N; ++i) {
- Val &t = func(Arr[i]);
- int y = t.x;
- }
- // CHECK: for (auto & [[VAR:[a-z_]+]] : Arr)
- // CHECK-NEXT: Val &t = func([[VAR]]);
- // CHECK-NEXT: int y = t.x;
-}
-
-void sameNames() {
- int num = 0;
- for (int i = 0; i < N; ++i) {
- printf("Fibonacci number is %d\n", nums[i]);
- sum += nums[i] + 2 + num;
- (void) nums[i];
- }
- // CHECK: int num = 0;
- // CHECK-NEXT: for (auto & [[VAR:[a-z_]+]] : nums)
- // CHECK-NEXT: printf("Fibonacci number is %d\n", [[VAR]]);
- // CHECK-NEXT: sum += [[VAR]] + 2 + num;
- // CHECK-NOT: (void) num;
- // CHECK: }
-}
Index: test/cpp11-migrate/LoopConvert/nesting.cpp
===================================================================
--- test/cpp11-migrate/LoopConvert/nesting.cpp
+++ test/cpp11-migrate/LoopConvert/nesting.cpp
@@ -16,10 +16,10 @@
int l = Arr[i].x + Arr[j].x;
}
}
- // CHECK: for (auto & [[VAR:[a-zA-Z_]+]] : Arr)
- // CHECK-NEXT: for (auto & [[INNERVAR:[a-zA-Z_]+]] : Arr)
- // CHECK-NEXT: int k = [[VAR]].x + [[INNERVAR]].x;
- // CHECK-NOT: int l = [[VAR]].x + [[VAR]].x;
+ // CHECK: for (auto & elem : Arr)
+ // CHECK-NEXT: for (auto & Arr_j : Arr)
+ // CHECK-NEXT: int k = elem.x + Arr_j.x;
+ // CHECK-NOT: int l = elem.x + elem.x;
Val Nest[N][M];
for (int i = 0; i < N; ++i) {
@@ -29,9 +29,9 @@
}
// The inner loop is also convertible, but doesn't need to be converted
// immediately. Update this test when that changes!
- // CHECK: for (auto & [[VAR:[a-zA-Z_]+]] : Nest)
+ // CHECK: for (auto & elem : Nest)
// CHECK-NEXT: for (int j = 0; j < M; ++j)
- // CHECK-NEXT: printf("Got item %d", [[VAR]][j].x);
+ // CHECK-NEXT: printf("Got item %d", elem[j].x);
// Note that the order of M and N are switched for this test.
for (int j = 0; j < M; ++j) {
@@ -41,17 +41,17 @@
}
// CHECK-NOT: for (auto & {{[a-zA-Z_]+}} : Nest[i])
// CHECK: for (int j = 0; j < M; ++j)
- // CHECK-NEXT: for (auto & [[VAR:[a-zA-Z_]+]] : Nest)
- // CHECK-NEXT: printf("Got item %d", [[VAR]][j].x);
+ // CHECK-NEXT: for (auto & elem : Nest)
+ // CHECK-NEXT: printf("Got item %d", elem[j].x);
Nested<T> NestT;
for (Nested<T>::iterator I = NestT.begin(), E = NestT.end(); I != E; ++I) {
for (T::iterator TI = (*I).begin(), TE = (*I).end(); TI != TE; ++TI) {
printf("%d", *TI);
}
}
// The inner loop is also convertible, but doesn't need to be converted
// immediately. Update this test when that changes!
- // CHECK: for (auto & [[VAR:[a-zA-Z_]+]] : NestT) {
- // CHECK-NEXT: for (T::iterator TI = ([[VAR]]).begin(), TE = ([[VAR]]).end(); TI != TE; ++TI) {
+ // CHECK: for (auto & elem : NestT) {
+ // CHECK-NEXT: for (T::iterator TI = (elem).begin(), TE = (elem).end(); TI != TE; ++TI) {
// CHECK-NEXT: printf("%d", *TI);
}
Index: test/cpp11-migrate/LoopConvert/pseudoarray.cpp
===================================================================
--- test/cpp11-migrate/LoopConvert/pseudoarray.cpp
+++ test/cpp11-migrate/LoopConvert/pseudoarray.cpp
@@ -15,25 +15,25 @@
printf("Fibonacci number is %d\n", v[i]);
sum += v[i] + 2;
}
- // CHECK: for (auto & [[VAR:[a-z_]+]] : v)
- // CHECK-NEXT: printf("Fibonacci number is %d\n", [[VAR]]);
- // CHECK-NEXT: sum += [[VAR]] + 2;
+ // CHECK: for (auto & elem : v)
+ // CHECK-NEXT: printf("Fibonacci number is %d\n", elem);
+ // CHECK-NEXT: sum += elem + 2;
for (int i = 0, e = v.size(); i < e; ++i) {
printf("Fibonacci number is %d\n", v.at(i));
sum += v.at(i) + 2;
}
- // CHECK: for (auto & [[VAR:[a-z_]+]] : v)
- // CHECK-NEXT: printf("Fibonacci number is %d\n", [[VAR]]);
- // CHECK-NEXT: sum += [[VAR]] + 2;
+ // CHECK: for (auto & elem : v)
+ // CHECK-NEXT: printf("Fibonacci number is %d\n", elem);
+ // CHECK-NEXT: sum += elem + 2;
for (int i = 0, e = pv->size(); i < e; ++i) {
printf("Fibonacci number is %d\n", pv->at(i));
sum += pv->at(i) + 2;
}
- // CHECK: for (auto & [[VAR:[a-z_]+]] : *pv)
- // CHECK-NEXT: printf("Fibonacci number is %d\n", [[VAR]]);
- // CHECK-NEXT: sum += [[VAR]] + 2;
+ // CHECK: for (auto & elem : *pv)
+ // CHECK-NEXT: printf("Fibonacci number is %d\n", elem);
+ // CHECK-NEXT: sum += elem + 2;
// This test will fail if size() isn't called repeatedly, since it
// returns unsigned int, and 0 is deduced to be signed int.
@@ -43,24 +43,24 @@
printf("Fibonacci number is %d\n", (*pv).at(i));
sum += (*pv)[i] + 2;
}
- // CHECK: for (auto & [[VAR:[a-z_]+]] : *pv)
- // CHECK-NEXT: printf("Fibonacci number is %d\n", [[VAR]]);
- // CHECK-NEXT: sum += [[VAR]] + 2;
+ // CHECK: for (auto & elem : *pv)
+ // CHECK-NEXT: printf("Fibonacci number is %d\n", elem);
+ // CHECK-NEXT: sum += elem + 2;
for (int i = 0; i < cv->size(); ++i) {
printf("Fibonacci number is %d\n", cv->at(i));
sum += cv->at(i) + 2;
}
- // CHECK: for (auto & [[VAR:[a-z_]+]] : *cv)
- // CHECK-NEXT: printf("Fibonacci number is %d\n", [[VAR]]);
- // CHECK-NEXT: sum += [[VAR]] + 2;
+ // CHECK: for (auto & elem : *cv)
+ // CHECK-NEXT: printf("Fibonacci number is %d\n", elem);
+ // CHECK-NEXT: sum += elem + 2;
}
// Check for loops that don't mention containers
void noContainer() {
for (auto i = 0; i < v.size(); ++i) { }
- // CHECK: for (auto & [[VAR:[a-z_]+]] : v) { }
+ // CHECK: for (auto & elem : v) { }
for (auto i = 0; i < v.size(); ++i) ;
- // CHECK: for (auto & [[VAR:[a-z_]+]] : v) ;
+ // CHECK: for (auto & elem : v) ;
}
Index: test/cpp11-migrate/LoopConvert/single-iterator.cpp
===================================================================
--- test/cpp11-migrate/LoopConvert/single-iterator.cpp
+++ test/cpp11-migrate/LoopConvert/single-iterator.cpp
@@ -12,104 +12,104 @@
MutableVal k = *i;
MutableVal j = *i;
}
- // CHECK: for ({{[a-zA-Z_ ]+&? ?}}[[VAR:[a-z_]+]] : exes[index].getS())
- // CHECK-NEXT: MutableVal k = [[VAR]];
- // CHECK-NEXT: MutableVal j = [[VAR]];
+ // CHECK: for (auto & elem : exes[index].getS())
+ // CHECK-NEXT: MutableVal k = elem;
+ // CHECK-NEXT: MutableVal j = elem;
}
void f() {
/// begin()/end() - based for loops here:
T t;
for (T::iterator it = t.begin(); it != t.end(); ++it) {
printf("I found %d\n", *it);
}
- // CHECK: for ({{[a-zA-Z_ ]+&? ?}}[[VAR:[a-z_]+]] : t)
- // CHECK-NEXT: printf("I found %d\n", [[VAR]]);
+ // CHECK: for (auto & elem : t)
+ // CHECK-NEXT: printf("I found %d\n", elem);
T *pt;
for (T::iterator it = pt->begin(); it != pt->end(); ++it) {
printf("I found %d\n", *it);
}
- // CHECK: for ({{[a-zA-Z_ ]+&? ?}}[[VAR:[a-z_]+]] : *pt)
- // CHECK-NEXT: printf("I found %d\n", [[VAR]]);
+ // CHECK: for (auto & elem : *pt)
+ // CHECK-NEXT: printf("I found %d\n", elem);
S s;
for (S::const_iterator it = s.begin(); it != s.end(); ++it) {
printf("s has value %d\n", (*it).x);
}
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : s)
- // CHECK-NEXT: printf("s has value %d\n", ([[VAR]]).x);
+ // CHECK: for (auto & elem : s)
+ // CHECK-NEXT: printf("s has value %d\n", (elem).x);
S *ps;
for (S::const_iterator it = ps->begin(); it != ps->end(); ++it) {
printf("s has value %d\n", (*it).x);
}
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : *ps)
- // CHECK-NEXT: printf("s has value %d\n", ([[VAR]]).x);
+ // CHECK: for (auto & p : *ps)
+ // CHECK-NEXT: printf("s has value %d\n", (p).x);
for (S::const_iterator it = s.begin(); it != s.end(); ++it) {
printf("s has value %d\n", it->x);
}
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : s)
- // CHECK-NEXT: printf("s has value %d\n", [[VAR]].x);
+ // CHECK: for (auto & elem : s)
+ // CHECK-NEXT: printf("s has value %d\n", elem.x);
for (S::iterator it = s.begin(); it != s.end(); ++it) {
it->x = 3;
}
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : s)
- // CHECK-NEXT: [[VAR]].x = 3;
+ // CHECK: for (auto & elem : s)
+ // CHECK-NEXT: elem.x = 3;
for (S::iterator it = s.begin(); it != s.end(); ++it) {
(*it).x = 3;
}
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : s)
- // CHECK-NEXT: ([[VAR]]).x = 3;
+ // CHECK: for (auto & elem : s)
+ // CHECK-NEXT: (elem).x = 3;
for (S::iterator it = s.begin(); it != s.end(); ++it) {
it->nonConstFun(4, 5);
}
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : s)
- // CHECK-NEXT: [[VAR]].nonConstFun(4, 5);
+ // CHECK: for (auto & elem : s)
+ // CHECK-NEXT: elem.nonConstFun(4, 5);
U u;
for (U::iterator it = u.begin(); it != u.end(); ++it) {
printf("s has value %d\n", it->x);
}
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : u)
- // CHECK-NEXT: printf("s has value %d\n", [[VAR]].x);
+ // CHECK: for (auto & elem : u)
+ // CHECK-NEXT: printf("s has value %d\n", elem.x);
for (U::iterator it = u.begin(); it != u.end(); ++it) {
printf("s has value %d\n", (*it).x);
}
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : u)
- // CHECK-NEXT: printf("s has value %d\n", ([[VAR]]).x);
+ // CHECK: for (auto & elem : u)
+ // CHECK-NEXT: printf("s has value %d\n", (elem).x);
U::iterator A;
for (U::iterator i = u.begin(); i != u.end(); ++i)
int k = A->x + i->x;
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : u)
- // CHECK-NEXT: int k = A->x + [[VAR]].x;
+ // CHECK: for (auto & elem : u)
+ // CHECK-NEXT: int k = A->x + elem.x;
dependent<int> v;
for (dependent<int>::const_iterator it = v.begin();
it != v.end(); ++it) {
printf("Fibonacci number is %d\n", *it);
}
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : v)
- // CHECK-NEXT: printf("Fibonacci number is %d\n", [[VAR]]);
+ // CHECK: for (auto & elem : v)
+ // CHECK-NEXT: printf("Fibonacci number is %d\n", elem);
for (dependent<int>::const_iterator it(v.begin());
it != v.end(); ++it) {
printf("Fibonacci number is %d\n", *it);
}
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : v)
- // CHECK-NEXT: printf("Fibonacci number is %d\n", [[VAR]]);
+ // CHECK: for (auto & elem : v)
+ // CHECK-NEXT: printf("Fibonacci number is %d\n", elem);
doublyDependent<int,int> intmap;
for (doublyDependent<int,int>::iterator it = intmap.begin();
it != intmap.end(); ++it) {
printf("intmap[%d] = %d", it->first, it->second);
}
- // CHECK: for ({{[a-zA-Z_ ]*&? ?}}[[VAR:[a-z_]+]] : intmap)
- // CHECK-NEXT: printf("intmap[%d] = %d", [[VAR]].first, [[VAR]].second);
+ // CHECK: for (auto & elem : intmap)
+ // CHECK-NEXT: printf("intmap[%d] = %d", elem.first, elem.second);
}
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits