summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-tidy
diff options
context:
space:
mode:
Diffstat (limited to 'clang-tools-extra/clang-tidy')
-rw-r--r--clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp14
-rw-r--r--clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.h3
-rw-r--r--clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.cpp75
-rw-r--r--clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.h16
-rw-r--r--clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp1
5 files changed, 85 insertions, 24 deletions
diff --git a/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp b/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp
index a476f6c92c1..b233196a972 100644
--- a/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.cpp
@@ -410,11 +410,21 @@ LoopConvertCheck::LoopConvertCheck(StringRef Name, ClangTidyContext *Context)
Options.get("MinConfidence", "reasonable"))
.Case("safe", Confidence::CL_Safe)
.Case("risky", Confidence::CL_Risky)
- .Default(Confidence::CL_Reasonable)) {}
+ .Default(Confidence::CL_Reasonable)),
+ NamingStyle(StringSwitch<VariableNamer::NamingStyle>(
+ Options.get("NamingStyle", "CamelCase"))
+ .Case("camelBack", VariableNamer::NS_CamelBack)
+ .Case("lower_case", VariableNamer::NS_LowerCase)
+ .Case("UPPER_CASE", VariableNamer::NS_UpperCase)
+ .Default(VariableNamer::NS_CamelCase)) {}
void LoopConvertCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
SmallVector<std::string, 3> Confs{"risky", "reasonable", "safe"};
Options.store(Opts, "MinConfidence", Confs[static_cast<int>(MinConfidence)]);
+
+ SmallVector<std::string, 4> Styles{"camelBack", "CamelCase", "lower_case",
+ "UPPER_CASE"};
+ Options.store(Opts, "NamingStyle", Styles[static_cast<int>(NamingStyle)]);
}
void LoopConvertCheck::registerMatchers(MatchFinder *Finder) {
@@ -466,7 +476,7 @@ void LoopConvertCheck::doConversion(
} else {
VariableNamer Namer(&TUInfo->getGeneratedDecls(),
&TUInfo->getParentFinder().getStmtToParentStmtMap(),
- Loop, IndexVar, MaybeContainer, Context);
+ Loop, IndexVar, MaybeContainer, Context, NamingStyle);
VarName = Namer.createIndexName();
// First, replace all usages of the array subscript expression with our new
// variable.
diff --git a/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.h b/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.h
index 41271696f04..f2bcf59691c 100644
--- a/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/LoopConvertCheck.h
@@ -64,7 +64,8 @@ private:
const ForStmt *Loop, LoopFixerKind FixerKind);
std::unique_ptr<TUTrackingInfo> TUInfo;
- Confidence::Level MinConfidence;
+ const Confidence::Level MinConfidence;
+ const VariableNamer::NamingStyle NamingStyle;
};
} // namespace modernize
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
diff --git a/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.h b/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.h
index 5290b4752ef..01f85099a3a 100644
--- a/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.h
+++ b/clang-tools-extra/clang-tidy/modernize/LoopConvertUtils.h
@@ -414,14 +414,22 @@ private:
/// index, if they exist.
class VariableNamer {
public:
+ // Supported naming styles.
+ enum NamingStyle {
+ NS_CamelBack,
+ NS_CamelCase,
+ NS_LowerCase,
+ NS_UpperCase,
+ };
+
VariableNamer(StmtGeneratedVarNameMap *GeneratedDecls,
const StmtParentMap *ReverseAST, const clang::Stmt *SourceStmt,
const clang::VarDecl *OldIndex,
const clang::VarDecl *TheContainer,
- const clang::ASTContext *Context)
+ const clang::ASTContext *Context, NamingStyle Style)
: GeneratedDecls(GeneratedDecls), ReverseAST(ReverseAST),
SourceStmt(SourceStmt), OldIndex(OldIndex), TheContainer(TheContainer),
- Context(Context) {}
+ Context(Context), Style(Style) {}
/// \brief Generate a new index name.
///
@@ -437,10 +445,14 @@ private:
const clang::VarDecl *OldIndex;
const clang::VarDecl *TheContainer;
const clang::ASTContext *Context;
+ const NamingStyle Style;
// 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(llvm::StringRef Symbol);
+
+ // Concatenates two identifiers following the current naming style.
+ std::string AppendWithStyle(StringRef Str, StringRef Suffix) const;
};
} // namespace modernize
diff --git a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
index ff0666f8e23..568abbdac4b 100644
--- a/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/ModernizeTidyModule.cpp
@@ -41,6 +41,7 @@ public:
ClangTidyOptions Options;
auto &Opts = Options.CheckOptions;
Opts["modernize-loop-convert.MinConfidence"] = "reasonable";
+ Opts["modernize-loop-convert.NamingStyle"] = "CamelCase";
Opts["modernize-pass-by-value.IncludeStyle"] = "llvm"; // Also: "google".
Opts["modernize-replace-auto-ptr.IncludeStyle"] = "llvm"; // Also: "google".
OpenPOWER on IntegriCloud