[clang-tools-extra] [clang-tidy] Create bugprone-bitwise-pointer-cast check (PR #108083)

2024-10-06 Thread Carlos Galvez via cfe-commits
Carlos =?utf-8?q?Gálvez?= 
Message-ID:
In-Reply-To: 


https://github.com/carlosgalvezp closed 
https://github.com/llvm/llvm-project/pull/108083
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Create bugprone-bitwise-pointer-cast check (PR #108083)

2024-10-03 Thread Carlos Galvez via cfe-commits
Carlos =?utf-8?q?Gálvez?= 
Message-ID:
In-Reply-To: 


https://github.com/carlosgalvezp updated 
https://github.com/llvm/llvm-project/pull/108083

>From 1b1d54e0ce0d0bc4250ff045840b0a0a7bac59a1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20G=C3=A1lvez?= 
Date: Tue, 10 Sep 2024 13:46:51 +
Subject: [PATCH 1/2] [clang-tidy] Create bugprone-bitwise-pointer-cast check

To detect unsafe usages of casting a pointer to another via copying
the bytes from one into the other, either via std::bit_cast or via
memcpy.use of bit_cast. This is currently not caught by any other
means.

Fixes #106987
---
 .../bugprone/BitwisePointerCastCheck.cpp  | 44 +
 .../bugprone/BitwisePointerCastCheck.h| 34 +++
 .../bugprone/BugproneTidyModule.cpp   |  3 +
 .../clang-tidy/bugprone/CMakeLists.txt|  1 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  8 ++-
 .../checks/bugprone/bitwise-pointer-cast.rst  | 52 
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../bugprone/bitwise-pointer-cast.cpp | 61 +++
 8 files changed, 203 insertions(+), 1 deletion(-)
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/bugprone/bitwise-pointer-cast.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/bitwise-pointer-cast.cpp

diff --git a/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.cpp
new file mode 100644
index 00..3a361a22ed9b73
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.cpp
@@ -0,0 +1,44 @@
+//===--- BitwisePointerCastCheck.cpp - clang-tidy 
-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "BitwisePointerCastCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+void BitwisePointerCastCheck::registerMatchers(MatchFinder *Finder) {
+  auto IsPointerType = refersToType(qualType(isAnyPointer()));
+  Finder->addMatcher(callExpr(hasDeclaration(functionDecl(allOf(
+  hasName("::std::bit_cast"),
+  hasTemplateArgument(0, IsPointerType),
+  hasTemplateArgument(1, IsPointerType)
+ .bind("bit_cast"),
+ this);
+
+  auto IsDoublePointerType =
+  hasType(qualType(pointsTo(qualType(isAnyPointer();
+  Finder->addMatcher(callExpr(hasArgument(0, IsDoublePointerType),
+  hasArgument(1, IsDoublePointerType),
+  
hasDeclaration(functionDecl(hasName("::memcpy"
+ .bind("memcpy"),
+ this);
+}
+
+void BitwisePointerCastCheck::check(const MatchFinder::MatchResult &Result) {
+  if (const auto *Call = Result.Nodes.getNodeAs("bit_cast"))
+diag(Call->getBeginLoc(),
+ "do not use 'std::bit_cast' to cast between pointers")
+<< Call->getSourceRange();
+  else if (const auto *Call = Result.Nodes.getNodeAs("memcpy"))
+diag(Call->getBeginLoc(), "do not use 'memcpy' to cast between pointers")
+<< Call->getSourceRange();
+}
+
+} // namespace clang::tidy::bugprone
diff --git a/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.h 
b/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.h
new file mode 100644
index 00..1515519b3c9fda
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.h
@@ -0,0 +1,34 @@
+//===--- BitwisePointerCastCheck.h - clang-tidy -*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITWISEPOINTERCASTCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITWISEPOINTERCASTCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::bugprone {
+
+/// Warns about code that tries to cast between pointers by means of
+/// ``std::bit_cast`` or ``memcpy``.
+///
+/// For the user-facing documentation see:
+/// 
http://clang.llvm.org/extra/clang-tidy/checks/bugprone/bitwise-pointer-cast.html
+class BitwisePointerCastCheck : public ClangTidyCheck {
+public:
+  BitwisePointerCastCheck(StringRef Nam

[clang-tools-extra] [clang-tidy] Create bugprone-bitwise-pointer-cast check (PR #108083)

2024-10-03 Thread Carlos Galvez via cfe-commits
Carlos =?utf-8?q?Gálvez?= 
Message-ID:
In-Reply-To: 


https://github.com/carlosgalvezp updated 
https://github.com/llvm/llvm-project/pull/108083

>From 1b1d54e0ce0d0bc4250ff045840b0a0a7bac59a1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20G=C3=A1lvez?= 
Date: Tue, 10 Sep 2024 13:46:51 +
Subject: [PATCH 1/2] [clang-tidy] Create bugprone-bitwise-pointer-cast check

To detect unsafe usages of casting a pointer to another via copying
the bytes from one into the other, either via std::bit_cast or via
memcpy.use of bit_cast. This is currently not caught by any other
means.

Fixes #106987
---
 .../bugprone/BitwisePointerCastCheck.cpp  | 44 +
 .../bugprone/BitwisePointerCastCheck.h| 34 +++
 .../bugprone/BugproneTidyModule.cpp   |  3 +
 .../clang-tidy/bugprone/CMakeLists.txt|  1 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  8 ++-
 .../checks/bugprone/bitwise-pointer-cast.rst  | 52 
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../bugprone/bitwise-pointer-cast.cpp | 61 +++
 8 files changed, 203 insertions(+), 1 deletion(-)
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/bugprone/bitwise-pointer-cast.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/bitwise-pointer-cast.cpp

diff --git a/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.cpp
new file mode 100644
index 00..3a361a22ed9b73
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.cpp
@@ -0,0 +1,44 @@
+//===--- BitwisePointerCastCheck.cpp - clang-tidy 
-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "BitwisePointerCastCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+void BitwisePointerCastCheck::registerMatchers(MatchFinder *Finder) {
+  auto IsPointerType = refersToType(qualType(isAnyPointer()));
+  Finder->addMatcher(callExpr(hasDeclaration(functionDecl(allOf(
+  hasName("::std::bit_cast"),
+  hasTemplateArgument(0, IsPointerType),
+  hasTemplateArgument(1, IsPointerType)
+ .bind("bit_cast"),
+ this);
+
+  auto IsDoublePointerType =
+  hasType(qualType(pointsTo(qualType(isAnyPointer();
+  Finder->addMatcher(callExpr(hasArgument(0, IsDoublePointerType),
+  hasArgument(1, IsDoublePointerType),
+  
hasDeclaration(functionDecl(hasName("::memcpy"
+ .bind("memcpy"),
+ this);
+}
+
+void BitwisePointerCastCheck::check(const MatchFinder::MatchResult &Result) {
+  if (const auto *Call = Result.Nodes.getNodeAs("bit_cast"))
+diag(Call->getBeginLoc(),
+ "do not use 'std::bit_cast' to cast between pointers")
+<< Call->getSourceRange();
+  else if (const auto *Call = Result.Nodes.getNodeAs("memcpy"))
+diag(Call->getBeginLoc(), "do not use 'memcpy' to cast between pointers")
+<< Call->getSourceRange();
+}
+
+} // namespace clang::tidy::bugprone
diff --git a/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.h 
b/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.h
new file mode 100644
index 00..1515519b3c9fda
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.h
@@ -0,0 +1,34 @@
+//===--- BitwisePointerCastCheck.h - clang-tidy -*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITWISEPOINTERCASTCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITWISEPOINTERCASTCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::bugprone {
+
+/// Warns about code that tries to cast between pointers by means of
+/// ``std::bit_cast`` or ``memcpy``.
+///
+/// For the user-facing documentation see:
+/// 
http://clang.llvm.org/extra/clang-tidy/checks/bugprone/bitwise-pointer-cast.html
+class BitwisePointerCastCheck : public ClangTidyCheck {
+public:
+  BitwisePointerCastCheck(StringRef Nam

[clang] [clang-format] Handle template closer followed by braces (PR #110971)

2024-10-03 Thread Carlos Galvez via cfe-commits


@@ -3538,6 +3538,11 @@ TEST_F(TokenAnnotatorTest, TemplateInstantiation) {
   ASSERT_EQ(Tokens.size(), 11u) << Tokens;
   EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
   EXPECT_TOKEN(Tokens[6], tok::greater, TT_TemplateCloser);
+
+  Tokens = annotate("return std::conditional_t{};");
+  ASSERT_EQ(Tokens.size(), 21u) << Tokens;
+  EXPECT_TOKEN(Tokens[4], tok::less, TT_TemplateOpener);
+  EXPECT_TOKEN(Tokens[16], tok::greater, TT_TemplateCloser);

carlosgalvezp wrote:

Ok, thanks for the clarification!

https://github.com/llvm/llvm-project/pull/110971
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-format] Handle template closer followed by braces (PR #110971)

2024-10-03 Thread Carlos Galvez via cfe-commits


@@ -3538,6 +3538,11 @@ TEST_F(TokenAnnotatorTest, TemplateInstantiation) {
   ASSERT_EQ(Tokens.size(), 11u) << Tokens;
   EXPECT_TOKEN(Tokens[2], tok::less, TT_TemplateOpener);
   EXPECT_TOKEN(Tokens[6], tok::greater, TT_TemplateCloser);
+
+  Tokens = annotate("return std::conditional_t{};");
+  ASSERT_EQ(Tokens.size(), 21u) << Tokens;
+  EXPECT_TOKEN(Tokens[4], tok::less, TT_TemplateOpener);
+  EXPECT_TOKEN(Tokens[16], tok::greater, TT_TemplateCloser);

carlosgalvezp wrote:

Will this also verify that there is no extra space between `>` and `{`? I 
didn't mention it in the ticket but it was another issue.

https://github.com/llvm/llvm-project/pull/110971
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Create bugprone-bitwise-pointer-cast check (PR #108083)

2024-10-02 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

> Both sound good to me.

Great, updated! Let me know there's anything else to fix or if we can merge.




https://github.com/llvm/llvm-project/pull/108083
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Create bugprone-bitwise-pointer-cast check (PR #108083)

2024-10-02 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp updated 
https://github.com/llvm/llvm-project/pull/108083

>From 1b1d54e0ce0d0bc4250ff045840b0a0a7bac59a1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20G=C3=A1lvez?= 
Date: Tue, 10 Sep 2024 13:46:51 +
Subject: [PATCH] [clang-tidy] Create bugprone-bitwise-pointer-cast check

To detect unsafe usages of casting a pointer to another via copying
the bytes from one into the other, either via std::bit_cast or via
memcpy.use of bit_cast. This is currently not caught by any other
means.

Fixes #106987
---
 .../bugprone/BitwisePointerCastCheck.cpp  | 44 +
 .../bugprone/BitwisePointerCastCheck.h| 34 +++
 .../bugprone/BugproneTidyModule.cpp   |  3 +
 .../clang-tidy/bugprone/CMakeLists.txt|  1 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  8 ++-
 .../checks/bugprone/bitwise-pointer-cast.rst  | 52 
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../bugprone/bitwise-pointer-cast.cpp | 61 +++
 8 files changed, 203 insertions(+), 1 deletion(-)
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/bugprone/bitwise-pointer-cast.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/bitwise-pointer-cast.cpp

diff --git a/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.cpp
new file mode 100644
index 00..3a361a22ed9b73
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.cpp
@@ -0,0 +1,44 @@
+//===--- BitwisePointerCastCheck.cpp - clang-tidy 
-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "BitwisePointerCastCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+void BitwisePointerCastCheck::registerMatchers(MatchFinder *Finder) {
+  auto IsPointerType = refersToType(qualType(isAnyPointer()));
+  Finder->addMatcher(callExpr(hasDeclaration(functionDecl(allOf(
+  hasName("::std::bit_cast"),
+  hasTemplateArgument(0, IsPointerType),
+  hasTemplateArgument(1, IsPointerType)
+ .bind("bit_cast"),
+ this);
+
+  auto IsDoublePointerType =
+  hasType(qualType(pointsTo(qualType(isAnyPointer();
+  Finder->addMatcher(callExpr(hasArgument(0, IsDoublePointerType),
+  hasArgument(1, IsDoublePointerType),
+  
hasDeclaration(functionDecl(hasName("::memcpy"
+ .bind("memcpy"),
+ this);
+}
+
+void BitwisePointerCastCheck::check(const MatchFinder::MatchResult &Result) {
+  if (const auto *Call = Result.Nodes.getNodeAs("bit_cast"))
+diag(Call->getBeginLoc(),
+ "do not use 'std::bit_cast' to cast between pointers")
+<< Call->getSourceRange();
+  else if (const auto *Call = Result.Nodes.getNodeAs("memcpy"))
+diag(Call->getBeginLoc(), "do not use 'memcpy' to cast between pointers")
+<< Call->getSourceRange();
+}
+
+} // namespace clang::tidy::bugprone
diff --git a/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.h 
b/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.h
new file mode 100644
index 00..1515519b3c9fda
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.h
@@ -0,0 +1,34 @@
+//===--- BitwisePointerCastCheck.h - clang-tidy -*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITWISEPOINTERCASTCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITWISEPOINTERCASTCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::bugprone {
+
+/// Warns about code that tries to cast between pointers by means of
+/// ``std::bit_cast`` or ``memcpy``.
+///
+/// For the user-facing documentation see:
+/// 
http://clang.llvm.org/extra/clang-tidy/checks/bugprone/bitwise-pointer-cast.html
+class BitwisePointerCastCheck : public ClangTidyCheck {
+public:
+  BitwisePointerCastCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name

[clang-tools-extra] [clang-tidy] Create bugprone-bitwise-pointer-cast check (PR #108083)

2024-10-02 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp edited 
https://github.com/llvm/llvm-project/pull/108083
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Create bugprone-bit-cast-pointers check (PR #108083)

2024-10-02 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp edited 
https://github.com/llvm/llvm-project/pull/108083
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Create bugprone-bit-cast-pointers check (PR #108083)

2024-10-02 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp updated 
https://github.com/llvm/llvm-project/pull/108083

>From 7c50e702c087047adfe43137d52e56cb98ff9918 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20G=C3=A1lvez?= 
Date: Tue, 10 Sep 2024 13:46:51 +
Subject: [PATCH] [clang-tidy] Create bugprone-bitwise-pointer-cast check

To detect unsafe usages of casting a pointer to another via copying
the bytes from one into the other, either via std::bit_cast or via
memcpy.use of bit_cast. This is currently not caught by any other
means.

Fixes #106987
---
 .../bugprone/BitwisePointerCastCheck.cpp  | 44 +
 .../bugprone/BitwisePointerCastCheck.h| 34 +++
 .../bugprone/BugproneTidyModule.cpp   |  3 +
 .../clang-tidy/bugprone/CMakeLists.txt|  1 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  6 ++
 .../checks/bugprone/bitwise-pointer-cast.rst  | 52 
 .../docs/clang-tidy/checks/list.rst   |  1 +
 .../bugprone/bitwise-pointer-cast.cpp | 61 +++
 8 files changed, 202 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.cpp
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/bugprone/bitwise-pointer-cast.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/bitwise-pointer-cast.cpp

diff --git a/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.cpp
new file mode 100644
index 00..3a361a22ed9b73
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.cpp
@@ -0,0 +1,44 @@
+//===--- BitwisePointerCastCheck.cpp - clang-tidy 
-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "BitwisePointerCastCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+void BitwisePointerCastCheck::registerMatchers(MatchFinder *Finder) {
+  auto IsPointerType = refersToType(qualType(isAnyPointer()));
+  Finder->addMatcher(callExpr(hasDeclaration(functionDecl(allOf(
+  hasName("::std::bit_cast"),
+  hasTemplateArgument(0, IsPointerType),
+  hasTemplateArgument(1, IsPointerType)
+ .bind("bit_cast"),
+ this);
+
+  auto IsDoublePointerType =
+  hasType(qualType(pointsTo(qualType(isAnyPointer();
+  Finder->addMatcher(callExpr(hasArgument(0, IsDoublePointerType),
+  hasArgument(1, IsDoublePointerType),
+  
hasDeclaration(functionDecl(hasName("::memcpy"
+ .bind("memcpy"),
+ this);
+}
+
+void BitwisePointerCastCheck::check(const MatchFinder::MatchResult &Result) {
+  if (const auto *Call = Result.Nodes.getNodeAs("bit_cast"))
+diag(Call->getBeginLoc(),
+ "do not use 'std::bit_cast' to cast between pointers")
+<< Call->getSourceRange();
+  else if (const auto *Call = Result.Nodes.getNodeAs("memcpy"))
+diag(Call->getBeginLoc(), "do not use 'memcpy' to cast between pointers")
+<< Call->getSourceRange();
+}
+
+} // namespace clang::tidy::bugprone
diff --git a/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.h 
b/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.h
new file mode 100644
index 00..1515519b3c9fda
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitwisePointerCastCheck.h
@@ -0,0 +1,34 @@
+//===--- BitwisePointerCastCheck.h - clang-tidy -*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITWISEPOINTERCASTCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITWISEPOINTERCASTCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::bugprone {
+
+/// Warns about code that tries to cast between pointers by means of
+/// ``std::bit_cast`` or ``memcpy``.
+///
+/// For the user-facing documentation see:
+/// 
http://clang.llvm.org/extra/clang-tidy/checks/bugprone/bitwise-pointer-cast.html
+class BitwisePointerCastCheck : public ClangTidyCheck {
+public:
+  BitwisePointerCastCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name, Context) {}
+ 

[clang] [clang-tools-extra] RFC: [clang-tidy] [analyzer] Nondeterministic pointer usage improvements (PR #110471)

2024-09-30 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

> > Why isn't `misc` suitable for this use case?
> 
> `bugprone` was just an initial thought. If the group think leads us to `misc` 
> I'm ok with moving in that direction. Other thoughts?

Maybe i misunderstood, I thought you meant creating a new clang-tidy module 
called `advisory`. `bugprone` sounds good to me!

https://github.com/llvm/llvm-project/pull/110471
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] RFC: [clang-tidy] [analyzer] Nondeterministic pointer usage improvements (PR #110471)

2024-09-30 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

> creation of an advisory group.

Why isn't `misc` suitable for this use case? It would be confusing to have both 
groups I think, and may lead to bike-shedding discussions in the future about 
where a check belongs in misc or advisory.

https://github.com/llvm/llvm-project/pull/110471
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Create bugprone-bit-cast-pointers check (PR #108083)

2024-09-29 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

What about `bugprone-bitwise-pointer-copy`?

https://github.com/llvm/llvm-project/pull/108083
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][NFC] Fix example code for memberPointerType() AST matcher (PR #109404)

2024-09-27 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp closed 
https://github.com/llvm/llvm-project/pull/109404
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][NFC] Fix example code for memberPointerType() AST matcher (PR #109404)

2024-09-27 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

Great!

https://github.com/llvm/llvm-project/pull/109404
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Create bugprone-bit-cast-pointers check (PR #108083)

2024-09-27 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

> > It now comes to mind that we probably also want to check `memcpy(ptr, 
> > ptr)`, which is equivalent to `bit_cast`. In that case I wonder if the 
> > check name still holds or it should be named something else?
> 
> Yeah, with the addition of `memcpy`, the `bit` part of the name makes a less 
> sense. Maybe
> 
> * `bugprone-pointer-cast[ing]`
> * `bugprone-cast[ing]-between-pointers`

Hmm, I think "pointer cast" is too ambiguous, it could warn about _any_ type of 
pointer cast via e.g. `reinterpret_cast`, `const_cast`, etc. I want to put 
emphasis on the act of _copying the bytes_ of the pointer into another pointer 
to perform such a cast. In that sense I think "bit_cast" is still a more 
accurate name. 



https://github.com/llvm/llvm-project/pull/108083
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Create bugprone-bit-cast-pointers check (PR #108083)

2024-09-27 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp updated 
https://github.com/llvm/llvm-project/pull/108083

>From c107a956e8fc27fd35f9a840e59ea748aac03586 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20G=C3=A1lvez?= 
Date: Tue, 10 Sep 2024 13:46:51 +
Subject: [PATCH] [clang-tidy] Create bugprone-bit-cast-pointers check

To detect unsafe use of bit_cast. Otherwise, bit_cast bypasses all
checks done by compilers and linters.

Fixes #106987
---
 .../bugprone/BitCastPointersCheck.cpp | 44 +
 .../bugprone/BitCastPointersCheck.h   | 34 +++
 .../bugprone/BugproneTidyModule.cpp   |  3 +
 .../clang-tidy/bugprone/CMakeLists.txt|  1 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  6 ++
 .../checks/bugprone/bit-cast-pointers.rst | 52 
 .../checkers/bugprone/bit-cast-pointers.cpp   | 61 +++
 7 files changed, 201 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
 create mode 100644 clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/bugprone/bit-cast-pointers.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/bit-cast-pointers.cpp

diff --git a/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
new file mode 100644
index 00..9c6df847fe09fd
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
@@ -0,0 +1,44 @@
+//===--- BitCastPointersCheck.cpp - clang-tidy 
===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "BitCastPointersCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+void BitCastPointersCheck::registerMatchers(MatchFinder *Finder) {
+  auto IsPointerType = refersToType(qualType(isAnyPointer()));
+  Finder->addMatcher(callExpr(hasDeclaration(functionDecl(allOf(
+  hasName("::std::bit_cast"),
+  hasTemplateArgument(0, IsPointerType),
+  hasTemplateArgument(1, IsPointerType)
+ .bind("bit_cast"),
+ this);
+
+  auto IsDoublePointerType =
+  hasType(qualType(pointsTo(qualType(isAnyPointer();
+  Finder->addMatcher(callExpr(hasArgument(0, IsDoublePointerType),
+  hasArgument(1, IsDoublePointerType),
+  
hasDeclaration(functionDecl(hasName("::memcpy"
+ .bind("memcpy"),
+ this);
+}
+
+void BitCastPointersCheck::check(const MatchFinder::MatchResult &Result) {
+  if (const auto *Call = Result.Nodes.getNodeAs("bit_cast"))
+diag(Call->getBeginLoc(),
+ "do not use 'std::bit_cast' to cast between pointers")
+<< Call->getSourceRange();
+  else if (const auto *Call = Result.Nodes.getNodeAs("memcpy"))
+diag(Call->getBeginLoc(), "do not use 'memcpy' to cast between pointers")
+<< Call->getSourceRange();
+}
+
+} // namespace clang::tidy::bugprone
diff --git a/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h 
b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
new file mode 100644
index 00..223ad95ddc90ba
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
@@ -0,0 +1,34 @@
+//===--- BitCastPointersCheck.h - clang-tidy *- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITCASTPOINTERSCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITCASTPOINTERSCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::bugprone {
+
+/// Warns about code that tries to cast between pointers by means of
+/// ``std::bit_cast`` or ``memcpy``.
+///
+/// For the user-facing documentation see:
+/// 
http://clang.llvm.org/extra/clang-tidy/checks/bugprone/bit-cast-pointers.html
+class BitCastPointersCheck : public ClangTidyCheck {
+public:
+  BitCastPointersCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  bool isLanguageVersionSupported(const LangOptions &LangOpts) const

[clang-tools-extra] [clang-tidy] Create bugprone-bit-cast-pointers check (PR #108083)

2024-09-27 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp updated 
https://github.com/llvm/llvm-project/pull/108083

>From 467b454a07e22437958832d306ae2c4b67c372ef Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20G=C3=A1lvez?= 
Date: Tue, 10 Sep 2024 13:46:51 +
Subject: [PATCH] [clang-tidy] Create bugprone-bit-cast-pointers check

To detect unsafe use of bit_cast. Otherwise, bit_cast bypasses all
checks done by compilers and linters.

Fixes #106987
---
 .../bugprone/BitCastPointersCheck.cpp | 45 ++
 .../bugprone/BitCastPointersCheck.h   | 34 +++
 .../bugprone/BugproneTidyModule.cpp   |  3 +
 .../clang-tidy/bugprone/CMakeLists.txt|  1 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  6 ++
 .../checks/bugprone/bit-cast-pointers.rst | 52 
 .../checkers/bugprone/bit-cast-pointers.cpp   | 61 +++
 7 files changed, 202 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
 create mode 100644 clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/bugprone/bit-cast-pointers.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/bit-cast-pointers.cpp

diff --git a/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
new file mode 100644
index 00..b881be84ea942a
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
@@ -0,0 +1,45 @@
+//===--- BitCastPointersCheck.cpp - clang-tidy 
===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "BitCastPointersCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+void BitCastPointersCheck::registerMatchers(MatchFinder *Finder) {
+  auto IsPointerType = refersToType(qualType(isAnyPointer()));
+  Finder->addMatcher(callExpr(hasDeclaration(functionDecl(allOf(
+  hasName("::std::bit_cast"),
+  hasTemplateArgument(0, IsPointerType),
+  hasTemplateArgument(1, IsPointerType)
+ .bind("bit_cast"),
+ this);
+
+  auto IsDoublePointerType =
+  hasType(qualType(pointsTo(qualType(isAnyPointer();
+  Finder->addMatcher(callExpr(hasArgument(0, IsDoublePointerType),
+  hasArgument(1, IsDoublePointerType),
+  
hasDeclaration(functionDecl(hasName("::memcpy"
+ .bind("memcpy"),
+ this);
+}
+
+void BitCastPointersCheck::check(const MatchFinder::MatchResult &Result) {
+  if (const auto *Call = Result.Nodes.getNodeAs("bit_cast"))
+diag(Call->getBeginLoc(),
+ "do not use 'std::bit_cast' to cast between pointers")
+<< Call->getSourceRange();
+  else if (const auto *Call = Result.Nodes.getNodeAs("memcpy"))
+diag(Call->getBeginLoc(),
+ "do not use 'memcpy' to cast between pointers")
+<< Call->getSourceRange();
+}
+
+} // namespace clang::tidy::bugprone
diff --git a/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h 
b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
new file mode 100644
index 00..223ad95ddc90ba
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
@@ -0,0 +1,34 @@
+//===--- BitCastPointersCheck.h - clang-tidy *- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITCASTPOINTERSCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITCASTPOINTERSCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::bugprone {
+
+/// Warns about code that tries to cast between pointers by means of
+/// ``std::bit_cast`` or ``memcpy``.
+///
+/// For the user-facing documentation see:
+/// 
http://clang.llvm.org/extra/clang-tidy/checks/bugprone/bit-cast-pointers.html
+class BitCastPointersCheck : public ClangTidyCheck {
+public:
+  BitCastPointersCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  bool isLanguageVersionSupported(const LangOptions &Lang

[clang-tools-extra] [clang-tidy] Create bugprone-bit-cast-pointers check (PR #108083)

2024-09-27 Thread Carlos Galvez via cfe-commits


@@ -0,0 +1,45 @@
+//===--- BitCastPointersCheck.cpp - clang-tidy 
===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "BitCastPointersCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+void BitCastPointersCheck::registerMatchers(MatchFinder *Finder) {
+  auto IsPointerType = refersToType(qualType(isAnyPointer()));
+  Finder->addMatcher(callExpr(hasDeclaration(functionDecl(allOf(
+  hasName("::std::bit_cast"),
+  hasTemplateArgument(0, IsPointerType),
+  hasTemplateArgument(1, IsPointerType)
+ .bind("bit_cast"),
+ this);
+
+  auto IsDoublePointerType =
+  hasType(qualType(pointsTo(qualType(isAnyPointer();
+  Finder->addMatcher(callExpr(hasArgument(0, IsDoublePointerType),
+  hasArgument(1, IsDoublePointerType),
+  
hasDeclaration(functionDecl(hasName("::memcpy"
+ .bind("memcpy"),
+ this);
+}
+
+void BitCastPointersCheck::check(const MatchFinder::MatchResult &Result) {
+  if (const auto *MatchedDecl = Result.Nodes.getNodeAs("bit_cast"))
+diag(MatchedDecl->getBeginLoc(),
+ "do not use std::bit_cast to cast between pointers")
+<< MatchedDecl->getSourceRange();

carlosgalvezp wrote:

Then I have to add braces, though. Replaced with `else if` which should achieve 
the same goal.

https://github.com/llvm/llvm-project/pull/108083
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][NFC] Fix example code for memberPointerType() AST matcher (PR #109404)

2024-09-23 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp updated 
https://github.com/llvm/llvm-project/pull/109404

>From 96ee182c19926a40331430a661bd959066e56bf9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20G=C3=A1lvez?= 
Date: Fri, 20 Sep 2024 11:32:07 +
Subject: [PATCH] [clang][NFC] Fix example code for memberPointerType() AST
 matcher

The example code doesn't compile otherwise.
---
 clang/docs/LibASTMatchersReference.html   | 8 +++-
 clang/include/clang/ASTMatchers/ASTMatchers.h | 6 +++---
 2 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/clang/docs/LibASTMatchersReference.html 
b/clang/docs/LibASTMatchersReference.html
index a16b9c44ef0eab..ae25b3797083eb 100644
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -2675,10 +2675,10 @@ Node Matchers
 MatcherType>memberPointerTypeMatcherMemberPointerType>...
 Matches member 
pointer types.
 Given
-  struct A { int i; }
-  A::* ptr = A::i;
+  struct A { int i; };
+  int A::* ptr = &A::i;
 memberPointerType()
-  matches "A::* ptr"
+  matches "int A::* ptr"
 
 
 
@@ -10659,5 +10659,3 @@ AST Traversal Matchers
 
 
 
-
-
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h 
b/clang/include/clang/ASTMatchers/ASTMatchers.h
index f1c72efc238784..c298ea864177e1 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -7327,11 +7327,11 @@ extern const AstTypeMatcher 
blockPointerType;
 /// Matches member pointer types.
 /// Given
 /// \code
-///   struct A { int i; }
-///   A::* ptr = A::i;
+///   struct A { int i; };
+///   int A::* ptr = &A::i;
 /// \endcode
 /// memberPointerType()
-///   matches "A::* ptr"
+///   matches "int A::* ptr"
 extern const AstTypeMatcher memberPointerType;
 
 /// Matches pointer types, but does not match Objective-C object pointer

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Create bugprone-bit-cast-pointers check (PR #108083)

2024-09-23 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

Friendly ping, let me know if there's anything else that should be addressed!

https://github.com/llvm/llvm-project/pull/108083
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][NFC] Fix example code for memberPointerType() AST matcher (PR #109404)

2024-09-20 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp created 
https://github.com/llvm/llvm-project/pull/109404

The example code doesn't compile otherwise.

>From f598e98d4deb66a313b7f944cb96df04bc4b91a7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20G=C3=A1lvez?= 
Date: Fri, 20 Sep 2024 11:32:07 +
Subject: [PATCH] [clang][NFC] Fix example code for memberPointerType() AST
 matcher

The example code doesn't compile otherwise.
---
 clang/docs/LibASTMatchersReference.html   | 6 ++
 clang/include/clang/ASTMatchers/ASTMatchers.h | 4 ++--
 2 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/clang/docs/LibASTMatchersReference.html 
b/clang/docs/LibASTMatchersReference.html
index a16b9c44ef0eab..30695efc3e304e 100644
--- a/clang/docs/LibASTMatchersReference.html
+++ b/clang/docs/LibASTMatchersReference.html
@@ -2675,8 +2675,8 @@ Node Matchers
 MatcherType>memberPointerTypeMatcherMemberPointerType>...
 Matches member 
pointer types.
 Given
-  struct A { int i; }
-  A::* ptr = A::i;
+  struct A { int i; };
+  int A::* ptr = &A::i;
 memberPointerType()
   matches "A::* ptr"
 
@@ -10659,5 +10659,3 @@ AST Traversal Matchers
 
 
 
-
-
diff --git a/clang/include/clang/ASTMatchers/ASTMatchers.h 
b/clang/include/clang/ASTMatchers/ASTMatchers.h
index f1c72efc238784..61877c435208fa 100644
--- a/clang/include/clang/ASTMatchers/ASTMatchers.h
+++ b/clang/include/clang/ASTMatchers/ASTMatchers.h
@@ -7327,8 +7327,8 @@ extern const AstTypeMatcher 
blockPointerType;
 /// Matches member pointer types.
 /// Given
 /// \code
-///   struct A { int i; }
-///   A::* ptr = A::i;
+///   struct A { int i; };
+///   int A::* ptr = &A::i;
 /// \endcode
 /// memberPointerType()
 ///   matches "A::* ptr"

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [clang][NFC] Fix example code for memberPointerType() AST matcher (PR #109403)

2024-09-20 Thread Carlos Galvez via cfe-commits
Carlos =?utf-8?q?Gálvez?= 
Message-ID:
In-Reply-To: 


https://github.com/carlosgalvezp closed 
https://github.com/llvm/llvm-project/pull/109403
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang-tools-extra] [clang][NFC] Fix example code for memberPointerType() AST matcher (PR #109403)

2024-09-20 Thread Carlos Galvez via cfe-commits
Carlos =?utf-8?q?Gálvez?= 
Message-ID: 
In-Reply-To:


https://github.com/carlosgalvezp created 
https://github.com/llvm/llvm-project/pull/109403

The example code doesn't compile otherwise.


>From 6916d5ecdc327b2771fbbca226095bd99d394dab Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20G=C3=A1lvez?= 
Date: Tue, 10 Sep 2024 13:46:51 +
Subject: [PATCH 1/2] [clang-tidy] Create bugprone-bit-cast-pointers check

To detect unsafe use of bit_cast that should be reinterpret_cast
instead. Otherwise, bit_cast bypasses all checks done by compilers and
linters.

Fixes #106987
---
 .../bugprone/BitCastPointersCheck.cpp | 30 ++
 .../bugprone/BitCastPointersCheck.h   | 34 
 .../bugprone/BugproneTidyModule.cpp   |  3 ++
 .../clang-tidy/bugprone/CMakeLists.txt|  1 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  6 +++
 .../checks/bugprone/bit-cast-pointers.rst | 39 +++
 .../checkers/bugprone/bit-cast-pointers.cpp   | 38 ++
 7 files changed, 151 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
 create mode 100644 clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/bugprone/bit-cast-pointers.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/bit-cast-pointers.cpp

diff --git a/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
new file mode 100644
index 00..4589a35a567552
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
@@ -0,0 +1,30 @@
+//===--- BitCastPointersCheck.cpp - clang-tidy 
===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "BitCastPointersCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+void BitCastPointersCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+  callExpr(callee(functionDecl(allOf(hasName("::std::bit_cast"),
+ hasAnyTemplateArgument(refersToType(
+ qualType(isAnyPointer(
+  .bind("x"),
+  this);
+}
+
+void BitCastPointersCheck::check(const MatchFinder::MatchResult &Result) {
+  if (const auto *MatchedDecl = Result.Nodes.getNodeAs("x"))
+diag(MatchedDecl->getBeginLoc(), "do not use std::bit_cast on pointers; 
use reinterpret_cast instead");
+}
+
+} // namespace clang::tidy::bugprone
diff --git a/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h 
b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
new file mode 100644
index 00..2083979e527fd3
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
@@ -0,0 +1,34 @@
+//===--- BitCastPointersCheck.h - clang-tidy *- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITCASTPOINTERSCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITCASTPOINTERSCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::bugprone {
+
+/// Warns about usage of ``std::bit_cast`` when either the input or output 
types
+/// are pointers.
+///
+/// For the user-facing documentation see:
+/// 
http://clang.llvm.org/extra/clang-tidy/checks/bugprone/bit-cast-pointers.html
+class BitCastPointersCheck : public ClangTidyCheck {
+public:
+  BitCastPointersCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
+return LangOpts.CPlusPlus20;
+  }
+};
+
+} // namespace clang::tidy::bugprone
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITCASTPOINTERSCHECK_H
diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp 
b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
index 689eb92a3d8d17..931624224d784b 100644
--- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
@@ -14,6 +14,7 @@
 #include "AssertSideEffectCheck.h"
 #include "AssignmentInIfCo

[clang-tools-extra] [clang-tidy][NFC] add qutation mark for C++ classes in warning message (PR #109068)

2024-09-18 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

> But the template mark is really hard to understand

I agree. Did you intend to remove that in this commit as well? I see it's still 
present.

https://github.com/llvm/llvm-project/pull/109068
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy][NFC] add qutation mark for C++ classes in warning message (PR #109068)

2024-09-18 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp approved this pull request.

LGTM, thanks for splitting the patch!

Personally I don't quite see the added value of having the quotes, I actually 
find them a bit noisy. It would have been good to write the motivation for this 
change in the commit message.

But this is quite subjective so if you find that it makes the diagnostic 
clearer then I won't object.

https://github.com/llvm/llvm-project/pull/109068
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Enforce SL.con.3: Add check to replace operator[] with at() [Cont.] (PR #95220)

2024-09-17 Thread Carlos Galvez via cfe-commits
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= 
Message-ID:
In-Reply-To: 


https://github.com/carlosgalvezp commented:

Looking good! I have some smaller comments. There are a few pending review 
comments, have they been addressed? If so it'd be good to mark them as Resolved.

https://github.com/llvm/llvm-project/pull/95220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Enforce SL.con.3: Add check to replace operator[] with at() [Cont.] (PR #95220)

2024-09-17 Thread Carlos Galvez via cfe-commits
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= 
Message-ID:
In-Reply-To: 



@@ -0,0 +1,227 @@
+// RUN: %check_clang_tidy -std=c++2b -check-suffix=DEFAULT %s \
+// RUN: cppcoreguidelines-pro-bounds-avoid-unchecked-container-accesses %t -- \
+// RUN: -config='{CheckOptions: 
{cppcoreguidelines-pro-bounds-avoid-unchecked-container-accesses.ExcludeClasses:
 "::ExcludedClass1;::ExcludedClass2"}}'
+
+// RUN: %check_clang_tidy -std=c++2b -check-suffix=AT %s \
+// RUN: cppcoreguidelines-pro-bounds-avoid-unchecked-container-accesses %t -- \
+// RUN: -config='{CheckOptions: 
{cppcoreguidelines-pro-bounds-avoid-unchecked-container-accesses.ExcludeClasses:
 "::ExcludedClass1;::ExcludedClass2", \
+// RUN: 
cppcoreguidelines-pro-bounds-avoid-unchecked-container-accesses.SubscriptFixMode:
 at}}'
+
+// RUN: %check_clang_tidy -std=c++2b -check-suffix=FUNC %s \
+// RUN: cppcoreguidelines-pro-bounds-avoid-unchecked-container-accesses %t -- \
+// RUN: -config='{CheckOptions: 
{cppcoreguidelines-pro-bounds-avoid-unchecked-container-accesses.ExcludeClasses:
 "::ExcludedClass1;::ExcludedClass2", \
+// RUN: 
cppcoreguidelines-pro-bounds-avoid-unchecked-container-accesses.SubscriptFixMode:
 function, \
+// RUN: 
cppcoreguidelines-pro-bounds-avoid-unchecked-container-accesses.SubscriptFixFunction:
 "f"}}'
+
+namespace std {
+  template
+  struct array {
+T operator[](unsigned i) {
+  return T{1};
+}
+T operator[]() {
+  return T{1};
+}
+T at(unsigned i) {
+  return T{1};
+}
+T at() {
+  return T{1};
+}
+  };
+
+  template
+  struct map {
+T operator[](unsigned i) {
+  return T{1};
+}
+T at(unsigned i) {
+  return T{1};
+}
+  };
+
+  template
+  struct unique_ptr {
+T operator[](unsigned i) {
+  return T{1};
+}
+  };
+
+  template
+  struct span {
+T operator[](unsigned i) {
+  return T{1};
+}
+  };
+} // namespace std
+
+namespace json {
+  template
+  struct node{
+T operator[](unsigned i) {
+  return T{1};
+}
+  };
+} // namespace json
+
+struct SubClass : std::array {};
+
+class ExcludedClass1 {
+  public:
+int operator[](unsigned i) {
+  return 1;
+}
+int at(unsigned i) {
+  return 1;
+}
+};
+
+class ExcludedClass2 {
+  public:
+int operator[](unsigned i) {
+  return 1;
+}
+int at(unsigned i) {
+  return 1;
+}
+};
+
+template int f(T, unsigned){ return 0;}
+template int f(T){ return 0;}
+
+std::array a;
+
+auto b = a[0];
+// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:11: warning: possibly unsafe 
'operator[]', consider bound-safe alternatives 
[cppcoreguidelines-pro-bounds-avoid-unchecked-container-accesses]
+// CHECK-FIXES-AT: auto b = a.at(0);
+// CHECK-FIXES-FUNC: auto b = f(a, 0);
+
+auto b23 = a[];

carlosgalvezp wrote:

Is this code legal? What is the index?

https://github.com/llvm/llvm-project/pull/95220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Enforce SL.con.3: Add check to replace operator[] with at() [Cont.] (PR #95220)

2024-09-17 Thread Carlos Galvez via cfe-commits
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= 
Message-ID:
In-Reply-To: 



@@ -0,0 +1,227 @@
+// RUN: %check_clang_tidy -std=c++2b -check-suffix=DEFAULT %s \
+// RUN: cppcoreguidelines-pro-bounds-avoid-unchecked-container-accesses %t -- \
+// RUN: -config='{CheckOptions: 
{cppcoreguidelines-pro-bounds-avoid-unchecked-container-accesses.ExcludeClasses:
 "::ExcludedClass1;::ExcludedClass2"}}'
+
+// RUN: %check_clang_tidy -std=c++2b -check-suffix=AT %s \
+// RUN: cppcoreguidelines-pro-bounds-avoid-unchecked-container-accesses %t -- \
+// RUN: -config='{CheckOptions: 
{cppcoreguidelines-pro-bounds-avoid-unchecked-container-accesses.ExcludeClasses:
 "::ExcludedClass1;::ExcludedClass2", \
+// RUN: 
cppcoreguidelines-pro-bounds-avoid-unchecked-container-accesses.SubscriptFixMode:
 at}}'
+
+// RUN: %check_clang_tidy -std=c++2b -check-suffix=FUNC %s \
+// RUN: cppcoreguidelines-pro-bounds-avoid-unchecked-container-accesses %t -- \
+// RUN: -config='{CheckOptions: 
{cppcoreguidelines-pro-bounds-avoid-unchecked-container-accesses.ExcludeClasses:
 "::ExcludedClass1;::ExcludedClass2", \
+// RUN: 
cppcoreguidelines-pro-bounds-avoid-unchecked-container-accesses.SubscriptFixMode:
 function, \
+// RUN: 
cppcoreguidelines-pro-bounds-avoid-unchecked-container-accesses.SubscriptFixFunction:
 "f"}}'
+
+namespace std {
+  template
+  struct array {
+T operator[](unsigned i) {
+  return T{1};
+}
+T operator[]() {
+  return T{1};
+}
+T at(unsigned i) {
+  return T{1};
+}
+T at() {
+  return T{1};
+}
+  };
+
+  template
+  struct map {
+T operator[](unsigned i) {
+  return T{1};
+}
+T at(unsigned i) {
+  return T{1};
+}
+  };
+
+  template
+  struct unique_ptr {
+T operator[](unsigned i) {
+  return T{1};
+}
+  };
+
+  template
+  struct span {
+T operator[](unsigned i) {
+  return T{1};
+}
+  };
+} // namespace std
+
+namespace json {
+  template
+  struct node{
+T operator[](unsigned i) {
+  return T{1};
+}
+  };
+} // namespace json
+
+struct SubClass : std::array {};
+
+class ExcludedClass1 {
+  public:
+int operator[](unsigned i) {
+  return 1;
+}
+int at(unsigned i) {
+  return 1;
+}
+};
+
+class ExcludedClass2 {
+  public:
+int operator[](unsigned i) {
+  return 1;
+}
+int at(unsigned i) {
+  return 1;
+}
+};
+
+template int f(T, unsigned){ return 0;}
+template int f(T){ return 0;}
+
+std::array a;
+
+auto b = a[0];
+// CHECK-MESSAGES-DEFAULT: :[[@LINE-1]]:11: warning: possibly unsafe 
'operator[]', consider bound-safe alternatives 
[cppcoreguidelines-pro-bounds-avoid-unchecked-container-accesses]

carlosgalvezp wrote:

Nit: I believe it should be "bounds-safe", not "bound-safe"

https://github.com/llvm/llvm-project/pull/95220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Enforce SL.con.3: Add check to replace operator[] with at() [Cont.] (PR #95220)

2024-09-17 Thread Carlos Galvez via cfe-commits
=��___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Enforce SL.con.3: Add check to replace operator[] with at() [Cont.] (PR #95220)

2024-09-17 Thread Carlos Galvez via cfe-commits
=��___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Enforce SL.con.3: Add check to replace operator[] with at() [Cont.] (PR #95220)

2024-09-17 Thread Carlos Galvez via cfe-commits
=��___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Enforce SL.con.3: Add check to replace operator[] with at() [Cont.] (PR #95220)

2024-09-17 Thread Carlos Galvez via cfe-commits
=��___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Enforce SL.con.3: Add check to replace operator[] with at() [Cont.] (PR #95220)

2024-09-17 Thread Carlos Galvez via cfe-commits
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= 
Message-ID:
In-Reply-To: 



@@ -101,6 +101,44 @@ Improvements to clang-tidy
 New checks
 ^^
 
+- New :doc:`bugprone-crtp-constructor-accessibility
+  ` check.
+
+  Detects error-prone Curiously Recurring Template Pattern usage, when the CRTP
+  can be constructed outside itself and the derived class.
+
+- New :doc:`bugprone-return-const-ref-from-parameter
+  ` check.
+
+  Detects return statements that return a constant reference parameter as 
constant
+  reference. This may cause use-after-free errors if the caller uses xvalues as
+  arguments.
+
+- New :doc:`bugprone-suspicious-stringview-data-usage
+  ` check.
+
+  Identifies suspicious usages of ``std::string_view::data()`` that could lead
+  to reading out-of-bounds data due to inadequate or incorrect string null
+  termination.
+
+- New :doc:`misc-use-internal-linkage
+  ` check.
+
+  Detects variables and functions that can be marked as static or moved into
+  an anonymous namespace to enforce internal linkage.
+
+- New :doc:`modernize-min-max-use-initializer-list
+  ` check.
+
+  Replaces nested ``std::min`` and ``std::max`` calls with an initializer list
+  where applicable.
+
+- New :doc:`cppcoreguidelines-pro-bounds-avoid-unchecked-container-accesses
+  
`
 check.
+
+  Flags the unsafe ``operator[]``. Can suggests fixing it with ``at()`` or a

carlosgalvezp wrote:

I'd rephrase it like "Flags calls to ``operator[]`` in STL containers, and 
suggests replacing it with safe alternatives.



https://github.com/llvm/llvm-project/pull/95220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Create bugprone-bit-cast-pointers check (PR #108083)

2024-09-17 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp updated 
https://github.com/llvm/llvm-project/pull/108083

>From c9e69b58e6c3e27fc6dd529783c02fec8fafbd9a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20G=C3=A1lvez?= 
Date: Tue, 10 Sep 2024 13:46:51 +
Subject: [PATCH] [clang-tidy] Create bugprone-bit-cast-pointers check

To detect unsafe use of bit_cast. Otherwise, bit_cast bypasses all
checks done by compilers and linters.

Fixes #106987
---
 .../bugprone/BitCastPointersCheck.cpp | 45 ++
 .../bugprone/BitCastPointersCheck.h   | 34 +++
 .../bugprone/BugproneTidyModule.cpp   |  3 +
 .../clang-tidy/bugprone/CMakeLists.txt|  1 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  6 ++
 .../checks/bugprone/bit-cast-pointers.rst | 52 
 .../checkers/bugprone/bit-cast-pointers.cpp   | 61 +++
 7 files changed, 202 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
 create mode 100644 clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/bugprone/bit-cast-pointers.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/bit-cast-pointers.cpp

diff --git a/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
new file mode 100644
index 00..30a700fc8c0062
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
@@ -0,0 +1,45 @@
+//===--- BitCastPointersCheck.cpp - clang-tidy 
===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "BitCastPointersCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+void BitCastPointersCheck::registerMatchers(MatchFinder *Finder) {
+  auto IsPointerType = refersToType(qualType(isAnyPointer()));
+  Finder->addMatcher(callExpr(hasDeclaration(functionDecl(allOf(
+  hasName("::std::bit_cast"),
+  hasTemplateArgument(0, IsPointerType),
+  hasTemplateArgument(1, IsPointerType)
+ .bind("bit_cast"),
+ this);
+
+  auto IsDoublePointerType =
+  hasType(qualType(pointsTo(qualType(isAnyPointer();
+  Finder->addMatcher(callExpr(hasArgument(0, IsDoublePointerType),
+  hasArgument(1, IsDoublePointerType),
+  
hasDeclaration(functionDecl(hasName("::memcpy"
+ .bind("memcpy"),
+ this);
+}
+
+void BitCastPointersCheck::check(const MatchFinder::MatchResult &Result) {
+  if (const auto *MatchedDecl = Result.Nodes.getNodeAs("bit_cast"))
+diag(MatchedDecl->getBeginLoc(),
+ "do not use std::bit_cast to cast between pointers")
+<< MatchedDecl->getSourceRange();
+  if (const auto *MatchedDecl = Result.Nodes.getNodeAs("memcpy"))
+diag(MatchedDecl->getBeginLoc(),
+ "do not use memcpy to cast between pointers")
+<< MatchedDecl->getSourceRange();
+}
+
+} // namespace clang::tidy::bugprone
diff --git a/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h 
b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
new file mode 100644
index 00..223ad95ddc90ba
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
@@ -0,0 +1,34 @@
+//===--- BitCastPointersCheck.h - clang-tidy *- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITCASTPOINTERSCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITCASTPOINTERSCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::bugprone {
+
+/// Warns about code that tries to cast between pointers by means of
+/// ``std::bit_cast`` or ``memcpy``.
+///
+/// For the user-facing documentation see:
+/// 
http://clang.llvm.org/extra/clang-tidy/checks/bugprone/bit-cast-pointers.html
+class BitCastPointersCheck : public ClangTidyCheck {
+public:
+  BitCastPointersCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  bool isLanguageVersion

[clang-tools-extra] [clang-tidy] Create bugprone-bit-cast-pointers check (PR #108083)

2024-09-17 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp updated 
https://github.com/llvm/llvm-project/pull/108083

>From d7eacb7d6614b374a959c33a7394f2652df32982 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20G=C3=A1lvez?= 
Date: Tue, 10 Sep 2024 13:46:51 +
Subject: [PATCH] [clang-tidy] Create bugprone-bit-cast-pointers check

To detect unsafe use of bit_cast. Otherwise, bit_cast bypasses all
checks done by compilers and linters.

Fixes #106987
---
 .../bugprone/BitCastPointersCheck.cpp | 45 ++
 .../bugprone/BitCastPointersCheck.h   | 34 +++
 .../bugprone/BugproneTidyModule.cpp   |  3 +
 .../clang-tidy/bugprone/CMakeLists.txt|  1 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  6 ++
 .../checks/bugprone/bit-cast-pointers.rst | 52 
 .../checkers/bugprone/bit-cast-pointers.cpp   | 61 +++
 7 files changed, 202 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
 create mode 100644 clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/bugprone/bit-cast-pointers.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/bit-cast-pointers.cpp

diff --git a/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
new file mode 100644
index 00..30a700fc8c0062
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
@@ -0,0 +1,45 @@
+//===--- BitCastPointersCheck.cpp - clang-tidy 
===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "BitCastPointersCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+void BitCastPointersCheck::registerMatchers(MatchFinder *Finder) {
+  auto IsPointerType = refersToType(qualType(isAnyPointer()));
+  Finder->addMatcher(callExpr(hasDeclaration(functionDecl(allOf(
+  hasName("::std::bit_cast"),
+  hasTemplateArgument(0, IsPointerType),
+  hasTemplateArgument(1, IsPointerType)
+ .bind("bit_cast"),
+ this);
+
+  auto IsDoublePointerType =
+  hasType(qualType(pointsTo(qualType(isAnyPointer();
+  Finder->addMatcher(callExpr(hasArgument(0, IsDoublePointerType),
+  hasArgument(1, IsDoublePointerType),
+  
hasDeclaration(functionDecl(hasName("::memcpy"
+ .bind("memcpy"),
+ this);
+}
+
+void BitCastPointersCheck::check(const MatchFinder::MatchResult &Result) {
+  if (const auto *MatchedDecl = Result.Nodes.getNodeAs("bit_cast"))
+diag(MatchedDecl->getBeginLoc(),
+ "do not use std::bit_cast to cast between pointers")
+<< MatchedDecl->getSourceRange();
+  if (const auto *MatchedDecl = Result.Nodes.getNodeAs("memcpy"))
+diag(MatchedDecl->getBeginLoc(),
+ "do not use memcpy to cast between pointers")
+<< MatchedDecl->getSourceRange();
+}
+
+} // namespace clang::tidy::bugprone
diff --git a/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h 
b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
new file mode 100644
index 00..857a35be13c72f
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
@@ -0,0 +1,34 @@
+//===--- BitCastPointersCheck.h - clang-tidy *- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITCASTPOINTERSCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITCASTPOINTERSCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::bugprone {
+
+/// Warns about usage of ``std::bit_cast`` when the input and output types are
+/// pointers.
+///
+/// For the user-facing documentation see:
+/// 
http://clang.llvm.org/extra/clang-tidy/checks/bugprone/bit-cast-pointers.html
+class BitCastPointersCheck : public ClangTidyCheck {
+public:
+  BitCastPointersCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  bool isLanguageVersionSupported(con

[clang-tools-extra] [clang-tidy]suggest use `std::span` as replacement of c array in C++20 for modernize-avoid-c-arrays (PR #108555)

2024-09-16 Thread Carlos Galvez via cfe-commits


@@ -1,9 +1,9 @@
-// RUN: %check_clang_tidy %s modernize-avoid-c-arrays %t -- \
+// RUN: %check_clang_tidy -std=c++17 %s modernize-avoid-c-arrays %t -- \
 // RUN:  -config='{CheckOptions: { modernize-avoid-c-arrays.AllowStringArrays: 
true }}'
 
 const char name[] = "name";
 const char array[] = {'n', 'a', 'm', 'e', '\0'};
 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not declare C-style arrays, use 
std::array<> instead [modernize-avoid-c-arrays]
 
 void takeCharArray(const char name[]);
-// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not declare C-style arrays, 
use std::array<> instead [modernize-avoid-c-arrays]
+// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not declare C-style arrays, 
use std::vector<> instead [modernize-avoid-c-arrays]

carlosgalvezp wrote:

(Doesn't need to be done in this patch)

https://github.com/llvm/llvm-project/pull/108555
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy]suggest use `std::span` as replacement of c array in C++20 for modernize-avoid-c-arrays (PR #108555)

2024-09-16 Thread Carlos Galvez via cfe-commits


@@ -1,9 +1,9 @@
-// RUN: %check_clang_tidy %s modernize-avoid-c-arrays %t -- \
+// RUN: %check_clang_tidy -std=c++17 %s modernize-avoid-c-arrays %t -- \
 // RUN:  -config='{CheckOptions: { modernize-avoid-c-arrays.AllowStringArrays: 
true }}'
 
 const char name[] = "name";
 const char array[] = {'n', 'a', 'm', 'e', '\0'};
 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not declare C-style arrays, use 
std::array<> instead [modernize-avoid-c-arrays]
 
 void takeCharArray(const char name[]);
-// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not declare C-style arrays, 
use std::array<> instead [modernize-avoid-c-arrays]
+// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not declare C-style arrays, 
use std::vector<> instead [modernize-avoid-c-arrays]

carlosgalvezp wrote:

Ok, I see what you mean now. While this solution is fine, I think it's probably 
easiest to let users specify what they should use via configuration. Most 
likely everyone has their own backports/coding guidelines to adhere to, and 
it'd be more valuable for them to be able to specify exactly what should be 
used.

https://github.com/llvm/llvm-project/pull/108555
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy]suggest use `std::span` as replacement of c array in C++20 for modernize-avoid-c-arrays (PR #108555)

2024-09-16 Thread Carlos Galvez via cfe-commits


@@ -10,29 +10,32 @@ modernize-avoid-c-arrays
 Finds C-style array types and recommend to use ``std::array<>`` /
 ``std::vector<>``. All types of C arrays are diagnosed.
 
+For parameters of incomplete C-style array type, it would be better to
+use ``std::span`` / ``gsl::span`` as replacement.
+
 However, fix-it are potentially dangerous in header files and are therefore not
 emitted right now.
 
 .. code:: c++
 
-  int a[] = {1, 2}; // warning: do not declare C-style arrays, use 
std::array<> instead
+  int a[] = {1, 2}; // warning: do not declare C-style arrays, use 
'std::array<>' instead

carlosgalvezp wrote:

Nit: is adding the single quotes needed for this patch? It introduces quite 
some review noise. It'd be preferable to apply this as a separate (NFC?) patch.

https://github.com/llvm/llvm-project/pull/108555
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Create bugprone-bit-cast-pointers check (PR #108083)

2024-09-16 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

Thanks for the clarifications, will address asap! 

It now comes to mind that we probably also want to check `memcpy(ptr, ptr)`, 
which is equivalent to `bit_cast`. In that case I wonder if the check name 
still holds or it should be named something else?

https://github.com/llvm/llvm-project/pull/108083
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Create bugprone-bit-cast-pointers check (PR #108083)

2024-09-15 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp edited 
https://github.com/llvm/llvm-project/pull/108083
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Create bugprone-bit-cast-pointers check (PR #108083)

2024-09-15 Thread Carlos Galvez via cfe-commits


@@ -0,0 +1,34 @@
+//===--- BitCastPointersCheck.h - clang-tidy *- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITCASTPOINTERSCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITCASTPOINTERSCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::bugprone {
+
+/// Warns about usage of ``std::bit_cast`` when the input and output types are
+/// pointers.
+///
+/// For the user-facing documentation see:
+/// 
http://clang.llvm.org/extra/clang-tidy/checks/bugprone/bit-cast-pointers.html
+class BitCastPointersCheck : public ClangTidyCheck {
+public:
+  BitCastPointersCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
+return LangOpts.CPlusPlus20;
+  }
+};

carlosgalvezp wrote:

I'm not familiar with traversal kinds, how should I change the code? What 
benefits would it bring? (Should the default traversal kind be changed in the 
add_new_check.py script?)

https://github.com/llvm/llvm-project/pull/108083
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Create bugprone-bit-cast-pointers check (PR #108083)

2024-09-15 Thread Carlos Galvez via cfe-commits


@@ -0,0 +1,32 @@
+//===--- BitCastPointersCheck.cpp - clang-tidy 
===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "BitCastPointersCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+void BitCastPointersCheck::registerMatchers(MatchFinder *Finder) {
+  auto IsPointerType = refersToType(qualType(isAnyPointer()));
+  Finder->addMatcher(callExpr(callee(functionDecl(allOf(
+  hasName("::std::bit_cast"),
+  hasTemplateArgument(0, IsPointerType),
+  hasTemplateArgument(1, IsPointerType)
+ .bind("x"),
+ this);
+}
+
+void BitCastPointersCheck::check(const MatchFinder::MatchResult &Result) {
+  if (const auto *MatchedDecl = Result.Nodes.getNodeAs("x"))

carlosgalvezp wrote:

In general it's good practice to check for null pointers, it's done in many 
tidy checks and is cheap to do. Node that the `getNodeAs` can return a nullptr 
even if there is a match, but it was not possible to cast to the chosen type. 

https://github.com/llvm/llvm-project/pull/108083
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Create bugprone-bit-cast-pointers check (PR #108083)

2024-09-15 Thread Carlos Galvez via cfe-commits


@@ -0,0 +1,32 @@
+//===--- BitCastPointersCheck.cpp - clang-tidy 
===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "BitCastPointersCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+void BitCastPointersCheck::registerMatchers(MatchFinder *Finder) {
+  auto IsPointerType = refersToType(qualType(isAnyPointer()));
+  Finder->addMatcher(callExpr(callee(functionDecl(allOf(
+  hasName("::std::bit_cast"),
+  hasTemplateArgument(0, IsPointerType),
+  hasTemplateArgument(1, IsPointerType)
+ .bind("x"),
+ this);
+}
+
+void BitCastPointersCheck::check(const MatchFinder::MatchResult &Result) {
+  if (const auto *MatchedDecl = Result.Nodes.getNodeAs("x"))
+diag(MatchedDecl->getBeginLoc(),
+ "do not use std::bit_cast on pointers; use it on values instead");

carlosgalvezp wrote:

I'm not sure I follow, how should I change the code?

https://github.com/llvm/llvm-project/pull/108083
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Create bugprone-bit-cast-pointers check (PR #108083)

2024-09-15 Thread Carlos Galvez via cfe-commits


@@ -0,0 +1,32 @@
+//===--- BitCastPointersCheck.cpp - clang-tidy 
===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "BitCastPointersCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+void BitCastPointersCheck::registerMatchers(MatchFinder *Finder) {
+  auto IsPointerType = refersToType(qualType(isAnyPointer()));
+  Finder->addMatcher(callExpr(callee(functionDecl(allOf(
+  hasName("::std::bit_cast"),
+  hasTemplateArgument(0, IsPointerType),
+  hasTemplateArgument(1, IsPointerType)
+ .bind("x"),
+ this);

carlosgalvezp wrote:

Yes, that's correct. I think maybe this can be added as an extension in a 
follow-up patch if needed. Right now we cover the most common pitfall; if 
people want to workaround the check it will always be possible to do that one 
way or another.

https://github.com/llvm/llvm-project/pull/108083
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Create bugprone-bit-cast-pointers check (PR #108083)

2024-09-15 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp updated 
https://github.com/llvm/llvm-project/pull/108083

>From cb4ea5f12c72dd547d6ae3e742e99d717289344e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20G=C3=A1lvez?= 
Date: Tue, 10 Sep 2024 13:46:51 +
Subject: [PATCH] [clang-tidy] Create bugprone-bit-cast-pointers check

To detect unsafe use of bit_cast. Otherwise, bit_cast bypasses all
checks done by compilers and linters.

Fixes #106987
---
 .../bugprone/BitCastPointersCheck.cpp | 32 ++
 .../bugprone/BitCastPointersCheck.h   | 34 +++
 .../bugprone/BugproneTidyModule.cpp   |  3 ++
 .../clang-tidy/bugprone/CMakeLists.txt|  1 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  6 +++
 .../checks/bugprone/bit-cast-pointers.rst | 42 +++
 .../checkers/bugprone/bit-cast-pointers.cpp   | 34 +++
 7 files changed, 152 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
 create mode 100644 clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/bugprone/bit-cast-pointers.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/bit-cast-pointers.cpp

diff --git a/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
new file mode 100644
index 00..30c026fba150a5
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
@@ -0,0 +1,32 @@
+//===--- BitCastPointersCheck.cpp - clang-tidy 
===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "BitCastPointersCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+void BitCastPointersCheck::registerMatchers(MatchFinder *Finder) {
+  auto IsPointerType = refersToType(qualType(isAnyPointer()));
+  Finder->addMatcher(callExpr(callee(functionDecl(allOf(
+  hasName("::std::bit_cast"),
+  hasTemplateArgument(0, IsPointerType),
+  hasTemplateArgument(1, IsPointerType)
+ .bind("x"),
+ this);
+}
+
+void BitCastPointersCheck::check(const MatchFinder::MatchResult &Result) {
+  if (const auto *MatchedDecl = Result.Nodes.getNodeAs("x"))
+diag(MatchedDecl->getBeginLoc(),
+ "do not use std::bit_cast on pointers; use it on values instead");
+}
+
+} // namespace clang::tidy::bugprone
diff --git a/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h 
b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
new file mode 100644
index 00..d7c23c187dae34
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
@@ -0,0 +1,34 @@
+//===--- BitCastPointersCheck.h - clang-tidy *- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITCASTPOINTERSCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITCASTPOINTERSCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::bugprone {
+
+/// Warns about usage of ``std::bit_cast`` when the input and output types are
+/// pointers.
+///
+/// For the user-facing documentation see:
+/// 
http://clang.llvm.org/extra/clang-tidy/checks/bugprone/bit-cast-pointers.html
+class BitCastPointersCheck : public ClangTidyCheck {
+public:
+  BitCastPointersCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
+return LangOpts.CPlusPlus20;
+  }
+};
+
+} // namespace clang::tidy::bugprone
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITCASTPOINTERSCHECK_H
diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp 
b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
index 689eb92a3d8d17..931624224d784b 100644
--- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
@@ -14,6 +14,7 @@
 #include "AssertSideEffectCheck.h"
 #include "AssignmentInIfConditionCheck.h"
 #include "BadSig

[clang-tools-extra] [clang-tidy]suggest use `std::span` as replacement of c array in C++20 for modernize-avoid-c-arrays (PR #108555)

2024-09-13 Thread Carlos Galvez via cfe-commits


@@ -1,9 +1,9 @@
-// RUN: %check_clang_tidy %s modernize-avoid-c-arrays %t -- \
+// RUN: %check_clang_tidy -std=c++17 %s modernize-avoid-c-arrays %t -- \
 // RUN:  -config='{CheckOptions: { modernize-avoid-c-arrays.AllowStringArrays: 
true }}'
 
 const char name[] = "name";
 const char array[] = {'n', 'a', 'm', 'e', '\0'};
 // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: do not declare C-style arrays, use 
std::array<> instead [modernize-avoid-c-arrays]
 
 void takeCharArray(const char name[]);
-// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not declare C-style arrays, 
use std::array<> instead [modernize-avoid-c-arrays]
+// CHECK-MESSAGES: :[[@LINE-1]]:26: warning: do not declare C-style arrays, 
use std::vector<> instead [modernize-avoid-c-arrays]

carlosgalvezp wrote:

The check should not advice this, because std::vector allocates dynamic memory, 
which C array does not. std::array is the modern equivalent.

https://github.com/llvm/llvm-project/pull/108555
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy]suggest use `std::span` as replacement of c array in C++20 for modernize-avoid-c-arrays (PR #108555)

2024-09-13 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

I'm not sure I understand this change. `std::span` is not a replacement for a C 
array, since it does not own memory. I don't understand also the change from 
suggesting to use `std::vector` instead of `std::array`

https://github.com/llvm/llvm-project/pull/108555
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Create bugprone-bit-cast-pointers check (PR #108083)

2024-09-11 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp updated 
https://github.com/llvm/llvm-project/pull/108083

>From c4d85703ee004522746df940f7b08cabaa0f4eca Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20G=C3=A1lvez?= 
Date: Tue, 10 Sep 2024 13:46:51 +
Subject: [PATCH] [clang-tidy] Create bugprone-bit-cast-pointers check

To detect unsafe use of bit_cast. Otherwise, bit_cast bypasses all
checks done by compilers and linters.

Fixes #106987
---
 .../bugprone/BitCastPointersCheck.cpp | 32 +++
 .../bugprone/BitCastPointersCheck.h   | 34 
 .../bugprone/BugproneTidyModule.cpp   |  3 ++
 .../clang-tidy/bugprone/CMakeLists.txt|  1 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  6 +++
 .../checks/bugprone/bit-cast-pointers.rst | 40 +++
 .../checkers/bugprone/bit-cast-pointers.cpp   | 34 
 7 files changed, 150 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
 create mode 100644 clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/bugprone/bit-cast-pointers.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/bit-cast-pointers.cpp

diff --git a/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
new file mode 100644
index 00..30c026fba150a5
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
@@ -0,0 +1,32 @@
+//===--- BitCastPointersCheck.cpp - clang-tidy 
===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "BitCastPointersCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+void BitCastPointersCheck::registerMatchers(MatchFinder *Finder) {
+  auto IsPointerType = refersToType(qualType(isAnyPointer()));
+  Finder->addMatcher(callExpr(callee(functionDecl(allOf(
+  hasName("::std::bit_cast"),
+  hasTemplateArgument(0, IsPointerType),
+  hasTemplateArgument(1, IsPointerType)
+ .bind("x"),
+ this);
+}
+
+void BitCastPointersCheck::check(const MatchFinder::MatchResult &Result) {
+  if (const auto *MatchedDecl = Result.Nodes.getNodeAs("x"))
+diag(MatchedDecl->getBeginLoc(),
+ "do not use std::bit_cast on pointers; use it on values instead");
+}
+
+} // namespace clang::tidy::bugprone
diff --git a/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h 
b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
new file mode 100644
index 00..d7c23c187dae34
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
@@ -0,0 +1,34 @@
+//===--- BitCastPointersCheck.h - clang-tidy *- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITCASTPOINTERSCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITCASTPOINTERSCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::bugprone {
+
+/// Warns about usage of ``std::bit_cast`` when the input and output types are
+/// pointers.
+///
+/// For the user-facing documentation see:
+/// 
http://clang.llvm.org/extra/clang-tidy/checks/bugprone/bit-cast-pointers.html
+class BitCastPointersCheck : public ClangTidyCheck {
+public:
+  BitCastPointersCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
+return LangOpts.CPlusPlus20;
+  }
+};
+
+} // namespace clang::tidy::bugprone
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITCASTPOINTERSCHECK_H
diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp 
b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
index 689eb92a3d8d17..931624224d784b 100644
--- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
@@ -14,6 +14,7 @@
 #include "AssertSideEffectCheck.h"
 #include "AssignmentInIfConditionCheck.h"
 #include "Bad

[clang-tools-extra] [clang-tidy] Create bugprone-bit-cast-pointers check (PR #108083)

2024-09-11 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

Perhaps we can start with pointer-to-pointer only, yes. Pointer-int conversions 
are still implementation-defined, but I guess it's less of a problem than UB.

The original paper only checked that both inputs are not pointers:

```cpp
  !(is_pointer_v &&
is_pointer_v) &&
```

https://github.com/llvm/llvm-project/pull/108083
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Create bugprone-bit-cast-pointers check (PR #108083)

2024-09-11 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

Perhaps we can start with only pointer-to-pointer, yes. The original intention 
was to forbid that:

https://www.open-std.org/jtc1/sc22/WG21/docs/papers/2016/p0476r0.html#det

```cpp


https://github.com/llvm/llvm-project/pull/108083
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Create bugprone-bit-cast-pointers check (PR #108083)

2024-09-10 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp created 
https://github.com/llvm/llvm-project/pull/108083

To detect unsafe use of bit_cast that should be reinterpret_cast instead. 
Otherwise, bit_cast bypasses all checks done by compilers and linters.

Fixes #106987

>From 6916d5ecdc327b2771fbbca226095bd99d394dab Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20G=C3=A1lvez?= 
Date: Tue, 10 Sep 2024 13:46:51 +
Subject: [PATCH] [clang-tidy] Create bugprone-bit-cast-pointers check

To detect unsafe use of bit_cast that should be reinterpret_cast
instead. Otherwise, bit_cast bypasses all checks done by compilers and
linters.

Fixes #106987
---
 .../bugprone/BitCastPointersCheck.cpp | 30 ++
 .../bugprone/BitCastPointersCheck.h   | 34 
 .../bugprone/BugproneTidyModule.cpp   |  3 ++
 .../clang-tidy/bugprone/CMakeLists.txt|  1 +
 clang-tools-extra/docs/ReleaseNotes.rst   |  6 +++
 .../checks/bugprone/bit-cast-pointers.rst | 39 +++
 .../checkers/bugprone/bit-cast-pointers.cpp   | 38 ++
 7 files changed, 151 insertions(+)
 create mode 100644 
clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
 create mode 100644 clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
 create mode 100644 
clang-tools-extra/docs/clang-tidy/checks/bugprone/bit-cast-pointers.rst
 create mode 100644 
clang-tools-extra/test/clang-tidy/checkers/bugprone/bit-cast-pointers.cpp

diff --git a/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
new file mode 100644
index 00..4589a35a567552
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.cpp
@@ -0,0 +1,30 @@
+//===--- BitCastPointersCheck.cpp - clang-tidy 
===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "BitCastPointersCheck.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+void BitCastPointersCheck::registerMatchers(MatchFinder *Finder) {
+  Finder->addMatcher(
+  callExpr(callee(functionDecl(allOf(hasName("::std::bit_cast"),
+ hasAnyTemplateArgument(refersToType(
+ qualType(isAnyPointer(
+  .bind("x"),
+  this);
+}
+
+void BitCastPointersCheck::check(const MatchFinder::MatchResult &Result) {
+  if (const auto *MatchedDecl = Result.Nodes.getNodeAs("x"))
+diag(MatchedDecl->getBeginLoc(), "do not use std::bit_cast on pointers; 
use reinterpret_cast instead");
+}
+
+} // namespace clang::tidy::bugprone
diff --git a/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h 
b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
new file mode 100644
index 00..2083979e527fd3
--- /dev/null
+++ b/clang-tools-extra/clang-tidy/bugprone/BitCastPointersCheck.h
@@ -0,0 +1,34 @@
+//===--- BitCastPointersCheck.h - clang-tidy *- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITCASTPOINTERSCHECK_H
+#define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITCASTPOINTERSCHECK_H
+
+#include "../ClangTidyCheck.h"
+
+namespace clang::tidy::bugprone {
+
+/// Warns about usage of ``std::bit_cast`` when either the input or output 
types
+/// are pointers.
+///
+/// For the user-facing documentation see:
+/// 
http://clang.llvm.org/extra/clang-tidy/checks/bugprone/bit-cast-pointers.html
+class BitCastPointersCheck : public ClangTidyCheck {
+public:
+  BitCastPointersCheck(StringRef Name, ClangTidyContext *Context)
+  : ClangTidyCheck(Name, Context) {}
+  void registerMatchers(ast_matchers::MatchFinder *Finder) override;
+  void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
+return LangOpts.CPlusPlus20;
+  }
+};
+
+} // namespace clang::tidy::bugprone
+
+#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_BUGPRONE_BITCASTPOINTERSCHECK_H
diff --git a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp 
b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
index 689eb92a3d8d17..931624224d784b 100644
--- a/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/BugproneTidyModule.cpp
@@ -14,6 +14,7 @@
 #inc

[clang-tools-extra] [clang-tidy][NFC] remove autosar link in documents (PR #107412)

2024-09-05 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp approved this pull request.

LGTM, thanks for the quick fix!

https://github.com/llvm/llvm-project/pull/107412
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Suggest using reinterpret_cast in bugprone-casting-thro… (PR #106784)

2024-09-04 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp closed 
https://github.com/llvm/llvm-project/pull/106784
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Suggest using reinterpret_cast in bugprone-casting-thro… (PR #106784)

2024-09-03 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp updated 
https://github.com/llvm/llvm-project/pull/106784

>From d88a21655116da6ce8fed558ed9d7ea1ceb5e862 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20G=C3=A1lvez?= 
Date: Fri, 30 Aug 2024 19:17:16 +
Subject: [PATCH] [clang-tidy] Suggest using reinterpret_cast in
 bugpronge-casting-through-void

reinterpret_cast is the equivalent construct, and more clearly
expresses intent.
---
 .../bugprone/CastingThroughVoidCheck.cpp  |  4 +-
 clang-tools-extra/docs/ReleaseNotes.rst   |  4 ++
 .../checks/bugprone/casting-through-void.rst  | 21 +-
 .../bugprone/casting-through-void.cpp | 40 +--
 4 files changed, 46 insertions(+), 23 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/CastingThroughVoidCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/CastingThroughVoidCheck.cpp
index 9e714b4be4dfea..f0a9ace2297406 100644
--- a/clang-tools-extra/clang-tidy/bugprone/CastingThroughVoidCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/CastingThroughVoidCheck.cpp
@@ -38,7 +38,9 @@ void CastingThroughVoidCheck::check(const 
MatchFinder::MatchResult &Result) {
   const auto ST = *Result.Nodes.getNodeAs("source_type");
   const auto VT = *Result.Nodes.getNodeAs("void_type");
   const auto *CE = Result.Nodes.getNodeAs("cast");
-  diag(CE->getExprLoc(), "do not cast %0 to %1 through %2") << ST << TT << VT;
+  diag(CE->getExprLoc(),
+   "do not cast %0 to %1 through %2; use reinterpret_cast instead")
+  << ST << TT << VT;
 }
 
 } // namespace clang::tidy::bugprone
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index b001a6ad446695..6999c1ef2ea4b0 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -104,6 +104,10 @@ New check aliases
 Changes in existing checks
 ^^
 
+- Improved :doc:`bugprone-casting-through-void
+  ` check to suggest replacing
+  the offending code with ``reinterpret_cast``, to more clearly express intent.
+
 - Improved :doc:`modernize-use-std-format
   ` check to support replacing
   member function calls too.
diff --git 
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/casting-through-void.rst 
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/casting-through-void.rst
index a9ab478b9a82e1..d9f94b6a3f20b9 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/casting-through-void.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/casting-through-void.rst
@@ -3,7 +3,9 @@
 bugprone-casting-through-void
 =
 
-Detects unsafe or redundant two-step casting operations involving ``void*``.
+Detects unsafe or redundant two-step casting operations involving ``void*``,
+which is equivalent to ``reinterpret_cast`` as per the
+`C++ Standard `_.
 
 Two-step type conversions via ``void*`` are discouraged for several reasons.
 
@@ -16,7 +18,17 @@ Two-step type conversions via ``void*`` are discouraged for 
several reasons.
 
 In summary, avoiding two-step type conversions through ``void*`` ensures 
clearer code,
 maintains essential compiler warnings, and prevents ambiguity and potential 
runtime
-errors, particularly in complex inheritance scenarios.
+errors, particularly in complex inheritance scenarios. If such a cast is 
wanted,
+it shall be done via ``reinterpret_cast``, to express the intent more clearly.
+
+Note: it is expected that, after applying the suggested fix and using
+``reinterpret_cast``, the check 
:doc:`cppcoreguidelines-pro-type-reinterpret-cast
+<../cppcoreguidelines/pro-type-reinterpret-cast>` will emit a warning.
+This is intentional: ``reinterpret_cast`` is a dangerous operation that can
+easily break the strict aliasing rules when dereferencing the casted pointer,
+invoking Undefined Behavior. The warning is there to prompt users to carefuly
+analyze whether the usage of ``reinterpret_cast`` is safe, in which case the
+warning may be suppressed.
 
 Examples:
 
@@ -29,3 +41,8 @@ Examples:
reinterpret_cast(reinterpret_cast(ptr)); // WRONG
(IntegerPointer)(void *)ptr; // WRONG
IntegerPointer(static_cast(ptr)); // WRONG
+
+   reinterpret_cast(ptr); // OK, clearly expresses intent.
+  // NOTE: dereferencing this pointer 
violates
+  // the strict aliasing rules, 
invoking
+  // Undefined Behavior.
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/casting-through-void.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/casting-through-void.cpp
index a784e498858738..68172212904f8a 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/casting-through-void.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/casting-through-void.cpp
@@ -10,42 +10,42 @@ const double cd = 100;
 
 void normal_test() {
   static_cast(s

[clang-tools-extra] [clang-tidy] Add option to cppcoreguidelines-pro-type-reinterpret-cas… (PR #106922)

2024-09-02 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp closed 
https://github.com/llvm/llvm-project/pull/106922
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add option to cppcoreguidelines-pro-type-reinterpret-cas… (PR #106922)

2024-09-02 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

On another thought, the use case is quite niche and it's probably best for 
people to create their own `safe_reinterpret_cast` wrapper. Let's keep the 
check simple.

https://github.com/llvm/llvm-project/pull/106922
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add option to cppcoreguidelines-pro-type-reinterpret-cas… (PR #106922)

2024-09-01 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp created 
https://github.com/llvm/llvm-project/pull/106922

…t to allow casts to byte types

These casts are safe according to the Standard, so add an option to allow them 
and not emit a warning. This helps silencing some noise and focusing on the 
unsafe casts.

>From 87f3bca5a4f0cdb7f560da1d11d76ee9f3ce9d0a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20G=C3=A1lvez?= 
Date: Sun, 1 Sep 2024 19:14:22 +
Subject: [PATCH] [clang-tidy] Add option to
 cppcoreguidelines-pro-type-reinterpret-cast to allow casts to byte types

These casts are safe according to the Standard, so add an option to
allow them and not emit a warning. This helps silencing some noise
and focusing on the unsafe casts.
---
 .../ProTypeReinterpretCastCheck.cpp   | 50 +--
 .../ProTypeReinterpretCastCheck.h |  7 ++-
 clang-tools-extra/docs/ReleaseNotes.rst   |  4 ++
 .../pro-type-reinterpret-cast.rst | 19 +++
 .../pro-type-reinterpret-cast.cpp | 36 -
 5 files changed, 109 insertions(+), 7 deletions(-)

diff --git 
a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeReinterpretCastCheck.cpp
 
b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeReinterpretCastCheck.cpp
index 14456caab612b7..6385dd5440901c 100644
--- 
a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeReinterpretCastCheck.cpp
+++ 
b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeReinterpretCastCheck.cpp
@@ -9,20 +9,64 @@
 #include "ProTypeReinterpretCastCheck.h"
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "llvm/ADT/STLExtras.h"
+#include 
+#include 
 
 using namespace clang::ast_matchers;
 
 namespace clang::tidy::cppcoreguidelines {
 
+static bool isCastToBytes(ASTContext const &Ctx,
+  CXXReinterpretCastExpr const &Expr) {
+  // https://eel.is/c++draft/basic.lval#11.3
+  static constexpr std::array AllowedByteTypes = {
+  "char",
+  "unsigned char",
+  "std::byte",
+  };
+
+  // We only care about pointer casts
+  QualType DestType = Expr.getTypeAsWritten();
+  if (!DestType->isPointerType())
+return false;
+
+  // Get the unqualified canonical type, and check if it's allowed
+  // We need to wrap the Type into a QualType to call getAsString()
+  const Type *UnqualDestType =
+  DestType.getCanonicalType()->getPointeeType().getTypePtr();
+  std::string DestTypeString = QualType(UnqualDestType, /*Quals=*/0)
+   .getAsString(Ctx.getPrintingPolicy());
+  return llvm::any_of(AllowedByteTypes, [DestTypeString](StringRef Type) {
+return Type == DestTypeString;
+  });
+}
+
+ProTypeReinterpretCastCheck::ProTypeReinterpretCastCheck(
+StringRef Name, ClangTidyContext *Context)
+: ClangTidyCheck(Name, Context),
+  AllowCastToBytes(Options.getLocalOrGlobal("AllowCastToBytes", false)) {}
+
+void ProTypeReinterpretCastCheck::storeOptions(
+ClangTidyOptions::OptionMap &Opts) {
+  Options.store(Opts, "AllowCastToBytes", AllowCastToBytes);
+}
+
 void ProTypeReinterpretCastCheck::registerMatchers(MatchFinder *Finder) {
   Finder->addMatcher(cxxReinterpretCastExpr().bind("cast"), this);
 }
 
 void ProTypeReinterpretCastCheck::check(
 const MatchFinder::MatchResult &Result) {
-  const auto *MatchedCast =
-  Result.Nodes.getNodeAs("cast");
-  diag(MatchedCast->getOperatorLoc(), "do not use reinterpret_cast");
+
+  if (const auto *MatchedCast =
+  Result.Nodes.getNodeAs("cast")) {
+ASTContext const &Ctx = *Result.Context;
+if (AllowCastToBytes && isCastToBytes(Ctx, *MatchedCast))
+  return;
+
+diag(MatchedCast->getOperatorLoc(), "do not use reinterpret_cast");
+  }
 }
 
 } // namespace clang::tidy::cppcoreguidelines
diff --git 
a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeReinterpretCastCheck.h 
b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeReinterpretCastCheck.h
index da001bfb85d787..66b46ba7e9f5b9 100644
--- 
a/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeReinterpretCastCheck.h
+++ 
b/clang-tools-extra/clang-tidy/cppcoreguidelines/ProTypeReinterpretCastCheck.h
@@ -19,13 +19,16 @@ namespace clang::tidy::cppcoreguidelines {
 /// 
http://clang.llvm.org/extra/clang-tidy/checks/cppcoreguidelines/pro-type-reinterpret-cast.html
 class ProTypeReinterpretCastCheck : public ClangTidyCheck {
 public:
-  ProTypeReinterpretCastCheck(StringRef Name, ClangTidyContext *Context)
-  : ClangTidyCheck(Name, Context) {}
+  ProTypeReinterpretCastCheck(StringRef Name, ClangTidyContext *Context);
   bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
 return LangOpts.CPlusPlus;
   }
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
+  void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
+
+private:
+  const bool AllowCastToBytes;
 };
 
 } // 

[clang-tools-extra] [NFC][clang-tidy] reword diagnostic note in definitions-in-headers (PR #106862)

2024-08-31 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

I suppose it doesn't hurt to mention it. Don't tests need to be updated too?

https://github.com/llvm/llvm-project/pull/106862
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Suggest using reinterpret_cast in bugprone-casting-thro… (PR #106784)

2024-08-30 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp created 
https://github.com/llvm/llvm-project/pull/106784

…ugh-void

reinterpret_cast is the equivalent construct, and more clearly expresses intent.

>From d3f0a650d7e3ad5bc8134e4c1fbb84ccb82d5105 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Carlos=20G=C3=A1lvez?= 
Date: Fri, 30 Aug 2024 19:17:16 +
Subject: [PATCH] [clang-tidy] Suggest using reinterpret_cast in
 bugpronge-casting-through-void

reinterpret_cast is the equivalent construct, and more clearly
expresses intent.
---
 .../bugprone/CastingThroughVoidCheck.cpp  |  4 +-
 clang-tools-extra/docs/ReleaseNotes.rst   |  4 ++
 .../checks/bugprone/casting-through-void.rst  | 20 +-
 .../bugprone/casting-through-void.cpp | 40 +--
 4 files changed, 45 insertions(+), 23 deletions(-)

diff --git a/clang-tools-extra/clang-tidy/bugprone/CastingThroughVoidCheck.cpp 
b/clang-tools-extra/clang-tidy/bugprone/CastingThroughVoidCheck.cpp
index 9e714b4be4dfea..f0a9ace2297406 100644
--- a/clang-tools-extra/clang-tidy/bugprone/CastingThroughVoidCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/CastingThroughVoidCheck.cpp
@@ -38,7 +38,9 @@ void CastingThroughVoidCheck::check(const 
MatchFinder::MatchResult &Result) {
   const auto ST = *Result.Nodes.getNodeAs("source_type");
   const auto VT = *Result.Nodes.getNodeAs("void_type");
   const auto *CE = Result.Nodes.getNodeAs("cast");
-  diag(CE->getExprLoc(), "do not cast %0 to %1 through %2") << ST << TT << VT;
+  diag(CE->getExprLoc(),
+   "do not cast %0 to %1 through %2; use reinterpret_cast instead")
+  << ST << TT << VT;
 }
 
 } // namespace clang::tidy::bugprone
diff --git a/clang-tools-extra/docs/ReleaseNotes.rst 
b/clang-tools-extra/docs/ReleaseNotes.rst
index b001a6ad446695..6999c1ef2ea4b0 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -104,6 +104,10 @@ New check aliases
 Changes in existing checks
 ^^
 
+- Improved :doc:`bugprone-casting-through-void
+  ` check to suggest replacing
+  the offending code with ``reinterpret_cast``, to more clearly express intent.
+
 - Improved :doc:`modernize-use-std-format
   ` check to support replacing
   member function calls too.
diff --git 
a/clang-tools-extra/docs/clang-tidy/checks/bugprone/casting-through-void.rst 
b/clang-tools-extra/docs/clang-tidy/checks/bugprone/casting-through-void.rst
index a9ab478b9a82e1..05125ae7bdc8ee 100644
--- a/clang-tools-extra/docs/clang-tidy/checks/bugprone/casting-through-void.rst
+++ b/clang-tools-extra/docs/clang-tidy/checks/bugprone/casting-through-void.rst
@@ -3,7 +3,9 @@
 bugprone-casting-through-void
 =
 
-Detects unsafe or redundant two-step casting operations involving ``void*``.
+Detects unsafe or redundant two-step casting operations involving ``void*``,
+which is equivalent to ``reinterpret_cast`` as per the
+`C++ Standard `_.
 
 Two-step type conversions via ``void*`` are discouraged for several reasons.
 
@@ -16,7 +18,16 @@ Two-step type conversions via ``void*`` are discouraged for 
several reasons.
 
 In summary, avoiding two-step type conversions through ``void*`` ensures 
clearer code,
 maintains essential compiler warnings, and prevents ambiguity and potential 
runtime
-errors, particularly in complex inheritance scenarios.
+errors, particularly in complex inheritance scenarios. If such a cast is 
wanted,
+it shall be done via ``reinterpret_cast``, to express the intent more clearly.
+
+Note: it is expected that, after applying the suggested fix and using
+``reinterpret_cast``, the check ``cppcoreguidelines-pro-type-reinterpret-cast``
+will emit a warning. This is intentional: ``reinterpret_cast`` is a dangerous
+operation that can easily break the strict aliasing rules when dereferencing
+the casted pointer, invoking Undefined Behavior. The warning is there to
+prompt users to carefuly analyze whether the usage of ``reinterpret_cast`` is
+safe, in which case the warning may be suppressed.
 
 Examples:
 
@@ -29,3 +40,8 @@ Examples:
reinterpret_cast(reinterpret_cast(ptr)); // WRONG
(IntegerPointer)(void *)ptr; // WRONG
IntegerPointer(static_cast(ptr)); // WRONG
+
+   reinterpret_cast(ptr); // OK, clearly expresses intent.
+  // NOTE: dereferencing this pointer 
violates
+  // the strict aliasing rules, 
invoking
+  // Undefined Behavior.
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/casting-through-void.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/casting-through-void.cpp
index a784e498858738..68172212904f8a 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/bugprone/casting-through-void.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/bugprone/casting-through-void.cpp
@@ -10,42 +10,42 @@ const double cd = 100;
 

[clang-tools-extra] [clang-tidy] Use upper case letters for bool conversion suffix (PR #104882)

2024-08-27 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

Thanks, merged! I've edited the commit message a bit to better reflect the 
content of the patch, as well as mark a related issue as fixed.

https://github.com/llvm/llvm-project/pull/104882
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Use upper case letters for bool conversion suffix (PR #104882)

2024-08-27 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp closed 
https://github.com/llvm/llvm-project/pull/104882
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Use upper case letters for bool conversion suffix (PR #104882)

2024-08-26 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

@Da-Viper I believe there's some merge conflicts to fix before being able to 
merge :) 

https://github.com/llvm/llvm-project/pull/104882
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Use upper case letters for bool conversion suffix (PR #104882)

2024-08-25 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp approved this pull request.

LGTM

https://github.com/llvm/llvm-project/pull/104882
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Use upper case letters for bool conversion suffix (PR #104882)

2024-08-20 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp approved this pull request.

LGTM, maybe give a couple more days for other reviewers in case they have more 
comments.

https://github.com/llvm/llvm-project/pull/104882
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] use upper case letters for bool conversion suffix (PR #102831)

2024-08-18 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp commented:

LGTM, very minor nits!

https://github.com/llvm/llvm-project/pull/102831
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] use upper case letters for bool conversion suffix (PR #102831)

2024-08-18 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp edited 
https://github.com/llvm/llvm-project/pull/102831
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] use upper case letters for bool conversion suffix (PR #102831)

2024-08-18 Thread Carlos Galvez via cfe-commits


@@ -133,3 +133,17 @@ Options
 
When `true`, the check will allow conditional pointer conversions. Default
is `false`.
+
+.. option::  UseUpperCaseLiteralSuffix
+
+   When `true`, the replacements will use an uppercase literal suffix in the
+   provided fixes. Default a lowercase literal suffix is used.
+
+Example

carlosgalvezp wrote:

Personally I don't think this is needed, the above text is already clear 
enough. But I'm fine to keep it.

https://github.com/llvm/llvm-project/pull/102831
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] use upper case letters for bool conversion suffix (PR #102831)

2024-08-18 Thread Carlos Galvez via cfe-commits


@@ -133,3 +133,17 @@ Options
 
When `true`, the check will allow conditional pointer conversions. Default
is `false`.
+
+.. option::  UseUpperCaseLiteralSuffix
+
+   When `true`, the replacements will use an uppercase literal suffix in the
+   provided fixes. Default a lowercase literal suffix is used.

carlosgalvezp wrote:

Default is `false`.

https://github.com/llvm/llvm-project/pull/102831
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] use upper case letters for bool conversion suffix (PR #102831)

2024-08-18 Thread Carlos Galvez via cfe-commits


@@ -108,6 +108,11 @@ Changes in existing checks
   ` check to
   remove `->`, when reduntant `get()` is removed.
 
+- Improved :doc:`readablility-implicit-bool-conversion
+  ` check
+  Added option `UseUpperCaseLiteralSuffix` to  to select the

carlosgalvezp wrote:

Remove duplicated "to" and extra whitespace.

https://github.com/llvm/llvm-project/pull/102831
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] use upper case letters for bool conversion suffix (PR #102831)

2024-08-18 Thread Carlos Galvez via cfe-commits


@@ -108,6 +108,11 @@ Changes in existing checks
   ` check to
   remove `->`, when reduntant `get()` is removed.
 
+- Improved :doc:`readablility-implicit-bool-conversion
+  ` check

carlosgalvezp wrote:

Improved... by adding the option `UseUpperCaseLiteralSuffix`...

https://github.com/llvm/llvm-project/pull/102831
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] use upper case letters for bool conversion suffix (PR #102831)

2024-08-18 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

> An inconsistency is that readability-uppercase-literal-suffix only handles l 
> and u by default whereas the check here also handles f

Actually `readability-uppercase-literal-suffix` does handle `f` as well so I'm 
not sure I understand this comment.

https://github.com/llvm/llvm-project/pull/102831
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Consider `readability-uppercase-literal-suffix` when dealing with `readability-implicit-bool-conversion`. (PR #104694)

2024-08-18 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

No problem, thanks for the initiative! We should probably clarify this in the 
developer documentation.

https://github.com/llvm/llvm-project/pull/104694
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Consider `readability-uppercase-literal-suffix` when dealing with `readability-implicit-bool-conversion`. (PR #104694)

2024-08-18 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp edited 
https://github.com/llvm/llvm-project/pull/104694
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Consider `readability-uppercase-literal-suffix` when dealing with `readability-implicit-bool-conversion`. (PR #104694)

2024-08-18 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp edited 
https://github.com/llvm/llvm-project/pull/104694
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Consider `readability-uppercase-literal-suffix` when dealing with `readability-implicit-bool-conversion`. (PR #104694)

2024-08-18 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp requested changes to this pull request.

We have explicitly said in the other PR that we do *not* want checks to depend 
on each other. Checks must remain independent of each other.

https://github.com/llvm/llvm-project/pull/104694
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] use upper case letters for bool conversion suffix (PR #102831)

2024-08-16 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

> An inconsistency is that readability-uppercase-literal-suffix only handles l 
> and u by default whereas the check here also handles f

Then that check should be updated to handle the F suffix as well. 

It doesn't make sense to have different style for different suffixes: either 
all lower case or all upper case.

https://github.com/llvm/llvm-project/pull/102831
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Turn -Wenum-constexpr-conversion into a hard error (PR #102364)

2024-08-14 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp closed 
https://github.com/llvm/llvm-project/pull/102364
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Turn -Wenum-constexpr-conversion into a hard error (PR #102364)

2024-08-14 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

Boost/MPL 1.86.0 is now released! 

https://github.com/boostorg/mpl/releases/tag/boost-1.86.0

Thus I'm merging this patch, thanks for the reviews! I will keep an eye in case 
there's need for a revert.

https://github.com/llvm/llvm-project/pull/102364
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] use upper case letters for bool conversion suffix (PR #102831)

2024-08-14 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

A bit of nitpick, but would it make sense to have some consistency with 
`readability-identifier-naming`?

Instead of `UseUpperCase`, I'm thinking of:

`LiteralSuffixCase: LowerCase/UpperCase`

Like I said, not a big deal, I'm just posting as suggestion in case you like it 
better.

https://github.com/llvm/llvm-project/pull/102831
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Turn -Wenum-constexpr-conversion into a hard error (PR #102364)

2024-08-13 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

> How soon after 1.86 landing do you plan to submit this?

I was thinking as soon as it's released, I don't see a reason for waiting any 
longer. The sooner we merge the sooner we can collect feedback and re-adjust if 
needed. But of course it's up to the Clang owners to decide :) 

https://github.com/llvm/llvm-project/pull/102364
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Turn -Wenum-constexpr-conversion into a hard error (PR #102364)

2024-08-12 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

Since boost/mpl is at the core of issues and many projects depend directly or 
transitively on it, I think it might be good to wait until version 1.86 is 
released, so people can bump to a release version instead of trunk. 

It should be around the corner, [AFAICS](https://www.boost.org/users/history/) 
they've been releasing around this date regularly in the past years.

https://github.com/llvm/llvm-project/pull/102364
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] use upper case letters for bool conversion suffix (PR #102831)

2024-08-12 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

> Is it possible to check whether is other check enable?

It might be technically possible, but it's undesirable as it makes the whole 
infrastructure more complex and doubles the amount of testing. Checks should be 
independent of each other.

https://github.com/llvm/llvm-project/pull/102831
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] use upper case letters for bool conversion suffix (PR #102831)

2024-08-11 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp requested changes to this pull request.

There might be people who don't want uppercase suffix, so this will cause 
problems to them. This should be put into a configuration option instead.

https://github.com/llvm/llvm-project/pull/102831
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Turn -Wenum-constexpr-conversion into a hard error (PR #102364)

2024-08-09 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

Thanks for the review and the info @thesamesam ! AFAICS the MPL issue is fixed 
on trunk, targeting release 1.86.

About Pycuda, it's also stemming from MPL according to the build logs.

So it seems to me the solution to these projects is too "just" bump to the 
newer MPL version, which should be backwards compatible.

https://github.com/llvm/llvm-project/pull/102364
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Turn -Wenum-constexpr-conversion into a hard error (PR #102364)

2024-08-08 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

By the way, the latest revision of the GDB patch (improved after the first 
round of review) can be found here: 
https://sourceware.org/pipermail/gdb-patches/2024-June/210252.html

https://github.com/llvm/llvm-project/pull/102364
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Turn -Wenum-constexpr-conversion into a hard error (PR #102364)

2024-08-08 Thread Carlos Galvez via cfe-commits


@@ -49,6 +49,9 @@ C++ Specific Potentially Breaking Changes
   few users and can be written as ``__is_same(__remove_cv(T), 
decltype(nullptr))``,
   which GCC supports as well.
 
+- The warning `-Wenum-constexpr-conversion` has been upgraded into a hard
+  compiler error that cannot be suppressed, as required by the C++ Standard.

carlosgalvezp wrote:

That's a great formulation so I'll go straight ahead and copy it here, thanks! 
Added further clarification

https://github.com/llvm/llvm-project/pull/102364
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang] Turn -Wenum-constexpr-conversion into a hard error (PR #102364)

2024-08-08 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp updated 
https://github.com/llvm/llvm-project/pull/102364

>From 88f1a873d6c6ef06ad9a1847d098d65845cf1469 Mon Sep 17 00:00:00 2001
From: Carlos Galvez 
Date: Tue, 6 Aug 2024 22:50:10 +0200
Subject: [PATCH 1/2] [clang] Turn -Wenum-constexpr-conversion into a hard
 error

The warning has been active for a few releases now, first only in
user code, later in system headers, and finally as an error by
default.

Therefore, we believe it is now time to transition into a hard error,
as required by the C++ Standard. The main affected C++ projects
have by now fixed the error, or there's a pending patch for review
that does it.

Fixes #59036
---
 clang/docs/ReleaseNotes.rst   |  3 ++
 .../include/clang/Basic/DiagnosticASTKinds.td |  5 ++-
 clang/lib/AST/ExprConstant.cpp|  6 ++--
 clang/lib/AST/Interp/Interp.cpp   |  4 +--
 clang/test/AST/Interp/cxx11.cpp   | 27 ++-
 .../enum-constexpr-conversion-system-header.h |  3 +-
 .../SemaCXX/constant-expression-cxx11.cpp | 33 ---
 clang/test/SemaCXX/cxx2a-consteval.cpp|  3 +-
 8 files changed, 53 insertions(+), 31 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index d322da81723a5f..18e20d04298ae2 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -49,6 +49,9 @@ C++ Specific Potentially Breaking Changes
   few users and can be written as ``__is_same(__remove_cv(T), 
decltype(nullptr))``,
   which GCC supports as well.
 
+- The warning `-Wenum-constexpr-conversion` has been upgraded into a hard
+  compiler error that cannot be suppressed, as required by the C++ Standard.
+
 ABI Changes in This Version
 ---
 
diff --git a/clang/include/clang/Basic/DiagnosticASTKinds.td 
b/clang/include/clang/Basic/DiagnosticASTKinds.td
index a024f9b2a9f8c0..af53cf651e51ab 100644
--- a/clang/include/clang/Basic/DiagnosticASTKinds.td
+++ b/clang/include/clang/Basic/DiagnosticASTKinds.td
@@ -410,10 +410,9 @@ def warn_integer_constant_overflow : Warning<
 def warn_fixedpoint_constant_overflow : Warning<
   "overflow in expression; result is %0 with type %1">,
   InGroup>;
-def warn_constexpr_unscoped_enum_out_of_range : Warning<
+def note_constexpr_unscoped_enum_out_of_range : Note<
   "integer value %0 is outside the valid range of values [%1, %2] for the "
-  "enumeration type %3">, DefaultError, ShowInSystemHeader, ShowInSystemMacro,
-  InGroup>;
+  "enumeration type %3">;
 
 // This is a temporary diagnostic, and shall be removed once our
 // implementation is complete, and like the preceding constexpr notes belongs
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index d4b9095c7509b3..f2b8161f4a9014 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -14406,14 +14406,12 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr 
*E) {
 if (ED->getNumNegativeBits() && ConstexprVar &&
 (Max.slt(Result.getInt().getSExtValue()) ||
  Min.sgt(Result.getInt().getSExtValue(
-  Info.Ctx.getDiagnostics().Report(
-  E->getExprLoc(), diag::warn_constexpr_unscoped_enum_out_of_range)
+  Info.CCEDiag(E, diag::note_constexpr_unscoped_enum_out_of_range)
   << llvm::toString(Result.getInt(), 10) << Min.getSExtValue()
   << Max.getSExtValue() << ED;
 else if (!ED->getNumNegativeBits() && ConstexprVar &&
  Max.ult(Result.getInt().getZExtValue()))
-  Info.Ctx.getDiagnostics().Report(
-  E->getExprLoc(), diag::warn_constexpr_unscoped_enum_out_of_range)
+  Info.CCEDiag(E, diag::note_constexpr_unscoped_enum_out_of_range)
   << llvm::toString(Result.getInt(), 10) << Min.getZExtValue()
   << Max.getZExtValue() << ED;
   }
diff --git a/clang/lib/AST/Interp/Interp.cpp b/clang/lib/AST/Interp/Interp.cpp
index 0f72b860ddad77..f2440f698b22b2 100644
--- a/clang/lib/AST/Interp/Interp.cpp
+++ b/clang/lib/AST/Interp/Interp.cpp
@@ -924,12 +924,12 @@ void diagnoseEnumValue(InterpState &S, CodePtr OpPC, 
const EnumDecl *ED,
   if (ED->getNumNegativeBits() &&
   (Max.slt(Value.getSExtValue()) || Min.sgt(Value.getSExtValue( {
 const SourceLocation &Loc = S.Current->getLocation(OpPC);
-S.report(Loc, diag::warn_constexpr_unscoped_enum_out_of_range)
+S.CCEDiag(Loc, diag::note_constexpr_unscoped_enum_out_of_range)
 << llvm::toString(Value, 10) << Min.getSExtValue() << 
Max.getSExtValue()
 << ED;
   } else if (!ED->getNumNegativeBits() && Max.ult(Value.getZExtValue())) {
 const SourceLocation &Loc = S.Current->getLocation(OpPC);
-S.report(Loc, diag::warn_constexpr_unscoped_enum_out_of_range)
+S.CCEDiag(Loc, diag::note_constexpr_unscoped_enum_out_of_range)
 << llvm::toString(Value, 10) << Min.getZExtValue() << 
Max.getZExtValue()
 << ED;
   }
d

[clang] [clang] Turn -Wenum-constexpr-conversion into a hard error (PR #102364)

2024-08-07 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp created 
https://github.com/llvm/llvm-project/pull/102364

The warning has been active for a few releases now, first only in user code, 
later in system headers, and finally as an error by default.

Therefore, we believe it is now time to transition into a hard error, as 
required by the C++ Standard. The main affected C++ projects have by now fixed 
the error, or there's a pending patch for review that does it.

Fixes #59036

>From 88f1a873d6c6ef06ad9a1847d098d65845cf1469 Mon Sep 17 00:00:00 2001
From: Carlos Galvez 
Date: Tue, 6 Aug 2024 22:50:10 +0200
Subject: [PATCH] [clang] Turn -Wenum-constexpr-conversion into a hard error

The warning has been active for a few releases now, first only in
user code, later in system headers, and finally as an error by
default.

Therefore, we believe it is now time to transition into a hard error,
as required by the C++ Standard. The main affected C++ projects
have by now fixed the error, or there's a pending patch for review
that does it.

Fixes #59036
---
 clang/docs/ReleaseNotes.rst   |  3 ++
 .../include/clang/Basic/DiagnosticASTKinds.td |  5 ++-
 clang/lib/AST/ExprConstant.cpp|  6 ++--
 clang/lib/AST/Interp/Interp.cpp   |  4 +--
 clang/test/AST/Interp/cxx11.cpp   | 27 ++-
 .../enum-constexpr-conversion-system-header.h |  3 +-
 .../SemaCXX/constant-expression-cxx11.cpp | 33 ---
 clang/test/SemaCXX/cxx2a-consteval.cpp|  3 +-
 8 files changed, 53 insertions(+), 31 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index d322da81723a5..18e20d04298ae 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -49,6 +49,9 @@ C++ Specific Potentially Breaking Changes
   few users and can be written as ``__is_same(__remove_cv(T), 
decltype(nullptr))``,
   which GCC supports as well.
 
+- The warning `-Wenum-constexpr-conversion` has been upgraded into a hard
+  compiler error that cannot be suppressed, as required by the C++ Standard.
+
 ABI Changes in This Version
 ---
 
diff --git a/clang/include/clang/Basic/DiagnosticASTKinds.td 
b/clang/include/clang/Basic/DiagnosticASTKinds.td
index a024f9b2a9f8c..af53cf651e51a 100644
--- a/clang/include/clang/Basic/DiagnosticASTKinds.td
+++ b/clang/include/clang/Basic/DiagnosticASTKinds.td
@@ -410,10 +410,9 @@ def warn_integer_constant_overflow : Warning<
 def warn_fixedpoint_constant_overflow : Warning<
   "overflow in expression; result is %0 with type %1">,
   InGroup>;
-def warn_constexpr_unscoped_enum_out_of_range : Warning<
+def note_constexpr_unscoped_enum_out_of_range : Note<
   "integer value %0 is outside the valid range of values [%1, %2] for the "
-  "enumeration type %3">, DefaultError, ShowInSystemHeader, ShowInSystemMacro,
-  InGroup>;
+  "enumeration type %3">;
 
 // This is a temporary diagnostic, and shall be removed once our
 // implementation is complete, and like the preceding constexpr notes belongs
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index d4b9095c7509b..f2b8161f4a901 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -14406,14 +14406,12 @@ bool IntExprEvaluator::VisitCastExpr(const CastExpr 
*E) {
 if (ED->getNumNegativeBits() && ConstexprVar &&
 (Max.slt(Result.getInt().getSExtValue()) ||
  Min.sgt(Result.getInt().getSExtValue(
-  Info.Ctx.getDiagnostics().Report(
-  E->getExprLoc(), diag::warn_constexpr_unscoped_enum_out_of_range)
+  Info.CCEDiag(E, diag::note_constexpr_unscoped_enum_out_of_range)
   << llvm::toString(Result.getInt(), 10) << Min.getSExtValue()
   << Max.getSExtValue() << ED;
 else if (!ED->getNumNegativeBits() && ConstexprVar &&
  Max.ult(Result.getInt().getZExtValue()))
-  Info.Ctx.getDiagnostics().Report(
-  E->getExprLoc(), diag::warn_constexpr_unscoped_enum_out_of_range)
+  Info.CCEDiag(E, diag::note_constexpr_unscoped_enum_out_of_range)
   << llvm::toString(Result.getInt(), 10) << Min.getZExtValue()
   << Max.getZExtValue() << ED;
   }
diff --git a/clang/lib/AST/Interp/Interp.cpp b/clang/lib/AST/Interp/Interp.cpp
index 0f72b860ddad7..f2440f698b22b 100644
--- a/clang/lib/AST/Interp/Interp.cpp
+++ b/clang/lib/AST/Interp/Interp.cpp
@@ -924,12 +924,12 @@ void diagnoseEnumValue(InterpState &S, CodePtr OpPC, 
const EnumDecl *ED,
   if (ED->getNumNegativeBits() &&
   (Max.slt(Value.getSExtValue()) || Min.sgt(Value.getSExtValue( {
 const SourceLocation &Loc = S.Current->getLocation(OpPC);
-S.report(Loc, diag::warn_constexpr_unscoped_enum_out_of_range)
+S.CCEDiag(Loc, diag::note_constexpr_unscoped_enum_out_of_range)
 << llvm::toString(Value, 10) << Min.getSExtValue() << 
Max.getSExtValue()
 << ED;
   } else if (!ED->get

[clang-tools-extra] Enforce SL.con.3: Add check to replace operator[] with at() [Cont.] (PR #95220)

2024-08-03 Thread Carlos Galvez via cfe-commits
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= 
Message-ID:
In-Reply-To: 


carlosgalvezp wrote:

> It seems that this check is very similar up the 
> `cppcoreguidelines-pro-bounds-constant-array-index` check, Is there not a way 
> this could be merged with that check. I'm mindful that people will have both 
> checks enabled and get 2 different warnings for the same violation

Since it's 2 different rules from the guidelines I think it's better to have 
them as separate  checks to have a 1:1 mapping. People can easily suppress both 
of they want via `NOLINT(cppcoreguidelines-probounds-*` if needed.

Otherwise the check will do "too much" and there will come a day where someone 
will want to split the check into two as it has happened in the past.

https://github.com/llvm/llvm-project/pull/95220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add modernize-use-cpp-style-comments check (PR #99713)

2024-07-20 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp requested changes to this pull request.

Good check idea! A couple of comments:

- This is not really a "modernize"check. Both styles are equally valid and it's 
a matter of taste. Modernize is typically for checks that bump from one 
standard to a newer one.

- Instead I think this check fits better in "readability".

- I think we can take the opportunity and make the check scope slightly more 
general, so that it enforces ether C style comments or C++ style comments. So 
the check could be called "readability-comment-style". Right now you can 
support only C->C++ conversion but can be extended in the future to do the 
opposite.

https://github.com/llvm/llvm-project/pull/99713
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Add modernize-use-cpp-style-comments check (PR #99713)

2024-07-19 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

Good check idea! A couple of comments: 

- This is not really a "modernize"check. Both styles are equally valid and it's 
a matter of taste. Modernize is typically for checks that bump from one 
standard to a newer one. 

- Instead I think this check fits better in "readability".

- I think we can take the opportunity and make the check scope slightly more 
general, so that it enforces ether C style comments or C++ style comments. So 
the check could be called "readability-comment-style". Right now you can 
support only C->C++ conversion but can be extended in the future to do the 
opposite.

https://github.com/llvm/llvm-project/pull/99713
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy][cppcoreguidelines-missing-std-forward] Do not warn when the parameter is used in a `static_cast`. (PR #99477)

2024-07-18 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

This is not allowed by the relevant rule of the C++ Core Guidelines. We must be 
careful to implement exactly only what the rules say, nothing more. You can on 
course open a ticket to the C++ Core Guidelines asking them to change the rule 
text.

IMHO this case seems too niche and should be handled by a NOLINT instead. 
Otherwise, and off-by-default  configuration option.

https://github.com/llvm/llvm-project/pull/99477
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Do not warn on const variables in misc-use-internal-linkage (PR #97969)

2024-07-09 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp closed 
https://github.com/llvm/llvm-project/pull/97969
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Do not warn on const variables in misc-use-internal-linkage (PR #97969)

2024-07-08 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

> When check were created with C++ in mind, it could work also for C. Would be 
> good to support both, or mention in documentation that check is limited to 
> C++, and maybe if there will be requests in future, it could be expanded to C.

Thanks for the input, in that case I think it's better to revert to the 
previous version, where the code is more self-documenting. Since I got it 
approved before I'll merge as soon as the checks pass!

https://github.com/llvm/llvm-project/pull/97969
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Do not warn on const variables in misc-use-internal-linkage (PR #97969)

2024-07-08 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp updated 
https://github.com/llvm/llvm-project/pull/97969

>From 07bd62f97d203bbdb865cd4b1e14cef3ead70c80 Mon Sep 17 00:00:00 2001
From: Carlos Galvez 
Date: Sun, 7 Jul 2024 21:11:54 +0200
Subject: [PATCH] [clang-tidy] Do not warn on const variables in
 misc-use-internal-linkage

Since in C++ they already have implicit internal linkage.

Fixes #97947
---
 .../clang-tidy/misc/UseInternalLinkageCheck.cpp | 6 ++
 .../clang-tidy/checkers/misc/use-internal-linkage-var.cpp   | 3 +++
 2 files changed, 9 insertions(+)

diff --git a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp 
b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
index 44ccc2bc906a5..c086e7721e02b 100644
--- a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
@@ -121,6 +121,12 @@ void UseInternalLinkageCheck::check(const 
MatchFinder::MatchResult &Result) {
 return;
   }
   if (const auto *VD = Result.Nodes.getNodeAs("var")) {
+// In C++, const variables at file scope have implicit internal linkage,
+// so we should not warn there. This is not the case in C.
+// https://eel.is/c++draft/diff#basic-3
+if (getLangOpts().CPlusPlus && VD->getType().isConstQualified())
+  return;
+
 DiagnosticBuilder DB = diag(VD->getLocation(), Message) << "variable" << 
VD;
 SourceLocation FixLoc = VD->getTypeSpecStartLoc();
 if (FixLoc.isInvalid() || FixLoc.isMacroID())
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp
index 01b8d28e61230..6777ce4bb0265 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp
@@ -42,3 +42,6 @@ int global_in_extern_c_1;
 }
 
 extern "C" int global_in_extern_c_2;
+
+const int const_global = 123;
+constexpr int constexpr_global = 123;

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Do not warn on const variables in misc-use-internal-linkage (PR #97969)

2024-07-08 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp updated 
https://github.com/llvm/llvm-project/pull/97969

>From a1fec907b5d3920d5dda8761b6e173e153b7f281 Mon Sep 17 00:00:00 2001
From: Carlos Galvez 
Date: Sun, 7 Jul 2024 21:11:54 +0200
Subject: [PATCH] [clang-tidy] Do not warn on const variables in
 misc-use-internal-linkage

Since in C++ they already have implicit internal linkage.
https://eel.is/c++draft/diff#basic-3

Also, ensure that the check is only active in C++ code, since the
warning message presents anonymous namespaces as a solution, which
is not applicable to C.

Fixes #97947
---
 .../clang-tidy/misc/UseInternalLinkageCheck.cpp  | 5 -
 clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.h  | 3 +++
 .../clang-tidy/checkers/misc/use-internal-linkage-var.cpp| 3 +++
 3 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp 
b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
index 44ccc2bc906a5b..bc3fc50ca75dff 100644
--- a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
@@ -103,7 +103,10 @@ void UseInternalLinkageCheck::registerMatchers(MatchFinder 
*Finder) {
   functionDecl(Common, unless(cxxMethodDecl()), unless(isMain()))
   .bind("fn"),
   this);
-  Finder->addMatcher(varDecl(Common, hasGlobalStorage()).bind("var"), this);
+  Finder->addMatcher(
+  varDecl(Common, hasGlobalStorage(), unless(hasType(isConstQualified(
+  .bind("var"),
+  this);
 }
 
 static constexpr StringRef Message =
diff --git a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.h 
b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.h
index 0d6c3e43aa9457..1ad28333ddc49d 100644
--- a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.h
+++ b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.h
@@ -21,6 +21,9 @@ namespace clang::tidy::misc {
 class UseInternalLinkageCheck : public ClangTidyCheck {
 public:
   UseInternalLinkageCheck(StringRef Name, ClangTidyContext *Context);
+  bool isLanguageVersionSupported(const LangOptions &LangOpts) const override {
+return LangOpts.CPlusPlus;
+  }
   void registerMatchers(ast_matchers::MatchFinder *Finder) override;
   void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
   void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp
index 01b8d28e612301..6777ce4bb0265e 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp
@@ -42,3 +42,6 @@ int global_in_extern_c_1;
 }
 
 extern "C" int global_in_extern_c_2;
+
+const int const_global = 123;
+constexpr int constexpr_global = 123;

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Do not warn on const variables in misc-use-internal-linkage (PR #97969)

2024-07-08 Thread Carlos Galvez via cfe-commits

carlosgalvezp wrote:

Thanks! Btw is the check intended to be used in C code as well? (I do not see C 
tests for it). If not, then i can move the logic to the matcher to keep it a 
bit cleaner, and restrict the check to C++ code. 

The warning text mentions anonymous namespaces which would not be applicable in 
C.

https://github.com/llvm/llvm-project/pull/97969
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] [clang-tidy] Do not warn on const variables in misc-use-internal-linkage (PR #97969)

2024-07-07 Thread Carlos Galvez via cfe-commits

https://github.com/carlosgalvezp created 
https://github.com/llvm/llvm-project/pull/97969

Since in C++ they already have implicit internal linkage.

Fixes #97947

>From 07bd62f97d203bbdb865cd4b1e14cef3ead70c80 Mon Sep 17 00:00:00 2001
From: Carlos Galvez 
Date: Sun, 7 Jul 2024 21:11:54 +0200
Subject: [PATCH] [clang-tidy] Do not warn on const variables in
 misc-use-internal-linkage

Since in C++ they already have implicit internal linkage.

Fixes #97947
---
 .../clang-tidy/misc/UseInternalLinkageCheck.cpp | 6 ++
 .../clang-tidy/checkers/misc/use-internal-linkage-var.cpp   | 3 +++
 2 files changed, 9 insertions(+)

diff --git a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp 
b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
index 44ccc2bc906a5..c086e7721e02b 100644
--- a/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/UseInternalLinkageCheck.cpp
@@ -121,6 +121,12 @@ void UseInternalLinkageCheck::check(const 
MatchFinder::MatchResult &Result) {
 return;
   }
   if (const auto *VD = Result.Nodes.getNodeAs("var")) {
+// In C++, const variables at file scope have implicit internal linkage,
+// so we should not warn there. This is not the case in C.
+// https://eel.is/c++draft/diff#basic-3
+if (getLangOpts().CPlusPlus && VD->getType().isConstQualified())
+  return;
+
 DiagnosticBuilder DB = diag(VD->getLocation(), Message) << "variable" << 
VD;
 SourceLocation FixLoc = VD->getTypeSpecStartLoc();
 if (FixLoc.isInvalid() || FixLoc.isMacroID())
diff --git 
a/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp 
b/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp
index 01b8d28e61230..6777ce4bb0265 100644
--- 
a/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp
+++ 
b/clang-tools-extra/test/clang-tidy/checkers/misc/use-internal-linkage-var.cpp
@@ -42,3 +42,6 @@ int global_in_extern_c_1;
 }
 
 extern "C" int global_in_extern_c_2;
+
+const int const_global = 123;
+constexpr int constexpr_global = 123;

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang-tools-extra] Enforce SL.con.3: Add check to replace operator[] with at() [Cont.] (PR #95220)

2024-06-27 Thread Carlos Galvez via cfe-commits
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= ,
Paul =?utf-8?q?Heidekrüger?= 
Message-ID:
In-Reply-To: 


carlosgalvezp wrote:

Nit: consider updating also the commit message / title of this PR to reflect 
the new behavior of this check (namely, to not enforce `at()`).

https://github.com/llvm/llvm-project/pull/95220
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


  1   2   3   >