Skip to content

Commit 0f8c721

Browse files
authored
[C++20][Modules] Disable preferred_name when writing a C++20 header unit (#144377)
https://reviews.llvm.org/D130331 added workaround for named modules only. But the same issue happens for headees units. Link issue #56490
1 parent 2c90ebf commit 0f8c721

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,10 @@ class ASTWriter : public ASTDeserializationListener,
899899
return WritingModule && WritingModule->isNamedModule();
900900
}
901901

902+
bool isWritingStdCXXHeaderUnit() const {
903+
return WritingModule && WritingModule->isHeaderUnit();
904+
}
905+
902906
bool isGeneratingReducedBMI() const { return GeneratingReducedBMI; }
903907

904908
bool getDoneWritingDeclsAndTypes() const { return DoneWritingDeclsAndTypes; }

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5167,8 +5167,9 @@ void ASTRecordWriter::AddAttr(const Attr *A) {
51675167
// FIXME: Clang can't handle the serialization/deserialization of
51685168
// preferred_name properly now. See
51695169
// https://github.com/llvm/llvm-project/issues/56490 for example.
5170-
if (!A || (isa<PreferredNameAttr>(A) &&
5171-
Writer->isWritingStdCXXNamedModules()))
5170+
if (!A ||
5171+
(isa<PreferredNameAttr>(A) && (Writer->isWritingStdCXXNamedModules() ||
5172+
Writer->isWritingStdCXXHeaderUnit())))
51725173
return Record.push_back(0);
51735174

51745175
Record.push_back(A->getKind() + 1); // FIXME: stable encoding, target attrs
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// RUN: rm -fR %t
2+
// RUN: split-file %s %t
3+
// RUN: cd %t
4+
// RUN: %clang_cc1 -verify -w -std=c++20 -fmodule-name=h1.h -emit-header-unit -xc++-user-header h1.h -o h1.pcm
5+
// RUN: %clang_cc1 -verify -w -std=c++20 -fmodule-map-file=module.modulemap -fmodule-file=h1.h=h1.pcm main.cpp -o main.o
6+
7+
//--- module.modulemap
8+
module "h1.h" {
9+
header "h1.h"
10+
export *
11+
}
12+
13+
//--- h0.h
14+
// expected-no-diagnostics
15+
#pragma once
16+
namespace std {
17+
18+
template <class _CharT, class = _CharT, class = _CharT> class basic_string;
19+
20+
namespace pmr {
21+
using string = basic_string<char>;
22+
}
23+
24+
template <class, class, class>
25+
class __attribute__((__preferred_name__(pmr::string))) basic_string;
26+
27+
template <class> class basic_string_view {};
28+
29+
template <class _CharT, class _Traits, class _Allocator> class basic_string {
30+
typedef _CharT value_type;
31+
typedef _Allocator allocator_type;
32+
struct __rep;
33+
public:
34+
template <class _Tp>
35+
basic_string(_Tp) {}
36+
basic_string operator+=(value_type);
37+
};
38+
39+
namespace filesystem {
40+
class path {
41+
typedef char value_type;
42+
value_type preferred_separator;
43+
typedef basic_string<value_type> string_type;
44+
typedef basic_string_view<value_type> __string_view;
45+
template <class _Source> void append(_Source) {
46+
__pn_ += preferred_separator;
47+
}
48+
void __root_directory() { append(string_type(__string_view{})); }
49+
string_type __pn_;
50+
};
51+
} // namespace filesystem
52+
} // namespace std
53+
54+
//--- h1.h
55+
// expected-no-diagnostics
56+
#pragma once
57+
58+
#include "h0.h"
59+
60+
//--- main.cpp
61+
// expected-no-diagnostics
62+
#include "h0.h"
63+
64+
import "h1.h";

0 commit comments

Comments
 (0)