[clang] [libclang] Document CXUnsavedFile::Length width and fix test narrowing (PR #228405)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 16 04:01:43 PDT 2026
https://github.com/UebelAndre updated https://github.com/llvm/llvm-project/pull/228405
>From acbc805c31adf21dad9d1255962362fd7651c09d 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] Document CXUnsavedFile::Length width and fix test
narrowing
`CXUnsavedFile::Length` is `unsigned long`, which is 64 bits on LP64
targets but 32 bits on 64-bit Windows and on 32-bit targets.
The libclang unit test fixture list-initializes it from
`std::string::size()`, which is a narrowing conversion on 64-bit
Windows and therefore 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.
Bindings that describe the struct layout by hand have also assumed a
fixed width for this field (see openjdk/jextract#289). Changing the
type would break the stable libclang ABI, so note the platform-
dependent width in the header instead.
Fixes #160729
---
clang/include/clang-c/Index.h | 3 +++
clang/unittests/libclang/TestUtils.h | 7 ++++---
2 files changed, 7 insertions(+), 3 deletions(-)
diff --git a/clang/include/clang-c/Index.h b/clang/include/clang-c/Index.h
index f83db6a428ccf2..66979097dc8bd9 100644
--- a/clang/include/clang-c/Index.h
+++ b/clang/include/clang-c/Index.h
@@ -118,6 +118,9 @@ struct CXUnsavedFile {
/**
* The length of the unsaved contents of this buffer.
+ *
+ * Note that this is an \c unsigned \c long, which has a platform-dependent
+ * width. Do not assume it is a 64-bit integer.
*/
unsigned long Length;
};
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