This is an archive of the discontinued LLVM Phabricator instance.

[NFC][clang] Fix Coverity bugs with AUTO_CAUSES_COPY
ClosedPublic

Authored by Manna on Apr 24 2023, 9:39 AM.

Details

Summary

Reported by Coverity:
AUTO_CAUSES_COPY
Unnecessary object copies can affect performance.

  1. Inside "ExtractAPIVisitor.h" file, in clang::​extractapi::​impl::​ExtractAPIVisitorBase<<unnamed>::​BatchExtractAPIVisitor>::​VisitFunctionDecl(clang::​FunctionDecl const *): Using the auto keyword without an & causes the copy of an object of type DynTypedNode.
  1. Inside "NeonEmitter.cpp" file, in <unnamed>::​Intrinsic::​Intrinsic(llvm::​Record *, llvm::​StringRef, llvm::​StringRef, <unnamed>::​TypeSpec, <unnamed>::​TypeSpec, <unnamed>::​ClassKind, llvm::​ListInit *, <unnamed>::​NeonEmitter &, llvm::​StringRef, llvm::​StringRef, bool, bool): Using the auto keyword without an & causes the copy of an object of type Type.
  1. Inside "MicrosoftCXXABI.cpp" file, in <unnamed>::​MSRTTIBuilder::​getClassHierarchyDescriptor(): Using the auto keyword without an & causes the copy of an object of type MSRTTIClass.
  1. Inside "CGGPUBuiltin.cpp" file, in clang::​CodeGen::​CodeGenFunction::​EmitAMDGPUDevicePrintfCallExpr(clang::​CallExpr const *): Using the auto keyword without an & causes the copy of an object of type CallArg.
  1. Inside "SemaDeclAttr.cpp" file, in threadSafetyCheckIsSmartPointer(clang::​Sema &, clang::​RecordType const *): Using the auto keyword without an & causes the copy of an object of type CXXBaseSpecifier.
  1. Inside "ComputeDependence.cpp" file, in clang::​computeDependence(clang::​DesignatedInitExpr *): Using the auto keyword without an & causes the copy of an object of type Designator.
  1. Inside "Format.cpp" file, In clang::​format::​affectsRange(llvm::​ArrayRef<clang::​tooling::​Range>, unsigned int, unsigned int): Using the auto keyword without an & causes the copy of an object of type Range.

Diff Detail

Event Timeline

Manna created this revision.Apr 24 2023, 9:39 AM
Herald added a reviewer: ributzka. · View Herald Transcript
Herald added projects: Restricted Project, Restricted Project, Restricted Project. · View Herald Transcript
Herald added subscribers: kosarev, tpr. · View Herald Transcript
Manna requested review of this revision.Apr 24 2023, 9:39 AM
Manna updated this revision to Diff 516445.Apr 24 2023, 9:52 AM
Manna edited the summary of this revision. (Show Details)

I added a comment to skip the one where the element type is a simple pair. The rest look fine to me.

clang/include/clang/ExtractAPI/ExtractAPIVisitor.h
182

I think this is probably a good change. getParents() returns a DynTypedNodeList (clang/include/clang/AST/ParentMapContext.h) that uses DynTypedNode as its element type. DynTypedNode (clang/include/clang/AST/ASTTypeTraits.h) has a llvm::AlignedCharArrayUnion data member that looks like it is sufficiently complicated that avoiding copies is probably advantageous.

clang/lib/AST/ComputeDependence.cpp
666

The element type here is Designator (clang/include/clang/AST/Expr.h). It functions as a discriminated union; its largest union member has three source locations and an unsigned value. Copy constructors look to be trivial, but still, this seems like a win.

clang/lib/CodeGen/CGGPUBuiltin.cpp
192

The element type is CallArg (clang/lib/CodeGen/CGCall.h). This looks like a clear win; CallArg is another discriminated union and its LValue member holds a number of pointer members.

clang/lib/CodeGen/MicrosoftCXXABI.cpp
3760

MSRTTIClass (clang/lib/CodeGen/MicrosoftCXXABI.cpp) holds two pointers and three uint32_t values. Seems like a reasonable change to me.

clang/lib/Format/Format.cpp
2809

The element type is Range (clang/include/clang/Tooling/Core/Replacement.h) and it just holds two unsigned values. I don't think this change is needed (though I don't think it would hurt anything either).

clang/lib/Sema/SemaDeclAttr.cpp
450

The element type is CXXBaseSpecifier (clang/include/clang/AST/DeclCXX.h). This change seems fine to me.

clang/lib/Sema/SemaLookup.cpp
4662 ↗(On Diff #516445)

The element type is a pair of NamespaceDecl * and bool. I would skip this change.

clang/utils/TableGen/NeonEmitter.cpp
392

The element type is Type (clang/utils/TableGen/NeonEmitter.cpp). It holds a TypeSpec, an enum value, 5 bool values, and 3 unsigned values. I'm ambivalent.

Manna updated this revision to Diff 516488.Apr 24 2023, 12:07 PM
Manna marked 7 inline comments as done.
Manna edited the summary of this revision. (Show Details)

I have addressed @tahonermann review comments.

clang/utils/TableGen/NeonEmitter.cpp
392

Thanks @tahonermann for reviews and feedbacks.

The element type is Type (clang/utils/TableGen/NeonEmitter.cpp). It holds a TypeSpec, an enum value, 5 bool values, and 3 unsigned values.

Yes, I had mixed feeling about this one too.

Line 362 in clang/utils/TableGen/NeonEmitter.cpp, we are passing it as a reference.

for (const auto &T : Types){

    if (T.isVector() && T.getNumElements() > 1)
      return false;
  }
  return true;
}
Manna added inline comments.Apr 24 2023, 12:09 PM
clang/lib/Sema/SemaLookup.cpp
4662 ↗(On Diff #516445)

Thanks @tahonermann for the explanation. Removed

tahonermann accepted this revision.Apr 24 2023, 1:13 PM

Looks good to me, thanks @Manna!

clang/utils/TableGen/NeonEmitter.cpp
392

Consistency is good then.

This revision is now accepted and ready to land.Apr 24 2023, 1:13 PM
Manna added a comment.Apr 24 2023, 1:15 PM

Looks good to me

Thank you @tahonermann

This revision was automatically updated to reflect the committed changes.