Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Clang][NFC] Capture by ref to avoid copying std::string #138231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

shafik
Copy link
Collaborator

@shafik shafik commented May 2, 2025

Static analysis flagged capturing BName by value as opposed to by reference. Updated capture to be by reference.

Static analysis flagged capturing BName by value as opposed to by reference.
Updated capture to be by reference.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer labels May 2, 2025
@llvmbot
Copy link
Member

llvmbot commented May 2, 2025

@llvm/pr-subscribers-clang-static-analyzer-1

@llvm/pr-subscribers-clang

Author: Shafik Yaghmour (shafik)

Changes

Static analysis flagged capturing BName by value as opposed to by reference. Updated capture to be by reference.


Full diff: https://github.com/llvm/llvm-project/pull/138231.diff

1 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Core/CheckerContext.cpp (+2-2)
diff --git a/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp b/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp
index d0145293fa3e5..1c4f08ae5dff5 100644
--- a/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp
+++ b/clang/lib/StaticAnalyzer/Core/CheckerContext.cpp
@@ -67,10 +67,10 @@ bool CheckerContext::isCLibraryFunction(const FunctionDecl *FD,
       //   _xxxxx_
       //   ^     ^ lookbehind and lookahead characters
 
-      const auto MatchPredecessor = [=]() -> bool {
+      const auto MatchPredecessor = [&]() -> bool {
         return start <= 0 || !llvm::isAlpha(BName[start - 1]);
       };
-      const auto MatchSuccessor = [=]() -> bool {
+      const auto MatchSuccessor = [&]() -> bool {
         std::size_t LookbehindPlace = start + Name.size();
         return LookbehindPlace >= BName.size() ||
                !llvm::isAlpha(BName[LookbehindPlace]);

Copy link
Contributor

@steakhal steakhal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks.
I just wonder why is BName an owning string? To me it would make a lot more sense if it was a StringRef owned by the ASTContext.
WDYT?

@shafik
Copy link
Collaborator Author

shafik commented May 2, 2025

Makes sense, thanks. I just wonder why is BName an owning string? To me it would make a lot more sense if it was a StringRef owned by the ASTContext. WDYT?

FD->getASTContext().BuiltinInfo.getName(BId) returns std::string by value, looking at the API it looks pretty purposeful. Definitely outside the scope of this kind of change.

@shafik shafik merged commit f313b0a into llvm:main May 2, 2025
14 checks passed
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Static analysis flagged capturing BName by value as opposed to by
reference. Updated capture to be by reference.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Static analysis flagged capturing BName by value as opposed to by
reference. Updated capture to be by reference.
IanWood1 pushed a commit to IanWood1/llvm-project that referenced this pull request May 6, 2025
Static analysis flagged capturing BName by value as opposed to by
reference. Updated capture to be by reference.
GeorgeARM pushed a commit to GeorgeARM/llvm-project that referenced this pull request May 7, 2025
Static analysis flagged capturing BName by value as opposed to by
reference. Updated capture to be by reference.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:static analyzer clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants