This is an archive of the discontinued LLVM Phabricator instance.

clang-tidy modernize-loop-convert: preserve type of alias declaration (bug 28341)
ClosedPublic

Authored by mgehre on Jul 6 2016, 2:04 PM.

Details

Summary

Previously, the added test failed with the following 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)

Diff Detail

Repository
rL LLVM

Event Timeline

mgehre updated this revision to Diff 62960.Jul 6 2016, 2:04 PM
mgehre retitled this revision from to clang-tidy modernize-loop-convert: preserve type of alias declaration (bug 28341).
mgehre updated this object.
mgehre added reviewers: alexfh, klimek.
mgehre added a subscriber: cfe-commits.
mgehre updated this object.Jul 6 2016, 2:06 PM
mgehre updated this object.
alexfh edited edge metadata.Jul 6 2016, 2:26 PM

Thank you for fixing the bug! One comment below.

clang-tidy/modernize/LoopConvertCheck.cpp
525 ↗(On Diff #62960)
  1. Can the AliasVar->getType().isNull() condition be true?
  2. If it can, consider !Descriptor.ElemType.isNull().isNull() and AliasVar->getType().isNull(). In this case setting Descriptor.ElemType to AliasVar->getType() (which is null) doesn't make much sense.

I'd probably just wrap the added code in if (!AliasVar->getType().isNull()).

mgehre added inline comments.Jul 7 2016, 1:18 AM
clang-tidy/modernize/LoopConvertCheck.cpp
525 ↗(On Diff #62960)

Thanks for you fast review.

I copied the block from isAliasDecl(). I don't see any reason why the types can be Null, but I'm also not an expert in llvm.

When would a VarDecl have no type? Maybe I should put an assert instead?

alexfh requested changes to this revision.Jul 7 2016, 5:39 AM
alexfh edited edge metadata.
alexfh added inline comments.
clang-tidy/modernize/LoopConvertCheck.cpp
525 ↗(On Diff #62960)

You can try to add an assert and run the check on llvm, for example.

Alternatively, wrap the added code in if (!AliasVar->getType().isNull())

This revision now requires changes to proceed.Jul 7 2016, 5:39 AM
mgehre updated this revision to Diff 63400.Jul 9 2016, 2:38 PM
mgehre edited edge metadata.

Added assert.
Runs on llvm code-base without triggering the assert.

I additionally have run the check on blender and ITK and the assert did not fire.

Friendly ping

alexfh accepted this revision.Jul 19 2016, 6:06 AM
alexfh edited edge metadata.

LG

test/clang-tidy/modernize-loop-convert-extra.cpp
1070 ↗(On Diff #63400)

I'd just CHECK-FIXES-NEXT: if (value > 127)

This revision is now accepted and ready to land.Jul 19 2016, 6:06 AM
This revision was automatically updated to reflect the committed changes.