[PATCH] Add -Wrange-loop-analysis to warn when a range-based for-loop is creating a copy.

David Blaikie dblaikie at gmail.com
Wed Mar 4 13:57:47 PST 2015


On Fri, Feb 27, 2015 at 6:03 PM, Richard Trieu <rtrieu at google.com> wrote:

> Results from running the warning on LLVM projects.  The actual errors are
> at the end.  In total, this warning was triggered 22 times.  The breakdown
> is:
>
> 7 - for (const Foo &x : Foos), where the range Foos only return a copy.
> Suggest using the non-reference type so the copy is obvious.
> 5 - for (const Foo x : Foos), where the range Foos does return a
> reference, but is copied into x. Suggest using the reference type to
> prevent a copy from being made.
> 10 - for (const Bar &x : Foos), where Bar is constructed from Foo. Suggest
> using the non-reference "const Bar" to indicate a copy is intended to be
> made, or "const Foo &" to prevent a copy from being made.
>

Cool - want to just go ahead with that data then iterate on more data (eg:
google-wide, or other large codebases) for improvements?


>
> llvm/lib/Analysis/RegionPass.cpp:197:22: error: loop variable 'BB' is
> always a copy because the range of type
> 'llvm::iterator_range<llvm::RegionBase<llvm::RegionTraits<llvm::Function>
> >::block_iterator_wrapper<false> >' does not return a reference
> llvm/lib/Analysis/RegionPass.cpp:197:10: note: use non-reference type
> 'llvm::BasicBlock *const'
> llvm/lib/Analysis/RegionPrinter.cpp:126:22: error: loop variable 'BB' is
> always a copy because the range of type
> 'llvm::iterator_range<llvm::RegionBase<llvm::RegionTraits<llvm::Function>
> >::block_iterator_wrapper<true> >' does not return a reference
> llvm/lib/Analysis/RegionPrinter.cpp:126:10: note: use non-reference type
> 'llvm::BasicBlock *const'
> llvm/lib/MC/MCDwarf.cpp:814:19: error: loop variable 'sec' of type 'const
> std::pair<const llvm::MCSection *, std::pair<llvm::MCSymbol *,
> llvm::MCSymbol *> >' creates a copy from type 'const std::pair<const
> llvm::MCSection *, std::pair<llvm::MCSymbol *, llvm::MCSymbol *> >'
> llvm/lib/MC/MCDwarf.cpp:814:8: note: use reference type 'const
> std::pair<const llvm::MCSection *, std::pair<llvm::MCSymbol *,
> llvm::MCSymbol *> > &' to prevent copying
> llvm/lib/Object/COFFObjectFile.cpp:263:31: error: loop variable 'SymbI'
> has type 'const llvm::object::symbol_iterator &' but is initialized with
> type 'const llvm::object::SymbolRef' resulting in a copy
> llvm/lib/Object/COFFObjectFile.cpp:263:8: note: use non-reference type
> 'const llvm::object::symbol_iterator' to keep the copy or type 'const
> llvm::object::SymbolRef &' to prevent copying
> llvm/lib/Target/AArch64/AArch64CollectLOH.cpp:331:25: error: loop variable
> 'Entry' of type 'const llvm::detail::DenseMapPair<unsigned int, unsigned
> int>' creates a copy from type 'const llvm::detail::DenseMapPair<unsigned
> int, unsigned int>'
> llvm/lib/Target/AArch64/AArch64CollectLOH.cpp:331:14: note: use reference
> type 'const llvm::detail::DenseMapPair<unsigned int, unsigned int> &' to
> prevent copying
> llvm/lib/Transforms/Scalar/StructurizeCFG.cpp:889:20: error: loop variable
> 'BB' is always a copy because the range of type
> 'llvm::iterator_range<llvm::RegionBase<llvm::RegionTraits<llvm::Function>
> >::block_iterator_wrapper<false> >' does not return a reference
> llvm/lib/Transforms/Scalar/StructurizeCFG.cpp:889:8: note: use
> non-reference type 'llvm::BasicBlock *const'
> llvm/tools/clang/lib/AST/MicrosoftMangle.cpp:1614:25: error: loop variable
> 'Arg' of type 'const clang::QualType' creates a copy from type 'const
> clang::QualType'
> llvm/tools/clang/lib/AST/MicrosoftMangle.cpp:1614:10: note: use reference
> type 'const clang::QualType &' to prevent copying
> llvm/tools/clang/lib/CodeGen/CodeGenModule.cpp:3439:19: error: loop
> variable 'I' of type 'const llvm::detail::DenseMapPair<const clang::Decl *,
> bool>' creates a copy from type 'const llvm::detail::DenseMapPair<const
> clang::Decl *, bool>'
> llvm/tools/clang/lib/CodeGen/CodeGenModule.cpp:3439:8: note: use reference
> type 'const llvm::detail::DenseMapPair<const clang::Decl *, bool> &' to
> prevent copying
> llvm/tools/clang/lib/CodeGen/CoverageMappingGen.cpp:945:22: error: loop
> variable 'Arg' is always a copy because the range of type
> 'llvm::iterator_range<clang::ConstExprIterator>' does not return a reference
> llvm/tools/clang/lib/CodeGen/CoverageMappingGen.cpp:945:10: note: use
> non-reference type 'const clang::Expr *const'
> llvm/tools/clang/lib/Sema/SemaLookup.cpp:3026:20: error: loop variable 'R'
> is always a copy because the range of type
> 'llvm::iterator_range<clang::DeclContext::all_lookups_iterator>' does not
> return a reference
> llvm/tools/clang/lib/Sema/SemaLookup.cpp:3026:8: note: use non-reference
> type 'const llvm::MutableArrayRef<clang::NamedDecl *>'
> llvm/tools/clang/lib/Sema/SemaTemplateInstantiate.cpp:1776:19: error: loop
> variable 'Base' of type 'const clang::CXXBaseSpecifier' creates a copy from
> type 'const clang::CXXBaseSpecifier'
> llvm/tools/clang/lib/Sema/SemaTemplateInstantiate.cpp:1776:8: note: use
> reference type 'const clang::CXXBaseSpecifier &' to prevent copying
> llvm/tools/clang/tools/extra/clang-rename/USRFindingAction.cpp:49:20:
> error: loop variable 'CtorDecl' is always a copy because the range of type
> 'llvm::iterator_range<clang::DeclContext::specific_decl_iterator<clang::CXXConstructorDecl>
> >' does not return a reference
> llvm/tools/clang/tools/extra/clang-rename/USRFindingAction.cpp:49:8: note:
> use non-reference type 'clang::CXXConstructorDecl *const'
> llvm/tools/llvm-vtabledump/llvm-vtabledump.cpp:299:68: error: loop
> variable 'VFTableEntry' has type 'const std::pair<std::pair<StringRef,
> uint64_t>, StringRef> &' but is initialized with type 'std::pair<const
> std::pair<llvm::StringRef, unsigned long>, llvm::StringRef>' resulting in a
> copy
> llvm/tools/llvm-vtabledump/llvm-vtabledump.cpp:299:8: note: use
> non-reference type 'const std::pair<std::pair<StringRef, uint64_t>,
> StringRef>' to keep the copy or type 'const std::pair<const
> std::pair<llvm::StringRef, unsigned long>, llvm::StringRef> &' to prevent
> copying
> llvm/tools/llvm-vtabledump/llvm-vtabledump.cpp:306:58: error: loop
> variable 'VBTable' has type 'const std::pair<StringRef,
> ArrayRef<little32_t> > &' but is initialized with type 'std::pair<const
> llvm::StringRef,
> llvm::ArrayRef<llvm::support::detail::packed_endian_specific_integral<int,
> llvm::support::endianness::little, 1> > >' resulting in a copy
> llvm/tools/llvm-vtabledump/llvm-vtabledump.cpp:306:8: note: use
> non-reference type 'const std::pair<StringRef, ArrayRef<little32_t> >' to
> keep the copy or type 'const std::pair<const llvm::StringRef,
> llvm::ArrayRef<llvm::support::detail::packed_endian_specific_integral<int,
> llvm::support::endianness::little, 1> > > &' to prevent copying
> llvm/tools/llvm-vtabledump/llvm-vtabledump.cpp:314:59: error: loop
> variable 'COLPair' has type 'const std::pair<StringRef,
> CompleteObjectLocator> &' but is initialized with type 'std::pair<const
> llvm::StringRef, CompleteObjectLocator>' resulting in a copy
> llvm/tools/llvm-vtabledump/llvm-vtabledump.cpp:314:8: note: use
> non-reference type 'const std::pair<StringRef, CompleteObjectLocator>' to
> keep the copy or type 'const std::pair<const llvm::StringRef,
> CompleteObjectLocator> &' to prevent copying
> llvm/tools/llvm-vtabledump/llvm-vtabledump.cpp:323:62: error: loop
> variable 'CHDPair' has type 'const std::pair<StringRef,
> ClassHierarchyDescriptor> &' but is initialized with type 'std::pair<const
> llvm::StringRef, ClassHierarchyDescriptor>' resulting in a copy
> llvm/tools/llvm-vtabledump/llvm-vtabledump.cpp:323:8: note: use
> non-reference type 'const std::pair<StringRef, ClassHierarchyDescriptor>'
> to keep the copy or type 'const std::pair<const llvm::StringRef,
> ClassHierarchyDescriptor> &' to prevent copying
> llvm/tools/llvm-vtabledump/llvm-vtabledump.cpp:331:68: error: loop
> variable 'BCAEntry' has type 'const std::pair<std::pair<StringRef,
> uint64_t>, StringRef> &' but is initialized with type 'std::pair<const
> std::pair<llvm::StringRef, unsigned long>, llvm::StringRef>' resulting in a
> copy
> llvm/tools/llvm-vtabledump/llvm-vtabledump.cpp:331:8: note: use
> non-reference type 'const std::pair<std::pair<StringRef, uint64_t>,
> StringRef>' to keep the copy or type 'const std::pair<const
> std::pair<llvm::StringRef, unsigned long>, llvm::StringRef> &' to prevent
> copying
> llvm/tools/llvm-vtabledump/llvm-vtabledump.cpp:338:57: error: loop
> variable 'BCDPair' has type 'const std::pair<StringRef,
> BaseClassDescriptor> &' but is initialized with type 'std::pair<const
> llvm::StringRef, BaseClassDescriptor>' resulting in a copy
> llvm/tools/llvm-vtabledump/llvm-vtabledump.cpp:338:8: note: use
> non-reference type 'const std::pair<StringRef, BaseClassDescriptor>' to
> keep the copy or type 'const std::pair<const llvm::StringRef,
> BaseClassDescriptor> &' to prevent copying
> llvm/tools/llvm-vtabledump/llvm-vtabledump.cpp:349:52: error: loop
> variable 'TDPair' has type 'const std::pair<StringRef, TypeDescriptor> &'
> but is initialized with type 'std::pair<const llvm::StringRef,
> TypeDescriptor>' resulting in a copy
> llvm/tools/llvm-vtabledump/llvm-vtabledump.cpp:349:8: note: use
> non-reference type 'const std::pair<StringRef, TypeDescriptor>' to keep the
> copy or type 'const std::pair<const llvm::StringRef, TypeDescriptor> &' to
> prevent copying
> llvm/tools/llvm-vtabledump/llvm-vtabledump.cpp:359:68: error: loop
> variable 'VTTPair' has type 'const std::pair<std::pair<StringRef,
> uint64_t>, StringRef> &' but is initialized with type 'std::pair<const
> std::pair<llvm::StringRef, unsigned long>, llvm::StringRef>' resulting in a
> copy
> llvm/tools/llvm-vtabledump/llvm-vtabledump.cpp:359:8: note: use
> non-reference type 'const std::pair<std::pair<StringRef, uint64_t>,
> StringRef>' to keep the copy or type 'const std::pair<const
> std::pair<llvm::StringRef, unsigned long>, llvm::StringRef> &' to prevent
> copying
> llvm/tools/llvm-vtabledump/llvm-vtabledump.cpp:366:47: error: loop
> variable 'TIPair' has type 'const std::pair<StringRef, StringRef> &' but is
> initialized with type 'std::pair<const llvm::StringRef, llvm::StringRef>'
> resulting in a copy
> llvm/tools/llvm-vtabledump/llvm-vtabledump.cpp:366:8: note: use
> non-reference type 'const std::pair<StringRef, StringRef>' to keep the copy
> or type 'const std::pair<const llvm::StringRef, llvm::StringRef> &' to
> prevent copying
> llvm/utils/TableGen/CodeGenRegisters.cpp:1773:24: error: loop variable
> 'SUI' is always a copy because the range of type 'const RegUnitList' (aka
> 'const SparseBitVector<>') does not return a reference
> llvm/utils/TableGen/CodeGenRegisters.cpp:1773:12: note: use non-reference
> type 'const unsigned int'
>
>
> http://reviews.llvm.org/D4169
>
> EMAIL PREFERENCES
>   http://reviews.llvm.org/settings/panel/emailpreferences/
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150304/aae0e4b7/attachment.html>


More information about the cfe-commits mailing list