[clang-tools-extra] r276111 - clang-tidy modernize-loop-convert: preserve type of alias declaration (bug 28341)
Matthias Gehre via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 20 05:32:06 PDT 2016
Author: mgehre
Date: Wed Jul 20 07:32:06 2016
New Revision: 276111
URL: http://llvm.org/viewvc/llvm-project?rev=276111&view=rev
Log:
clang-tidy modernize-loop-convert: preserve type of alias declaration (bug 28341)
Summary:
Previoly, the added test failed with the fillowing fixit:
char v[5];
- for(size_t i = 0; i < 5; ++i)
+ for(char value : v)
{
- unsigned char value = v[i];
if (value > 127)
i.e. the variable 'value' changes from unsigned char to signed char. And
thus the following 'if' does not work anymore.
With this commit, the fixit is changed to:
char v[5];
- for(size_t i = 0; i < 5; ++i)
+ for(unsigned char value : v)
{
- unsigned char value = v[i];
if (value > 127)
Reviewers: alexfh, klimek
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D22069
Modified:
clang-tools-extra/trunk/clang-tidy/modernize/LoopConvertCheck.cpp
clang-tools-extra/trunk/test/clang-tidy/modernize-loop-convert-extra.cpp
Modified: clang-tools-extra/trunk/clang-tidy/modernize/LoopConvertCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/LoopConvertCheck.cpp?rev=276111&r1=276110&r2=276111&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/LoopConvertCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/LoopConvertCheck.cpp Wed Jul 20 07:32:06 2016
@@ -517,7 +517,17 @@ void LoopConvertCheck::doConversion(
if (VarNameFromAlias) {
const auto *AliasVar = cast<VarDecl>(AliasDecl->getSingleDecl());
VarName = AliasVar->getName().str();
- AliasVarIsRef = AliasVar->getType()->isReferenceType();
+
+ // Use the type of the alias if it's not the same
+ QualType AliasVarType = AliasVar->getType();
+ assert(!AliasVarType.isNull() && "Type in VarDecl is null");
+ if (AliasVarType->isReferenceType()) {
+ AliasVarType = AliasVarType.getNonReferenceType();
+ AliasVarIsRef = true;
+ }
+ if (Descriptor.ElemType.isNull() ||
+ !Context->hasSameUnqualifiedType(AliasVarType, Descriptor.ElemType))
+ Descriptor.ElemType = AliasVarType;
// We keep along the entire DeclStmt to keep the correct range here.
SourceRange ReplaceRange = AliasDecl->getSourceRange();
Modified: clang-tools-extra/trunk/test/clang-tidy/modernize-loop-convert-extra.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-loop-convert-extra.cpp?rev=276111&r1=276110&r2=276111&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-loop-convert-extra.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-loop-convert-extra.cpp Wed Jul 20 07:32:06 2016
@@ -1060,3 +1060,15 @@ void f() {
}
} // namespace InitLists
+
+void bug28341() {
+ char v[5];
+ for(int i = 0; i < 5; ++i) {
+ unsigned char value = v[i];
+ if (value > 127)
+ ;
+ // CHECK-MESSAGES: :[[@LINE-4]]:3: warning: use range-based for loop instead
+ // CHECK-FIXES: for(unsigned char value : v)
+ // CHECK-FIXES-NEXT: if (value > 127)
+ }
+}
More information about the cfe-commits
mailing list