Skip to content

[NFC] Fixes proposed by code sanitizer. #134138

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
merged 5 commits into from
Apr 4, 2025
Merged

Conversation

zahiraam
Copy link
Contributor

@zahiraam zahiraam commented Apr 2, 2025

No description provided.

@zahiraam zahiraam requested a review from tahonermann April 3, 2025 17:09
@zahiraam zahiraam marked this pull request as ready for review April 3, 2025 17:09
@llvmbot
Copy link
Member

llvmbot commented Apr 3, 2025

@llvm/pr-subscribers-clang-tools-extra

Author: Zahira Ammarguellat (zahiraam)

Changes

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

3 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp (+1-1)
  • (modified) clang-tools-extra/clangd/ConfigCompile.cpp (+1-1)
  • (modified) clang-tools-extra/clangd/Headers.cpp (+1-1)
diff --git a/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
index bafcd402ca851..7650d9e8a592f 100644
--- a/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
@@ -260,7 +260,7 @@ static IntegerRange createFromType(const ASTContext &Context,
     llvm::APSInt LowerValue(PrecisionBits + 2, /*isUnsigned*/ false);
     LowerValue.setBit(PrecisionBits);
     LowerValue.setSignBit();
-    return {LowerValue, UpperValue};
+    return {std::move(LowerValue), UpperValue};
   }
   assert(T.isInteger() && "Unexpected builtin type");
   uint64_t TypeSize = Context.getTypeSize(&T);
diff --git a/clang-tools-extra/clangd/ConfigCompile.cpp b/clang-tools-extra/clangd/ConfigCompile.cpp
index 3d7f792aa136b..13c2405e76df7 100644
--- a/clang-tools-extra/clangd/ConfigCompile.cpp
+++ b/clang-tools-extra/clangd/ConfigCompile.cpp
@@ -535,7 +535,7 @@ struct FragmentCompiler {
     }
     if (Filters->empty())
       return std::nullopt;
-    auto Filter = [Filters](llvm::StringRef Path) {
+    auto Filter = [Filters = std::move(Filters)](llvm::StringRef Path) {
       for (auto &Regex : *Filters)
         if (Regex.match(Path))
           return true;
diff --git a/clang-tools-extra/clangd/Headers.cpp b/clang-tools-extra/clangd/Headers.cpp
index 0ffd9ee4d2751..0d43ada87d359 100644
--- a/clang-tools-extra/clangd/Headers.cpp
+++ b/clang-tools-extra/clangd/Headers.cpp
@@ -305,7 +305,7 @@ IncludeInserter::calculateIncludePath(const HeaderFile &InsertedHeader,
   if (llvm::sys::path::is_absolute(Suggested))
     return std::nullopt;
   bool IsAngled = false;
-  for (auto Filter : AngledHeaders) {
+  for (auto &Filter : AngledHeaders) {
     if (Filter(Suggested)) {
       IsAngled = true;
       break;

@llvmbot
Copy link
Member

llvmbot commented Apr 3, 2025

@llvm/pr-subscribers-clang-tidy

Author: Zahira Ammarguellat (zahiraam)

Changes

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

3 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp (+1-1)
  • (modified) clang-tools-extra/clangd/ConfigCompile.cpp (+1-1)
  • (modified) clang-tools-extra/clangd/Headers.cpp (+1-1)
diff --git a/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
index bafcd402ca851..7650d9e8a592f 100644
--- a/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
@@ -260,7 +260,7 @@ static IntegerRange createFromType(const ASTContext &Context,
     llvm::APSInt LowerValue(PrecisionBits + 2, /*isUnsigned*/ false);
     LowerValue.setBit(PrecisionBits);
     LowerValue.setSignBit();
-    return {LowerValue, UpperValue};
+    return {std::move(LowerValue), UpperValue};
   }
   assert(T.isInteger() && "Unexpected builtin type");
   uint64_t TypeSize = Context.getTypeSize(&T);
diff --git a/clang-tools-extra/clangd/ConfigCompile.cpp b/clang-tools-extra/clangd/ConfigCompile.cpp
index 3d7f792aa136b..13c2405e76df7 100644
--- a/clang-tools-extra/clangd/ConfigCompile.cpp
+++ b/clang-tools-extra/clangd/ConfigCompile.cpp
@@ -535,7 +535,7 @@ struct FragmentCompiler {
     }
     if (Filters->empty())
       return std::nullopt;
-    auto Filter = [Filters](llvm::StringRef Path) {
+    auto Filter = [Filters = std::move(Filters)](llvm::StringRef Path) {
       for (auto &Regex : *Filters)
         if (Regex.match(Path))
           return true;
diff --git a/clang-tools-extra/clangd/Headers.cpp b/clang-tools-extra/clangd/Headers.cpp
index 0ffd9ee4d2751..0d43ada87d359 100644
--- a/clang-tools-extra/clangd/Headers.cpp
+++ b/clang-tools-extra/clangd/Headers.cpp
@@ -305,7 +305,7 @@ IncludeInserter::calculateIncludePath(const HeaderFile &InsertedHeader,
   if (llvm::sys::path::is_absolute(Suggested))
     return std::nullopt;
   bool IsAngled = false;
-  for (auto Filter : AngledHeaders) {
+  for (auto &Filter : AngledHeaders) {
     if (Filter(Suggested)) {
       IsAngled = true;
       break;

@llvmbot
Copy link
Member

llvmbot commented Apr 3, 2025

@llvm/pr-subscribers-clangd

Author: Zahira Ammarguellat (zahiraam)

Changes

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

3 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp (+1-1)
  • (modified) clang-tools-extra/clangd/ConfigCompile.cpp (+1-1)
  • (modified) clang-tools-extra/clangd/Headers.cpp (+1-1)
diff --git a/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
index bafcd402ca851..7650d9e8a592f 100644
--- a/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/NarrowingConversionsCheck.cpp
@@ -260,7 +260,7 @@ static IntegerRange createFromType(const ASTContext &Context,
     llvm::APSInt LowerValue(PrecisionBits + 2, /*isUnsigned*/ false);
     LowerValue.setBit(PrecisionBits);
     LowerValue.setSignBit();
-    return {LowerValue, UpperValue};
+    return {std::move(LowerValue), UpperValue};
   }
   assert(T.isInteger() && "Unexpected builtin type");
   uint64_t TypeSize = Context.getTypeSize(&T);
diff --git a/clang-tools-extra/clangd/ConfigCompile.cpp b/clang-tools-extra/clangd/ConfigCompile.cpp
index 3d7f792aa136b..13c2405e76df7 100644
--- a/clang-tools-extra/clangd/ConfigCompile.cpp
+++ b/clang-tools-extra/clangd/ConfigCompile.cpp
@@ -535,7 +535,7 @@ struct FragmentCompiler {
     }
     if (Filters->empty())
       return std::nullopt;
-    auto Filter = [Filters](llvm::StringRef Path) {
+    auto Filter = [Filters = std::move(Filters)](llvm::StringRef Path) {
       for (auto &Regex : *Filters)
         if (Regex.match(Path))
           return true;
diff --git a/clang-tools-extra/clangd/Headers.cpp b/clang-tools-extra/clangd/Headers.cpp
index 0ffd9ee4d2751..0d43ada87d359 100644
--- a/clang-tools-extra/clangd/Headers.cpp
+++ b/clang-tools-extra/clangd/Headers.cpp
@@ -305,7 +305,7 @@ IncludeInserter::calculateIncludePath(const HeaderFile &InsertedHeader,
   if (llvm::sys::path::is_absolute(Suggested))
     return std::nullopt;
   bool IsAngled = false;
-  for (auto Filter : AngledHeaders) {
+  for (auto &Filter : AngledHeaders) {
     if (Filter(Suggested)) {
       IsAngled = true;
       break;

Copy link
Member

@PiotrZSL PiotrZSL left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -260,7 +260,7 @@ static IntegerRange createFromType(const ASTContext &Context,
llvm::APSInt LowerValue(PrecisionBits + 2, /*isUnsigned*/ false);
LowerValue.setBit(PrecisionBits);
LowerValue.setSignBit();
return {LowerValue, UpperValue};
return {std::move(LowerValue), UpperValue};
Copy link
Member

Choose a reason for hiding this comment

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

this doesn't have any impact.
And if using move, why not for a second too

Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this actually an antipattern? What exactly do you mean by "code sanitizer", can you show the actual error message?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the error message:
" LowerValue is passed-by-value as parameter to llvm::APSInt::APSInt(llvm::APSInt const &) /implicit =default/, when it could be moved instead"
"Use std::move(LowerValue) instead of LowerValue."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you think this should be reverted?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this doesn't have any impact. And if using move, why not for a second too

Good point. I was only addressing the issues that the sanitizer identified.

Copy link
Contributor

Choose a reason for hiding this comment

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

What tool did you use to get that error message?

I do not see the point of this change, please revert. We must not blindly do what tools tell us, we must critically think if they make sense or not.

@@ -305,7 +305,7 @@ IncludeInserter::calculateIncludePath(const HeaderFile &InsertedHeader,
if (llvm::sys::path::is_absolute(Suggested))
return std::nullopt;
bool IsAngled = false;
for (auto Filter : AngledHeaders) {
for (auto &Filter : AngledHeaders) {
Copy link
Member

Choose a reason for hiding this comment

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

same thing in line 315

Copy link
Contributor

@carlosgalvezp carlosgalvezp left a comment

Choose a reason for hiding this comment

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

The changes to the clang-tidy check should be reverted IMO.

Also, please provide more information. What do you mean by "code sanitizer", what is the exact error message, etc.

@zahiraam
Copy link
Contributor Author

zahiraam commented Apr 3, 2025

The changes to the clang-tidy check should be reverted IMO.

Also, please provide more information. What do you mean by "code sanitizer", what is the exact error message, etc.

Please see above the error message from sanitizer. I can revert the change if you think the don't make sense.

@@ -535,7 +535,7 @@ struct FragmentCompiler {
}
if (Filters->empty())
return std::nullopt;
auto Filter = [Filters](llvm::StringRef Path) {
auto Filter = [Filters = std::move(Filters)](llvm::StringRef Path) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Line 520: this can now be a make_unique.

@@ -535,7 +535,7 @@ struct FragmentCompiler {
}
if (Filters->empty())
return std::nullopt;
auto Filter = [Filters](llvm::StringRef Path) {
auto Filter = [Filters = std::move(Filters)](llvm::StringRef Path) {
for (auto &Regex : *Filters)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
for (auto &Regex : *Filters)
for (const auto &Regex : *Filters)

Same for both cases in other file.

@carlosgalvezp
Copy link
Contributor

In general, I don't fully understand the scope of this patch. Surely, there's thousands of things that could be improved in the codebase. Which of those belong to this patch? Reviewers can always point out things that could be improved ad infinitum, leading to a patch with lots of unrelated changes. When do we decide that we are done?

@zahiraam
Copy link
Contributor Author

zahiraam commented Apr 4, 2025

In general, I don't fully understand the scope of this patch. Surely, there's thousands of things that could be improved in the codebase. Which of those belong to this patch? Reviewers can always point out things that could be improved ad infinitum, leading to a patch with lots of unrelated changes. When do we decide that we are done?

I totally agree. Our code sanitizer points us to many edits that are most of the time false positive. I have proposed in this PR fixes that I have judged might be real issues (I might be wrong).
As you suggested I have reverted the changes for clang-tidy. The 2 that are left are in cland/Headers.cpp and clang/ConfigCompile.cpp. If you agree with the edits, please approve. If not I will close the PR.
Thanks.

@zahiraam zahiraam merged commit babbc6f into llvm:main Apr 4, 2025
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants