[clang] [analyzer] Fix zext assertion failure in loop unrolling (PR #121203)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Dec 27 06:20:47 PST 2024
https://github.com/shenjunjiekoda updated https://github.com/llvm/llvm-project/pull/121203
>From 2cee5fc2abd0cf2979c9ba975906e659adcd4463 Mon Sep 17 00:00:00 2001
From: shenjunjie <shenjunjiekoda at foxmail.com>
Date: Fri, 27 Dec 2024 14:08:55 +0000
Subject: [PATCH 1/2] [analyzer] Fix zext assertion failure in loop unrolling
---
clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp b/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
index 96f5d7c44baf89..9227a7876c0b2f 100644
--- a/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
+++ b/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
@@ -283,10 +283,12 @@ static bool shouldCompletelyUnroll(const Stmt *LoopStmt, ASTContext &ASTCtx,
llvm::APInt InitNum =
Matches[0].getNodeAs<IntegerLiteral>("initNum")->getValue();
auto CondOp = Matches[0].getNodeAs<BinaryOperator>("conditionOperator");
- if (InitNum.getBitWidth() != BoundNum.getBitWidth()) {
- InitNum = InitNum.zext(BoundNum.getBitWidth());
- BoundNum = BoundNum.zext(InitNum.getBitWidth());
- }
+ unsigned MaxWidth = std::max(InitNum.getBitWidth(), BoundNum.getBitWidth());
+
+ if (InitNum.getBitWidth() != MaxWidth)
+ InitNum = InitNum.zext(MaxWidth);
+ if (BoundNum.getBitWidth() != MaxWidth)
+ BoundNum = BoundNum.zext(MaxWidth);
if (CondOp->getOpcode() == BO_GE || CondOp->getOpcode() == BO_LE)
maxStep = (BoundNum - InitNum + 1).abs().getZExtValue();
>From a4d3a24ea797c5f53c5171e05f7172434096bd65 Mon Sep 17 00:00:00 2001
From: shenjunjie <shenjunjiekoda at foxmail.com>
Date: Fri, 27 Dec 2024 14:20:23 +0000
Subject: [PATCH 2/2] add testcase
---
clang/test/Analysis/PR121201.cpp | 50 ++++++++++++++++++++++++++++++++
1 file changed, 50 insertions(+)
create mode 100644 clang/test/Analysis/PR121201.cpp
diff --git a/clang/test/Analysis/PR121201.cpp b/clang/test/Analysis/PR121201.cpp
new file mode 100644
index 00000000000000..a2cea89dc10b79
--- /dev/null
+++ b/clang/test/Analysis/PR121201.cpp
@@ -0,0 +1,50 @@
+
+template < bool, typename T, typename > using conditional_t = T;
+class basic_format_arg;
+template < typename > struct formatter;
+template < typename Context > struct value {
+ template < typename T > value(T) {
+ using value_type = T;
+ format_custom_arg<
+ value_type, typename Context::template formatter_type< value_type > >;
+ }
+ template < typename, typename Formatter > static void format_custom_arg() {
+ Context ctx;
+ auto f = Formatter();
+ f.format(0, ctx);
+ }
+};
+struct context {
+ template < typename T > using formatter_type = formatter< T >;
+};
+enum { max_packed_args };
+template < typename Context, long >
+using arg_t =
+ conditional_t< max_packed_args, value< Context >, basic_format_arg >;
+template < int NUM_ARGS > struct format_arg_store {
+ arg_t< context, NUM_ARGS > args;
+};
+template < typename... T, long NUM_ARGS = sizeof...(T) >
+auto make_format_args(T... args) -> format_arg_store< NUM_ARGS > {
+ return {args...};
+}
+template < typename F > void write_padded(F write) { write(0); }
+template < typename... T > void format(T... args) { make_format_args(args...); }
+template < int > struct bitset {
+ bitset(long);
+};
+template < long N > struct formatter< bitset< N > > {
+ struct writer {
+ bitset< N > bs;
+ template < typename OutputIt > void operator()(OutputIt) {
+ for (auto pos = N; pos > 0; --pos)
+ ;
+ }
+ };
+ template < typename FormatContext >
+ void format(bitset< N > bs, FormatContext) {
+ write_padded(writer{bs});
+ }
+};
+bitset< 6 > TestBody_bs(2);
+void TestBody() { format(TestBody_bs); }
\ No newline at end of file
More information about the cfe-commits
mailing list