Skip to content

Commit 87502f3

Browse files
davemgreenDhruvSrivastavaX
authored andcommitted
[ASTWriter] Do not write ObjCCategories if empty. (llvm#141841)
This is a fix for a completely unrelated patch, that started to cause failures in the explicit-build.cpp test because the size of the b.pcm and b-not-a.pcm files became the same. The alignment added by empty ObjCCategory blobs being written to the file causes them to become the same size, and the error 'module file has a different size than expected' will not be emitted as the pcms only track module size, not content, for whether they are valid. This prevents that issue by not saving the ObjCCategories if it is empty. The change in clang/lib/Serialization/ASTReaderDecl.cpp is just formatting, but shows that the only use of ObjCCategoriesMap loaded from the file will be OK with null (never loaded) data. It is a bit of a weird fix, but should help decrease the size of the modules for objects that are not used.
1 parent d01860c commit 87502f3

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

clang/lib/Serialization/ASTReaderDecl.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4631,11 +4631,10 @@ namespace {
46314631

46324632
// Perform a binary search to find the local redeclarations for this
46334633
// declaration (if any).
4634-
const ObjCCategoriesInfo Compare = { LocalID, 0 };
4635-
const ObjCCategoriesInfo *Result
4636-
= std::lower_bound(M.ObjCCategoriesMap,
4637-
M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap,
4638-
Compare);
4634+
const ObjCCategoriesInfo Compare = {LocalID, 0};
4635+
const ObjCCategoriesInfo *Result = std::lower_bound(
4636+
M.ObjCCategoriesMap,
4637+
M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap, Compare);
46394638
if (Result == M.ObjCCategoriesMap + M.LocalNumObjCCategoriesInMap ||
46404639
LocalID != Result->getDefinitionID()) {
46414640
// We didn't find anything. If the class definition is in this module

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4972,6 +4972,9 @@ void ASTWriter::WriteCUDAPragmas(Sema &SemaRef) {
49724972
}
49734973

49744974
void ASTWriter::WriteObjCCategories() {
4975+
if (ObjCClassesWithCategories.empty())
4976+
return;
4977+
49754978
SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
49764979
RecordData Categories;
49774980

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: rm -rf %t.pcm
2+
3+
// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -x c++ -std=c++11 -emit-module -fmodules -fmodule-name=cxx_library %S/Inputs/module.modulemap -o %t.pcm
4+
// RUN: llvm-bcanalyzer %t.pcm | FileCheck %s --check-prefix=CXX_LIBRARY
5+
// CXX_LIBRARY: AST_BLOCK
6+
// CXX_LIBRARY-NOT: OBJC_CATEGORIES
7+
// CXX_LIBRARY-NOT: OBJC_CATEGORIES_MAP
8+
9+
// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -x objective-c -emit-module -fmodules -fmodule-name=category_top %S/Inputs/module.modulemap -o %t.pcm
10+
// RUN: llvm-bcanalyzer %t.pcm | FileCheck %s --check-prefix=CATEGORY_TOP
11+
// CATEGORY_TOP: AST_BLOCK
12+
// CATEGORY_TOP: OBJC_CATEGORIES
13+
// CATEGORY_TOP: OBJC_CATEGORIES_MAP
14+

0 commit comments

Comments
 (0)