Skip to content

Commit f3761a4

Browse files
[C++20][Modules] Allow using stdarg.h with header units (#100739)
Summary: Macro like `va_start`/`va_end` marked as builtin functions that makes these identifiers special and it results in redefinition of the identifiers as builtins and it hides macro definitions during preloading C++ modules. In case of modules Clang ignores special identifiers but `PP.getCurrentModule()` was not set. This diff fixes IsModule detection logic for this particular case. Test Plan: check-clang --------- Co-authored-by: Chuanqi Xu <[email protected]>
1 parent 4f42deb commit f3761a4

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

clang/lib/Serialization/ASTReader.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,10 +1051,10 @@ IdentifierID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d)
10511051
return Reader.getGlobalIdentifierID(F, RawID >> 1);
10521052
}
10531053

1054-
static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II) {
1054+
static void markIdentifierFromAST(ASTReader &Reader, IdentifierInfo &II,
1055+
bool IsModule) {
10551056
if (!II.isFromAST()) {
10561057
II.setIsFromAST();
1057-
bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
10581058
if (isInterestingIdentifier(Reader, II, IsModule))
10591059
II.setChangedSinceDeserialization();
10601060
}
@@ -1080,7 +1080,8 @@ IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
10801080
II = &Reader.getIdentifierTable().getOwn(k);
10811081
KnownII = II;
10821082
}
1083-
markIdentifierFromAST(Reader, *II);
1083+
bool IsModule = Reader.getPreprocessor().getCurrentModule() != nullptr;
1084+
markIdentifierFromAST(Reader, *II, IsModule);
10841085
Reader.markIdentifierUpToDate(II);
10851086

10861087
IdentifierID ID = Reader.getGlobalIdentifierID(F, RawID);
@@ -4547,7 +4548,7 @@ ASTReader::ASTReadResult ASTReader::ReadAST(StringRef FileName, ModuleKind Type,
45474548

45484549
// Mark this identifier as being from an AST file so that we can track
45494550
// whether we need to serialize it.
4550-
markIdentifierFromAST(*this, *II);
4551+
markIdentifierFromAST(*this, *II, /*IsModule=*/true);
45514552

45524553
// Associate the ID with the identifier so that the writer can reuse it.
45534554
auto ID = Trait.ReadIdentifierID(Data + KeyDataLen.first);
@@ -8966,7 +8967,8 @@ IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
89668967
auto Key = Trait.ReadKey(Data, KeyDataLen.first);
89678968
auto &II = PP.getIdentifierTable().get(Key);
89688969
IdentifiersLoaded[Index] = &II;
8969-
markIdentifierFromAST(*this, II);
8970+
bool IsModule = getPreprocessor().getCurrentModule() != nullptr;
8971+
markIdentifierFromAST(*this, II, IsModule);
89708972
if (DeserializationListener)
89718973
DeserializationListener->IdentifierRead(ID, &II);
89728974
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// RUN: rm -fR %t
2+
// RUN: split-file %s %t
3+
// RUN: cd %t
4+
// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header h1.h
5+
// RUN: %clang_cc1 -std=c++20 -emit-header-unit -xc++-user-header h2.h -fmodule-file=h1.pcm
6+
// RUN: %clang_cc1 -std=c++20 -fsyntax-only main.cpp -fmodule-file=h1.pcm -fmodule-file=h2.pcm
7+
8+
//--- h1.h
9+
#include <stdarg.h>
10+
// expected-no-diagnostics
11+
12+
//--- h2.h
13+
import "h1.h";
14+
// expected-no-diagnostics
15+
16+
//--- main.cpp
17+
import "h1.h";
18+
import "h2.h";
19+
20+
void foo(int x, ...) {
21+
va_list v;
22+
va_start(v, x);
23+
va_end(v);
24+
}
25+
// expected-no-diagnostics

0 commit comments

Comments
 (0)