This is an archive of the discontinued LLVM Phabricator instance.

[NFC][clang] Fix static analyzer concerns
ClosedPublic

Authored by Manna on Apr 20 2023, 9:42 AM.

Details

Summary

Reported by Coverity:

AUTO_CAUSES_COPY
Unnecessary object copies can affect performance.

  1. Inside "SemaDeclCXX.cpp" file, in <unnamed>::​DiagnoseUninitializedFields(clang::​Sema &, clang::​CXXConstructorDecl const *): Using the auto keyword without an & causes the copy of an object of type CXXBaseSpecifier.
  1. Inside "ClangAttrEmitter.cpp" file, in clang::​EmitClangAttrParsedAttrImpl(llvm::​RecordKeeper &, llvm::​raw_ostream &): Using the auto keyword without an & causes the copy of an object of type pair.
  1. Inside "Marshallers.h" file, in clang::​ast_matchers::​dynamic::​internal::​MapAnyOfBuilderDescriptor::​buildMatcherCtor(clang::​ast_matchers::​dynamic::​SourceRange, llvm::​ArrayRef<clang::​ast_matchers::​dynamic::​ParserValue>, clang::​ast_matchers::​dynamic::​Diagnostics *): Using the auto keyword without an & causes the copy of an object of type ParserValue.
  1. Inside "CGVTables.cpp" file, in clang::​CodeGen::​CodeGenModule::​GetVCallVisibilityLevel(clang::​CXXRecordDecl const *, llvm::​DenseSet<clang::​CXXRecordDecl const *, llvm::​DenseMapInfo<clang::​CXXRecordDecl const *, void>> &): Using the auto keyword without an & causes the copy of an object of type CXXBaseSpecifier.
  1. Inside "ASTContext.cpp" file, in hasTemplateSpecializationInEncodedString(clang::​Type const *, bool): Using the auto keyword without an & causes the copy of an object of type CXXBaseSpecifier.
  1. Inside "ComputeDependence.cpp" file, in clang::​computeDependence(clang::​DependentScopeDeclRefExpr *): Using the auto keyword without an & causes the copy of an object of type TemplateArgumentLoc.

Diff Detail

Event Timeline

Manna created this revision.Apr 20 2023, 9:42 AM
Herald added a project: Restricted Project. · View Herald Transcript
Manna requested review of this revision.Apr 20 2023, 9:42 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 20 2023, 9:42 AM
Manna abandoned this revision.Apr 20 2023, 9:42 AM
Manna reclaimed this revision.Apr 20 2023, 10:15 AM
Manna added inline comments.Apr 20 2023, 11:04 AM
clang/lib/AST/ASTContext.cpp
8206

CXXBaseSpecifier is less in size, but most of cases we are using reference semantics.

clang/lib/AST/ComputeDependence.cpp
763

TemplateArgumentLoc has a TemplateArgument (pass by value usually), but it also has a TemplateArgumentLocInfo, which is a pair of pointers + source locations.

clang/lib/ASTMatchers/Dynamic/Marshallers.h
1011

This returns ParserValue which passes as a reference.

clang/lib/CodeGen/CGVTables.cpp
1278

CXXBaseSpecifier is less in size, but most of cases we are using reference semantics.

clang/lib/Lex/Pragma.cpp
1110 ↗(On Diff #515355)

This returns a std::pair<IdentifierInfo *, SourceLocation> which is not particularly expensive to copy

clang/lib/Sema/SemaDeclCXX.cpp
3974

CXXBaseSpecifier is less in size, but most of cases we are using reference semantics.

clang/utils/TableGen/ClangAttrEmitter.cpp
4262

Passes as a reference

erichkeane added inline comments.Apr 20 2023, 11:36 AM
clang/lib/AST/ASTContext.cpp
8206

A bit bigger than 2 pointers actually, its ~1 byte in bitfields, 12 bytes in SourceLocation, and a pointer.

So perhaps still on the line. However, it is only dereferenced 1x.

clang/lib/Lex/Pragma.cpp
1110 ↗(On Diff #515355)

In that case, I'd skip the change here and leave it as a copy.

clang/utils/TableGen/ClangAttrEmitter.cpp
4262

Each element is a pair of a std::string and const Record*, which should justify this. Not sure what you mean by 'passes as a reference'?

clang/utils/TableGen/RISCVVEmitter.cpp
562 ↗(On Diff #515355)

What type is 'P' here? What size is it? It is being used quite a few times in this loop, which means it would have to be pretty sizable to justify making it a reference unless it has a copy ctor of cost.

With the exception of the case involving the Policy class, these changes all look fine to me.

clang/utils/TableGen/RISCVVEmitter.cpp
562 ↗(On Diff #515355)

P is class Policy from clang/include/clang/Support/RISCVVIntrinsicUtils.h. It holds two enum values (with non-fixed underlying type) and has an implicit copy constructor, so cheap to copy. I suspect the code gen impact is negligible here, but I see no need for reference semantics. I recommend skipping this change.

Manna updated this revision to Diff 515494.Apr 20 2023, 2:45 PM
Manna marked an inline comment as done and an inline comment as not done.
Manna edited the summary of this revision. (Show Details)

I have removed the case involving the Policy class

tahonermann accepted this revision.Apr 20 2023, 3:33 PM

Thanks, Soumi! Looks good to me!

This revision is now accepted and ready to land.Apr 20 2023, 3:33 PM
Manna marked an inline comment as done.Apr 20 2023, 7:37 PM

Thank you @tahonermann and @erichkeane for reviews and feedback.

clang/utils/TableGen/ClangAttrEmitter.cpp
4262

Each element is a pair of a std::string and const Record*, which should justify this.

Sorry i meant to say this instead since ParsedAttrMap Attrs = getParsedAttrList(Records, &Dupes);

Manna marked an inline comment as done.Apr 21 2023, 7:13 AM

@erichkeane do you have any more concerns?

Still had this convo which wasn't resolved.

clang/lib/Lex/Pragma.cpp
1110 ↗(On Diff #515355)

This seems like it should be a copy. About a pointer-and-a-half with a cheap ctor.

Manna updated this revision to Diff 515729.Apr 21 2023, 7:44 AM
Manna edited the summary of this revision. (Show Details)

I have addressed @erichkeane's review comment.

Manna marked an inline comment as done.Apr 21 2023, 7:45 AM
Manna added inline comments.
clang/lib/Lex/Pragma.cpp
1110 ↗(On Diff #515355)

I missed that. Thanks @erichkeane. Done

erichkeane accepted this revision.Apr 21 2023, 7:47 AM
This revision was automatically updated to reflect the committed changes.
Manna marked an inline comment as done.