1 #include "VariableNaming.h"
4 namespace loop_migrate {
6 std::string VariableNamer::createIndexName() {
7 // FIXME: Add in naming conventions to handle:
8 // - Uppercase/lowercase indices
9 // - How to handle conflicts
10 // - An interactive process for naming
11 std::string IteratorName;
12 std::string ContainerName;
14 ContainerName = TheContainer->getName().str();
16 size_t Len = ContainerName.length();
17 if (Len > 1 && ContainerName[Len - 1] == 's')
18 IteratorName = ContainerName.substr(0, Len - 1);
20 IteratorName = "elem";
22 if (!declarationExists(IteratorName))
25 IteratorName = ContainerName + "_" + OldIndex->getName().str();
26 if (!declarationExists(IteratorName))
29 IteratorName = ContainerName + "_elem";
30 if (!declarationExists(IteratorName))
33 IteratorName += "_elem";
34 if (!declarationExists(IteratorName))
37 IteratorName = "_elem_";
39 // Someone defeated my naming scheme...
40 while (declarationExists(IteratorName))
45 /// \brief Determines whether or not the the name Symbol exists in LoopContext,
46 /// any of its parent contexts, or any of its child statements.
48 /// We also check to see if the same identifier was generated by this loop
49 /// converter in a loop nested within SourceStmt.
50 bool VariableNamer::declarationExists(const StringRef Symbol) {
51 // Determine if the symbol was generated in a parent context.
52 for (const Stmt *S = SourceStmt; S != NULL; S = ReverseAST->lookup(S)) {
53 StmtGeneratedVarNameMap::const_iterator I = GeneratedDecls->find(S);
54 if (I != GeneratedDecls->end() && I->second == Symbol)
58 // FIXME: Rather than detecting conflicts at their usages, we should check the
60 // For some reason, lookup() always returns the pair (NULL, NULL) because its
61 // StoredDeclsMap is not initialized (i.e. LookupPtr.getInt() is false inside
62 // of DeclContext::lookup()). Why is this?
64 // Finally, determine if the symbol was used in the loop or a child context.
65 DeclFinderASTVisitor DeclFinder(Symbol, GeneratedDecls);
66 return DeclFinder.findUsages(SourceStmt);
69 } // namespace loop_migrate