summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp
diff options
context:
space:
mode:
authorAngel Garcia Gomez <angelgarcia@google.com>2015-09-24 17:02:19 +0000
committerAngel Garcia Gomez <angelgarcia@google.com>2015-09-24 17:02:19 +0000
commit8535c6c278b3c54bf6dfa8afbf9d3c0ff63f5bec (patch)
tree70ace4add2407f4cbab66b672b96fd90d1e7cee1 /clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp
parent0da2e9334551dcccfe40786769fbb7e6f52f6da3 (diff)
downloadbcm5719-llvm-8535c6c278b3c54bf6dfa8afbf9d3c0ff63f5bec.tar.gz
bcm5719-llvm-8535c6c278b3c54bf6dfa8afbf9d3c0ff63f5bec.zip
Add NamingStyle option to modernize-loop-convert.
Summary: Add an option to specify wich style must be followed when choosing the new index name. Reviewers: alexfh Subscribers: cfe-commits, klimek Differential Revision: http://reviews.llvm.org/D13052 llvm-svn: 248517
Diffstat (limited to 'clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp')
-rw-r--r--clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp75
1 files changed, 56 insertions, 19 deletions
diff --git a/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp b/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp
index d12d845855e..70af93d878c 100644
--- a/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp
@@ -809,40 +809,63 @@ bool ForLoopIndexUseVisitor::TraverseStmt(Stmt *S) {
std::string VariableNamer::createIndexName() {
// FIXME: Add in naming conventions to handle:
- // - Uppercase/lowercase indices.
// - How to handle conflicts.
// - An interactive process for naming.
std::string IteratorName;
- std::string ContainerName;
+ StringRef ContainerName;
if (TheContainer)
- ContainerName = TheContainer->getName().str();
+ ContainerName = TheContainer->getName();
- size_t Len = ContainerName.length();
- if (Len > 1 && ContainerName[Len - 1] == 's')
+ size_t Len = ContainerName.size();
+ if (Len > 1 && ContainerName.endswith(Style == NS_UpperCase ? "S" : "s")) {
IteratorName = ContainerName.substr(0, Len - 1);
- else
- IteratorName = "elem";
-
- if (!declarationExists(IteratorName))
- return IteratorName;
+ if (!declarationExists(IteratorName))
+ return IteratorName;
+ }
- IteratorName = ContainerName + "_" + OldIndex->getName().str();
- if (!declarationExists(IteratorName))
- return IteratorName;
+ std::string Elem;
+ switch (Style) {
+ case NS_CamelBack:
+ case NS_LowerCase:
+ Elem = "elem";
+ break;
+ case NS_CamelCase:
+ Elem = "Elem";
+ break;
+ case NS_UpperCase:
+ Elem = "ELEM";
+ }
+ if (!declarationExists(Elem))
+ return Elem;
- IteratorName = ContainerName + "_elem";
+ IteratorName = AppendWithStyle(ContainerName, OldIndex->getName());
if (!declarationExists(IteratorName))
return IteratorName;
- IteratorName += "_elem";
+ IteratorName = AppendWithStyle(ContainerName, Elem);
if (!declarationExists(IteratorName))
return IteratorName;
- IteratorName = "_elem_";
-
// Someone defeated my naming scheme...
- while (declarationExists(IteratorName))
- IteratorName += "i";
+ std::string GiveMeName;
+ switch (Style) {
+ case NS_CamelBack:
+ GiveMeName = "giveMeName";
+ break;
+ case NS_CamelCase:
+ GiveMeName = "GiveMeName";
+ break;
+ case NS_LowerCase:
+ GiveMeName = "give_me_name_";
+ break;
+ case NS_UpperCase:
+ GiveMeName = "GIVE_ME_NAME_";
+ }
+ int Attempt = 0;
+ do {
+ IteratorName = GiveMeName + std::to_string(Attempt++);
+ } while (declarationExists(IteratorName));
+
return IteratorName;
}
@@ -882,6 +905,20 @@ bool VariableNamer::declarationExists(StringRef Symbol) {
return DeclFinder.findUsages(SourceStmt);
}
+std::string VariableNamer::AppendWithStyle(StringRef Str,
+ StringRef Suffix) const {
+ std::string Name = Str;
+ if (!Suffix.empty()) {
+ if (Style == NS_LowerCase || Style == NS_UpperCase)
+ Name += "_";
+ int SuffixStart = Name.size();
+ Name += Suffix;
+ if (Style == NS_CamelBack)
+ Name[SuffixStart] = toupper(Name[SuffixStart]);
+ }
+ return Name;
+}
+
} // namespace modernize
} // namespace tidy
} // namespace clang
OpenPOWER on IntegriCloud