[clang] [libclang] Document CXUnsavedFile::Length width and fix test narrowing (PR #228405)

via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 16 06:58:28 PDT 2026


https://github.com/UebelAndre updated https://github.com/llvm/llvm-project/pull/228405

>From 0d8a3b1e53fac2a755a8abb691e1e125006ac7e2 Mon Sep 17 00:00:00 2001
From: UebelAndre <github at uebelandre.com>
Date: Fri, 2 Oct 2026 05:06:50 -0700
Subject: [PATCH] [libclang][test] Fix narrowing when initializing
 CXUnsavedFile::Length

`CXUnsavedFile::Length` is `unsigned long`, which is 32 bits on 64-bit
Windows. The libclang unit test fixture list-initializes it from
`std::string::size()`, a narrowing conversion there and so ill-formed.
MSVC rejects it with error C2397 and clang-cl with `-Wc++11-narrowing`,
a default error.

No CMake bot sees this because clang/unittests/CMakeLists.txt skips the
libclang unit tests on Windows. The Bazel libclang_tests target builds
them there and fails to compile under `--config=msvc`. Cast explicitly,
as libclang itself already does where it narrows to `unsigned long`.

Related: #160729
---
 clang/unittests/libclang/TestUtils.h | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/clang/unittests/libclang/TestUtils.h b/clang/unittests/libclang/TestUtils.h
index 013aad17358211..9a7e8b34fefc53 100644
--- a/clang/unittests/libclang/TestUtils.h
+++ b/clang/unittests/libclang/TestUtils.h
@@ -82,9 +82,10 @@ class LibclangParseTest : public ::testing::Test {
         fixed_addr_string(new std::string(Filename)),
         fixed_addr_string(new std::string(Contents))));
     UnsavedFiles.push_back({
-        it.first->first->c_str(),   // filename
-        it.first->second->c_str(),  // contents
-        it.first->second->size()    // length
+        it.first->first->c_str(),  // filename
+        it.first->second->c_str(), // contents
+        // Length is unsigned long: 32 bits on 64-bit Windows, so this narrows.
+        static_cast<unsigned long>(it.first->second->size()) // length
     });
   }
   template <typename F>



More information about the cfe-commits mailing list